UNPKG

6.97 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.EyzyTreeAPI=e()}(this,function(){"use strict";function t(t){return n(t)?t:[t]}var e=function(t){return t.isBatch||t.child.length>0},n=function(t){return Array.isArray(t)},r=function(){function r(t,e,n){this._api=e,this._state=e.state,this._tree=e.tree,this._props=e.tree.props,this._nodes=t,this._opts=n}return Object.defineProperty(r.prototype,"length",{get:function(){var t=this._nodes;return n(t)?t.length:t?1:0},enumerable:!0,configurable:!0}),Object.defineProperty(r.prototype,"result",{get:function(){return this._nodes},enumerable:!0,configurable:!0}),r.prototype.isSilence=function(){return this._opts&&this._opts.silence},r.prototype._insert=function(t){var e=this;return this._nodes?t(this._nodes,this._state).then(function(t){return new r(t,e._api,e._opts)}):Promise.resolve(this)},r.prototype._operate=function(e,r){var i=this._nodes;if(!i||n(i)&&!i.length)return null;var o=this._tree,s=this._state;this.isSilence()&&(o.silence=!0);var p,u=t(p=n(i)?i.map(function(t){return r(t,s)}):r(i,s));return"boolean"!=typeof u[0]&&void 0!==u[0]||(p=u.every(function(t){return!1!==t})),e&&o.updateState(s),this.isSilence()&&(o.silence=!1),p},r.prototype.remove=function(){var t=this;return this._operate(!1,function(e){return t._api.remove(e.id)})},r.prototype.empty=function(){var t=this;return this._operate(!0,function(n,r){if(!e(n))return!1;t._api.core.clearKeys(n,!0),r.set(n.id,{child:[],indeterminate:!1,isBatch:!1}),n.parent&&t._tree.refreshDefinite(n.id,!!n.checked,!1)})},r.prototype.select=function(t,e){var n=this;return void 0===e&&(e=this._props.expandOnSelect),this._operate(!1,function(r){if(r.selected)return!1;n._props.multiple?n._tree.select(r,!1,t):n._tree.select(r),e&&!r.expanded&&n._tree.expand(r)})},r.prototype.unselect=function(){var t=this;return this._operate(!1,function(e){if(!e.selected)return!1;t._tree.unselect(e)})},r.prototype.check=function(){var t=this;return!!this._props.checkable&&this._operate(!1,function(e){if(e.checked)return!1;t._tree.check(e)})},r.prototype.uncheck=function(){var t=this;return!!this._props.checkable&&this._operate(!1,function(e,n){if(!e.checked)return!1;t._tree.check(e)})},r.prototype.disable=function(){return this._operate(!0,function(t,e){if(t.disabled)return!1;e.set(t.id,"disabled",!0)})},r.prototype.enable=function(){return this._operate(!0,function(t,e){if(!t.disabled)return!1;e.set(t.id,"disabled",!1)})},r.prototype.disableCheckbox=function(){return!!this._props.checkable&&this._operate(!0,function(t,e){if(t.disabledCheckbox)return!1;e.set(t.id,"disabledCheckbox",!0)})},r.prototype.enableCheckbox=function(){var t=this;return!!this._props.checkable&&this._operate(!0,function(n,r){if(!n.disabledCheckbox)return!1;if(r.set(n.id,"disabledCheckbox",!1),e(n)&&t._props.useIndeterminateState){var i=n.checked?n:n.child[0];t._tree.refreshDefinite(i.id,!!i.checked,!1)}})},r.prototype.expand=function(t){var n=this;return this._operate(!1,function(r){return!(!e(r)||r.expanded)&&(!(r.disabled&&!t)&&void n._tree.expand(r,t))})},r.prototype.collapse=function(t){var n=this;return this._operate(!1,function(r){return!(!e(r)||!r.expanded)&&(!(r.disabled&&!t)&&void n._tree.expand(r,t))})},r.prototype.data=function(e,n){var i=this;if(!this._nodes)return null;var o=t(this._nodes);if(1===o.length){var s=this._api.core.data(o[0],e,n);return o[0]===s?new r(o[0],this._api,this._opts):s}return o.map(function(t){return i._api.core.data(t,e,n)})},r.prototype.hasClass=function(e){var n=this;return this._nodes?t(this._nodes).every(function(t){return n._api.core.hasClass(t,e)}):null},r.prototype.addClass=function(e){var n=this;return this._nodes?(t(this._nodes).forEach(function(t){return n._api.core.addClass(t,e)}),!0):null},r.prototype.removeClass=function(t){var e=this;return this._operate(!1,function(n){if(n.className)return n.className!==e._api.core.removeClass(n,t).className})},r.prototype.append=function(t,e){var n=this;return this._insert(function(r){return n._api.core.insert(r,t,e)})},r.prototype.prepend=function(t,e){var n=this;return this._insert(function(r){return n._api.core.insert(r,t,Object.assign({},e,{index:0}))})},r.prototype.before=function(t){var e=this;return this._insert(function(n){return e._api.core.beside(n,t,0)})},r.prototype.after=function(t){var e=this;return this._insert(function(n){return e._api.core.beside(n,t,1)})},r.prototype._find=function(n,r){var i=t(this._nodes).reduce(function(t,n){return e(n)&&t.push.apply(t,n.child),t},[]);return this._api.core.find(i,r,n)},r.prototype.find=function(t){return new r(this._find(t,!1),this._api,this._opts)},r.prototype.findAll=function(t){return new r(this._find(t,!0),this._api,this._opts)},r}(),i=function(t,e,n,r,i){return t._operate(n,!!r,function(t){return t[e].apply(t,i||[])})};return function(){function t(t,e){this._tree=t.tree,this._state=t.state,this._api=t,this.opts=e||{}}return t.prototype._operate=function(t,e,i){var o=e?this._api.findAll(t):this._api.find(t);return!o||n(o)&&!o.length?null:i(new r(o,this._api,this.opts))},t.prototype.find=function(t){return new r(this._api.find(t),this._api,this.opts)},t.prototype.findAll=function(t){return new r(this._api.findAll(t),this._api,this.opts)},t.prototype.remove=function(t,e){return i(this,"remove",t,e)},t.prototype.empty=function(t,e){return i(this,"empty",t,e)},t.prototype.selected=function(){return new r(this._api.selected(),this._api,this.opts)},t.prototype.select=function(t,e,n){return i(this,"select",t,!1,[e,n])},t.prototype.unselectAll=function(){this._api.unselectAll()},t.prototype.unselect=function(t,e){return i(this,"unselect",t,e)},t.prototype.checked=function(t,e){return new r(this._api.checked(t,e),this._api,this.opts)},t.prototype.check=function(t,e){return i(this,"check",t,e)},t.prototype.uncheck=function(t,e){return i(this,"uncheck",t,e)},t.prototype.uncheckAll=function(){this._api.uncheckAll()},t.prototype.disable=function(t,e){return i(this,"disable",t,e)},t.prototype.enable=function(t,e){return i(this,"enable",t,e)},t.prototype.disableCheckbox=function(t,e){return i(this,"disableCheckbox",t,e)},t.prototype.enableCheckbox=function(t,e){return i(this,"enableCheckbox",t,e)},t.prototype.expand=function(t,e,n){return i(this,"expand",t,e,[n])},t.prototype.collapse=function(t,e,n){return i(this,"collapse",t,e,[n])},t.prototype.data=function(t,e,n){return i(this,"data",t,!1,[e,n])},t.prototype.hasClass=function(t,e){return i(this,"hasClass",t,!1,[e])},t.prototype.addClass=function(t,e,n){return i(this,"addClass",t,n,[e])},t.prototype.removeClass=function(t,e,n){return i(this,"removeClass",t,n,[e])},t.prototype.append=function(t,e,n){return this.find(t).append(e,n)},t.prototype.prepend=function(t,e,n){return this.find(t).prepend(e,n)},t.prototype.before=function(t,e){return this.find(t).before(e)},t.prototype.after=function(t,e){return this.find(t).after(e)},t}()});