/*
    *
    * Wijmo Library 5.20182.500
    * http://wijmo.com/
    *
    * Copyright(c) GrapeCity, Inc.  All rights reserved.
    *
    * Licensed under the GrapeCity Commercial License.
    * sales@wijmo.com
    * wijmo.com/products/wijmo-5/license/
    *
    */
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const wjcCore=require("wijmo/wijmo"),wjcSelfRef=require("wijmo/wijmo.nav");var wjcSelf=wjcSelfRef||exports;window.wijmo=window.wijmo||{},window.wijmo.nav=wjcSelf;class TreeView extends wjcCore.Control{constructor(e,t){super(e),this._itmPath=new _BindingArray("items"),this._dspPath=new _BindingArray("header"),this._imgPath=new _BindingArray,this._html=!1,this._animated=!0,this._xpndOnClick=!0,this._autoColl=!0,this._showChk=!1,this._srch="",this._isReadOnly=!0,this.itemsSourceChanged=new wjcCore.Event,this.loadingItems=new wjcCore.Event,this.loadedItems=new wjcCore.Event,this.itemClicked=new wjcCore.Event,this.selectedItemChanged=new wjcCore.Event,this.checkedItemsChanged=new wjcCore.Event,this.isCollapsedChanging=new wjcCore.Event,this.isCollapsedChanged=new wjcCore.Event,this.isCheckedChanging=new wjcCore.Event,this.isCheckedChanged=new wjcCore.Event,this.formatItem=new wjcCore.Event,this.dragStart=new wjcCore.Event,this.dragOver=new wjcCore.Event,this.drop=new wjcCore.Event,this.dragEnd=new wjcCore.Event,this.nodeEditStarting=new wjcCore.Event,this.nodeEditStarted=new wjcCore.Event,this.nodeEditEnding=new wjcCore.Event,this.nodeEditEnded=new wjcCore.Event;let s=this.getTemplate();this.applyTemplate("wj-control wj-content wj-treeview",s,{_root:"root"});let i=this.hostElement;wjcCore.setAttribute(i,"role","tree",!0),wjcCore.addClass(this._root,TreeView._CNDL),wjcCore.setAttribute(this._root,"role","group",!0),this.addEventListener(i,"mousedown",this._mousedown.bind(this)),this.addEventListener(i,"click",this._click.bind(this)),this.addEventListener(i,"keydown",this._keydown.bind(this)),this.addEventListener(i,"keypress",this._keypress.bind(this)),this.addEventListener(i,"wheel",e=>{i.scrollHeight>i.offsetHeight&&(e.deltaY<0&&0==i.scrollTop||e.deltaY>0&&i.scrollTop+i.offsetHeight>=i.scrollHeight)&&(e.preventDefault(),e.stopPropagation())}),this.addEventListener(i,"blur",e=>{this._edtNode&&!wjcCore.contains(this._edtNode.element,wjcCore.getActiveElement())&&this.finishEditing()},!0),this.initialize(t),this.refresh()}get itemsSource(){return this._items}set itemsSource(e){this._items!=e&&(this._items=wjcCore.asArray(e),this.onItemsSourceChanged(),this._reload())}get childItemsPath(){return this._itmPath.path}set childItemsPath(e){e!=this.childItemsPath&&(this._itmPath.path=e,this._reload())}get displayMemberPath(){return this._dspPath.path}set displayMemberPath(e){e!=this.displayMemberPath&&(this._dspPath.path=e,this._reload())}get imageMemberPath(){return this._imgPath.path}set imageMemberPath(e){e!=this.imageMemberPath&&(this._imgPath.path=e,this._reload())}get isContentHtml(){return this._html}set isContentHtml(e){e!=this._html&&(this._html=wjcCore.asBoolean(e),this._reload())}get showCheckboxes(){return this._showChk}set showCheckboxes(e){e!=this._showChk&&(this._showChk=wjcCore.asBoolean(e),this._reload())}get autoCollapse(){return this._autoColl}set autoCollapse(e){this._autoColl=wjcCore.asBoolean(e)}get isAnimated(){return this._animated}set isAnimated(e){e!=this._animated&&(this._animated=wjcCore.asBoolean(e))}get isReadOnly(){return this._isReadOnly}set isReadOnly(e){this._isReadOnly=wjcCore.asBoolean(e),wjcCore.toggleClass(this.hostElement,"wj-state-readonly",this.isReadOnly)}startEditing(e){if(this.isReadOnly)return!1;if(e||(e=this.selectedNode),!e||e.isDisabled)return!1;if(!this.finishEditing())return!1;let t=e.element.querySelector("."+TreeView._CNDT);if(!t)return!1;let s=new TreeNodeEventArgs(e);if(!this.onNodeEditStarting(s))return!1;t.tabIndex=0,t.focus(),t.contentEditable="true",t.style.cursor="auto";let i=document.createRange();i.selectNodeContents(t);let r=getSelection();return r.removeAllRanges(),r.addRange(i),t.focus(),wjcCore.setAttribute(t,"autocomplete","off"),wjcCore.setAttribute(t,"autocorrect","off"),this._edtNode=e,this.onNodeEditStarted(s),!0}finishEditing(e){let t=this._edtNode;if(t){let s=t.element.querySelector("."+TreeView._CNDT);if(!s)return!1;let i=new TreeNodeEventArgs(t);if(!this.onNodeEditEnding(i))return!1;let r=t.dataItem,n=t.level;this.isContentHtml?e?s.innerHTML=this._dspPath.getValue(r,n):this._dspPath.setValue(r,n,s.innerHTML):e?s.textContent=this._dspPath.getValue(r,n):this._dspPath.setValue(r,n,s.textContent),document.createRange().selectNodeContents(s),getSelection().removeAllRanges(),s.contentEditable="false",s.style.cursor="",this._edtNode=null,this.onNodeEditEnded(i)}return!0}get allowDragging(){return null!=this._dd}set allowDragging(e){if(e!=this.allowDragging){wjcCore.asBoolean(e)?this._dd=new _TreeDragDropManager(this):(this._dd.dispose(),this._dd=null);let t=this.hostElement.querySelectorAll("."+TreeView._CND);for(let e=0;e<t.length;e++){let s=t[e];wjcCore.setAttribute(s,"draggable",!!this._dd||null)}}}get expandOnClick(){return this._xpndOnClick}set expandOnClick(e){this._xpndOnClick=wjcCore.asBoolean(e)}get selectedItem(){return this._selNode?this._selNode.dataItem:null}set selectedItem(e){e!=this.selectedItem&&(this.selectedNode=e?this.getNode(e):null)}get selectedNode(){return this._selNode}set selectedNode(e){if(e!=this.selectedNode)if(this._prevSel=this._selNode,e)e.select();else if(this._selNode){var t=this._selNode.element;wjcCore.removeClass(t,TreeView._CSEL),wjcCore.setAttribute(t,"aria-selected",!1),this._selNode=null,this._updateFocus(this._prevSel),this.onSelectedItemChanged()}}get selectedPath(){let e=[];for(let t=this.selectedNode;t;t=t.parentNode){let s=this._dspPath.getValue(t.dataItem,t.level);e.splice(0,0,s)}return e}get checkedItems(){if(null==this._chkItems){let e=TreeView,t="."+e._CND+"."+e._CEMP+" > input:checked."+e._CNDC,s=this._root.querySelectorAll(t);this._chkItems=[];for(let t=0;t<s.length;t++){let i=s[t].parentElement[e._DATAITEM_KEY];this._chkItems.push(i)}}return this._chkItems}set checkedItems(e){if(this.showCheckboxes){let t=TreeView,s="."+t._CND+"."+t._CEMP,i=this._root.querySelectorAll(s);for(let t=0;t<i.length;t++){let s=new TreeNode(this,i[t]),r=e.indexOf(s.dataItem)>-1;s.isChecked!=r&&(s.isChecked=r)}}}checkAllItems(e){if(this.showCheckboxes){let t=TreeView,s="."+t._CND+"."+t._CEMP,i=this._root.querySelectorAll(s);for(let t=0;t<i.length;t++){let s=new TreeNode(this,i[t]);s.isChecked!=e&&(s.isChecked=e)}}}get totalItemCount(){return this.hostElement.querySelectorAll("."+TreeView._CND).length}get lazyLoadFunction(){return this._lazyLoad}set lazyLoadFunction(e){e!=this._lazyLoad&&(this._lazyLoad=wjcCore.asFunction(e),this._reload())}getFirstNode(e,t){let s=this.hostElement.querySelector("."+TreeView._CND),i=s?new TreeNode(this,s):null;return e&&i&&!i.element.offsetHeight&&(i=i.next(e,t)),t&&i&&i.isDisabled&&(i=i.next(e,t)),i}getLastNode(e,t){let s=this.hostElement.querySelectorAll("."+TreeView._CND+":last-child"),i=s.length?new TreeNode(this,s[s.length-1]):null;return e&&i&&!i.element.offsetHeight&&(i=i.previous(e,t)),t&&i&&i.isDisabled&&(i=i.previous(e,t)),i}get nodes(){return TreeNode._getChildNodes(this,this._root)}getNode(e){this._isDirty&&this._loadTree();let t=this.hostElement.querySelectorAll("."+TreeView._CND);for(let s=0;s<t.length;s++){let i=t[s];if(i[TreeView._DATAITEM_KEY]==e)return new TreeNode(this,i)}return null}addChildNode(e,t){let s=this._createNode(t),i=this.nodes;return i?e<i.length?s.move(i[e],DropPosition.Before):s.move(i[i.length-1],DropPosition.After):s.move(this,DropPosition.Into),s}collapseToLevel(e){let t=this._animated,s=this._autoColl;this._animated=this._autoColl=!1,this._collapseToLevel(this.nodes,e,0),this._animated=t,this._autoColl=s}loadTree(e){this._loadTree(e)}onItemsSourceChanged(e){this.itemsSourceChanged.raise(this,e)}onLoadingItems(e){return this.loadingItems.raise(this,e),!e.cancel}onLoadedItems(e){this.loadedItems.raise(this,e)}onItemClicked(e){this.itemClicked.raise(this,e)}onSelectedItemChanged(e){this.selectedItemChanged.raise(this,e)}onCheckedItemsChanged(e){this._chkItems=null,this.checkedItemsChanged.raise(this,e)}onIsCollapsedChanging(e){return this.isCollapsedChanging.raise(this,e),!e.cancel}onIsCollapsedChanged(e){this.isCollapsedChanged.raise(this,e)}onIsCheckedChanging(e){return this.isCheckedChanging.raise(this,e),!e.cancel}onIsCheckedChanged(e){this.isCheckedChanged.raise(this,e)}onFormatItem(e){this.formatItem.raise(this,e)}onDragStart(e){return this.dragStart.raise(this,e),!e.cancel}onDragOver(e){return this.dragOver.raise(this,e),!e.cancel}onDrop(e){return this.drop.raise(this,e),!e.cancel}onDragEnd(e){this.dragEnd.raise(this,e)}onNodeEditStarting(e){return this.nodeEditStarting.raise(this,e),!e.cancel}onNodeEditStarted(e){this.nodeEditStarted.raise(this,e)}onNodeEditEnding(e){return this.nodeEditEnding.raise(this,e),!e.cancel}onNodeEditEnded(e){this.nodeEditEnded.raise(this,e)}refresh(e=!0){super.refresh(e),!this.isUpdating&&this._isDirty&&this._loadTree()}_updateFocus(e){let t=this._selNode;t&&(t.element.tabIndex=this._orgTabIndex),this.hostElement.tabIndex=t?-1:this._orgTabIndex,this.containsFocus()&&(t?t.element.focus():this.hostElement.focus()),e&&(e.element.tabIndex=-1)}_raiseCheckedItemsChanged(){this._toItemsChanged&&clearTimeout(this._toItemsChanged),this._toItemsChanged=setTimeout(()=>{this._toItemsChanged=null,this.onCheckedItemsChanged()},10)}_reload(){this._isDirty=!0,this.invalidate()}_createNode(e){return new TreeView(document.createElement("div"),{childItemsPath:this.childItemsPath,displayMemberPath:this.displayMemberPath,imageMemberPath:this.imageMemberPath,isContentHtml:this.isContentHtml,showCheckboxes:this.showCheckboxes,itemsSource:[e]}).getFirstNode()}_mousedown(e){if(!e.defaultPrevented){let t=wjcCore.closest(e.target,"input."+TreeView._CNDC),s=wjcCore.closestClass(e.target,TreeView._CND),i=s?new TreeNode(this,s):null;i&&!i.isDisabled&&(this.selectedNode=i),this._dnIndet=t&&t.indeterminate}}_click(e){if(!e.defaultPrevented){let t=wjcCore.closestClass(e.target,TreeView._CND);if(t){let s=new TreeNode(this,t),i=wjcCore.closest(e.target,"input."+TreeView._CNDC);if(s.isDisabled)return;if(!i&&s.equals(this._edtNode))return;if(t.focus(),i&&(e.preventDefault(),e.stopPropagation(),setTimeout(()=>{i.indeterminate=!1,s.isChecked=!s.isChecked})),!i){let i=e.target,r=(e.ctrlKey||e.metaKey)&&!s.hasPendingChildren,n=t.getBoundingClientRect(),o=!1;(this.rightToLeft?n.right-e.clientX:e.clientX-n.left)<=i.offsetHeight?(r?this.collapseToLevel(s.isCollapsed?s.level+1:s.level):s.isCollapsed=!s.isCollapsed,o=!0):this.expandOnClick&&s.isCollapsed&&(r?this.collapseToLevel(s.level):s.isCollapsed=!1,o=!0),o&&r&&this.selectedNode&&this.selectedNode.ensureVisible(),o||this.isReadOnly||this.selectedNode&&this.selectedNode.equals(this._prevSel)&&this.startEditing()}this.selectedItem&&this.onItemClicked()}}}_keydown(e){if(!e.defaultPrevented){let t,s=e.keyCode,i=this._selNode,r=!0;if(!i)switch(s){case wjcCore.Key.Up:case wjcCore.Key.Down:case wjcCore.Key.Left:case wjcCore.Key.Right:case wjcCore.Key.Enter:case wjcCore.Key.Home:case wjcCore.Key.End:if(t=this.getFirstNode(!0,!0))return this.selectedNode=t,void e.preventDefault()}if(i&&!i.isDisabled){switch(s){case wjcCore.Key.F2:this.startEditing(),e.preventDefault();break;case wjcCore.Key.Escape:this.finishEditing(!0),e.preventDefault();break;case wjcCore.Key.Up:case wjcCore.Key.Down:this.finishEditing();break;case wjcCore.Key.Enter:this._edtNode?(this.finishEditing(),s=wjcCore.Key.Down):(this.startEditing(),e.preventDefault())}if(this._edtNode)return;if(this.rightToLeft)switch(s){case wjcCore.Key.Left:s=wjcCore.Key.Right;break;case wjcCore.Key.Right:s=wjcCore.Key.Left}switch(s){case wjcCore.Key.Left:!i.isCollapsed&&i.hasChildren?i.setCollapsed(!0):(i=i.parentNode)&&i.select();break;case wjcCore.Key.Right:i.setCollapsed(!1);break;case wjcCore.Key.Up:t=i.previous(!0,!0);break;case wjcCore.Key.Down:t=i.next(!0,!0);break;case wjcCore.Key.Home:t=this.getFirstNode(!0,!0);break;case wjcCore.Key.End:t=this.getLastNode(!0,!0);break;case wjcCore.Key.Space:if(this.selectedItem){let e=i.checkBox;e&&(i.isChecked=!e.checked)}break;case wjcCore.Key.Enter:this.selectedItem&&this.onItemClicked();break;default:r=!1}r&&(e.preventDefault(),t&&t.select())}}}_keypress(e){if(!e.defaultPrevented){if(e.ctrlKey||e.metaKey||e.altKey)return;if(e.target instanceof HTMLInputElement)return;if(this._edtNode)return;if(e.charCode>32&&this.startEditing(this.selectedNode)){let t=wjcCore.getActiveElement();if(wjcCore.contains(this._edtNode.element,t)){t.textContent=String.fromCharCode(e.charCode),e.preventDefault();let s=document.createRange();s.selectNodeContents(t),s.collapse(!1);let i=getSelection();i.removeAllRanges(),i.addRange(s)}return}if(e.charCode>32||32==e.charCode&&this._srch){e.preventDefault(),this._srch+=String.fromCharCode(e.charCode).toLowerCase(),this._toSrch&&clearTimeout(this._toSrch),this._toSrch=setTimeout(()=>{this._toSrch=null,this._srch=""},TreeView._AS_DLY);let t=this._findNext();null==t&&this._srch.length>1&&(this._srch=this._srch[this._srch.length-1],t=this._findNext()),null!=t&&(this.selectedItem=t)}}}_findNext(){if(this.hostElement&&this.selectedItem){let e=this.getNode(this.selectedItem),t=e,s=!1,i=!1;for(1==this._srch.length&&(i=!0);t;){if(!t.isDisabled&&!i&&0==t.element.textContent.trim().toLowerCase().indexOf(this._srch))return t.dataItem;let r=t.next(!0,!0);if(r==e&&s)break;r||s||(r=this.getFirstNode(!0,!0),s=!0),t=r,i=!1}}return null}_loadTree(e){let t=this._root;if(t){if(!this.onLoadingItems(new wjcCore.CancelEventArgs))return;this._isDirty=!1;let s=this.containsFocus(),i=this.selectedItem;this.selectedItem=null,this._chkItems=null,this._ldLvl=-1;let r;if(e&&wjcCore.isFunction(window.Map)){r=new Map;let e=this.hostElement.querySelectorAll("."+TreeView._CND);for(let t=0;t<e.length;t++){let s=e[t];wjcCore.hasClass(s,TreeView._CCLD)&&r.set(s[TreeView._DATAITEM_KEY],!0)}}if(t.innerHTML="",this._items)for(let e=0;e<this._items.length;e++)this._addItem(t,0,this._items[e]);if(r){let e=this.hostElement.querySelectorAll("."+TreeView._CND);for(let t=0;t<e.length;t++){let s=e[t],i=TreeNode._isNodeList(s),n=r.get(s[TreeView._DATAITEM_KEY]);wjcCore.toggleClass(s,TreeView._CCLD,1==n),wjcCore.setAttribute(s,"aria-expanded",i?(!n).toString():null)}}s&&!this.containsFocus()&&this.focus(),this.selectedItem=i,this.onLoadedItems(),this._ldLvl=-1}}_addItem(e,t,s){let i=this._dspPath.getValue(s,t),r=this._imgPath.getValue(s,t),n=wjcCore.asArray(this._itmPath.getValue(s,t),!0),o=document.createElement("div");wjcCore.addClass(o,TreeView._CND),o.tabIndex=-1,wjcCore.setAttribute(o,"role","treeitem",!0),wjcCore.setAttribute(o,"aria-selected",!1);let l=document.createElement("span");if(this.isContentHtml?l.innerHTML=i:l.textContent=i,wjcCore.addClass(l,TreeView._CNDT),o.appendChild(l),r){let e=document.createElement("img");e.src=r,o.insertBefore(e,o.firstChild)}if(this._showChk&&!this._lazyLoad){let e=document.createElement("input");e.type="checkbox",e.tabIndex=-1,wjcCore.addClass(e,TreeView._CNDC),o.insertBefore(e,o.firstChild)}if(this._dd&&o.setAttribute("draggable","true"),e.appendChild(o),o[TreeView._DATAITEM_KEY]=s,n&&0==n.length&&!this.lazyLoadFunction&&(n=null),n){let s=!0;if(t>this._ldLvl?(this._ldLvl=t,0==n.length&&(wjcCore.addClass(o,TreeView._CCLD),s=!1)):(wjcCore.addClass(o,TreeView._CCLD),s=!1,t<this._ldLvl&&(this._ldLvl=1e4)),n.length>0){let i=document.createElement("div");i.tabIndex=-1,wjcCore.addClass(i,TreeView._CNDL);for(let e=0;e<n.length;e++)this._addItem(i,t+1,n[e]);e.appendChild(i),wjcCore.setAttribute(o,"aria-expanded",s.toString(),!0),wjcCore.setAttribute(i,"role","group",!0)}}else wjcCore.addClass(o,TreeView._CEMP);this.formatItem.hasHandlers&&this.onFormatItem(new FormatNodeEventArgs(s,o,t))}_collapseToLevel(e,t,s){for(let i=0;i<e.length;i++){let r=e[i];r.hasPendingChildren||(r.isCollapsed=s>=t,r.hasChildren&&this._collapseToLevel(r.nodes,t,s+1))}}_lazyLoadNode(e){let t=this.hostElement;wjcCore.hasClass(t,TreeView._CLDG)||(wjcCore.addClass(t,TreeView._CLDG),wjcCore.addClass(e.element,TreeView._CLDG),this.lazyLoadFunction(e,this._lazyLoadCallback.bind(e)))}_lazyLoadCallback(e){let t=this;t.treeView._lazyLoadNodeDone(t,e)}_lazyLoadNodeDone(e,t){let s=TreeView;wjcCore.removeClass(e.element,s._CLDG),wjcCore.removeClass(this.hostElement,s._CLDG);let i=e.dataItem,r=e.level,n=wjcCore.asArray(t,!0);if(null==n||0==n.length)this._itmPath.setValue(i,r,null),wjcCore.addClass(e.element,s._CEMP);else if(n.length){this._itmPath.setValue(i,r,n);let t=document.createElement("div"),o=e.element;wjcCore.addClass(t,s._CNDL),o.parentElement.insertBefore(t,o.nextSibling);for(let e=0;e<n.length;e++)this._addItem(t,r+1,n[e]);e.isCollapsed=!1}}}TreeView._DATAITEM_KEY="wj-Data-Item",TreeView._AS_DLY=600,TreeView._AN_DLY=200,TreeView._CND="wj-node",TreeView._CNDL="wj-nodelist",TreeView._CEMP="wj-state-empty",TreeView._CNDT="wj-node-text",TreeView._CNDC="wj-node-check",TreeView._CSEL="wj-state-selected",TreeView._CCLD="wj-state-collapsed",TreeView._CCLG="wj-state-collapsing",TreeView._CLDG="wj-state-loading",TreeView.controlTemplate='<div wj-part="root"></div>',exports.TreeView=TreeView;class TreeNode{constructor(e,t){wjcCore.hasClass(t,"wj-treeview")?(e=wjcCore.Control.getControl(t),t=null):TreeNode._assertNode(t),this._t=e,this._e=t}get dataItem(){return this._e[TreeView._DATAITEM_KEY]}get element(){return this._e}get treeView(){return this._t}ensureVisible(){for(let e=this.parentNode;e;e=e.parentNode)e.isCollapsed=!1;let e=this._t.hostElement,t=this.element.getBoundingClientRect(),s=e.getBoundingClientRect();t.bottom>s.bottom?e.scrollTop+=t.bottom-s.bottom:t.top<s.top&&(e.scrollTop-=s.top-t.top)}equals(e){return null!=e&&e.element==this.element}select(){let e=this._t,t=e._selNode;this.equals(t)||(t&&(wjcCore.removeClass(t.element,TreeView._CSEL),wjcCore.setAttribute(t.element,"aria-selected",!1)),e._selNode=this,wjcCore.addClass(this.element,TreeView._CSEL),wjcCore.setAttribute(this.element,"aria-selected",!0),this.ensureVisible(),e._updateFocus(t),e.onSelectedItemChanged())}get index(){let e=0;for(let t=this._pse(this.element);t;t=this._pse(t))TreeNode._isNode(t)&&e++;return e}get parentNode(){let e=null;if(this._e){let t=this._e.parentElement;TreeNode._assertNodeList(t),e=this._pse(t)}return e?new TreeNode(this._t,e):null}get level(){let e=-1;for(let t=this;t;t=t.parentNode)e++;return e}get hasChildren(){return TreeNode._isNode(this._e)&&!TreeNode._isEmpty(this._e)}get hasPendingChildren(){return this.isCollapsed&&this.hasChildren&&!TreeNode._isNodeList(this.element.nextElementSibling)&&wjcCore.isFunction(this._t.lazyLoadFunction)}get nodes(){return this.hasChildren?TreeNode._getChildNodes(this._t,this._e.nextSibling):null}get checkBox(){return this._e.querySelector("input."+TreeView._CNDC)}get isCollapsed(){return this.hasChildren&&wjcCore.hasClass(this._e,TreeView._CCLD)}set isCollapsed(e){if(e!=this.isCollapsed){let t=this._t,s=new TreeNodeEventArgs(this);t.onIsCollapsedChanging(s)&&(this.setCollapsed(wjcCore.asBoolean(e),t.isAnimated,t.autoCollapse),t.onIsCollapsedChanged(s))}}get isChecked(){let e=this.checkBox;return e&&!e.indeterminate?e.checked:null}set isChecked(e){if(e!=this.isChecked){let t=this._t,s=new TreeNodeEventArgs(this);t.onIsCheckedChanging(s)&&(this.setChecked(wjcCore.asBoolean(e),!0),t.onIsCheckedChanged(s))}}get isDisabled(){return this._e&&null!=this._e.getAttribute("disabled")}set isDisabled(e){(e=wjcCore.asBoolean(e,!0))!=this.isDisabled&&wjcCore.enable(this._e,!e)}previous(e,t){let s=this._pse(this._e);if(!s&&TreeNode._isNodeList(this._e.parentElement)&&(s=this._pse(this._e.parentElement)),TreeNode._isNodeList(s)){for(;TreeNode._isNodeList(s)&&s.childElementCount;)s=s.lastChild;TreeNode._isNodeList(s)&&(s=this._pse(s))}let i=TreeNode._isNode(s)?new TreeNode(this._t,s):null;return e&&i&&!i.element.offsetHeight&&(i=i.previous(e,t)),t&&i&&i.isDisabled&&(i=i.previous(e,t)),i}next(e,t){let s=this._e.nextSibling;if(TreeNode._isNodeList(s)&&(s=s.childElementCount?s.firstChild:s.nextSibling),!s)for(let e=this._e.parentElement;!s&&TreeNode._isNodeList(e);e=e.parentElement)s=e.nextSibling;let i=TreeNode._isNode(s)?new TreeNode(this._t,s):null;return e&&i&&!i.element.offsetHeight&&(i=i.next(e,t)),t&&i&&i.isDisabled&&(i=i.next(e,t)),i}previousSibling(){let e=this._pse(this.element);for(;TreeNode._isNodeList(e);)e=this._pse(e);return e?new TreeNode(this._t,e):null}nextSibling(){let e=this.element.nextSibling;return TreeNode._isNodeList(e)&&(e=e.nextSibling),e?new TreeNode(this._t,e):null}setCollapsed(e,t,s){let i=this._t,r=this._e,n=this._e.nextElementSibling,o=TreeNode._isNodeList(n);if(wjcCore.setAttribute(r,"aria-expanded",o?(!e).toString():null),e!=this.isCollapsed)if(e||o||!wjcCore.isFunction(i.lazyLoadFunction)){if(null==t&&(t=i.isAnimated),null==s&&(s=i.autoCollapse),t){if(o){let t=n.offsetHeight,s=n.style,o=i.hostElement,l=o.style;o.scrollHeight<=o.clientHeight&&(l.overflowY="hidden"),e?(wjcCore.toggleClass(r,TreeView._CCLG,!0),wjcCore.animate(e=>{e<1?(e=1-e,s.height=(e*t).toFixed(0)+"px"):(s.height=s.opacity=l.overflowY="",wjcCore.toggleClass(r,TreeView._CCLD,!0),wjcCore.toggleClass(r,TreeView._CCLG,!1))},TreeView._AN_DLY)):(wjcCore.toggleClass(r,TreeView._CCLD,!1),s.height=s.opacity="0",wjcCore.animate(e=>{s.height=e>=1?s.opacity=l.overflowY="":(e*t).toFixed(0)+"px"},TreeView._AN_DLY))}}else wjcCore.toggleClass(r,TreeView._CCLD,e);if(!e&&s){let e=r.parentElement;if(TreeNode._isNodeList(e))for(let t=0;t<e.children.length;t++){let s=e.children[t];s!=r&&TreeNode._isNode(s)&&(wjcCore.toggleClass(s,TreeView._CCLD,!0),s.setAttribute("aria-expanded","false"))}}}else i._lazyLoadNode(this)}setChecked(e,t){let s=this.checkBox,i=s.checked!=e;if(s.checked=e,s.indeterminate=!1,this.hasChildren){let t=this.nodes;for(let s=0;s<t.length;s++)t[s].setChecked(e,!1)}if(t){let e=this.parentNode;e&&e._updateCheckedState()}this._t&&i&&this._t._raiseCheckedItemsChanged()}remove(){let e=this._t,t=this.parentNode,s=this._getArray(),i=s.indexOf(this.dataItem);e.selectedNode==this&&(e.selectedNode=this.nextSibling()||this.previousSibling()||t);let r=this.element.nextSibling;TreeNode._isNodeList(r)&&wjcCore.removeChild(r),wjcCore.removeChild(this.element),t&&t._updateState(),s.splice(i,1),this._t=null}addChildNode(e,t){let s=this._t._createNode(t),i=this.nodes;return i?e<i.length?s.move(i[e],DropPosition.Before):s.move(i[i.length-1],DropPosition.After):s.move(this,DropPosition.Into),s}refresh(e){let t=this._getArray();e&&(t[this.index]=e),e=t[this.index];let s=this._t._createNode(e),i=this.hasChildren&&!this.hasPendingChildren?this.element.nextSibling:null;i&&wjcCore.removeChild(i),(i=s.hasChildren&&!s.hasPendingChildren?s.element.nextSibling:null)&&this.element.parentElement.insertBefore(i,this.element.nextSibling),this.element.innerHTML=s.element.innerHTML,this._updateState()}move(e,t){if(e instanceof TreeNode&&this._contains(e))return!1;let s=this.parentNode,i=this._getArray();this._moveElements(e,t);let r=this.parentNode,n=this._getArray();s&&s._updateState(),r&&r._updateState();let o=this.dataItem,l=i.indexOf(o);return i.splice(l,1),n.splice(this.index,0,o),e.treeView&&(this._t=e.treeView),!0}get itemsSource(){return this._getArray()}_pse(e){return e.previousElementSibling}_contains(e){for(;e;e=e.parentNode)if(e.element==this.element)return!0;return!1}_getArray(){let e=this._t,t=this.parentNode,s=e.itemsSource;if(t){let i=e._itmPath;(s=i.getValue(t.dataItem,this.level))||(s=[],i.setValue(t.dataItem,this.level,s))}return s}_moveElements(e,t){let s=document.createDocumentFragment(),i=this.hasChildren&&!this.hasPendingChildren?this.element.nextSibling:null;if(s.appendChild(this.element),i&&(TreeNode._assertNodeList(i),s.appendChild(i)),e instanceof TreeView)return void e._root.insertBefore(s,null);let r=e.element,n=r?r.parentElement:e.treeView._root;TreeNode._assertNodeList(n);let o=DropPosition;switch(t){case o.Before:n.insertBefore(s,r);break;case o.After:r=(e=e.nextSibling())?e.element:null,n.insertBefore(s,r);break;case o.Into:e.hasChildren&&!e.hasPendingChildren||(i=document.createElement("div"),wjcCore.addClass(i,TreeView._CNDL),n.insertBefore(i,r.nextSibling)),n=e.element.nextSibling,TreeNode._assertNodeList(n),n.insertBefore(s,null)}}_updateState(){this._updateEmptyState(),this._updateCheckedState()}_updateEmptyState(){let e=this.element.nextSibling,t=!1;TreeNode._isNodeList(e)&&(e.childElementCount?t=!0:wjcCore.removeChild(e)),wjcCore.toggleClass(this.element,TreeView._CEMP,!t),t||this.element.removeAttribute("aria-expanded")}_updateCheckedState(){let e=this.checkBox,t=this.nodes,s=0,i=0;if(e&&t){for(let e=0;e<t.length;e++)switch(t[e].isChecked){case!0:s++;break;case!1:i++}s==t.length?(e.checked=!0,e.indeterminate=!1):i==t.length?(e.checked=!1,e.indeterminate=!1):(e.checked=!1,e.indeterminate=!0)}let r=this.parentNode;r&&r._updateCheckedState()}static _getChildNodes(e,t){TreeNode._assertNodeList(t);let s=[];if(TreeNode._isNodeList(t)){let i=t.children;for(let t=0;t<i.length;t++){let r=i[t];TreeNode._isNode(r)&&s.push(new TreeNode(e,r))}}return s}static _isNode(e){return e&&wjcCore.hasClass(e,TreeView._CND)}static _isNodeList(e){return e&&wjcCore.hasClass(e,TreeView._CNDL)}static _isEmpty(e){return TreeNode._isNode(e)&&wjcCore.hasClass(e,TreeView._CEMP)}static _isCollapsed(e){return TreeNode._isNode(e)&&!TreeNode._isEmpty(e)&&wjcCore.hasClass(e,TreeView._CCLD)}static _assertNode(e){wjcCore.assert(TreeNode._isNode(e),"node expected")}static _assertNodeList(e){wjcCore.assert(TreeNode._isNodeList(e),"nodeList expected")}}exports.TreeNode=TreeNode;class FormatNodeEventArgs extends wjcCore.EventArgs{constructor(e,t,s){super(),this._data=e,this._e=wjcCore.asType(t,HTMLElement),this._level=s}get dataItem(){return this._data}get element(){return this._e}get level(){return this._level}}exports.FormatNodeEventArgs=FormatNodeEventArgs;class TreeNodeEventArgs extends wjcCore.CancelEventArgs{constructor(e){super(),this._node=e}get node(){return this._node}}exports.TreeNodeEventArgs=TreeNodeEventArgs;class TreeNodeDragDropEventArgs extends wjcCore.CancelEventArgs{constructor(e,t,s){super(),this._src=wjcCore.asType(e,TreeNode),this._tgt=wjcCore.asType(t,TreeNode),this._pos=s}get dragSource(){return this._src}get dropTarget(){return this._tgt}get position(){return this._pos}set position(e){this._pos=wjcCore.asEnum(e,DropPosition)}}exports.TreeNodeDragDropEventArgs=TreeNodeDragDropEventArgs;var DropPosition;!function(e){e[e.Before=0]="Before",e[e.After=1]="After",e[e.Into=2]="Into"}(DropPosition=exports.DropPosition||(exports.DropPosition={}));class _TreeDragDropManager{constructor(e){this._tree=wjcCore.asType(e,TreeView),this._dragstartBnd=this._dragstart.bind(this),this._dragoverBnd=this._dragover.bind(this),this._dropBnd=this._drop.bind(this),this._dragendBnd=this._dragend.bind(this);let t=this._tree,s=t.hostElement;t.addEventListener(s,"dragstart",this._dragstartBnd),t.addEventListener(s,"dragover",this._dragoverBnd),t.addEventListener(s,"dragleave",this._dragoverBnd),t.addEventListener(s,"drop",this._dropBnd),t.addEventListener(s,"dragend",this._dragendBnd),t.addEventListener(s,"keydown",this._keydown)}dispose(){let e=this._tree,t=e.hostElement;e.removeEventListener(t,"dragstart",this._dragstartBnd),e.removeEventListener(t,"dragover",this._dragoverBnd),e.removeEventListener(t,"dragleave",this._dragoverBnd),e.removeEventListener(t,"drop",this._dropBnd),e.removeEventListener(t,"dragend",this._dragendBnd),e.removeEventListener(t,"keydown",this._keydown),this._showDragMarker()}_dragstart(e){if(!e.defaultPrevented){let t=this._tree,s=wjcCore.closestClass(e.target,TreeView._CND),i=_TreeDragDropManager;if(i._drgSrc=TreeNode._isNode(s)?new TreeNode(t,s):null,i._drgSrc){let e=new TreeNodeEventArgs(i._drgSrc);t.onDragStart(e)||(i._drgSrc=null)}i._drgSrc&&e.dataTransfer?(wjcCore._startDrag(e.dataTransfer,"copyMove"),e.stopPropagation()):e.preventDefault()}}_dragover(e){this._handleDragDrop(e,!1)}_drop(e){this._handleDragDrop(e,!0)}_dragend(e){_TreeDragDropManager._drgSrc=null,this._showDragMarker(),this._tree.onDragEnd()}_keydown(e){e.defaultPrevented||e.keyCode==wjcCore.Key.Escape&&this._dragendBnd(null)}_handleDragDrop(e,t){let s,i,r=this._tree,n=_TreeDragDropManager,o=DropPosition,l=o.Into;if(!e.defaultPrevented&&n._drgSrc){let a=document.elementFromPoint(e.clientX,e.clientY),d=wjcCore.closestClass(a,TreeView._CND);if(null==d){let e=wjcCore.Control.getControl(wjcCore.closest(a,".wj-treeview"));e instanceof TreeView&&0==e.totalItemCount&&(d=e.hostElement)}if(d==n._drgSrc.element&&(d=null),d){i=d.getBoundingClientRect();let t=new TreeNode(r,d),a=t.hasPendingChildren?i.height/2:i.height/3;null==t.element?((i=wjcCore.Rect.fromBoundingRect(i)).inflate(-12,-12),l=o.Before):e.clientY<i.top+a?l=o.Before:(e.clientY>i.bottom-a||t.hasPendingChildren)&&(l=o.After,!t.hasChildren||t.isCollapsed||t.hasPendingChildren||(l=o.Before,i=(d=(t=t.next(!0,!1)).element).getBoundingClientRect())),n._drgSrc._contains(t)?d=null:((s=new TreeNodeDragDropEventArgs(n._drgSrc,t,l)).cancel=n._drgSrc.treeView!=t.treeView,r.onDragOver(s)||(d=null))}if(d)if((l=s.position)==o.Before){let e=s.dragSource.next(!0,!1);e&&e.element==d&&(d=null)}else if(l==o.After){let e=s.dragSource.previous(!0,!1);e&&e.element==d&&(d=null)}if(d&&!t?(e.dataTransfer.dropEffect="move",e.preventDefault(),e.stopPropagation(),this._showDragMarker(i,l)):this._showDragMarker(),d&&t&&r.onDrop(s)){r.hostElement.focus();let e=s.dragSource;e.move(s.dropTarget,s.position),e.select()}}}_showDragMarker(e,t){let s=this._tree,i=_TreeDragDropManager._dMarker.parentElement;if(e){let r=s.hostElement.getBoundingClientRect(),n=t==DropPosition.After?e.bottom:e.top,o={top:Math.round(n-r.top+s.hostElement.scrollTop-2),width:"75%",height:t==DropPosition.Into?e.height:4,opacity:t==DropPosition.Into?"0.15":""};s.rightToLeft?o.right=Math.round(r.right-e.right):o.left=Math.round(e.left-r.left),wjcCore.setCss(_TreeDragDropManager._dMarker,o),i!=s._root&&s._root.appendChild(_TreeDragDropManager._dMarker)}else i&&i.removeChild(_TreeDragDropManager._dMarker)}}_TreeDragDropManager._dMarker=wjcCore.createElement('<div class="wj-marker">&nbsp;</div>'),exports._TreeDragDropManager=_TreeDragDropManager;class _BindingArray{constructor(e){this.path=e}get path(){return this._path}set path(e){if(this._path=e,wjcCore.isString(e))this._bindings=[new wjcCore.Binding(e)];else if(wjcCore.isArray(e)){this._bindings=[];for(let t=0;t<e.length;t++)this._bindings.push(new wjcCore.Binding(e[t]))}else null!=e&&wjcCore.assert(!1,"Path should be a string or an array of strings.");this._maxLevel=this._bindings?this._bindings.length-1:-1}getValue(e,t){let s=Math.min(t,this._maxLevel);return s>-1?this._bindings[s].getValue(e):null}setValue(e,t,s){let i=Math.min(t,this._maxLevel);i>-1&&this._bindings[i].setValue(e,s)}}exports._BindingArray=_BindingArray;class TabPanel extends wjcCore.Control{constructor(e,t,s){super(e,null,!0),this._tabs=new wjcCore.ObservableArray,this._selectedIndex=-1,this._animated=!0,this._autoSwitch=!0,this.selectedIndexChanged=new wjcCore.Event;let i=this.hostElement,r=[];if(!s)for(;i.firstElementChild;){let e=i.firstElementChild;i.removeChild(e),r.push(e)}let n=this.getTemplate();this.applyTemplate("wj-control wj-tabpanel",n,{_dRoot:"root",_dTabHeaders:"tabheaders",_dTabPanes:"tabpanes"}),i.tabIndex=-1,this._dRoot.tabIndex=this._dTabHeaders.tabIndex=this._dTabPanes.tabIndex=-1,this.addEventListener(i,"click",this._click.bind(this)),this.addEventListener(i,"keydown",this._keydown.bind(this)),this._tabs.collectionChanged.addHandler(this._populateControl.bind(this)),this.tabs.deferUpdate(()=>{r.forEach(e=>{wjcCore.assert(2==e.childElementCount,"TabPanel children should contain header and pane elements"),this.tabs.push(new Tab(e.children[0],e.children[1]))})}),this.initialize(t),this.selectedIndex<0&&this.tabs.length?this.selectedIndex=0:this.onSelectedIndexChanged()}get tabs(){return this._tabs}get selectedIndex(){return this._selectedIndex}set selectedIndex(e){(e=wjcCore.asInt(e,!1))!=this._selectedIndex?(this._selectedIndex=e,this._updateContent(),this.onSelectedIndexChanged()):this._updateContent()}get selectedTab(){let e=this._selectedIndex;return e>-1?this._tabs[e]:null}set selectedTab(e){let t=-1;for(let s=0;s<this._tabs.length&&t<0;s++)this._tabs[s]==e&&(t=s);this.selectedIndex=t}get isAnimated(){return this._animated}set isAnimated(e){this._animated=wjcCore.asBoolean(e)}get autoSwitch(){return this._autoSwitch}set autoSwitch(e){e!=this._autoSwitch&&(this._autoSwitch=wjcCore.asBoolean(e),this._updateContent())}getTab(e){for(let t=0;t<this._tabs.length;t++){let s=this._tabs[t];if(s.header.id==e||s.pane.id==e)return s}for(let t=0;t<this._tabs.length;t++){let s=this._tabs[t];if(s.header.textContent==e)return s}return null}onSelectedIndexChanged(e){this.selectedIndexChanged.raise(this,e)}_populateControl(){this._removeChildren(this._dTabHeaders),this._removeChildren(this._dTabPanes);let e=-1;this._tabs.forEach((t,s)=>{wjcCore.assert(t instanceof Tab,"tabs array must contain Tab objects."),t._setPanel(this);let i=t.header,r=t.pane;wjcCore.addClass(i,"wj-tabheader"),wjcCore.setAttribute(i,"role","tab"),this._dTabHeaders.appendChild(i),wjcCore.addClass(r,"wj-tabpane"),wjcCore.setAttribute(r,"role","tabpanel"),wjcCore.setAttribute(r,"aria-labelledby",i.id?i.id:null),this._dTabPanes.appendChild(r),e<0&&(wjcCore.hasClass(i,"wj-state-active")||"true"==i.getAttribute("aria-selected"))&&(e=s)}),e<0&&this._tabs.length>0&&(e=0),e>-1?this.selectedIndex=e:this.selectedIndex>-1&&this.selectedIndex<this._tabs.length&&this._updateContent(),this._validateSelection()}_validateSelection(){let e=this.selectedTab;if(e&&(e.isDisabled||!e.isVisible)){let e=this._getNextIndex(this.selectedIndex,1);e<0&&(e=this._getNextIndex(this.selectedIndex,-1)),this.selectedIndex=e}}_updateContent(){let e=wjcCore.contains(this._dTabHeaders,wjcCore.getActiveElement()),t=this._dTabHeaders.children,s=this._dTabPanes.children,i=this._selectedIndex;if(i>-1&&i<s.length){let e=s[i].style;this.isAnimated?(e.opacity="0",this._toAnim&&clearInterval(this._toAnim),this._toAnim=wjcCore.animate(t=>{1==t?(e.opacity="",this._toAnim=null):e.opacity=t.toString()})):e.opacity=""}for(let r=0;r<t.length;r++){let n=r==i,o=t[r],l=s[r];wjcCore.setAttribute(o,"aria-selected",n),wjcCore.toggleClass(o,"wj-state-active",n),wjcCore.toggleClass(l,"wj-state-active",n),o.tabIndex=n||!this._autoSwitch?this._orgTabIndex:-1,n&&(e&&o.focus(),wjcCore.Control.invalidateAll(l))}}_removeChildren(e){for(;e.firstChild;)e.removeChild(e.firstChild)}_click(e){let t=this._getTabIndex(e.target);t>-1&&(this.selectedIndex=t)}_keydown(e){if(!e.defaultPrevented){let t=this._getTabIndex(wjcCore.getActiveElement());if(t>-1){let s=e.keyCode;if(this.rightToLeft)switch(s){case wjcCore.Key.Left:s=wjcCore.Key.Right;break;case wjcCore.Key.Right:s=wjcCore.Key.Left}switch(s){case wjcCore.Key.Left:case wjcCore.Key.Up:case wjcCore.Key.Right:case wjcCore.Key.Down:case wjcCore.Key.Home:case wjcCore.Key.PageUp:case wjcCore.Key.End:case wjcCore.Key.PageDown:switch(s){case wjcCore.Key.Left:case wjcCore.Key.Up:t=this._getNextIndex(t,-1);break;case wjcCore.Key.Right:case wjcCore.Key.Down:t=this._getNextIndex(t,1);break;case wjcCore.Key.Home:case wjcCore.Key.PageUp:t=this._getNextIndex(-1,1);break;case wjcCore.Key.End:case wjcCore.Key.PageDown:t=this._getNextIndex(this._tabs.length,-1)}t>-1&&(this._autoSwitch?this.selectedIndex=t:this._tabs[t].header.focus()),e.preventDefault();break;case wjcCore.Key.Enter:case wjcCore.Key.Space:t>-1&&this._tabs[t].header.click(),e.preventDefault()}}}}_getTabIndex(e){let t=wjcCore.closest(e,".wj-tabheader");if(t&&wjcCore.closest(t,".wj-tabpanel")==this.hostElement)for(let e=0;e<this._tabs.length;e++)if(this._tabs[e].header==t)return e;return-1}_getNextIndex(e,t){for(let s=e+t;s>-1&&s<this._tabs.length;s+=t){let e=this._tabs[s];if(!e.isDisabled&&e.isVisible)return s}return-1}}TabPanel.controlTemplate='<div wj-part="root"><div wj-part="tabheaders" class="wj-tabheaders" role="tablist"></div><div wj-part="tabpanes" class="wj-tabpanes"></div></div>',exports.TabPanel=TabPanel;class Tab{constructor(e,t){this._hdr=wjcCore.asType(wjcCore.getElement(e),HTMLElement),this._pane=wjcCore.asType(wjcCore.getElement(t),HTMLElement)}get tabPanel(){return this._p}get header(){return this._hdr}get pane(){return this._pane}get isDisabled(){return wjcCore.hasClass(this._hdr,"wj-state-disabled")}set isDisabled(e){wjcCore.toggleClass(this._hdr,"wj-state-disabled",wjcCore.asBoolean(e)),this._p&&this._p._validateSelection()}get isVisible(){return"none"!=this._hdr.style.display}set isVisible(e){this._hdr.style.display=wjcCore.asBoolean(e)?"":"none",this._p&&this._p._validateSelection()}_setParts(e,t){if(e=wjcCore.asType(wjcCore.getElement(e),HTMLElement),t=wjcCore.asType(wjcCore.getElement(t),HTMLElement,!1),this._hdr!==e||this._pane!==t){let s=this.isDisabled,i=this.isVisible;this._hdr=e,this._pane=t,this.isDisabled=s,this.isVisible=i;let r=this.tabPanel;r&&!r.tabs.isUpdating&&r._populateControl()}}_setPanel(e){this._p=e}}exports.Tab=Tab;