Skip to content

Instantly share code, notes, and snippets.

@mr21
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mr21/770a88fd0d994cd593c3 to your computer and use it in GitHub Desktop.
Save mr21/770a88fd0d994cd593c3 to your computer and use it in GitHub Desktop.
// `yourElement` can be a NodeElement or a jQuery object as you want.
var selection = $.plugin_selection(yourElement)
// `.applyThis` is useful if you want to specify the `this` for all your callbacks.
// By default it's `window`.
.applyThis(object)
.onElementsAdded(function(jq_elems) {
// This callback is called when some elements are selected.
// Here, `jq_elems` is a jQuery object who contain the new elements selected.
// This object doesn't contain only one element if the user use the shift key.
// In this callback the `this` pointer is `window` by default
// or it's the object you specify with `.applyThis`.
})
.onElementsRemoved(function(jq_elems) {
// Same behaviour here.
// If the user unselect all his items in the same time
// `jq_elems` will be a jQuery list.
});
// Here you have an access to the list of the elements actually selected.
// This array is automatically updated (it's not a copy).
var elSelected = selection.getArraySelection();
// By calling `lockCtrlKey` you lock the Ctrl key.
// The user can't unselect all the items by clicking around.
// There is also a `.unlockCtrlKey` method.
selection.lockCtrlKey();
// You can disable the shift key if you want.
// There is also a `.enableShiftKey` method.
selection.disableShiftKey();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment