UNPKG

liquor-tree

Version:
8 lines (7 loc) 41.3 kB
/*! * LiquorTree v0.2.70 * (c) 2019 amsik * Released under the MIT License. */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.LiquorTree=t()}(this,function(){"use strict";var e={name:"node-content",props:["node"],render:function(e){var t=this,n=this.node,i=this.node.tree.vm;if(n.isEditing){var r=n.text;return this.$nextTick(function(e){t.$refs.editCtrl.focus()}),e("input",{domProps:{value:n.text,type:"text"},class:"tree-input",on:{input:function(e){r=e.target.value},blur:function(){n.stopEditing(r)},keyup:function(e){13===e.keyCode&&n.stopEditing(r)},mouseup:function(e){e.stopPropagation()}},ref:"editCtrl"})}return i.$scopedSlots.default?i.$scopedSlots.default({node:this.node}):e("span",{domProps:{innerHTML:n.text}})}};function t(e,t,n,i,r,o,s,a,d,c){"function"==typeof s&&(d=a,a=s,s=!1);var h,l="function"==typeof n?n.options:n;if(e&&e.render&&(l.render=e.render,l.staticRenderFns=e.staticRenderFns,l._compiled=!0,r&&(l.functional=!0)),i&&(l._scopeId=i),o?(h=function(e){(e=e||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext)||"undefined"==typeof __VUE_SSR_CONTEXT__||(e=__VUE_SSR_CONTEXT__),t&&t.call(this,d(e)),e&&e._registeredComponents&&e._registeredComponents.add(o)},l._ssrRegister=h):t&&(h=s?function(){t.call(this,c(this.$root.$options.shadowRoot))}:function(e){t.call(this,a(e))}),h)if(l.functional){var p=l.render;l.render=function(e,t){return h.call(t),p(e,t)}}else{var u=l.beforeCreate;l.beforeCreate=u?[].concat(u,h):[h]}return n}var n=e;e.__file="NodeContent.vue";var i={name:"Node",inject:["tree"],props:["node","options"],components:{NodeContent:t({},void 0,n,void 0,void 0,void 0,void 0,void 0)},watch:{node:function(){this.node.vm=this}},data:function(){return this.node.vm=this,{loading:!1}},computed:{padding:function(){return this.node.depth*(this.options.paddingLeft?this.options.paddingLeft:this.options.nodeIndent)+"px"},nodeClass:function(){var e=this.node.states,t=this.hasChildren(),n={"has-child":t,expanded:t&&e.expanded,selected:e.selected,disabled:e.disabled,matched:e.matched,dragging:e.dragging,loading:this.loading,draggable:e.draggable};return this.options.checkbox&&(n.checked=e.checked,n.indeterminate=e.indeterminate),n},visibleChildren:function(){return this.node.children.filter(function(e){return e&&e.visible()})}},methods:{onNodeFocus:function(){this.tree.activeElement=this.node},focus:function(){this.$refs.anchor.focus(),this.node.select()},check:function(){this.node.checked()?this.node.uncheck():this.node.check()},select:function(e){void 0===e&&(e=evnt);var t=e.ctrlKey,n=this.options,i=this.tree,r=this.node;if(i.$emit("node:clicked",r),!n.editing||!r.isEditing){if(n.editing&&r.editable())return this.startEditing();if(n.checkbox&&n.checkOnSelect)return!n.parentSelect&&this.hasChildren()?this.toggleExpand():this.check(t);!n.parentSelect&&this.hasChildren()&&this.toggleExpand(),n.multiple?r.selected()?t?r.unselect():1!=this.tree.selectedNodes.length&&(i.unselectAll(),r.select()):r.select(t):r.selected()&&t?r.unselect():r.select()}},toggleExpand:function(){this.hasChildren()&&this.node.toggleExpand()},hasChildren:function(){return this.node.hasChildren()},startEditing:function(){this.tree._editingNode&&this.tree._editingNode.stopEditing(),this.node.startEditing()},stopEditing:function(){this.node.stopEditing()},handleMouseDown:function(e){this.options.dnd&&this.tree.vm.startDragging(this.node,e)}}},d="undefined"!=typeof navigator&&/msie [6-9]\\b/.test(navigator.userAgent.toLowerCase());function r(e){return function(e,t){return function(e,t){var n=d?t.media||"default":e,i=h[n]||(h[n]={ids:new Set,styles:[]});if(!i.ids.has(e)){i.ids.add(e);var r=t.source;if(t.map&&(r+="\n/*# sourceURL="+t.map.sources[0]+" */",r+="\n/*# sourceMappingURL=data:application/json;base64,"+btoa(unescape(encodeURIComponent(JSON.stringify(t.map))))+" */"),i.element||(i.element=document.createElement("style"),i.element.type="text/css",t.media&&i.element.setAttribute("media",t.media),c.appendChild(i.element)),"styleSheet"in i.element)i.styles.push(r),i.element.styleSheet.cssText=i.styles.filter(Boolean).join("\n");else{var o=i.ids.size-1,s=document.createTextNode(r),a=i.element.childNodes;a[o]&&i.element.removeChild(a[o]),a.length?i.element.insertBefore(s,a[o]):i.element.appendChild(s)}}}(e,t)}}var c=document.head||document.getElementsByTagName("head")[0],h={};var o=t({render:function(){var t=this,e=t.$createElement,n=t._self._c||e;return n("li",{staticClass:"tree-node",class:t.nodeClass,attrs:{"data-id":t.node.id},on:{mousedown:function(e){return e.stopPropagation(),t.handleMouseDown(e)}}},[n("div",{staticClass:"tree-content",style:["ltr"==t.options.direction?{"padding-left":t.padding}:{"padding-right":t.padding}],on:{click:function(e){return e.stopPropagation(),t.select(e)}}},[n("i",{staticClass:"tree-arrow",class:[{expanded:t.node.states.expanded,"has-child":t.node.children.length||t.node.isBatch},t.options.direction],on:{click:function(e){return e.stopPropagation(),t.toggleExpand(e)}}}),t._v(" "),t.options.checkbox?n("i",{staticClass:"tree-checkbox",class:{checked:t.node.states.checked,indeterminate:t.node.states.indeterminate},on:{click:function(e){return e.stopPropagation(),t.check(e)}}}):t._e(),t._v(" "),n("span",{ref:"anchor",staticClass:"tree-anchor",attrs:{tabindex:"-1"},on:{focus:t.onNodeFocus,dblclick:function(e){return t.tree.$emit("node:dblclick",t.node)}}},[n("node-content",{attrs:{node:t.node}})],1)]),t._v(" "),n("transition",{attrs:{name:"l-fade"}},[t.hasChildren()&&t.node.states.expanded?n("ul",{staticClass:"tree-children"},t._l(t.visibleChildren,function(e){return n("node",{key:e.id,attrs:{node:e,options:t.options}})}),1):t._e()])],1)},staticRenderFns:[]},function(e){e&&e("data-v-20094490_0",{source:'.tree-node{white-space:nowrap;display:flex;flex-direction:column;position:relative;box-sizing:border-box}.tree-content{display:flex;align-items:center;padding:3px;cursor:pointer;width:100%;box-sizing:border-box}.tree-node:not(.selected)>.tree-content:hover{background:#f6f8fb}.tree-node.selected>.tree-content{background-color:#e7eef7}.tree-node.disabled>.tree-content:hover{background:inherit}.tree-arrow{flex-shrink:0;height:30px;cursor:pointer;margin-left:30px;width:0}.tree-arrow.has-child{margin-left:0;width:30px;position:relative}.tree-arrow.has-child:after{border:1.5px solid #494646;position:absolute;border-left:0;border-top:0;left:9px;top:50%;height:9px;width:9px;transform:rotate(-45deg) translateY(-50%) translateX(0);transition:transform .25s;transform-origin:center}.tree-arrow.has-child.rtl:after{border:1.5px solid #494646;position:absolute;border-right:0;border-bottom:0;right:0;top:50%;height:9px;width:9px;transform:rotate(-45deg) translateY(-50%) translateX(0);transition:transform .25s;transform-origin:center}.tree-arrow.expanded.has-child:after{transform:rotate(45deg) translateY(-50%) translateX(-5px)}.tree-checkbox{flex-shrink:0;position:relative;width:30px;height:30px;box-sizing:border-box;border:1px solid #dadada;border-radius:2px;background:#fff;transition:border-color .25s,background-color .25s}.tree-arrow:after,.tree-checkbox:after{position:absolute;display:block;content:""}.tree-checkbox.checked,.tree-checkbox.indeterminate{background-color:#3a99fc;border-color:#218eff}.tree-checkbox.checked:after{box-sizing:content-box;border:1.5px solid #fff;border-left:0;border-top:0;left:9px;top:3px;height:15px;width:8px;transform:rotate(45deg) scaleY(0);transition:transform .25s;transform-origin:center}.tree-checkbox.checked:after{transform:rotate(45deg) scaleY(1)}.tree-checkbox.indeterminate:after{background-color:#fff;top:50%;left:20%;right:20%;height:2px}.tree-anchor{flex-grow:2;outline:0;display:flex;text-decoration:none;color:#343434;vertical-align:top;margin-left:3px;line-height:24px;padding:3px 6px;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.tree-node.selected>.tree-content>.tree-anchor{outline:0}.tree-node.disabled>.tree-content>.tree-anchor{color:#989191;background:#fff;opacity:.6;cursor:default;outline:0}.tree-input{display:block;width:100%;height:24px;line-height:24px;outline:0;border:1px solid #3498db;padding:0 4px}.l-fade-enter-active,.l-fade-leave-active{transition:opacity .3s,transform .3s;transform:translateX(0)}.l-fade-enter,.l-fade-leave-to{opacity:0;transform:translateX(-2em)}.tree--small .tree-anchor{line-height:19px}.tree--small .tree-checkbox{width:23px;height:23px}.tree--small .tree-arrow{height:23px}.tree--small .tree-checkbox.checked:after{left:7px;top:3px;height:11px;width:5px}.tree-node.has-child.loading>.tree-content>.tree-arrow,.tree-node.has-child.loading>.tree-content>.tree-arrow:after{border-radius:50%;width:15px;height:15px;border:0}.tree-node.has-child.loading>.tree-content>.tree-arrow{font-size:3px;position:relative;border-top:1.1em solid rgba(45,45,45,.2);border-right:1.1em solid rgba(45,45,45,.2);border-bottom:1.1em solid rgba(45,45,45,.2);border-left:1.1em solid #2d2d2d;-webkit-transform:translateZ(0);-ms-transform:translateZ(0);transform:translateZ(0);left:5px;-webkit-animation:loading 1.1s infinite linear;animation:loading 1.1s infinite linear;margin-right:8px}@-webkit-keyframes loading{0%{-webkit-transform:rotate(0);transform:rotate(0)}100%{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}@keyframes loading{0%{-webkit-transform:rotate(0);transform:rotate(0)}100%{-webkit-transform:rotate(360deg);transform:rotate(360deg)}}',map:void 0,media:void 0})},i,void 0,!(i.__file="TreeNode.vue"),void 0,r,void 0),s={name:"DragNode",props:["target"],computed:{style:function(){return void 0===this.target.top?"display: none":"top: "+this.target.top+"px; left: "+this.target.left+"px"}}},a=t({render:function(){var e=this,t=e.$createElement;return(e._self._c||t)("div",{staticClass:"tree-dragnode",style:e.style},[e._v("\n "+e._s(e.target.node.text)+"\n")])},staticRenderFns:[]},function(e){e&&e("data-v-7a41ac3e_0",{source:".tree-dragnode{padding:10px;border:1px solid #e7eef7;position:fixed;border-radius:8px;background:#fff;transform:translate(-50%,-110%);z-index:10}",map:void 0,media:void 0})},s,void 0,!(s.__file="DraggableNode.vue"),void 0,r,void 0);function l(e,t){var n;return Array.isArray(e)?e.map(function(e){return l(e,t)}):(!1!==(n=t(e))&&e.children&&e.children.length&&(n=l(e.children,t)),n)}function p(e){return!1==!!document?e:(p.__element||(p.__element=document.createElement("div")),p.__element.innerHTML=e,p.__element.innerText)}function u(o){return function(r){return Object.keys(o).every(function(t){if("text"===t||"id"===t){var e=o[t],n=r[t];return n=p(n),e instanceof RegExp?e.test(n):e===n}var i=o[t];return"state"===t&&(t="states"),Object.keys(i).every(function(e){return r[t][e]===i[e]})})}}function f(e,t,n){if(void 0===n&&(n=!0),!e||!e.length||!t)return null;var i;if(n&&(i=[],e.forEach(function e(t){i.push(t),t.children&&t.children.forEach(e)}),e=i),"number"==typeof t)return e[t]||null;("string"==typeof t||t instanceof RegExp)&&(t={text:t}),"function"!=typeof t&&(t=u(t));var r=e.filter(t);return r.length?r:null}function g(){return Math.floor(65536*(1+Math.random())).toString(16).substring(1)}function m(){return g()+g()+"-"+g()+"-"+g()+"-"+g()+"-"+g()+g()+g()}function v(e,t){for(var n=[],i=arguments.length-2;0<i--;)n[i]=arguments[i+2];e.forEach(function(e){return e[t].apply(e,n)})}var b=function(i){function e(e,t){var n;void 0===t&&(t=[]),i.call(this),this.tree=e,(n=this).push.apply(n,t)}return i&&(e.__proto__=i),((e.prototype=Object.create(i&&i.prototype)).constructor=e).prototype.remove=function(){return v(this,"remove"),this},e.prototype.expand=function(){return v(this,"expand"),this},e.prototype.collapse=function(){return v(this,"collapse"),this},e.prototype.select=function(e){return v(this,"select",e),this},e.prototype.unselect=function(){return v(this,"unselect"),this},e.prototype.check=function(){return this.tree.options.checkbox&&v(this,"check"),this},e.prototype.uncheck=function(){return this.tree.options.checkbox&&v(this,"uncheck"),this},e.prototype.disable=function(){return v(this,"disable"),this},e.prototype.enable=function(){return v(this,"enable"),this},e}(Array),y=function(e,t){if(!t)throw new Error("Node can not be empty");if(this.id=t.id||m(),this.states=t.state||{},this.showChildren=!0,this.children=t.children||[],this.parent=t.parent||null,this.isBatch=t.isBatch||!1,this.isEditing=!1,this.data=Object.assign({},t.data||{},{text:t.text}),!e)throw new Error("Node must have a Tree context!");this.tree=e},x={key:{configurable:!0},depth:{configurable:!0},text:{configurable:!0}};y.prototype.$emit=function(e){for(var t,n=[],i=arguments.length-1;0<i--;)n[i]=arguments[i+1];(t=this.tree).$emit.apply(t,["node:"+e,this].concat(n))},y.prototype.getPath=function(){if(!this.parent)return[this];for(var e=[this],t=this;null!==(t=t.parent);)e.push(t);return e},x.key.get=function(){return this.id+this.text},x.depth.get=function(){var e=0,t=this.parent;if(!t||!1===this.showChildren)return e;for(;e++,t=t.parent;);return e},x.text.get=function(){return this.data.text},x.text.set=function(e){var t=this.text;t!==e&&(this.data.text=e,this.$emit("text:changed",e,t))},y.prototype.setData=function(e){return this.data=Object.assign({},this.data,e),this.$emit("data:changed",this.data),this.data},y.prototype.state=function(e,t){return void 0===t?this.states[e]:(this.states[e]=t,this)},y.prototype.recurseUp=function(e,t){if(void 0===t&&(t=this),t.parent)return!1!==e(t.parent)?this.recurseUp(e,t.parent):void 0},y.prototype.recurseDown=function(e,t){!0!==t&&e(this),this.hasChildren()&&l(this.children,e)},y.prototype.refreshIndeterminateState=function(){if(!this.tree.options.autoCheckChildren)return this;if(this.state("indeterminate",!1),this.hasChildren()){var e=this.children.length,t=0,n=0,i=0;this.children.forEach(function(e){e.checked()&&t++,e.disabled()&&i++,e.indeterminate()&&n++}),0<t&&t===e-i?this.checked()||(this.state("checked",!0),this.tree.check(this),this.$emit("checked")):(this.checked()&&(this.state("checked",!1),this.tree.uncheck(this),this.$emit("unchecked")),this.state("indeterminate",0<n||0<t&&t<e))}this.parent&&this.parent.refreshIndeterminateState()},y.prototype.indeterminate=function(){return this.state("indeterminate")},y.prototype.editable=function(){return!this.state("disabled")&&this.state("editable")},y.prototype.selectable=function(){return!this.state("disabled")&&this.state("selectable")},y.prototype.selected=function(){return this.state("selected")},y.prototype.select=function(e){return!this.selectable()||this.selected()||(this.tree.select(this,e),this.state("selected",!0),this.$emit("selected")),this},y.prototype.unselect=function(){return this.selectable()&&this.selected()&&(this.tree.unselect(this),this.state("selected",!1),this.$emit("unselected")),this},y.prototype.checked=function(){return this.state("checked")},y.prototype.check=function(){var t=this;if(this.checked()||this.disabled())return this;if(this.indeterminate())return this.uncheck();var n=this.tree.options.checkDisabledChildren,i=this;return this.tree.options.autoCheckChildren?(this.recurseDown(function(e){e.state("indeterminate",!1),e.disabled()&&!n||e.checked()||(t.tree.check(e),e.state("checked",!0),e.$emit("checked",e.id===i.id?void 0:i))}),this.parent&&this.parent.refreshIndeterminateState()):(this.tree.check(this),this.state("checked",!0),this.$emit("checked")),this},y.prototype.uncheck=function(){var t=this;if(!this.indeterminate()&&!this.checked()||this.disabled())return this;var n=this;return this.tree.options.autoCheckChildren?(this.recurseDown(function(e){e.state("indeterminate",!1),e.checked()&&(t.tree.uncheck(e),e.state("checked",!1),e.$emit("unchecked",e.id===n.id?void 0:n))}),this.parent&&this.parent.refreshIndeterminateState()):(this.tree.uncheck(this),this.state("checked",!1),this.$emit("unchecked")),this},y.prototype.show=function(){return this.visible()||(this.state("visible",!0),this.$emit("shown")),this},y.prototype.hide=function(){return this.hidden()||(this.state("visible",!1),this.$emit("hidden")),this},y.prototype.visible=function(){return this.state("visible")},y.prototype.hidden=function(){return!this.state("visible")},y.prototype.enable=function(){return this.enabled()||(this.tree.options.autoDisableChildren?this.recurseDown(function(e){e.disabled()&&(e.state("disabled",!1),e.$emit("enabled"))}):(this.state("disabled",!1),this.$emit("enabled"))),this},y.prototype.enabled=function(){return!this.state("disabled")},y.prototype.disable=function(){return this.disabled()||(this.tree.options.autoDisableChildren?this.recurseDown(function(e){e.enabled()&&(e.state("disabled",!0),e.$emit("disabled"))}):(this.state("disabled",!0),this.$emit("disabled"))),this},y.prototype.disabled=function(){return this.state("disabled")},y.prototype.expandTop=function(t){var n=this;this.recurseUp(function(e){e.state("expanded",!0),!0!==t&&n.$emit("expanded",e)})},y.prototype.expand=function(){var t=this;return this.canExpand()&&(this.isBatch?this.tree.loadChildren(this).then(function(e){t.state("expanded",!0),t.$emit("expanded")}):(this.state("expanded",!0),this.$emit("expanded"))),this},y.prototype.canExpand=function(){return!(this.disabled()||!this.hasChildren())&&(this.collapsed()&&(!this.tree.autoDisableChildren||this.disabled()))},y.prototype.canCollapse=function(){return!(this.disabled()||!this.hasChildren())&&(this.expanded()&&(!this.tree.autoDisableChildren||this.disabled()))},y.prototype.expanded=function(){return this.state("expanded")},y.prototype.collapse=function(){return this.canCollapse()&&(this.state("expanded",!1),this.$emit("collapsed")),this},y.prototype.collapsed=function(){return!this.state("expanded")},y.prototype.toggleExpand=function(){return this._toggleOpenedState()},y.prototype.toggleCollapse=function(){return this._toggleOpenedState()},y.prototype._toggleOpenedState=function(){return this.canCollapse()?this.collapse():this.canExpand()?this.expand():void 0},y.prototype.isDropable=function(){return this.enabled()&&this.state("dropable")},y.prototype.isDraggable=function(){return this.enabled()&&this.state("draggable")&&!this.isEditing},y.prototype.startDragging=function(){return!(!this.isDraggable()||this.state("dragging"))&&((!this.isRoot()||1!==this.tree.model.length)&&(this.tree.options.store&&(this.tree.__silence=!0),this.select(),this.state("dragging",!0),this.$emit("dragging:start"),!(this.tree.__silence=!1)))},y.prototype.finishDragging=function(e,t){if(e.isDropable()||"drag-on"!==t){var n=this.tree,i=this.clone(),r=this.parent;i.id=this.id,n.__silence=!0,this.remove(),"drag-on"===t?n.append(e,i):"drag-below"===t?n.after(e,i):"drag-above"===t&&n.before(e,i),e.refreshIndeterminateState(),r&&r.refreshIndeterminateState(),n.__silence=!1,i.state("dragging",!1),this.state("dragging",!1),i.$emit("dragging:finish",e,t),i.state("selected")&&(n.selectedNodes.remove(this),n.selectedNodes.add(i),n.vm.$set(this.state,"selected",!1),n.vm.$set(i.state,"selected",!0)),this.tree.options.store&&this.tree.vm.$emit("LIQUOR_NOISE")}},y.prototype.startEditing=function(){if(this.disabled())return!1;this.isEditing||(((this.tree._editingNode=this).tree.activeElement=this).isEditing=!0,this.$emit("editing:start"))},y.prototype.stopEditing=function(e){if(this.isEditing){this.isEditing=!1,this.tree._editingNode=null,this.tree.activeElement=null;var t=this.text;e&&!1!==e&&this.text!==e&&(this.text=e),this.$emit("editing:stop",t)}},y.prototype.index=function(e){return this.tree.index(this,e)},y.prototype.first=function(){return this.hasChildren()?this.children[0]:null},y.prototype.last=function(){return this.hasChildren()?this.children[this.children.length-1]:null},y.prototype.next=function(){return this.tree.nextNode(this)},y.prototype.prev=function(){return this.tree.prevNode(this)},y.prototype.insertAt=function(e,t){var n=this;if(void 0===t&&(t=this.children.length),e)return e=this.tree.objectToNode(e),Array.isArray(e)?(e.reverse().map(function(e){return n.insertAt(e,t)}),new b(this.tree,[].concat(e))):((e.parent=this).children.splice(t,0,e),e.disabled()&&e.hasChildren()&&e.recurseDown(function(e){e.state("disabled",!0)}),this.isBatch||this.$emit("added",e),e)},y.prototype.addChild=function(e){return this.insertAt(e)},y.prototype.append=function(e){return this.addChild(e)},y.prototype.prepend=function(e){return this.insertAt(e,0)},y.prototype.before=function(e){return this.tree.before(this,e)},y.prototype.after=function(e){return this.tree.after(this,e)},y.prototype.empty=function(){for(var e;e=this.children.pop();)e.remove();return this},y.prototype.remove=function(){return this.tree.removeNode(this)},y.prototype.removeChild=function(e){var t=this.find(e);return t?this.tree.removeNode(t):null},y.prototype.find=function(e,t){return this.tree.isNode(e)?e:f(this.children,e,t)},y.prototype.focus=function(){this.vm&&this.vm.focus()},y.prototype.hasChildren=function(){return this.showChildren&&this.isBatch||0<this.children.length},y.prototype.isRoot=function(){return null===this.parent},y.prototype.clone=function(){return this.tree.objectToNode(this.toJSON())},y.prototype.toJSON=function(){var t=this;return{text:this.text,data:this.data,state:this.states,children:this.children.map(function(e){return t.tree.objectToNode(e).toJSON()})}},Object.defineProperties(y.prototype,x);var k={selected:!1,selectable:!0,checked:!1,expanded:!1,disabled:!1,visible:!0,indeterminate:!1,matched:!1,editable:!0,dragging:!1,draggable:!0,dropable:!0};function $(e){return void 0===e&&(e={}),Object.assign({},k,e)}function N(t,e){var n=null;if(e instanceof y)return e;if("string"==typeof e)n=new y(t,{text:e,state:$(),id:m()});else{if(Array.isArray(e))return e.map(function(e){return N(t,e)});(n=new y(t,e)).states=$(n.states),n.id||(n.id=m()),n.children.length&&(n.children=n.children.map(function(e){return(e=N(t,e)).parent=n,e}))}return n}var w=function(e){function t(){e.apply(this,arguments)}return e&&(t.__proto__=e),((t.prototype=Object.create(e&&e.prototype)).constructor=t).prototype.empty=function(){return this.splice(0,this.length),this},t.prototype.has=function(e){return this.includes(e)},t.prototype.add=function(){for(var e,t=[],n=arguments.length;n--;)t[n]=arguments[n];return(e=this).push.apply(e,t),this},t.prototype.remove=function(e){var t=this.indexOf(e);return-1===t||this.splice(t,1),this},t.prototype.removeAll=function(e){for(;this.includes(e);)this.remove(e);return this},t.prototype.top=function(){return this[this.length-1]},t}(Array),D={id:"id",text:"text",children:"children",state:"state",data:"data",isBatch:"isBatch"};var C=function(e,t,n){void 0===n&&(n={}),"string"==typeof e&&(e=JSON.parse(e)),Array.isArray(e)||(e=[e]);var o=Object.assign({},D,n);return e.map(function e(t){var n,i,r={id:(n=t)[(i=o).id],text:n[i.text],children:n[i.children],state:n[i.state],data:n[i.data],isBatch:n[i.isBatch]};return r.children&&!Array.isArray(r.children)&&(r.children=[r.children]),r.children&&(r.children=r.children.map(e)),r}).map(function(e){return N(t,e)})};function _(e){return t=e,new Promise(function(n,i){var r=new XMLHttpRequest;r.open("GET",t),r.setRequestHeader("Content-Type","application/json"),r.addEventListener("load",function(e){try{var t=JSON.parse(r.response);n(t)}catch(e){i(e)}}),r.send(null)});var t}function E(e,t){return e.text<t.text?-1:e.text>t.text?1:0}function O(e,t){return e.text<t.text?1:e.text>t.text?-1:0}function S(e,t){"string"==typeof t&&(t=function(e){switch(e.toLowerCase()){case"asc":return E;case"desc":return O}}(t)),Array.isArray(e)&&"function"==typeof t&&e.sort(t)}function A(e){var t=this;this.vm=e,this.options=e.opts,this.activeElement=null;var r,n,i=this.options.fetchData;"string"==typeof i&&(this.options.fetchData=(r=i,n=function(e){for(var t,n=/{([^}]+)}/,i=r;t=n.exec(i);)i=i.replace(t[0],e[t[1]]);return i},function(e){return _(n(e)).catch(t.options.onFetchError)}))}A.prototype.$on=function(e){for(var t,n=[],i=arguments.length-1;0<i--;)n[i]=arguments[i+1];(t=this.vm).$on.apply(t,[e].concat(n))},A.prototype.$once=function(e){for(var t,n=[],i=arguments.length-1;0<i--;)n[i]=arguments[i+1];(t=this.vm).$once.apply(t,[e].concat(n))},A.prototype.$off=function(e){for(var t,n=[],i=arguments.length-1;0<i--;)n[i]=arguments[i+1];(t=this.vm).$off.apply(t,[e].concat(n))},A.prototype.$emit=function(e){for(var t,n=[],i=arguments.length-1;0<i--;)n[i]=arguments[i+1];this.__silence||((t=this.vm).$emit.apply(t,[e].concat(n)),this.options.store&&this.vm.$emit("LIQUOR_NOISE"))},A.prototype._sort=function(e,t,n){!1!==n&&this.recurseDown(e,function(e){e.hasChildren()&&S(e.children,t)}),S(e,t)},A.prototype.sortTree=function(e,t){this._sort(this.model,e,t)},A.prototype.sort=function(e,t,n){var i=this,r=this.find(e,!0);r&&t&&r.forEach(function(e){i._sort(e.children,t,n)})},A.prototype.clearFilter=function(){this.recurseDown(function(e){e.state("matched",!1),e.state("visible",!0),e.state("expanded",e.__expanded),e.__expanded=void 0,e.showChildren=!0}),this.vm.matches.length=0,this.vm.$emit("tree:filtered",[],"")},A.prototype.filter=function(t){if(!t)return this.clearFilter();var n=[],i=this.options.filter.matcher,e=this.options.filter,r=e.showChildren,o=e.plainList;return this.recurseDown(function(e){i(t,e)&&n.push(e),e.showChildren=!0,void 0===e.__expanded&&(e.__expanded=e.state("expanded")),e.state("visible",!1),e.state("matched",!1),e.state("expanded",!0)}),n.reverse().forEach(function(e){e.state("matched",!0),e.state("visible",!0),e.showChildren=!o,e.hasChildren()&&e.recurseDown(function(e){e.state("visible",!!r)},!0),e.recurseUp(function(e){e.state("visible",!0),e.state("expanded",!0)}),e.hasChildren()&&e.state("expanded",!1)}),this.vm.matches=n,this.vm.$emit("tree:filtered",n,t),n},A.prototype.selected=function(){return new(Function.prototype.bind.apply(b,[null].concat([this],this.selectedNodes)))},A.prototype.checked=function(){return this.options.checkbox?new(Function.prototype.bind.apply(b,[null].concat([this],this.checkedNodes))):null},A.prototype.loadChildren=function(t){var n=this;if(t){this.$emit("tree:data:fetch",t),0<this.options.minFetchDelay&&t.vm&&(t.vm.loading=!0);var i,r=this.fetch(t).then(function(e){t.append(e),t.isBatch=!1,n.options.autoCheckChildren&&(t.checked()&&t.recurseDown(function(e){e.state("checked",!0)}),t.refreshIndeterminateState()),n.$emit("tree:data:received",t)});return Promise.all([(i=this.options.minFetchDelay,new Promise(function(e){setTimeout(e,i)})),r]).then(function(e){return t.vm&&(t.vm.loading=!1),r})}},A.prototype.fetch=function(e,t){var n=this,i=this.options.fetchData(e);return i.then||(i=_(i).catch(this.options.onFetchError)),!1===t?i:i.then(function(e){try{return n.parse(e,n.options.modelParse)}catch(e){throw new Error(e)}}).catch(this.options.onFetchError)},A.prototype.fetchInitData=function(){return this.fetch({id:"root",name:"root"},!1)},A.prototype.setModel=function(e){var i=this;return new Promise(function(t){if(i.model=i.parse(e,i.options.modelParse),requestAnimationFrame(function(e){i.vm.model=i.model,t()}),i.selectedNodes=new w,i.checkedNodes=new w,l(i.model,function(e){e.tree=i,e.selected()&&i.selectedNodes.add(e),e.checked()&&(i.checkedNodes.add(e),e.parent&&e.parent.refreshIndeterminateState()),i.options.autoDisableChildren&&e.disabled()&&e.recurseDown(function(e){e.state("disabled",!0)})}),!i.options.multiple&&i.selectedNodes.length){var n=i.selectedNodes.top();i.selectedNodes.forEach(function(e){n!==e&&e.state("selected",!1)}),i.selectedNodes.empty().add(n)}i.options.checkOnSelect&&i.options.checkbox&&i.unselectAll()})},A.prototype.recurseDown=function(e,t){return!t&&e&&(t=e,e=this.model),l(e,t)},A.prototype.select=function(e,t){var n=this.getNode(e);return!!n&&(this.options.multiple&&t?this.selectedNodes.add(n):(this.unselectAll(),this.selectedNodes.empty().add(n)),!0)},A.prototype.selectAll=function(){var t=this;return!!this.options.multiple&&(this.selectedNodes.empty(),this.recurseDown(function(e){t.selectedNodes.add(e.select(!0))}),!0)},A.prototype.unselect=function(e){var t=this.getNode(e);return!!t&&(this.selectedNodes.remove(t),!0)},A.prototype.unselectAll=function(){for(var e;e=this.selectedNodes.pop();)e.unselect();return!0},A.prototype.check=function(e){this.checkedNodes.add(e)},A.prototype.uncheck=function(e){this.checkedNodes.remove(e)},A.prototype.checkAll=function(){this.recurseDown(function(e){0===e.depth&&(e.indeterminate()&&e.state("indeterminate",!1),e.check())})},A.prototype.uncheckAll=function(){for(var e;e=this.checkedNodes.pop();)e.uncheck();return!0},A.prototype.expand=function(e){return!e.expanded()&&(e.expand(),!0)},A.prototype.collapse=function(e){return!e.collapsed()&&(e.collapse(),!0)},A.prototype.toggleExpand=function(e){return!!e.hasChildren()&&(e.toggleExpand(),!0)},A.prototype.toggleCollapse=function(e){return!!e.hasChildren()&&(e.toggleCollapse(),!0)},A.prototype.expandAll=function(){this.recurseDown(function(e){e.hasChildren()&&e.collapsed()&&e.expand()})},A.prototype.collapseAll=function(){this.recurseDown(function(e){e.hasChildren()&&e.expanded()&&e.collapse()})},A.prototype.index=function(e,t){var n=e.parent,i=(n=n?n.children:this.model).indexOf(e);return t?{index:i,target:n,node:n[i]}:i},A.prototype.nextNode=function(e){var t=this.index(e,!0);return t.target[t.index+1]||null},A.prototype.nextVisibleNode=function(e){if(e.hasChildren()&&e.expanded())return e.first();var t=this.nextNode(e);return!t&&e.parent?e.parent.next():t},A.prototype.prevNode=function(e){var t=this.index(e,!0);return t.target[t.index-1]||null},A.prototype.prevVisibleNode=function(e){var t=this.prevNode(e);return t?t.hasChildren()&&t.expanded()?t.last():t:e.parent},A.prototype.addToModel=function(e,t){var n=this;return void 0===t&&(t=this.model.length),e=this.objectToNode(e),this.model.splice(t,0,e),this.recurseDown(e,function(e){e.tree=n}),this.$emit("node:added",e),e},A.prototype.append=function(e,t){var n=this.find(e);return!!n&&n.append(t)},A.prototype.prepend=function(e,t){var n=this.find(e);return!!n&&n.prepend(t)},A.prototype.before=function(e,t){e=this.find(e);var n=this.index(e,!0),i=this.objectToNode(t);return!!~n.index&&(n.target.splice(n.index,0,i),i.parent=e.parent,this.$emit("node:added",i),i)},A.prototype.after=function(e,t){e=this.find(e);var n=this.index(e,!0),i=this.objectToNode(t);return!!~n.index&&(n.target.splice(n.index+1,0,i),i.parent=e.parent,this.$emit("node:added",i),i)},A.prototype.addNode=function(e){var t=this.model.length;return e=N(e),this.model.splice(t,0,e),this.$emit("node:added",e),e},A.prototype.remove=function(e,t){return this.removeNode(this.find(e,t))},A.prototype.removeNode=function(e){if(e instanceof b)return e.remove();if(!e)return!1;if(e.parent){var t=e.parent.children;~t.indexOf(e)&&t.splice(t.indexOf(e),1)}else~this.model.indexOf(e)&&this.model.splice(this.model.indexOf(e),1);e.parent&&e.parent.indeterminate()&&!e.parent.hasChildren()&&e.parent.state("indeterminate",!1),null!==this.activeElement&&e.id===this.activeElement.id&&(this.activeElement=null),e.parent=null,this.$emit("node:removed",e),this.selectedNodes.remove(e),this.checkedNodes.remove(e);var n=this.vm.matches;return n&&n.length&&n.includes(e)&&n.splice(n.indexOf(e),1),e},A.prototype.isNode=function(e){return e instanceof y},A.prototype.find=function(e,t){if(this.isNode(e))return e;var n=f(this.model,e);return n&&n.length?new b(this,!0===t?n:[n[0]]):new b(this,[])},A.prototype.updateData=function(e,t){var n=this.find(e);return n.forEach(function(e){return e.setData(t(e))}),n},A.prototype.getNodeById=function(t){var n=null;return l(this.model,function(e){if(""+e.id===t)return n=e,!1}),n},A.prototype.getNode=function(e){return this.isNode(e)?e:null},A.prototype.objectToNode=function(e){return N(this,e)};var T=37,L=38,P=39,R=40,M=32,j=46,I=13,B=27,F=[37,38,39,40,32];function z(s){s.vm.$el.addEventListener("keydown",function(e){var t,n,i,r=e.keyCode,o=s.activeElement;if(s.isNode(o))if(o.isEditing)switch(r){case B:return o.stopEditing(!1)}else switch(F.includes(r)&&(e.preventDefault(),e.stopPropagation()),r){case T:return function(e){if(e.expanded())e.collapse();else{var t=e.parent;t&&t.focus()}}(o);case P:return function(e){if(e.collapsed())e.expand();else{var t=e.first();t&&t.focus()}}(o);case L:return function e(t,n){var i=t.prevVisibleNode(n);if(i)return i.disabled()?e(t,i):void i.focus()}(s,o);case R:return function e(t,n){var i=t.nextVisibleNode(n);if(i)return i.disabled()?e(t,i):void i.focus()}(s,o);case M:case I:return i=o,void(s.options.checkbox&&(i.checked()?i.uncheck():i.check()));case j:return t=o,void((n=s.options.deletion)&&("function"==typeof n?!0===n(t)&&t.remove():!0===n&&t.remove()))}},!0)}function U(e,t){if(!1===e)throw new Error(t)}function Y(n){function i(e){var t=n.selected();o?n.$emit("input",{selected:r?t:t[0]||null,checked:n.checked()}):n.$emit("input",r?t:t[0]||null)}var e=n.opts,r=e.multiple,o=e.checkbox,s=n.tree;i(),s.$on("node:selected",i),s.$on("node:unselected",i),o&&(s.$on("node:checked",i),s.$on("node:unchecked",i)),s.$on("node:added",function(e,t){var n=t||e;o&&(n.state("checked")&&!s.checkedNodes.has(n)&&s.checkedNodes.add(n),n.refreshIndeterminateState()),n.state("selected")&&!s.selectedNodes.has(n)&&s.select(n),i()})}var X={ABOVE:"drag-above",BELOW:"drag-below",ON:"drag-on"};function J(e){return e.path?e.path:e.composedPath?e.composedPath():function(e){for(var t=e.target,n=[];t;){if(n.push(t),"HTML"===t.tagName)return n.push(document),n.push(window),n;t=t.parentElement}return n}(e)}function V(e){var t=function(e){for(var t,n=0,i=J(e);n<i.length;n++)if(t=i[n].className||"",/tree-node/.test(t))return i[n];return null}(e);return t||null}function H(e,t){if(e){var n=e.className;if(t)new RegExp(t).test(n)||(n+=" "+t);else{for(var i in X)n=n.replace(X[i],"");n.replace("dragging","")}e.className=n.replace(/\s+/g," ")}}function q(e,t,n){if(t&&t[n]&&"function"==typeof t[n])return!1!==t[n].apply(t,e)}var Q={direction:"ltr",multiple:!0,checkbox:!(A.prototype.parse=function(e,t){t=t||this.options.propertyNames;try{return C(e,this,t)}catch(e){return[]}}),checkOnSelect:!1,autoCheckChildren:!0,autoDisableChildren:!0,checkDisabledChildren:!0,parentSelect:!1,keyboardNavigation:!0,nodeIndent:24,minFetchDelay:0,fetchData:null,propertyNames:null,deletion:!1,dnd:!1,editing:!1,onFetchError:function(e){throw e}},Z={emptyText:"Nothing found!",matcher:function(e,t){var n=new RegExp(e,"i").test(t.text);return!(n&&t.parent&&new RegExp(e,"i").test(t.parent.text))&&n},plainList:!1,showChildren:!0},W={name:"Tree",components:{TreeNode:o,DraggableNode:a},mixins:[{mounted:function(){var e,t=this,n=new A(this);this.tree=n,this._provided.tree=n,!this.data&&this.opts.fetchData?e=n.fetchInitData():this.data&&this.data.then?(e=this.data,this.loading=!0):e=Promise.resolve(this.data),e.then(function(e){e=e||[],t.opts.store?t.connectStore(t.opts.store):t.tree.setModel(e),t.loading&&(t.loading=!1),t.$emit("tree:mounted",t),Y(t)}),!1!==this.opts.keyboardNavigation&&z(n)},methods:{connectStore:function(e){var n=this,t=e.store,i=e.mutations,r=e.getter,o=e.dispatcher;U("function"==typeof r,"`getter` must be a function"),U("function"==typeof o,"`dispatcher` must be a function"),void 0!==i&&U(Array.isArray(i),"`mutations` must be an array"),t.subscribe(function(e,t){i?i.includes(e.type)&&n.tree.setModel(r()):n.tree.setModel(r())}),this.tree.setModel(r()),this.$on("LIQUOR_NOISE",function(){n.$nextTick(function(e){o(n.toJSON())})})},recurseDown:function(e){this.tree.recurseDown(e)},selected:function(){return this.tree.selected()},checked:function(){return this.tree.checked()},append:function(e,t){return t?this.tree.append(e,t):this.tree.addToModel(e,this.tree.model.length)},prepend:function(e,t){return t?this.tree.prepend(e,t):this.tree.addToModel(e,0)},addChild:function(e,t){return this.append(e,t)},remove:function(e,t){return this.tree.remove(e,t)},before:function(e,t){return t?this.tree.before(e,t):this.prepend(e)},after:function(e,t){return t?this.tree.after(e,t):this.append(e)},find:function(e,t){return this.tree.find(e,t)},findAll:function(e){return this.tree.find(e,!0)},expandAll:function(){return this.tree.expandAll()},updateData:function(e,t){return this.tree.updateData(e,t)},collapseAll:function(){return this.tree.collapseAll()},sortTree:function(e,t){return this.tree.sortTree(e,t)},sort:function(){for(var e,t=[],n=arguments.length;n--;)t[n]=arguments[n];return(e=this.tree).sort.apply(e,t)},setModel:function(e){return this.tree.setModel(e)},getRootNode:function(){return 1===this.tree.model.length?this.tree.model[0]:this.tree.model},toJSON:function(){return JSON.parse(JSON.stringify(this.model))}}},{methods:{onDragStart:function(e){e.preventDefault()},startDragging:function(e,t){e.isDraggable()&&!1!==q([e],this.tree.options.dnd,"onDragStart")&&(this.$$startDragPosition=[t.clientX,t.clientY],this.$$possibleDragNode=e,this.initDragListeners())},initDragListeners:function(){function h(){window.removeEventListener("mouseup",e,!0),window.removeEventListener("mousemove",t,!0)}var l,p=this,e=function(e){p.$$startDragPosition||e.stopPropagation(),p.draggableNode&&p.draggableNode.node.state("dragging",!1),p.$$dropDestination&&p.tree.isNode(p.$$dropDestination)&&p.$$dropDestination.vm&&(H(p.$$dropDestination.vm.$el,null),!1!==q([p.draggableNode.node,p.$$dropDestination,l],p.tree.options.dnd,"onDragFinish")&&(p.$$dropDestination.isDropable()||l!==X.ON)&&l&&(p.draggableNode.node.finishDragging(p.$$dropDestination,l),p.draggableNode.node.parent=p.$$dropDestination),p.$$dropDestination=null);p.$$possibleDragNode=null,p.$set(p,"draggableNode",null),h()},t=function(e){if(!p.$$startDragPosition||(t=e,n=p.$$startDragPosition,5<Math.abs(t.clientX-n[0])||5<Math.abs(t.clientY-n[1]))){var t,n;if(p.$$startDragPosition=null,p.$$possibleDragNode){if(!1===p.$$possibleDragNode.startDragging())return h(),void(p.$$possibleDragNode=null);p.$set(p,"draggableNode",{node:p.$$possibleDragNode,left:0,top:0}),p.$$possibleDragNode=null}p.draggableNode.left=e.clientX,p.draggableNode.top=e.clientY;var i,r,o,s,a=V(e);if(!function(e){for(var t in X)for(var n=e.querySelectorAll("."+X[t]),i=0;i<n.length;i++)H(n[i])}(p.$el),a){var d=a.getAttribute("data-id");if(p.draggableNode.node.id===d)return;if(p.$$dropDestination&&p.$$dropDestination.id===d||(p.$$dropDestination=p.tree.getNodeById(d)),p.$$dropDestination&&p.draggableNode.node)if(p.$$dropDestination.getPath().includes(p.draggableNode.node))return void(p.$$dropDestination=null);i=e,r=a.getBoundingClientRect(),o=r.height/3,s=X.ON,r.top+o>=i.clientY?s=X.ABOVE:r.top+2*o<=i.clientY&&(s=X.BELOW),l=s;var c=q([p.draggableNode.node,p.$$dropDestination,l],p.tree.options.dnd,"onDragOn");p.$$dropDestination.isDropable()&&!1!==c||l!==X.ON||(l=null),H(a,l)}}};window.addEventListener("mouseup",e,!0),window.addEventListener("mousemove",t,!0)}}}],provide:function(e){return{tree:null}},props:{data:{},options:{type:Object,default:function(e){return{}}},filter:String,tag:{type:String,default:"div"}},watch:{filter:function(e){this.tree.filter(e)}},computed:{visibleModel:function(){return this.model.filter(function(e){return e&&e.visible()})},visibleMatches:function(){return this.matches.filter(function(e){return e&&e.visible()})}},data:function(){var e=Object.assign({},Q,this.options);return e.filter=Object.assign({},Z,e.filter),{model:[],tree:null,loading:!1,opts:e,matches:[],draggableNode:null}}},G=t({render:function(){var t=this,e=t.$createElement,n=t._self._c||e;return n(t.tag,{tag:"component",class:{tree:!0,"tree-loading":this.loading,"tree--draggable":!!this.draggableNode},attrs:{role:"tree"}},[t.filter&&0==t.matches.length?[n("div",{staticClass:"tree-filter-empty",domProps:{innerHTML:t._s(t.opts.filter.emptyText)}})]:[n("ul",{staticClass:"tree-root",on:{dragstart:t.onDragStart}},[t.opts.filter.plainList&&0<t.matches.length?t._l(t.visibleMatches,function(e){return n("TreeNode",{key:e.id,attrs:{node:e,options:t.opts}})}):t._l(t.visibleModel,function(e){return n("TreeNode",{key:e.id,attrs:{node:e,options:t.opts}})})],2)],t._v(" "),t.draggableNode?n("DraggableNode",{attrs:{target:t.draggableNode}}):t._e()],2)},staticRenderFns:[]},function(e){e&&e("data-v-6db2091d_0",{source:".tree{overflow:auto}.tree-children,.tree-root{list-style:none;padding:0}.tree>.tree-filter-empty,.tree>.tree-root{padding:3px;box-sizing:border-box}.tree.tree--draggable .tree-node:not(.selected)>.tree-content:hover{background:0 0}.drag-above,.drag-below,.drag-on{position:relative;z-index:1}.drag-on>.tree-content{background:#fafcff;outline:1px solid #7baff2}.drag-above>.tree-content::before,.drag-below>.tree-content::after{display:block;content:'';position:absolute;height:8px;left:0;right:0;z-index:2;box-sizing:border-box;background-color:#3367d6;border:3px solid #3367d6;background-clip:padding-box;border-bottom-color:transparent;border-top-color:transparent;border-radius:0}.drag-above>.tree-content::before{top:0;transform:translateY(-50%)}.drag-below>.tree-content::after{bottom:0;transform:translateY(50%)}",map:void 0,media:void 0})},W,void 0,!(W.__file="TreeRoot.vue"),void 0,r,void 0);return G.install=function(e){e.component(G.name,G)},"undefined"!=typeof window&&window.Vue&&window.Vue.use(G),G}); //# sourceMappingURL=liquor-tree.umd.js.map