Skip to main content

Methods

DragSelect exposes some useful methods. When the function is saved into a variable const ds = new DragSelect() you have access to all its inner functions. You can simply use them like this:

const ds = new DragSelect({});
ds.methodName(method_props);

Methods

Functionality

methodpropertiesusage
stop/Will teardown/stop the whole functionality
start/Reset the functionality after a stop/teardown
break/Utility to override DragSelect internal functionality. Breaks out of current flow. Read Custom Drag and Drop for more info.

Pub/Sub

methodpropertiesusage
subscribe(event_name, callback:(callback_object) => void):numberSubscribes to an event, see the Events API for infos on the names and callback_object.
unsubscribe(event_name, callback, id)Unsubscribes from an event, either pass the exact same callback method as when subscribing or the ID which is returned from the subscribe method
publish(event_name, callback_object:any)Publishes an event, see the Events API for infos on the names and callback_object.

Read the Events API for more information

Settings

methodpropertiesusage
setSettingsSettingsUpdate any setting dynamically, see Updating Settings

Interaction

methodpropertiesusage
isDragging/Whether the user is currently drag n dropping elements (instead of selection)
isMultiSelectevent:KeyboardEventMouseEvent

Selection

methodpropertiesusage
getSelection/Returns all currently selected nodes
addSelectionelements:[HTMLElement | SVGElement] | HTMLElement | SVGElement, triggerCallback:boolean, dontAddToSelectables:booleanadds one or multiple elements to the selection. If boolean is set to true: callback will be called afterwards. Adds them to the selectables if they're not yet in the set (can be turned off by setting the last boolean to true)
removeSelectionelements:[HTMLElement | SVGElement] | HTMLElement | SVGElement, triggerCallback:boolean, removeFromSelectables:booleanremoves one or multiple elements to the selection. If boolean is set to true: callback will be called afterwards. If last boolean is set to true, it also removes them from the possible selectable nodes if they were.
toggleSelectionelements:[HTMLElement | SVGElement] | HTMLElement | SVGElement, triggerCallback:boolean, removeFromSelectables:booleantoggles one or multiple elements to the selection. If element is not in selection it will be added, if it is already selected, it will be removed. If triggerCallback is set to true: callback will be called afterward. If removeFromSelectables is set to true: will NOT add elements to the list of selectable elements but will remove elements from the selectables if they are toggled off. (By default the toggling will add elements to the list of selectables and will not remove them).
setSelectionelements:[HTMLElement | SVGElement] | HTMLElement | SVGElement, triggerCallback:boolean, dontAddToSelectables:booleansets the selection to one or multiple elements. If boolean is set to true: callback will be called afterwards. Adds them to the selectables if they're not yet in the set (can be turned off by setting the last boolean to true)
clearSelectionelements:[HTMLElement | SVGElement] | HTMLElement | SVGElement, triggerCallback:booleanRemoves all elements from the selection. If callback is set to true: callback will be called afterwards.
clearSelectiontriggerCallback:booleanUnselect / Deselect all current selected Nodes

Selectables

methodpropertiesusage
addSelectableselements:[HTMLElement | SVGElement] | HTMLElement | SVGElement, addToSelection:booleanAdds elements that can be selected. Don’t worry, nodes are never added twice. addToSelection: elements will also be added to current selection.
removeSelectableselements:[HTMLElement | SVGElement] | HTMLElement | SVGElement, removeFromSelection:booleanRemove elements from the set of elements that can be selected. removeFromSelection: elements will also be removed from current selection.
getSelectables/Returns array with all nodes that can be selected.

Pointer

methodpropertiesusage
getInitialCursorPosition/Returns the registered x, y coordinates the cursor had when first clicked
getCurrentCursorPosition/Returns current x, y coordinates of the cursor
getPreviousCursorPosition/Returns last registered x, y coordinates of the cursor (after last end callback)
getInitialCursorPositionArea/Returns the registered x, y coordinates relative to the area the cursor had when first clicked
getCurrentCursorPositionArea/Returns current x, y coordinates of the cursor relative to the area
getPreviousCursorPositionArea/Returns last registered x, y coordinates of the cursor relative to the area (after last end callback)

DropZones

methodpropertiesusage
getZoneByCoordinatescoordinates:{ x:number, y:number } (Optional)Returns the first drop target under the current mouse position, or, if provided at the coordinates.
getItemsDroppedByZoneIdzoneId:stringReturns the items dropped into a specific zone (by zone.id)
getItemsInsideByZoneIdzoneId:string, addClasses:booleanReturns the items that are visually inside a specific zone (by zone.id). addClasses: also add the respective CSS classes to those elements if they don’t have them yet.

Example

const ds = new DragSelect({});
ds.setSettings({ selectables: document.querySelectorAll('.selectable') });
ds.getSelection();