GithubHelp home page GithubHelp logo

raymondseger / selection Goto Github PK

View Code? Open in Web Editor NEW

This project forked from simonwep/selection

0.0 2.0 0.0 687 KB

๐Ÿ–ฑ๏ธ Selection - A simple and lightweight library to realize visual DOM Selections, like on your Desktop. No jQuery. Supports any CSS library, e.g. Bootstrap. Including vertical & horizontal scroll support.

Home Page: https://simonwep.github.io/selection/

License: MIT License

CSS 12.24% JavaScript 58.44% HTML 29.32%

selection's Introduction

Logo

Simple and easy selection library to enable visual DOM-Selection

License MIT No dependencies Support me jsdelivr hits Build Status downloads per week gzip size brotli size Current version

Demo

Features

  • Supports touch devices
  • Simple usage
  • No jQuery
  • Vertical and horizontal scroll support

Install

Via npm

$ npm install @simonwep/selection-js --save

Include via jsdelivr.net

<script src="https://cdn.jsdelivr.net/npm/@simonwep/selection-js/dist/selection.min.js"></script>

Usage

const options = {

  // All elements with the class 'selectable' selectable.
  selectables: ['.selectable']
};
const selection = Selection.create(options);

It's recommended to also specify a bounding area for the selection (see 'Options').


Options

const selection = new Selection({

    // Class for the selection-area-element
    class: 'selection-area',

    // px, how many pixels the point should move before starting the selection
    startThreshold: 10,

    // Disable the selection functionality for touch devices
    disableTouch: false,

    // On which point an element should be selected.
    // Available modes are cover (cover the entire element), center (touch the center) or
    // the default mode is touch (just touching it).
    mode: 'touch',

    // Enable single-click selection
    singleClick: true,

    // Query selectors from elements which can be selected
    selectables: [],

    // Query selectors for elements from where a selection can be start
    startareas: ['html'],

    // Query selectors for elements which will be used as boundaries for the selection
    boundaries: ['html'],

    // Query selector or dom node to set up container for selection-area-element
    selectionAreaContainer: 'body',
    
    // On scrollable areas the number on px per frame is devided by this amount.
    // Default is 10 to provide a enjoyable scroll experience.
    scrollSpeedDivider: 10,

    // Will be called before the selection starts (mouse / touchdown). Can be used
    // to specify which action / mousebutton are needed to start the selection.
    validateStart(evt) {
        evt; // MouseEvent or TouchEvent

        // Return true to start the selection, false to cancel it.
        return true;
    },

    // Element selection stardet, see Events for details
    onStart(evt) {
        evt.selection;
        evt.eventName;
        evt.areaElement;
        evt.originalEvent;
        evt.selectedElements;
        evt.changedElements;
    },

    // Single-click selection
    onSelect(evt) {
       // Same properties as onStart
       evt.target; // Clicked element
    },

    // Element selection move
    onMove(evt) {
       // Same properties as onStart
    },

    // Element selection stopped
    onStop(evt) {
       // Same properties as onStart
    },

    // Filter single element
    selectionFilter(evt) {
       evt.selection; // This selection instance
       evt.eventName; // The event name
       evt.element;   // The element which is in the current selection
       // return true to keep the element in the current selection
    }
});

Methods

  • selection.option(name:String) - Returns the option by name.

  • selection.option(name:String, value:Mixed) - Set a new option value.

  • selection.disable() - Disable the functionality to make selections.

  • selection.enable() - Enable the functionality to make selections.

  • selection.destroy() - Unbinds all events and removes the area-element.

  • selection.cancel() - Cancels the current selection process.

  • selection.keepSelection() - Will save the current selected elements and will append those to the next selection. Can be used to allow multiple selections.

  • selection.clearSelection() - Clear the previous selection (elements which were saved by keepSelection()).

  • selection.getSelection() - Returns currently selected elements as an Array.

  • selection.removeFromSelection(el:HTMLElement) - Removes a particular element from the current selection.

  • selection.resolveSelectables() - Need to be called if during a selection elements have been added.

  • selection.select(query:[String]|String) - Manually adds elements to the selection, can be a / an array of queries / elements.

Events

Event properties of start, stop and move event.

  • selection:Selection - Current selection object.
  • eventName:String - The event name.
  • areaElement:HTMLElement - The selection element.
  • originalEvent:Event - The original mouse-event.
  • selectedElements:Array[HTMLElements] - Array with currently selected HTMLElements.
  • changedElements:Object
  • added:Array[HTMLElements] - Elements which are added to selectedElements since the last interaction (mousemove).
  • removed:Array[HTMLElements] - Elements which are removed from selectedElements since last interaction (mousemove).

Filter event

Will be called on every selection, can be used to ignore specific elements in the current selection.

  • selection:Selection - Current selection object.
  • eventName:String - The event name.
  • element:HTMLElement - HTMLElement, return false if you didn't want it in the selection.

Static methods

Selection

  • Selection.create(options:Object):Selection - Creates a new instance.

Selection.utils

  • on(el:HTMLElement, event:String, fn:Function[, options :Object]) - Attach an event handler function.
  • off(el:HTMLElement, event:String, fn:Function[, options :Object]) - Remove an event handler.
  • css(el:HTMLElement):Object - Get all css properties from this element.
  • css(el:HTMLElement, attr:String):Mixed - Get the value from a style property.
  • css(el:HTMLElement, attr:String, val:String) - Set a specific style property.
  • css(el:HTMLElement, attr:Object) - Set multiple style properties.
  • intersects(ela:HTMLElement, elb:HTMLElement):Boolean - Check if an HTMLElement intersects another.
  • selectAll(selector:String|Array):Array - Returns all HTMLElements which were selected by the selector.
  • eventPath(evt:DOMEvent):NodeList - Event.composedPath() polyfill.
  • removeElement(arr:Array, el:Object) - Removes an particular element from an Array.

selection's People

Contributors

simonwep avatar sauldoescode avatar acusti avatar hendrysadrak avatar cisolarix avatar alvarobernalg avatar daniel08s avatar impressivewebs avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.