Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dragselect

Package Overview
Dependencies
Maintainers
1
Versions
97
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dragselect

easy javascript drag select functionality to your projects

  • 1.7.13
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10K
increased by10.52%
Maintainers
1
Weekly downloads
 
Created
Source
    ____                   _____      __          __ 
   / __ \_________ _____ _/ ___/___  / /__  _____/ /_
  / / / / ___/ __ `/ __ `/\__ \/ _ \/ / _ \/ ___/ __/
 / /_/ / /  / /_/ / /_/ /___/ /  __/ /  __/ /__/ /_  
/_____/_/   \__,_/\__, //____/\___/_/\___/\___/\__/  
                 /____/                              

DragSelect

easily add a selection algorythm to your application/website.

Project Page: Demo & Info

https://thibaultjanbeyer.github.io/DragSelect/

Key-Features

  • No dependencies
  • Accessibility (a11y)
  • Add drag selection
  • Choose which elements can be selected
  • Great browser support, works even like a charm on IE9
  • Lightweight, only ~3KB gzipped
  • DragSelect was written with Performance in mind
  • Supports SVG
  • Free & open source under MIT License
  • Ease of use

Why?

Because apparently there is nothing that does not require jquery out there yet.
This is better than https://jqueryui.com/selectable/ and has no dependencies. We use it currently in a professional rich interface application where we have a file management system. The user can select files to organize them and change their metadata, with this plugin our users are able to select multiple files and perform batch/bulk-operations (applying changes to multiple files at once). I can easily think of dozens other possibilites this can be used for. I’m really keen to know how you use it in your projects, please let me know.

Installation

easy

Just download the file (minified) and add it to your document:

<script src="https://thibaultjanbeyer.github.io/DragSelect/ds.min.js"></script>

npm

npm install --save-dev dragselect

bower

bower install --save-dev dragselect

That's it, you're ready to rock!
Of course you can also just include the code within your code to save a request.

DragSelect supports module.exports, AMD Modules with define and has a fallback to global namespace for maximum out of the box support.

Usage

Now in your JavaScript you can simply pass elements to the function like so:

simple

Choose which elements can be selected:

new DragSelect({
  selectables: document.getElementsByClassName('selectable-nodes')
});

complete

All options are optional. You could also just initiate the Dragselect by new DragSelect(); without any option.

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.
  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 algorythm never adds elements twice.
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

You can also use the "shift", "ctrl" or "command" key to make multiple independent selections.

Accessibility (a11y)

DragSelect is accessibly by default:

TLDR; => Your selectables should be buttons: <button type="button"></button>.

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. There is one little thing you have to do tho’: the selectables have to be pressable (clickable)! To achieve this, they should be of type <button type="button"></button>.

Properties:

propertytypeusage
selectablesDOM elements (nodes)OPTIONAL. The elements that can be selected
selectorsingle DOM element (node)OPTIONAL. The square that will draw the selection. Autocreated by default
areasingle DOM element (node)OPTIONAL. The square in which you are able to select
customStylesbooleanOPTIONAL. If true, no styles will be automatically applied (except position: absolute). Default: false
multiSelectKeysarrayOPTIONAL. 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']
autoScrollSpeedintegerOPTIONAL. The speed in which the area scrolls while selecting (if available). The unit is pixel per movement. Set to 0.0001 to disable autoscrolling. Default = 1
onDragStartfunctionOPTIONAL. 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
onDragMovefunctionOPTIONAL. Fired when the user drags. This callback gets the event object. Executed before DragSelect function code ran, after getting the current mouse position
onElementSelectfunctionOPTIONAL. Fired every time an element is selected. This callback gets a property which is the selected node
onElementUnselectfunctionOPTIONAL. Fired every time an element is de-selected. This callback gets a property which is the de-selected node
callbackfunctionOPTIONAL. Callback function that gets fired when the selection is released. This callback gets a property which is an array that holds all selected nodes

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:

methodpropertiesusage
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
addSelectablesDOM elements (nodes), Boolean (addToSelection)Adds elements that can be selected. Don’t worry, a smart algorythm makes sure that nodes are never added twice. If boolean is set to true: elements will also be added to current selection.
removeSelectablesDOM elements (nodes), Boolean (removeFromSelection)Remove elements that can be selected. If boolean is set to true: elements will also be removed from current selection.
getSelectables/Returns array with all nodes that can be selected.
getInitialCursorPosition/Returns the x, y coordinates the cursor had when first clicked
getCurrentCursorPosition/Returns current/last x, y coordinates of the cursor
getCursorPositionDifference/Returns object with the x, y difference between the initial and the last cursor position

Classes

nametrigger
.ds-selectedOn elements that are selected
.ds-hoverOn elements that are currently hovered
.ds-selectorOn the selector element
.ds-selectableOn elements that can be selected

Have Fun!

Typewriter Gif

Keywords

FAQs

Package last updated on 06 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc