/*
    *
    * 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/
    *
    */
import*as wjcCore from"wijmo/wijmo";import*as wjcGrid from"wijmo/wijmo.grid";import*as wjcSelfRef from"wijmo/wijmo.grid.grouppanel";var wjcSelf=wjcSelfRef||exports;window.wijmo=window.wijmo||{},window.wijmo.grid=window.wijmo.grid||{},window.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){e!=this._maxGroups&&(this._maxGroups=wjcCore.asNumber(e))}get placeholder(){return this._divPH.textContent}set placeholder(e){this._divPH.textContent=e}get grid(){return this._g}set grid(e){(e=wjcCore.asType(e,wjcGrid.FlexGrid,!0))!=this._g&&(this._g&&(this._g.draggingColumn.removeHandler(this._draggingColumn),this._g.sortedColumn.removeHandler(this.invalidate),this._g.itemsSourceChanging.removeHandler(this._itemsSourceChanging),this._g.itemsSourceChanged.removeHandler(this._itemsSourceChanged),this._g.columns.collectionChanged.removeHandler(this._itemsSourceChanged)),this._g=e,this._hiddenCols=[],this._g&&(this._g.draggingColumn.addHandler(this._draggingColumn,this),this._g.sortedColumn.addHandler(this.invalidate,this),this._g.itemsSourceChanging.addHandler(this._itemsSourceChanging,this),this._g.itemsSourceChanged.addHandler(this._itemsSourceChanged,this),this._g.columns.collectionChanged.addHandler(this._itemsSourceChanged,this)),this._itemsSourceChanged(this._g,null))}refresh(){if(super.refresh(),this._divMarkers.innerHTML="",this._dragMarker=this._dragCol=null,this._gds){for(let e=0;e<this._gds.length;e++){let t=this._gds[e],i=this._g.columnHeaders,r=-1,s=-1;for(let e=i.rows.length-1;e>=0&&s<0;e--)for(let d=0;d<i.columns.length&&s<0;d++){let n=this._g._getBindingColumn(i,e,i.columns[d]);if(n&&n.binding==t.propertyName){s=d,r=e;break}}if(s>-1&&r>-1){let e=document.createElement("div");this._g.cellFactory.updateCell(this._g.columnHeaders,r,s,e),e.setAttribute("class","wj-cell wj-header wj-groupmarker"),wjcCore.setCss(e,{position:"static",display:"inline-block",verticalAlign:"top",left:"",top:"",right:"",height:"auto",width:"auto"});let t=e.querySelector(".wj-elem-filter");t&&wjcCore.removeChild(t);wjcCore.createElement('<span wj-remove="" style="font-weight:normal;cursor:pointer;pointer;padding:12px;padding-right:3px">&times;</span>',e);this._divMarkers.appendChild(e)}}this._divMarkers.children.length>0?(this._divPH.style.display="none",this._divMarkers.style.display=""):(this._divPH.style.display="",this._divMarkers.style.display="none")}}_addGroup(e,t){let i=this._getIndex(t),r=this._gds;for(let t=0;t<r.length;t++)if(r[t].propertyName==e.binding){r.removeAt(t),t<i&&i--;break}for(let e=this.maxGroups-1;e<r.length;e++)this._removeGroup(e,r),e<i&&i--;r.deferUpdate(()=>{let t=new wjcCore.PropertyGroupDescription(e.binding);r.insert(i,t)}),e&&this.hideGroupedColumns&&(e.visible=!1,this._hiddenCols.push(e)),this.invalidate()}_moveGroup(e,t){let i=this._gds,r=this._getElementIndex(this._dragMarker),s=this._getIndex(t);s>r&&s--,s>=this._gds.length&&(s=this._gds.length),r!=s&&i.deferUpdate(()=>{let e=i[r];i.removeAt(r),i.insert(s,e)})}_removeGroup(e,t=this._gds){let i=null;t&&e>-1&&(i=t[e],t.removeAt(e));let r=i?i.propertyName:null,s=r?this._g.columns.getColumn(r):null;if(s){s.visible=!0;let e=this._hiddenCols.indexOf(s);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._gds&&this._gds.collectionChanged.removeHandler(this._groupsChanged),this._gds=null,this._g.collectionView&&(this._gds=this._g.collectionView.groupDescriptions,this._gds.collectionChanged.addHandler(this._groupsChanged,this)),this.invalidate()}_groupsChanged(e,t){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=null!=t.getAttribute("wj-remove"),r=wjcCore.closest(t,".wj-cell");if(wjcCore.hasClass(r,"wj-cell")){let t=this._getElementIndex(r),s=this._g.collectionView.sortDescriptions;if(i)this._removeGroup(t);else if(e.ctrlKey)s.clear(),this.invalidate();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),this.invalidate()}}}};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>';