forked from Mobius1/Selectable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectable.min.js
11 lines (11 loc) · 13 KB
/
selectable.min.js
1
2
3
4
5
6
7
8
9
10
11
/*!
*
* Selectable
* Copyright (c) 2017 Karl Saunders (http://mobius.ovh)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Version: 0.10.8
*
*/
(function(a,f){var g="Selectable";"object"==typeof exports?module.exports=f(g):"function"==typeof define&&define.amd?define([],f):a[g]=f(g)})("undefined"==typeof global?this.window||this.global:global,function(){"use strict";function a(G){this.version="0.10.8",this.config=v(k,G),this.init()}var g="ontouchstart"in window||window.DocumentTouch&&document instanceof DocumentTouch,j="classList"in document.documentElement,k={toggle:!1,autoRefresh:!0,throttle:50,appendTo:document.body,filter:".ui-selectable",tolerance:"touch",autoScroll:{offset:40,increment:10},lasso:{border:"1px dotted #000",backgroundColor:"rgba(52, 152, 219, 0.2)"},classes:{lasso:"ui-lasso",selected:"ui-selected",container:"ui-container",selecting:"ui-selecting",selectable:"ui-selectable",unselecting:"ui-unselecting"}},m=function(G,H,I){G.addEventListener(H,I,!1)},p=function(G,H,I){G.removeEventListener(H,I)},q=function(G,H){return G&&G!==document.documentElement&&(H(G)?G:q(G.parentNode,H))},s=function(G){return"[object Object]"===Object.prototype.toString.call(G)},u=function(G){return Array.isArray(G)||G instanceof HTMLCollection||G instanceof NodeList},v=function(G,H){for(var I in H)if(H.hasOwnProperty(I)){var J=H[I];J&&s(J)?(G[I]=G[I]||{},v(G[I],J)):G[I]=J}return G},z=function(G,H,I){if(s(G))for(var J in G)Object.prototype.hasOwnProperty.call(G,J)&&H.call(I,G[J],J);else for(J=0;J<G.length;J++)H.call(I,G[J],J)},A=function(G,H){var I=G.style;if(G){if(void 0===H)return window.getComputedStyle(G);s(H)&&z(H,function(J,K){K in I||(K="-webkit-"+K),G.style[K]=J+("string"==typeof J?"":"opacity"===K?"":"px")})}},B=function(G){var H=window,I=G.getBoundingClientRect(),J=document.documentElement||document.body.parentNode||document.body,K=void 0===H.pageXOffset?J.scrollLeft:H.pageXOffset,L=void 0===H.pageYOffset?J.scrollTop:H.pageYOffset;return{x1:I.left+K,x2:I.left+I.width+K,y1:I.top+L,y2:I.top+I.height+L,height:I.height,width:I.width}},C=function(G,H,I){var J;return function(){if(I=I||this,!J)return G.apply(I,arguments),J=!0,setTimeout(function(){J=!1},H)}},D={add:function(G,H){j?G.classList.add(H):!D.contains(G,H)&&(G.className=G.className.trim()+" "+H)},remove:function(G,H){j?G.classList.remove(H):D.contains(G,H)&&(G.className=G.className.replace(new RegExp("(^|\\s)"+H.split(" ").join("|")+"(\\s|$)","gi")," "))},contains:function(G,H){if(G)return j?G.classList.contains(H):!!G.className&&!!G.className.match(new RegExp("(\\s|^)"+H+"(\\s|$)"))}},E=function(G){return!!G.ctrlKey||!!G.metaKey},F=function(G){return!!G.shiftKey};return a.prototype={init:function(){var G=this,H=this.config;this.autoscroll=s(H.autoScroll),this.data={x:0,y:0,right:0,left:0,down:0,up:0},this.lasso=!1,H.lasso&&s(H.lasso)&&(this.lasso=document.createElement("div"),this.lasso.className=H.classes.lasso,A(this.lasso,v({position:"fixed",opacity:0},H.lasso))),g&&(H.toggle=!1),this.events={start:this.start.bind(this),touchstart:this.touchstart.bind(this),drag:this.drag.bind(this),end:this.end.bind(this),keyup:this.keyup.bind(this),keydown:this.keydown.bind(this),recalculate:C(this.recalculate,H.throttle,this)},this.autoscroll&&(this.events.scroll=C(this.scroll,H.throttle,this)),this.setContainer(),this.update(),this.enable(),setTimeout(function(){G.emit("selectable.init")},10)},update:function(){var G=this.config.classes,H=D.contains;this.size={rect:B(this.container),x:this.container.scrollLeft,y:this.container.scrollTop,w:this.container.scrollWidth,h:this.container.scrollHeight},this.items=[],z(this.nodes,function(I,J){D.add(I,G.selectable),this.items[J]={node:I,rect:B(I),startselected:!1,selected:H(I,G.selected),selecting:H(I,G.selecting),unselecting:H(I,G.unselecting)}},this),this.emit("selectable.update",this.items)},bind:function(){var G=this.events;m(this.container,"mousedown",G.start),m(document,"mouseup",G.end),m(document,"keydown",G.keydown),m(document,"keyup",G.keyup),this.autoscroll&&m(this.container,"scroll",G.scroll),m(this.container,"touchstart",G.touchstart),m(document,"touchend",G.end),m(document,"touchcancel",G.end),!1!==this.lasso&&(m(document,"touchmove",G.drag),m(document,"mousemove",G.drag)),m(window,"resize",G.recalculate),m(window,"scroll",G.recalculate)},unbind:function(){var G=this.events;p(this.container,"mousedown",G.start),p(document,"mousemove",G.drag),p(document,"mouseup",G.end),p(document,"keydown",G.keydown),p(document,"keyup",G.keyup),this.autoscroll&&p(this.container,"scroll",G.scroll),p(this.container,"touchstart",G.start),p(document,"touchend",G.end),p(document,"touchcancel",G.end),p(document,"touchmove",G.drag),p(window,"resize",G.recalculate),p(window,"scroll",G.recalculate)},touchstart:function(G){p(this.container,"mousedown",this.events.start),this.start(G)},start:function(G){if(this.container.contains(G.target)&&3!==G.which&&!(0<G.button)){var J,H=this,I=this.config,K=q(G.target,function(Q){return Q===H.container||D.contains(Q,I.classes.selectable)});if(!K||I.disabled)return!1;-1===["INPUT","SELECT","BUTTON","TEXTAREA","OPTION"].indexOf(G.target.tagName)&&G.preventDefault();var L="touchstart"===G.type;if(this.dragging=!0,this.origin={x:L?G.touches[0].pageX:G.pageX,y:L?G.touches[0].pageY:G.pageY},this.autoscroll&&(this.origin.scroll={x:this.container.scrollLeft,y:this.container.scrollTop}),this.lasso&&this.container.appendChild(this.lasso),K!==this.container&&D.add(K,I.classes.selecting),I.autoRefresh&&this.update(),F(G)&&this.startEl)for(var M=this.items,N=this.getNodes().indexOf(K),O=this.getNodes().indexOf(this.startEl),P=N<O?1:-1;(N+=P)&&N!==O;)M[N].selecting=!0;z(this.items,function(Q){var R=Q.node,S=R===K;if(Q.selected){Q.startselected=!0;var T=!1;T=L||I.toggle||E(G)?S:!S&&!F(G),T&&(D.remove(R,I.classes.selected),Q.selected=!1,D.add(R,I.classes.unselecting),Q.unselecting=!0)}S&&(J=Q)}),this.startEl=K,this.emit("selectable.start",G,J)}},drag:function(G){var H=this.config;if(!(H.disabled||!this.dragging||F(G))){var J,K,I=this,L="touchmove"===G.type;this.offset=J={x1:this.origin.x,y1:this.origin.y,x2:L?G.touches[0].pageX:G.pageX,y2:L?G.touches[0].pageY:G.pageY,scroll:{x:0,y:0}},this.autoscroll&&(J.scroll={x:this.container.scrollLeft,y:this.container.scrollTop},this.scrolling={x:0,y:0},this.autoScroll(G)),J.x1>J.x2&&(K=J.x2,J.x2=J.x1,J.x1=K),J.y1>J.y2&&(K=J.y2,J.y2=J.y1,J.y1=K),z(this.items,function(N){I.highlight(N,E(G))});var M={x1:J.x1+this.data.right,x2:J.x2+this.data.left-(J.x1+this.data.right),y1:J.y1+this.data.down,y2:J.y2+this.data.up-(J.y1+this.data.down)};this.lasso&&this.updateHelper(M),this.emit("selectable.drag",G,M)}},end:function(G){if(this.dragging){this.dragging=!1;var K,H=this,I=H.config,J=G.target,L=[],M=[],N="touchend"===G.type?G.touches[0]||G.changedTouches[0]:G;this.lasso&&this.container.contains(this.lasso)&&this.container.removeChild(this.lasso),this.lasso&&(A(this.lasso,{opacity:0,left:0,width:0,top:0,height:0}),J=document.elementFromPoint(N.pageX,N.pageY)),K=q(J,function(O){return D.contains(O,I.classes.selectable)}),this.data.right=0,this.data.left=0,this.data.down=0,this.data.up=0,z(this.items,function(O){I.toggle&&O.node===K&&O.node===H.startEl&&O.selecting&&O.startselected&&(O.unselecting=!0,O.selecting=!1),O.unselecting&&(M.push(O),this.unselect(O)),O.selecting&&(L.push(O),this.select(O))},this),this.emit("selectable.end",G,L,M)}},keydown:function(G){if(this.cmdDown=E(G),this.shiftDown=F(G),this.cmdDown){var H=G.code||G.keyCode;(65==H||97==H)&&(G.preventDefault(),this.selectAll())}},keyup:function(G){this.cmdDown=E(G),this.shiftDown=F(G)},scroll:function(){z(this.items,function(H){H.rect=B(H.node)})},updateHelper:function(G){var H={opacity:1,left:G.x1,width:G.x2,top:G.y1,height:G.y2};this.autoscroll&&(H=v(H,{zIndex:0,position:"absolute",left:G.x1-this.size.rect.x1+this.offset.scroll.x,top:G.y1-this.size.rect.y1+this.offset.scroll.y})),A(this.lasso,H)},autoScroll:function(G){var H=this.config.autoScroll,I=this.size.rect,J=this.offset.scroll.x,K=this.offset.scroll.y,L=this.size.w,M=this.size.h,N=0,O=0,P=J,Q=K;(0<K||K<M-I.height)&&(G.pageY>=I.y2-H.offset||G.pageY<=I.y1+H.offset)&&(G.pageY>=I.y2-H.offset?(O=H.increment,this.scrolling.y=1):G.pageY<=I.y1+H.offset&&(O=-H.increment,this.scrolling.y=-1),Q+=O,this.container.scrollTop=this.offset.scroll.y=Q),(0<J||J<L-I.width)&&(G.pageX>=I.x2-H.offset||G.pageX<=I.x1+H.offset)&&(G.pageX>=I.x2-H.offset?(N=H.increment,this.scrolling.x=1):G.pageX<=I.x1+H.offset&&(N=-H.increment,this.scrolling.x=-1),P+=N,this.container.scrollLeft=this.offset.scroll.x=P),this.data.x=this.origin.scroll.x-this.offset.scroll.x,this.data.y=this.origin.scroll.y-this.offset.scroll.y,0<this.scrolling.x?this.data.right=this.data.x:0>this.scrolling.x&&(this.data.left=this.data.x),0<this.scrolling.y?this.data.down=this.data.y:0>this.scrolling.y&&(this.data.up=this.data.y)},highlight:function(G,H){var I=this.offset,J=this.config,K=J.classes,L=G.node,M=G.rect,N=this.data,O=!1;"touch"==J.tolerance?O=!(M.x1>I.x2+N.left||M.x2<I.x1+N.right||M.y1>I.y2+N.up||M.y2<I.y1+N.down):"fit"==J.tolerance&&(O=M.x1>I.x1+N.right&&M.x2<I.x2+N.left&&M.y1>I.y1+N.down&&M.y2<I.y2+N.up),O?(G.selected&&!J.toggle&&(D.remove(L,K.selected),G.selected=!1),G.unselecting&&(!J.toggle||J.toggle&&"drag"!==J.toggle)&&(D.remove(L,K.unselecting),G.unselecting=!1),!G.selecting&&(D.add(L,K.selecting),G.selecting=!0)):(G.selecting&&(H&&G.startselected?(D.remove(L,K.selecting),G.selecting=!1,D.add(L,K.selected),G.selected=!0):(D.remove(L,K.selecting),G.selecting=!1,G.startselected&&!J.toggle&&(D.add(L,K.unselecting),G.unselecting=!0))),L.selected&&!H&&!G.startselected&&(D.remove(L,K.selected),G.selected=!1,D.add(L,K.unselecting),G.unselecting=!0))},setContainer:function(G){var I,H=this.config;if(this.container&&(I=this.container,this.unbind()),G=G||H.appendTo,"string"==typeof G?this.container=document.querySelector(G):G instanceof Element&&G.nodeName&&(this.container=G),D.add(this.container,this.config.classes.container),I&&D.remove(I,this.config.classes.container),u(H.filter)?this.nodes=[].slice.call(H.filter):"string"==typeof H.filter&&(this.nodes=[].slice.call(this.container.querySelectorAll(H.filter))),this.autoscroll){var J=A(this.container);"static"===J.position&&A(this.container,{position:"relative"})}this.bind()},select:function(G,H){if(u(G))return z(G,function(K){this.select(K)},this),this.getSelectedItems();if(G=this.get(G),G){if(this.config.toggle&&"drag"===this.config.toggle&&!H&&G.selected&&!this.cmdDown)return this.unselect(G);var I=G.node,J=this.config.classes;return D.remove(I,J.selecting),D.add(I,J.selected),G.selecting=!1,G.selected=!0,G.startselected=!0,this.emit("selectable.select",G),G}return!1},unselect:function(G){if(u(G))return z(G,function(J){this.unselect(J)},this),this.getSelectedItems();if(G=this.get(G),G){var H=G.node,I=this.config.classes;return G.selecting=!1,G.selected=!1,G.unselecting=!1,G.startselected=!1,D.remove(H,I.unselecting),D.remove(H,I.selecting),D.remove(H,I.selected),this.emit("selectable.unselect",G),G}return!1},add:function(G){u(G)?z(G,function(H){0>this.nodes.indexOf(H)&&H instanceof Element&&this.nodes.push(H)},this):0>this.nodes.indexOf(G)&&G instanceof Element&&this.nodes.push(G),this.update()},remove:function(G,H){if(G=this.get(G),G){if(u(G))for(var I=G.length-1;0<=I;I--)this.remove(G[I],0<I);else{var J=G.node,K=this.config.classes,L=D.remove;L(J,K.selectable),L(J,K.unselecting),L(J,K.selecting),L(J,K.selected),this.nodes.splice(this.nodes.indexOf(G.node),1)}return H||this.update(),!0}return!1},recalculate:function(){z(this.nodes,function(G,H){this.items[H].rect=B(G)},this),this.emit("selectable.recalculate")},selectAll:function(){z(this.items,function(G){this.select(G,!0)},this)},clear:function(){for(var G=this.items.length-1;0<=G;G--)this.unselect(this.items[G])},get:function(G){var H=!1;return u(G)?(H=[],z(G,function(I){I=this.get(I),I&&H.push(I)},this)):isNaN(G)?G instanceof Element?H=this.items[this.nodes.indexOf(G)]:s(G)&&0<=this.items.indexOf(G)&&(H=G):0<=this.items.indexOf(this.items[G])&&(H=this.items[G]),H},getItems:function(){return this.items},getNodes:function(){return this.nodes},getSelectedItems:function(){return this.getItems().filter(function(G){return G.selected})},getSelectedNodes:function(){return this.getSelectedItems().map(function(G){return G.node})},on:function(G,H){this.events=this.events||{},this.events[G]=this.events[G]||[],this.events[G].push(H)},off:function(G,H){this.events=this.events||{},!1==G in this.events||this.events[G].splice(this.events[G].indexOf(H),1)},emit:function(G){if(this.events=this.events||{},!1!=G in this.events)for(var H=0;H<this.events[G].length;H++)this.events[G][H].apply(this,Array.prototype.slice.call(arguments,1))},enable:function(){return this.enabled||(this.enabled=!0,this.bind(),D.add(this.container,this.config.classes.container),this.emit("selectable.enable")),this.enabled},disable:function(){return this.enabled&&(this.enabled=!1,this.unbind(),D.remove(this.container,this.config.classes.container),this.emit("selectable.disable")),this.enabled},destroy:function(){this.disable(),this.remove(this.items),z(this,function(G,H){"version"!==H&&"config"!==H&&delete this[H]},this)}},a});