"use strict";function _typeof(obj){if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj}}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj}}return _typeof(obj)}function _toConsumableArray(arr){return _arrayWithoutHoles(arr)||_iterableToArray(arr)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _iterableToArray(iter){if(Symbol.iterator in Object(iter)||Object.prototype.toString.call(iter)==="[object Arguments]")return Array.from(iter)}function _arrayWithoutHoles(arr){if(Array.isArray(arr)){for(var i=0,arr2=new Array(arr.length);i-1&&remove){this._removeSelectable(selectable,indexOf,fromSelection)}}}},{key:"_addSelectable",value:function _addSelectable(selectable,toSelection){selectable.classList.add(this.selectableClass);selectable.addEventListener("click",this._onClick);this.selectables.push(selectable);if(toSelection&&this._selected.indexOf(selectable)<0){selectable.classList.add(this.selectedClass);this._selected.push(selectable)}}},{key:"_removeSelectable",value:function _removeSelectable(selectable,indexOf,fromSelection){selectable.classList.remove(this.hoverClass);selectable.classList.remove(this.selectableClass);selectable.removeEventListener("click",this._onClick);this.selectables.splice(indexOf,1);if(fromSelection&&this._selected.indexOf(selectable)>-1){selectable.classList.remove(this.selectedClass);this._selected.splice(this._selected.indexOf(selectable),1)}}},{key:"handleClick",value:function handleClick(event){if(this.mouseInteraction){return}if(this._isRightClick(event)){return}var node=event.target;if(this._isMultiSelectKeyPressed(event)){this._prevSelected=this._selected.slice()}else{this._prevSelected=[]}this.checkIfInsideSelection(true);if(this.selectables.indexOf(node)>-1){this.toggle(node)}this._end(event)}},{key:"_createSelector",value:function _createSelector(){var selector=document.createElement("div");selector.style.position="absolute";if(!this.customStyles){selector.style.background="rgba(0, 0, 255, 0.1)";selector.style.border="1px solid rgba(0, 0, 255, 0.45)";selector.style.display="none";selector.style.pointerEvents="none"}var _area=this.area===document?document.body:this.area;_area.appendChild(selector);return selector}},{key:"start",value:function start(){this._handleSelectables(this._initialSelectables);this.area.addEventListener("mousedown",this._startUp);this.area.addEventListener("touchstart",this._startUp,{passive:false})}},{key:"startUp",value:function startUp(event){if(event.type==="touchstart")event.preventDefault();if(this._isRightClick(event))return;if(this._isScrollbarClick(event,this.area))return;this.onDragStartBegin(event);if(this._breaked)return false;this.mouseInteraction=true;this.selector.style.display="block";if(this._isMultiSelectKeyPressed(event))this._prevSelected=this._selected.slice();else this._prevSelected=[];this._getStartingPositions(event);this.checkIfInsideSelection(true);this.selector.style.display="none";this.moveStartCallback(event);if(this._breaked)return false;this.area.removeEventListener("mousedown",this._startUp);this.area.removeEventListener("touchstart",this._startUp,{passive:false});this.area.addEventListener("mousemove",this._handleMove);this.area.addEventListener("touchmove",this._handleMove,{passive:false});document.addEventListener("mouseup",this._end);document.addEventListener("touchend",this._end)}},{key:"_isMultiSelectKeyPressed",value:function _isMultiSelectKeyPressed(event){this._multiSelectKeyPressed=false;if(this.multiSelectMode){this._multiSelectKeyPressed=true}else{for(var index=0;indexthis._initialCursorPos.x-scrollAmount.x){selectorPos.x=this._initialCursorPos.x+this._initialScroll.x;selectorPos.w=cursorPosNew.x-this._initialCursorPos.x+scrollAmount.x}else{selectorPos.x=cursorPosNew.x+scrollNew.x;selectorPos.w=this._initialCursorPos.x-cursorPosNew.x-scrollAmount.x}if(cursorPosNew.y>this._initialCursorPos.y-scrollAmount.y){selectorPos.y=this._initialCursorPos.y+this._initialScroll.y;selectorPos.h=cursorPosNew.y-this._initialCursorPos.y+scrollAmount.y}else{selectorPos.y=cursorPosNew.y+scrollNew.y;selectorPos.h=this._initialCursorPos.y-cursorPosNew.y-scrollAmount.y}return selectorPos}},{key:"checkIfInsideSelection",value:function checkIfInsideSelection(force){var anyInside=false;for(var i=0,il=this.selectables.length;i-1&&this._multiSelectKeyPressed){this.unselect(item)}item.classList.add(this.hoverClass)}},{key:"_handleUnselection",value:function _handleUnselection(item,force){if(!item.classList.contains(this.hoverClass)&&!force){return false}var posInSelectedArray=this._selected.indexOf(item);var isInPrevSelection=this._prevSelected.indexOf(item);if(posInSelectedArray>-1&&isInPrevSelection<0){this.unselect(item)}else if(posInSelectedArray<0&&isInPrevSelection>-1){this.select(item)}item.classList.remove(this.hoverClass)}},{key:"select",value:function select(item){if(this._selected.indexOf(item)>-1)return false;this._selected.push(item);item.classList.add(this.selectedClass);this.selectCallback(item);if(this._breaked)return false;return item}},{key:"unselect",value:function unselect(item){if(this._selected.indexOf(item)<0)return false;this._selected.splice(this._selected.indexOf(item),1);item.classList.remove(this.selectedClass);this.unselectCallback(item);if(this._breaked)return false;return item}},{key:"toggle",value:function toggle(item){if(this._selected.indexOf(item)>-1){this.unselect(item)}else{this.select(item)}return item}},{key:"_isElementTouching",value:function _isElementTouching(element,selectionRect,scroll){var rect=element.getBoundingClientRect();var elementRect={y:rect.top/this.zoom+scroll.y,x:rect.left/this.zoom+scroll.x,h:rect.height/this.zoom,w:rect.width/this.zoom};if(selectionRect.xelementRect.x&&selectionRect.yelementRect.y){return true}else{return false}}},{key:"_autoScroll",value:function _autoScroll(event){var edge=this.isCursorNearEdge(event,this.area);var docEl=document&&document.documentElement&&document.documentElement.scrollTop&&document.documentElement;var _area=this.area===document?docEl||document.body:this.area;if(edge==="top"&&_area.scrollTop>0){_area.scrollTop-=1*this.autoScrollSpeed}else if(edge==="bottom"){_area.scrollTop+=1*this.autoScrollSpeed}else if(edge==="left"&&_area.scrollLeft>0){_area.scrollLeft-=1*this.autoScrollSpeed}else if(edge==="right"){_area.scrollLeft+=1*this.autoScrollSpeed}}},{key:"isCursorNearEdge",value:function isCursorNearEdge(event,area){var cursorPosition=this._getCursorPos(event,area);var areaRect=this.getAreaRect(area);var tolerance={x:Math.max(areaRect.width/10,30),y:Math.max(areaRect.height/10,30)};if(cursorPosition.y0&&arguments[0]!==undefined?arguments[0]:true;var fromSelection=arguments.length>1&&arguments[1]!==undefined?arguments[1]:true;var withCallback=arguments.length>2?arguments[2]:undefined;this.reset(false,withCallback);this.area.removeEventListener("mousedown",this._startUp);this.area.removeEventListener("touchstart",this._startUp,{passive:false});document.removeEventListener("mouseup",this._end);document.removeEventListener("touchend",this._end);this._handleSelectables(_toConsumableArray(this.selectables),remove,fromSelection)}},{key:"getSelection",value:function getSelection(){return _toConsumableArray(this._selected)}},{key:"getCursorPos",value:function getCursorPos(event,_area,ignoreScroll){if(!event)return{x:0,y:0};var area=_area||_area!==false&&this.area;var pos=this._getCursorPos(event,area);var scroll=ignoreScroll?{x:0,y:0}:this.getScroll(area);return{x:pos.x+scroll.x,y:pos.y+scroll.y}}},{key:"addSelection",value:function addSelection(_nodes,triggerCallback,dontAddToSelectables){var nodes=this._toArray(_nodes);for(var index=0,il=nodes.length;index=0;i--){array[i]=nodes[i]}return array}},{key:"_isElement",value:function _isElement(node){try{return node instanceof HTMLElement||node instanceof SVGElement}catch(e){return _typeof(node)==="object"&&node.nodeType===1&&_typeof(node.style)==="object"&&_typeof(node.ownerDocument)==="object"}}},{key:"_getCursorPos",value:function _getCursorPos(event,area){if(!event)return{x:0,y:0};if(event.touches&&event.type!=="touchend"){this._lastTouch=event}event=event.touches?this._lastTouch.touches[0]:event;var cPos={x:event.pageX||event.clientX,y:event.pageY||event.clientY};var areaRect=this.getAreaRect(area||document);var docScroll=this.getScroll();return{x:(cPos.x-areaRect.left-docScroll.x)/this.zoom,y:(cPos.y-areaRect.top-docScroll.y)/this.zoom}}},{key:"getInitialCursorPosition",value:function getInitialCursorPosition(){return this._initialCursorPos}},{key:"getCurrentCursorPosition",value:function getCurrentCursorPosition(){return this._newCursorPos}},{key:"getPreviousCursorPosition",value:function getPreviousCursorPosition(){return this._previousCursorPos}},{key:"getCursorPositionDifference",value:function getCursorPositionDifference(usePreviousCursorDifference){var posA=this.getCurrentCursorPosition();var posB=usePreviousCursorDifference?this.getPreviousCursorPosition():this.getInitialCursorPosition();return{x:posA.x-posB.x,y:posA.y-posB.y}}},{key:"getScroll",value:function getScroll(area){var body={top:document.body.scrollTop>0?document.body.scrollTop:document.documentElement.scrollTop,left:document.body.scrollLeft>0?document.body.scrollLeft:document.documentElement.scrollLeft};var scroll={y:area&&area.scrollTop>=0?area.scrollTop:body.top,x:area&&area.scrollLeft>=0?area.scrollLeft:body.left};return scroll}},{key:"getAreaRect",value:function getAreaRect(area){if(area===document){var size={y:area.documentElement.clientHeight>0?area.documentElement.clientHeight:window.innerHeight,x:area.documentElement.clientWidth>0?area.documentElement.clientWidth:window.innerWidth};return{top:0,left:0,bottom:0,right:0,width:size.x,height:size.y}}var rect=area.getBoundingClientRect();return{top:rect.top,left:rect.left,bottom:rect.bottom,right:rect.right,width:area.clientWidth||rect.width,height:area.clientHeight||rect.height}}},{key:"_updatePos",value:function _updatePos(node,pos){node.style.left=pos.x+"px";node.style.top=pos.y+"px";node.style.width=pos.w+"px";node.style.height=pos.h+"px";return node}}]);return DragSelect}();if(typeof module!=="undefined"&&module!==null){module.exports=DragSelect}else if(typeof define!=="undefined"&&typeof define==="function"&&define){define(function(){return DragSelect})}else{window.DragSelect=DragSelect}