/*
    *
    * Wijmo Library 5.20183.550
    * 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/
    *
    */
import*as wjcCore from"wijmo/wijmo";import*as wjcGrid from"wijmo/wijmo.grid";import*as wjcGridFilter from"wijmo/wijmo.grid.filter";var __glob="undefined"!=typeof window?window:self;import*as wjcSelfRef from"wijmo/wijmo.grid.grouppanel";var wjcSelf=wjcSelfRef||exports;__glob.wijmo=__glob.wijmo||{},__glob.wijmo.grid=__glob.wijmo.grid||{},__glob.wijmo.grid.grouppanel=wjcSelf;export class GroupPanel extends wjcCore.Control{constructor(e,t){super(e),this._hideGroupedCols=!0,this._maxGroups=6,this._hiddenCols=[];wjcCore.assert(null!=wjcGrid,"Missing dependency: GroupPanel requires wijmo.grid.");let i=this.getTemplate();this.applyTemplate("wj-grouppanel wj-control",i,{_divMarkers:"div-markers",_divPH:"div-ph"});let r=this.hostElement;this.addEventListener(r,"dragstart",this._dragStart.bind(this)),this.addEventListener(r,"dragover",this._dragOver.bind(this)),this.addEventListener(r,"drop",this._drop.bind(this)),this.addEventListener(r,"dragend",this._dragEnd.bind(this)),this.addEventListener(r,"click",this._click.bind(this)),this.initialize(t)}get hideGroupedColumns(){return this._hideGroupedCols}set hideGroupedColumns(e){e!=this._hideGroupedCols&&(this._hideGroupedCols=wjcCore.asBoolean(e))}get maxGroups(){return this._maxGroups}set maxGroups(e){if(e!=this._maxGroups){this._maxGroups=wjcCore.asNumber(e);let t=this._gds,i=this._maxGroups;t&&i>-1&&i<t.length&&t.splice(i,t.length-i)}}get placeholder(){return this._divPH.textContent}set placeholder(e){this._divPH.textContent=e}get grid(){return this._g}set grid(e){if((e=wjcCore.asType(e,wjcGrid.FlexGrid,!0))!=this._g){let t=this._g;t&&(t.draggingColumn.removeHandler(this._draggingColumn),t.itemsSourceChanging.removeHandler(this._itemsSourceChanging),t.itemsSourceChanged.removeHandler(this._itemsSourceChanged),t.columns.collectionChanged.removeHandler(this._itemsSourceChanged)),t=this._g=e,this._hiddenCols=[],t&&(t.draggingColumn.addHandler(this._draggingColumn,this),t.itemsSourceChanging.addHandler(this._itemsSourceChanging,this),t.itemsSourceChanged.addHandler(this._itemsSourceChanged,this),t.columns.collectionChanged.addHandler(this._itemsSourceChanged,this)),this._itemsSourceChanged(t,null)}}get filter(){return this._filter}set filter(e){if((e=wjcCore.asType(e,wjcGridFilter.FlexGridFilter,!0))!=this._filter){let t=this._filter;t&&t.filterApplied.removeHandler(this.refresh,this),(t=this._filter=e)&&t.filterApplied.addHandler(this.refresh,this),this.refresh()}}refresh(){if(super.refresh(),this._divMarkers.innerHTML="",this._dragMarker=this._dragCol=null,this._gds){let e=this._g,t=e.columnHeaders;for(let i=0;i<this._gds.length;i++){let r=this._gds[i],l=-1,s=-1;for(let i=t.rows.length-1;i>=0&&s<0;i--)for(let o=0;o<t.columns.length&&s<0;o++){let n=e._getBindingColumn(t,i,t.columns[o]);if(n&&n.binding==r.propertyName){s=o,l=i;break}}if(s>-1&&l>-1){let i=document.createElement("div");e.cellFactory.updateCell(this._g.columnHeaders,l,s,i),i.setAttribute("class","wj-cell wj-header wj-groupmarker"),wjcCore.setCss(i,{position:"static",display:"inline-block",verticalAlign:"top",left:"",top:"",right:"",height:"auto",width:"auto"});let r=i.querySelector(".wj-elem-filter");r&&wjcCore.removeChild(r);let o=this._getColumnFilter(t.columns[s]);o&&(r=wjcCore.createElement('<span class="wj-filter wj-glyph-filter"></span>',i),wjcCore.toggleClass(r,"wj-filter-on",o.isActive),wjcCore.toggleClass(r,"wj-filter-off",!o.isActive)),wjcCore.createElement('<span class="wj-remove">&times;</span>',i),this._divMarkers.appendChild(i)}}let i=this._divMarkers.children.length>0;this._divPH.style.display=i?"none":"",this._divMarkers.style.display=i?"":"none"}}_getColumnFilter(e){let t=this._filter,i=null;return t&&(i=t.filterColumns&&t.filterColumns.indexOf(e.binding)<0?null:t.getColumnFilter(e)),i}_editFilter(e){let t=this._gds,i=this._getElementIndex(e),r=t&&i>-1?t[i]:null,l=r?r.propertyName:null,s=l?this._g.columns.getColumn(l):null;s&&this._filter.editColumnFilter(s,null,e)}_addGroup(e,t){let i=this._getIndex(t),r=this._gds,l=this._maxGroups;for(let t=0;t<r.length;t++)if(r[t].propertyName==e.binding){r.removeAt(t),t<i&&i--;break}if(l>-1)for(let e=l-1;e<r.length;e++)this._removeGroup(e,r),e<i&&i--;(l<0||r.length<l)&&(r.deferUpdate(()=>{let t=new wjcCore.PropertyGroupDescription(e.binding);r.insert(i,t)}),e&&this.hideGroupedColumns&&(e.visible=!1,this._hiddenCols.push(e)))}_moveGroup(e,t){let i=this._gds,r=this._getElementIndex(this._dragMarker),l=this._getIndex(t);l>r&&l--,l>=this._gds.length&&(l=this._gds.length),r!=l&&i.deferUpdate(()=>{let e=i[r];i.removeAt(r),i.insert(l,e)})}_removeGroup(e,t=this._gds){let i=null;t&&e>-1&&(i=t[e],t.removeAt(e));let r=i?i.propertyName:null,l=r?this._g.columns.getColumn(r):null;if(l){l.visible=!0;let e=this._hiddenCols.indexOf(l);e>-1&&this._hiddenCols.splice(e,1)}}_getIndex(e){let t=this._divMarkers.children;for(let i=0;i<t.length;i++){let r=t[i].getBoundingClientRect();if(e.clientX<r.left+r.width/2)return i}return t.length}_getElementIndex(e){if(e&&e.parentElement){let t=e.parentElement.children;for(let i=0;i<t.length;i++)if(t[i]==e)return i}return-1}_draggingColumn(e,t){let i=this._g,r=i._getBindingColumn(t.panel,t.row,i.columns[t.col]);this._dragCol=r.binding?r:null}_itemsSourceChanging(e,t){this._hiddenCols.forEach(e=>{e.visible=!0}),this._hiddenCols=[]}_itemsSourceChanged(e,t){this._view&&this._view.collectionChanged.removeHandler(this._collectionChanged),this._view=this._g?this._g.collectionView:null,this._gds=this._view?this._view.groupDescriptions:null,this._view&&this._view.collectionChanged.addHandler(this._collectionChanged,this),this.invalidate()}_collectionChanged(e,t){t.action==wjcCore.NotifyCollectionChangedAction.Reset&&this.invalidate()}_dragStart(e){wjcCore._startDrag(e.dataTransfer,"move"),this._dragMarker=e.target,this._dragCol=null}_dragOver(e){(this._dragCol||this._dragMarker)&&(e.dataTransfer.dropEffect="move",e.preventDefault(),e.stopPropagation())}_drop(e){this._dragMarker?this._moveGroup(this._dragMarker,e):this._dragCol&&this._addGroup(this._dragCol,e)}_dragEnd(e){this._dragMarker=this._dragCol=null}_click(e){let t=e.target,i=wjcCore.hasClass(t,"wj-remove"),r=wjcCore.hasClass(t,"wj-filter"),l=wjcCore.closest(t,".wj-cell");if(wjcCore.hasClass(l,"wj-cell")){let t=this._getElementIndex(l),s=this._g.collectionView.sortDescriptions;if(r)this._editFilter(l);else if(i)this._removeGroup(t);else if(e.ctrlKey)s.clear();else{let e=this._gds[t],i=!0;for(let t=0;t<s.length;t++)if(s[t].property==e.propertyName){i=!s[t].ascending;break}let r=new wjcCore.SortDescription(e.propertyName,i);s.splice(0,s.length,r)}}}};GroupPanel.controlTemplate='<div style="cursor:default;overflow:hidden;height:100%;width:100%;min-height:1em"><div wj-part="div-ph"></div><div wj-part="div-markers"></div></div>';