Selectable
Inspired by the jQuery UI Selectable plugin. Functionality and options are identical to the jQuery UI version with some additions and performance enhancements.
Selectable mimics the Windows file / directory behaviour, i.e. click and / or drag to select items, hold CTRL to select multiple or hold SHIFT to select consecutive groups of items.
Install
Bower
bower install mobius1-selectable --save
npm
npm install mobius1-selectable --save
Browser
Grab the files from one of the CDNs and include them in your page:
https://unpkg.com/mobius1-selectable@latest/selectable.min.js
//or
https://cdn.jsdelivr.net/npm/mobius1-selectable@latest/selectable.min.js
You can replace latest
with the required release number if needed.
Initialise the plugin:
const selectable = new Selectable(options);
Options
Option | Type | Default | Required | Effect |
---|
filter | string or array | "*" | ✔ | The elements that can be selected. You can pass either a CSS3 selector string or a collection of nodes. |
appendTo | string or object | document.body | ✖ | The container element to append the lasso to. |
tolerance | string | touch | ✖ | How far the lasso overlaps an element before it's highlighted. "fit" (lasso overlaps the item entirely) or "touch" (lasso overlaps the item by any amount). |
autoRefresh | boolean | true | ✖ | Recalculate the coords of the items. Set to false if you know the selectable items won't move or change size. |
lasso | object | | ✖ | Style the lasso. Must be an object of valid CSS declarations. Demo |
Public Methods
destroy()
Destroy the instance. This will return the DOM to it's initial state before initialsing.
init()
Initialise the instance after destroying.
disable()
Disable the instance. Removes all event listeners to prevent further selection / deselection.
enable()
Enable the instance.
update()
Updates the instance.
Can be used if new items are added or old ones removed. All item coords are updated as well.
recalculate()
Recalculates the coords for all valid items.
If the dimensions of the item / items change then call this method otherwise the lasso will not select items correctly.
selectItem(item)
Select an item.
deselectItem(item)
Deselect an item.
selectAll()
Select all valid items.
clear()
Deselects all valid items.
Events
const selectable = new Selectable(options);
selectable.on('selectable.XXXX', function() {
});
selectable.down
fires on mousedown (within container)selectable.drag
fires when dragging the lassoselectable.up
fires on mouse up (within container)selectable.selected
fires on each element selected
selectable.on('selectable.down', function(item) {
});
selectable.on('selectable.drag', function(coords) {
});
selectable.on('selectable.up', function(selectedItems) {
});
selectable.on('selectable.selected', function(item) {
});
Note that items returned by these events are objects of the following format:
{
element: HTMLElement,
index: Number,
rect: DOMRect Object,
startselected: Boolean,
selected: Boolean,
selecting: Boolean,
unselecting: Boolean
}
Copyright © 2017 Karl Saunders | BSD & MIT license