Selection
Selection
enables the ability to select shapes on the paper.
Learn more about the Selection plugin.
constructorβ
constructor(options?: Selection.Options);
The ui.Selection
constructor accepts the following parameters:
paperβ
[required] The joint.dia.Paper
or joint.ui.PaperScroller
the Selection should operate on. If a PaperScroller
object is provided, then its paper option is used as the basis for this Selection; in addition, the behavior specified
by the PaperScroller's scrollWhileDragging
option is automatically applied
for relevant Selection activities (selecting, translating, resizing).
If the paper
option is not provided, the Selection throws an exception:
ui.Selection: paper required
graphβ
[optional] The joint.dia.Graph
the Selection should operate on. If no graph is passed paper.model
is used.
collectionβ
[optional] A mvc Collection object used to store selected cells. If no collection is passed new mvc.Collection
is used.
framesβ
[optional] The SelectionFrameList which will be used to render the selection frames.
If no frames are passed, a legacy SelectionFrameList
is created (to support the backward compatibility).
If you want to customize the selection frames, you need to pass your own SelectionFrameList
instance.
filterβ
[optional] Either an array in which case it can contain (even intermixed) cells or cell types that will be ignored by the selection. This is useful
if you have certain elements in the diagram that are not supposed to get selected. It can also be function in which case it will be called with a cell
as an argument and should return true
if that cell should be filtered out of the selection. The function gives you more flexibility in deciding whether
an element can or cannot be selected. Example:
filter: ['standard.Rectangle', 'mymodule.HiddenShape']
boxContentβ
[optional] A function that returns an HTML string with the content that will be used in the information box below the selection. By default, the box shows the number of selected elements.
boolean | mvc.$HTMLElement | ((boxElement: HTMLElement) => mvc.$HTMLElement);
useModelGeometryβ
[optional] If set to true
, the cells position and dimensions will be used as a basis for the Selection wrapping rectangle position. By default, this is set to
false
which causes the Selection wrapping rectangle position be based on the bounding box of the selected element views. Sometimes though, your shapes can have
certain SVG sub elements that stick out of the view and you don't want these sub elements to affect the Selection wrapping rectangle position. In this case, set
the useModelGeometry
to true
.
selectLinksβ
[optional] If set to true
, links under the selection rectangle will be selected as well. It defaults to false
.
strictSelectionβ
[optional] If set to true
, only elements that are fully contained in the selection rectangle will be selected as opposed to the default behaviour that selects
elements whose bounding box intersects the selection rectangle.
allowTranslateβ
[optional] If set to false
, users are not allowed to move selected elements around by dragging selection boxes. It defaults to true
.
preserveAspectRatioβ
[optional] Set to true
if you want the resizing to preserve the aspect ratio of the elements. The default is false
.
rotateAngleGridβ
[optional] When selected elements are rotated the resulting angles are snapped to this value. It defaults to 15
degrees.
handlesβ
[optional] A array of objects defining the tools around the selection. This array defaults to:
[
{
name: 'remove',
position: 'nw',
events: {
pointerdown: 'removeElements'
}
},
{
name: 'rotate',
position: 'sw',
events: {
pointerdown: 'startRotating',
pointermove: 'doRotate',
pointerup: 'stopBatch'
}
}
]
Normally, you do not need to set this array. It's better to use the addHandle()
, removeHandle()
and changeHandle()
methods described below in the methods section.
translateConnectedLinksβ
[optional] When translating selected elements, should the connected links also be translated? Based on the selected value
of the ui.Selection.ConnectedLinksTranslation
enumerator, the behavior is as follows:
NONE
- none of the links are translatedSUBGRAPH
- only links whose both ends are connected to one of the selected elements are translated.ALL
- all connected links are translated
The default is ui.Selection.ConnectedLinksTranslation.ALL
.
allowCellInteractionβ
[optional] Allows interaction with underlying element's features, e. g. buttons or ports. In this case selection-box events will be triggered
by browser event from the cell instead of the selection-box html element. It defaults to false
.
wrapperβ
[optional] Should all the selection frames be wrapped in an rectangular HTML element? It defaults to true
.
For backwards compatibility reasons, when the wrapper
option is set to true
, the selection wrapper is styled in CSS using built-in themes.
And the class name of the selection wrapper is selection-wrapper
.
It is joint-selection-wrapper
when the wrapper
option is set to false
or an object (i.e theme is not applied).
The look of the selection wrapper can be customized by setting the wrapper
option to an object with several properties.
The option object is passed to the selection wrapper mvc.View constructor, therefore properties such as className
or style
are also available.
new Selection({
/* ... */
wrapper: {
style: {
border: '2px dashed red',
},
margin: 10,
}
})
Here're the selection wrapper properties:
marginβ
margin: number;
The gap between the selection wrapper and frames. It defaults to 7
and it does not account for thickness of the selection frames.
usePaperScaleβ
usePaperScale: boolean;
If set to true
, the selection wrapper and its handles scale with the paper scale. It defaults to false
.
visibilityβ
visibility: WrapperVisibility;
A callback function to determine the visibility of the selection wrapper. It defaults to () => true
.
wrapper: {
// Do not show the selection wrapper if there is only one element selected.
visibility: (selection) => selection.collection.length > 1
}
Methodsβ
addHandle()β
selection.addHandle(handle?: Selection.Handle): void;
Add a custom tool to the Selection. opt.name is the name of the custom tool. This name will be also set as a CSS class to the handle
DOM element making it easy to select it your CSS stylesheet. opt.position is a string that specifies a position of the tool handle.
Possible values are n
, nw
, w
, sw
, s
, se
, e
and ne
. opt.icon is a URL of the icon used to render the tool. This icons
is set as a background image on the tool handle DOM element.
removeHandle()β
selection.removeHandle(name: string): void;
Remove a tool handle named name
from the Selection
.
changeHandle()β
selection.changeHandle(name: string, opt?: Selection.HandleOptions): void;
Change a tool handle named name
in the Selection
. The opt object can contain the same parameters as with the addHandle()
method except of the
name
property. opt
parameters will be merged with those defined previously.
on()β
selection.on(eventName: string, callback: EventHandler, context?: any): this;
// or
selection.on(eventMap: EventMap, context?: any): this;
Register a handler (callback)
for an event
See the Selection Events section for the list of events the Selection object triggers.
setWrapperVisibility()β
selection.setWrapperVisibility(visible: WrapperVisibility): void;
Change the visibility of the selection wrapper dynamically. The visible
parameter can be a boolean or a function that returns a boolean.
startSelecting()β
selection.startSelecting(evt: dia.Event): void;
Initiate bulk selection. See the Common Setup section how that can be used.
createSelectionBox()β
selection.createSelectionBox(cell: dia.Cell): void;
Create a single selection box above an element.
destroySelectionBox()β
selection.destroySelectionBox(cell: dia.Cell): void;
Destroy a single selection box, which is rendered above an element.
Static Methodsβ
getDefaultHandle()β
Selection.getDefaultHandle(name: string): Selection.Handle;
Get the default handle configuration by its name. The default handles are remove
, rotate
, resize
and clone
.
The method throws an exception if the handle with the given name does not exist:
ui.Selection: default handle not found: ${name}
Eventsβ
action:[name]:pointerdownβ
Triggered when the user clicks (touches) on a tool handle named name
. The handler is called with the mousedown event object, and x
and y
of the pointer in local coordinates.
action:[name]:pointermoveβ
Triggered when the user moves with mouse cursor after a tool handle named name
was mousedowned (touched). The handler is called with the
mousemove event object, and x
and y
of the pointer in local coordinates.
action:[name]:pointerupβ
Triggered when the user releases his mouse cursor after a tool handle named name
was mousedowned (touched). The handler is called with the mouseup
event object, and x
and y
of the pointer in local coordinates.
action:[name]:contextmenuβ
Triggered when the user invokes contextmenu on a tool handle named name
. The handler is called with the mousedown event object, and x
and y
of the pointer in local coordinates.
selection-box:pointerdownβ
Triggered when the user starts interacting with a selected element, an element that has a selection box above it. The handler gets passed the element
view, the DOM event object, x
and y
paper local coordinates. Use elementView.model
if you wish to access the actual element model. In the case of
allowCellInteraction: true
the DOM event will be triggered by the element instead of selection-box. See below in the
Common Setup section how that can be used.
selection-box:pointermoveβ
Triggered when the user is moving with a selected element, an element that has a selection box above it. The handler gets passed the element view,
the DOM event object, x
and y
paper local coordinates.
selection-box:pointerupβ
Triggered when the user stops interacting with a selected element, an element that has a selection box above it. The handler gets passed the element
view, the DOM event object, x
and y
paper local coordinates.
Typesβ
HandlePositionβ
enum HandlePosition {
N = 'n', NW = 'nw',
W = 'w', SW = 'sw',
S = 's', SE = 'se',
E = 'e', NE = 'ne'
}
ConnectedLinksTranslationβ
enum ConnectedLinksTranslation {
NONE = 'none',
SUBGRAPH = 'subgraph',
ALL = 'all'
}
EventHandlerβ
type EventHandler = (evt: dia.Event, x: number, y: number) => void;
EventMapβ
interface EventMap {
[event: string]: EventHandler;
}
HandleEventsβ
interface HandleEvents {
pointerdown?: string | EventHandler;
pointermove?: string | EventHandler;
pointerup?: string | EventHandler;
contextmenu?: string | EventHandler;
}
HandleOptionsβ
interface HandleOptions {
position?: HandlePosition;
events?: HandleEvents;
attrs?: any;
icon?: string;
content?: mvc.$HTMLElement
}
Handleβ
interface Handle extends HandleOptions {
name: string;
}
Optionsβ
The options for the ui.Selection
class.
interface Options extends mvc.ViewOptions<undefined> {
paper: dia.Paper | ui.PaperScroller;
graph?: dia.Graph;
boxContent?: boolean | mvc.$HTMLElement | ((boxElement: HTMLElement) => mvc.$HTMLElement);
handles?: Array<Handle>;
useModelGeometry?: boolean;
strictSelection?: boolean;
rotateAngleGrid?: number;
allowTranslate?: boolean;
preserveAspectRatio?: boolean;
collection?: any;
filter?: ((cell: dia.Cell) => boolean) | Array<string | dia.Cell>;
translateConnectedLinks?: ConnectedLinksTranslation;
allowCellInteraction?: boolean;
frames?: ui.SelectionFrameList;
wrapper?: boolean | ui.HTMLSelectionWrapper.Options | ui.SelectionWrapper;
}
WrapperVisibilityβ
The type of the wrapper visibility callback.
type WrapperVisibility = boolean | ((selection: Selection) => boolean);