/*
    *
    * 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/
    *
    */
"use strict";var __extends=this&&this.__extends||function(){var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])};return function(t,r){function i(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(i.prototype=r.prototype,new i)}}();Object.defineProperty(exports,"__esModule",{value:!0});var wjcCore=require("wijmo/wijmo"),wjcGrid=require("wijmo/wijmo.grid"),wjcGridFilter=require("wijmo/wijmo.grid.filter"),__glob="undefined"!=typeof window?window:self,wjcSelfRef=require("wijmo/wijmo.grid.grouppanel"),wjcSelf=wjcSelfRef||exports;__glob.wijmo=__glob.wijmo||{},__glob.wijmo.grid=__glob.wijmo.grid||{},__glob.wijmo.grid.grouppanel=wjcSelf;var GroupPanel=function(e){function t(t,r){var i=e.call(this,t)||this;i._hideGroupedCols=!0,i._maxGroups=6,i._hiddenCols=[];wjcCore.assert(null!=wjcGrid,"Missing dependency: GroupPanel requires wijmo.grid.");var o=i.getTemplate();i.applyTemplate("wj-grouppanel wj-control",o,{_divMarkers:"div-markers",_divPH:"div-ph"});var n=i.hostElement;return i.addEventListener(n,"dragstart",i._dragStart.bind(i)),i.addEventListener(n,"dragover",i._dragOver.bind(i)),i.addEventListener(n,"drop",i._drop.bind(i)),i.addEventListener(n,"dragend",i._dragEnd.bind(i)),i.addEventListener(n,"click",i._click.bind(i)),i.initialize(r),i}return __extends(t,e),Object.defineProperty(t.prototype,"hideGroupedColumns",{get:function(){return this._hideGroupedCols},set:function(e){e!=this._hideGroupedCols&&(this._hideGroupedCols=wjcCore.asBoolean(e))},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"maxGroups",{get:function(){return this._maxGroups},set:function(e){if(e!=this._maxGroups){this._maxGroups=wjcCore.asNumber(e);var t=this._gds,r=this._maxGroups;t&&r>-1&&r<t.length&&t.splice(r,t.length-r)}},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"placeholder",{get:function(){return this._divPH.textContent},set:function(e){this._divPH.textContent=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"grid",{get:function(){return this._g},set:function(e){if((e=wjcCore.asType(e,wjcGrid.FlexGrid,!0))!=this._g){var 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)}},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"filter",{get:function(){return this._filter},set:function(e){if((e=wjcCore.asType(e,wjcGridFilter.FlexGridFilter,!0))!=this._filter){var t=this._filter;t&&t.filterApplied.removeHandler(this.refresh,this),(t=this._filter=e)&&t.filterApplied.addHandler(this.refresh,this),this.refresh()}},enumerable:!0,configurable:!0}),t.prototype.refresh=function(){if(e.prototype.refresh.call(this),this._divMarkers.innerHTML="",this._dragMarker=this._dragCol=null,this._gds){for(var t=this._g,r=t.columnHeaders,i=0;i<this._gds.length;i++){for(var o=this._gds[i],n=-1,s=-1,l=r.rows.length-1;l>=0&&s<0;l--)for(var a=0;a<r.columns.length&&s<0;a++){var d=t._getBindingColumn(r,l,r.columns[a]);if(d&&d.binding==o.propertyName){s=a,n=l;break}}if(s>-1&&n>-1){var h=document.createElement("div");t.cellFactory.updateCell(this._g.columnHeaders,n,s,h),h.setAttribute("class","wj-cell wj-header wj-groupmarker"),wjcCore.setCss(h,{position:"static",display:"inline-block",verticalAlign:"top",left:"",top:"",right:"",height:"auto",width:"auto"});var c=h.querySelector(".wj-elem-filter");c&&wjcCore.removeChild(c);var g=this._getColumnFilter(r.columns[s]);g&&(c=wjcCore.createElement('<span class="wj-filter wj-glyph-filter"></span>',h),wjcCore.toggleClass(c,"wj-filter-on",g.isActive),wjcCore.toggleClass(c,"wj-filter-off",!g.isActive)),wjcCore.createElement('<span class="wj-remove">&times;</span>',h),this._divMarkers.appendChild(h)}}var u=this._divMarkers.children.length>0;this._divPH.style.display=u?"none":"",this._divMarkers.style.display=u?"":"none"}},t.prototype._getColumnFilter=function(e){var t=this._filter,r=null;return t&&(r=t.filterColumns&&t.filterColumns.indexOf(e.binding)<0?null:t.getColumnFilter(e)),r},t.prototype._editFilter=function(e){var t=this._gds,r=this._getElementIndex(e),i=t&&r>-1?t[r]:null,o=i?i.propertyName:null,n=o?this._g.columns.getColumn(o):null;n&&this._filter.editColumnFilter(n,null,e)},t.prototype._addGroup=function(e,t){for(var r=this._getIndex(t),i=this._gds,o=this._maxGroups,n=0;n<i.length;n++)if(i[n].propertyName==e.binding){i.removeAt(n),n<r&&r--;break}if(o>-1)for(n=o-1;n<i.length;n++)this._removeGroup(n,i),n<r&&r--;(o<0||i.length<o)&&(i.deferUpdate(function(){var t=new wjcCore.PropertyGroupDescription(e.binding);i.insert(r,t)}),e&&this.hideGroupedColumns&&(e.visible=!1,this._hiddenCols.push(e)))},t.prototype._moveGroup=function(e,t){var r=this._gds,i=this._getElementIndex(this._dragMarker),o=this._getIndex(t);o>i&&o--,o>=this._gds.length&&(o=this._gds.length),i!=o&&r.deferUpdate(function(){var e=r[i];r.removeAt(i),r.insert(o,e)})},t.prototype._removeGroup=function(e,t){void 0===t&&(t=this._gds);var r=null;t&&e>-1&&(r=t[e],t.removeAt(e));var i=r?r.propertyName:null,o=i?this._g.columns.getColumn(i):null;if(o){o.visible=!0;var n=this._hiddenCols.indexOf(o);n>-1&&this._hiddenCols.splice(n,1)}},t.prototype._getIndex=function(e){for(var t=this._divMarkers.children,r=0;r<t.length;r++){var i=t[r].getBoundingClientRect();if(e.clientX<i.left+i.width/2)return r}return t.length},t.prototype._getElementIndex=function(e){if(e&&e.parentElement)for(var t=e.parentElement.children,r=0;r<t.length;r++)if(t[r]==e)return r;return-1},t.prototype._draggingColumn=function(e,t){var r=this._g,i=r._getBindingColumn(t.panel,t.row,r.columns[t.col]);this._dragCol=i.binding?i:null},t.prototype._itemsSourceChanging=function(e,t){this._hiddenCols.forEach(function(e){e.visible=!0}),this._hiddenCols=[]},t.prototype._itemsSourceChanged=function(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()},t.prototype._collectionChanged=function(e,t){t.action==wjcCore.NotifyCollectionChangedAction.Reset&&this.invalidate()},t.prototype._dragStart=function(e){wjcCore._startDrag(e.dataTransfer,"move"),this._dragMarker=e.target,this._dragCol=null},t.prototype._dragOver=function(e){(this._dragCol||this._dragMarker)&&(e.dataTransfer.dropEffect="move",e.preventDefault(),e.stopPropagation())},t.prototype._drop=function(e){this._dragMarker?this._moveGroup(this._dragMarker,e):this._dragCol&&this._addGroup(this._dragCol,e)},t.prototype._dragEnd=function(e){this._dragMarker=this._dragCol=null},t.prototype._click=function(e){var t=e.target,r=wjcCore.hasClass(t,"wj-remove"),i=wjcCore.hasClass(t,"wj-filter"),o=wjcCore.closest(t,".wj-cell");if(wjcCore.hasClass(o,"wj-cell")){var n=this._getElementIndex(o),s=this._g.collectionView.sortDescriptions;if(i)this._editFilter(o);else if(r)this._removeGroup(n);else if(e.ctrlKey)s.clear();else{for(var l=this._gds[n],a=!0,d=0;d<s.length;d++)if(s[d].property==l.propertyName){a=!s[d].ascending;break}var h=new wjcCore.SortDescription(l.propertyName,a);s.splice(0,s.length,h)}}},t.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>',t}(wjcCore.Control);exports.GroupPanel=GroupPanel;