UNPKG

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