DragSelect was successfully tested on all modern browsers and a wide range of legacy browsers, it even works perfectly on ie9. Also, DragSelect is accessible by default.
Ease of use
Your time matters, so DragSelect is super quick and easy to use. If you know the basics of javascript you're ready to rock. Also, no dependencies.
Lightweight
You want your website to be loaded as fast as possible. So DragSelect only weights ~2KB gzipped which is an average of 0,00004s loadtime. You name it – it's nothing. Also, no dependencies.
Free
DragSelect is published under the MIT license and completely free to use. Oh and it's Open Source as well, so you can make it even better.
All options are optional. You could also just initiate the Dragselect by `new DragSelect();` without any option. If you add the function to a variable you get full controll over all its functions:
var ds = new DragSelect({
selectables: document.getElementsByClassName('selectable-nodes'), // node/nodes that can be selected. This is also optional, you could just add them later with .addSelectables.
selector: document.getElementById('rectangle'), // draggable element. By default one will be created.
area: document.getElementById('area'), // area in which you can drag. If not provided it will be the whole document.
customStyles: false, // If set to true, no styles (except for position absolute) will be applied by default.
multiSelectKeys: ['ctrlKey', 'shiftKey', 'metaKey'], // special keys that allow multiselection. These key will allow the user add more elements to the selection instead of clearing the selection. The only possible values are keys that are provided via the event object. So far: ctrlKey, shiftKey, metaKey and altKey. Provide an empty array [] if you want to turn off the funcionality. Default: ['ctrlKey', 'shiftKey', 'metaKey']
multiSelectMode: false, // If set to true, the multiselection behavior will be turned on by default without the need of modifier keys. Default: false
autoScrollSpeed: 3, // Speed in which the area scrolls while selecting (if available). Unit is pixel per movement. Set to 0.0001 to disable autoscrolling. Default = 1
onDragStart: function(element) {}, // fired when the user clicks in the area. This callback gets the event object. Executed after DragSelect function code ran, befor the setup of event listeners.
onDragMove: function(element) {}, // fired when the user drags. This callback gets the event object. Executed before DragSelect function code ran, after getting the current mouse position.
onElementSelect: function(element) {}, // fired every time an element is selected. (element) = just selected node
onElementUnselect: function(element) {}, // fired every time an element is de-selected. (element) = just de-selected node.
callback: function(elements) {} // fired once the user releases the mouse. (elements) = selected nodes.
});
// if you add the function to a variable like we did, you have access to all its functions
// and can now use start() and stop() like so:
ds.getSelection(); // returns all currently selected nodes
ds.addSelectables(document.getElementsByClassName('selectable-node')); // adds elements that can be selected. Intelligent algorithm never adds elements twice.
ds.removeSelectables(document.getElementsByClassName('selectable-node')); // removes elements that can be selected.
ds.getSelectables(); // returns array of nodes that can be selected.
ds.break(); // used in callbacks to disable the execution of the upcoming code. It will not teardown the functionality.
ds.stop(); // will teardown/stop the whole functionality
ds.start(); // reset the functionality after a teardown
// and many more, see "methods" section in documentation
2d. Accessibility (a11y)
DragSelect is accessibly by default.
TLDR; => Your selectables should be buttons:
Obviously, keyboard users won’t get the full visual experience but it works similarely to the OS default behaviour. You can select items using the default select keys (usually space or enter) and also multiselect when using a modifier key at the same time (unfortunately this does not work in firefox for now since FF doesn’t add the modifier key in the event object when using the keyboard). There is one little thing you have to do tho’: the selectables have to be pressable (clickable)! To achieve this, they should be buttons: . Try it out:
OPTIONAL. The square that will draw the selection. Autocreated by default
area
single DOM element (node)
OPTIONAL. The square in which you are able to select
customStyles
boolean
OPTIONAL. If true, no styles will be automatically applied (except position: absolute). Default: false
multiSelectKeys
array
OPTIONAL. An array of keys that allows switching to the multi-select mode (see the multiSelectMode option). The only possible values are keys that are provided via the event object. So far: ctrlKey, shiftKey, metaKey and altKey. Provide an empty array [] if you want to turn off the funcionality. Default: ['ctrlKey', 'shiftKey', 'metaKey']
multiSelectMode
boolean
OPTIONAL. Add newly selected elements to the selection instead of replacing them. Default = false
autoScrollSpeed
integer
OPTIONAL. Speed in which the area scrolls while selecting (if available). Unit is pixel per movement. Set to 0.0001 to disable autoscrolling. Default = 1
selectedClass
string
OPTIONAL. The class name assigned to the selected items. Default = see classes
hoverClass
string
OPTIONAL. The class name assigned to the mouse hovered items. Default = see classes
selectorClass
string
OPTIONAL. The class name assigned to the square selector helper. Default = see classes
selectableClass
string
OPTIONAL. The class name assigned to the elements that can be selected. Default = see classes
onDragStart
function
OPTIONAL. Fired when the user clicks in the area. This callback gets the event object. Executed after DragSelect function code ran, befor the setup of event listeners
onDragMove
function
OPTIONAL. Fired when the user drags. This callback gets the event object. Executed before DragSelect function code ran, after getting the current mouse position
onElementSelect
function
OPTIONAL. Fired every time an element is selected. This callback gets a property which is the selected node
onElementUnselect
function
OPTIONAL. Fired every time an element is de-selected. This callback gets a property which is the de-selected node
callback
function
OPTIONAL. Callback function that gets fired when the selection is released. This callback gets a property which is an array that holds all selected nodes
Info: use method: ".getSelection()" to returns all currently selected nodes.
4. Methods
When the function is saved into a variable (var foo = new DragSelect({...})) you have access to all its inner functions. There are way more than listed here. Here are just the most usable:
Reference:
Method
Properties
Use
stop
/
will teardown/stop the whole functionality
start
/
reset the functionality after a teardown
break
/
used in callbacks to disable the execution of the upcoming code. It will not teardown the functionality
getSelection
/
returns all currently selected nodes
addSelection
nodes, boolean, boolean
adds one or multiple elements to the selection. If boolean is set to true: callback will be called afterwards. By default, it checks if all elements ere alos in the list of selectables and adds them if not (can be turned off by setting the last boolean to true)
removeSelection
nodes, boolean, boolean
removes one or multiple elements to the selection. If boolean is set to true: callback will be called afterwards. If last bolean is set to true, it also removes them from the possible selectable nodes if they were.
toggleSelection
nodes, boolean, boolean
toggles 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 boolean is set to true: callback will be called afterward. If last boolean is set to true, it also removes selected elements from possible selectable nodes & doesn’t add them to selectables if they are not.
setSelection
nodes, boolean, boolean
sets the selection to one or multiple elements. If boolean is set to true: callback will be called afterwards. By default, it checks if all elements ere alos in the list of selectables and adds them if not (can be turned off by setting the last boolean to true)
clearSelection
nodes, boolean
remove all elements from the selection. If boolean is set to true: callback will be called afterwards
addSelectables
nodes, boolean
adds elements that can be selected. Don’t worry, a smart algorithm makes sure that nodes are never added twice. If boolean is set to true: elements will also be added to current selection
removeSelectables
nodes, boolean
remove elements that can be selected. If boolean is set to true: elements will also be removed from current selection.
getSelectables
nodes
returns array with all nodes that can be selected.
setSelectables
nodes, boolean, boolean
sets all elements that can be selected. Removes all current selectables (& their respective applied classes). Adds the new set to the selectables set. Thus, replacing the original set. First boolean if old elements should be removed from the selection. Second boolean if new elements should be added to the selection.
getInitialCursorPosition
/
returns the x, y coordinates the cursor had when the first click was registered
getCurrentCursorPosition
/
returns last registered x, y coordinates of the cursor
getCursorPositionDifference
boolean
returns object with the x, y difference between the initial and the last cursor position. If the argument is set to true, it will instead return the x, y difference to the previous selection
getCursorPos
click-event, nodes, boolean
returns the cursor x, y coordinates based on a click event object. The click event object is required. By default, takes scroll and area into consideration. Area is this.area by default and can be fully ignored by setting the second argument explicitely to false. Scroll can be ignored by setting the third argument to true.
5. Classes
Reference:
.ds-hover
on elements that are currently hovered
.ds-selected
on elements that are selected
.ds-selector
on the selector element
.ds-selectable
on elements that can be selected
Note: note that you can change the class names by providing special properties to the constructor. See Properties