/*
    *
    * 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";Object.defineProperty(exports,"__esModule",{value:!0});const wjcGrid=require("wijmo/wijmo.grid"),wjcCore=require("wijmo/wijmo");var __glob="undefined"!=typeof window?window:self;const wjcSelfRef=require("wijmo/wijmo.grid.multirow");var wjcSelf=wjcSelfRef||exports;__glob.wijmo=__glob.wijmo||{},__glob.wijmo.grid=__glob.wijmo.grid||{},__glob.wijmo.grid.multirow=wjcSelf;class _MultiRow extends wjcGrid.Row{constructor(e,t,l){super(e),this._idxData=t,this._idxRecord=l}get recordIndex(){return this._idxRecord}get dataIndex(){return this._idxData}}exports._MultiRow=_MultiRow;class _Cell extends wjcGrid.Column{constructor(e){super(),this._row=this._col=0,this._rowspan=this._colspan=1,e&&wjcCore.copy(this,e)}get colspan(){return this._colspan}set colspan(e){this._colspan=wjcCore.asInt(e,!1,!0)}}exports._Cell=_Cell;class _CellGroup extends _Cell{constructor(e,t){if(super(),this._colstart=0,this._g=e,t&&wjcCore.copy(this,t),!this._cells)throw"Cell group with no cells?";let l=0,s=0,r=0;for(let e=0;e<this._cells.length;e++){let t=this._cells[e];s+t.colspan>this._colspan&&(l++,s=0),t._row=l,t._col=s,s+=t.colspan,r+=t.colspan}this._rowspan=l+1,this._colspan>r&&(this._colspan=r);for(let e=0;e<this._cells.length;e++){let t=this._cells[e];(e==this._cells.length-1||this._cells[e+1]._row>t._row)&&(s=t._col,t._colspan=this._colspan-s)}}_copy(e,t){return"cells"==e&&(this._cells=[],wjcCore.isArray(t)&&t.forEach(e=>{let t=new _Cell(e);!e.header&&t.binding&&(t.header=wjcCore.toHeaderCase(t.binding)),this._cells.push(t),this._colspan=Math.max(this._colspan,t.colspan)}),!0)}get cells(){return this._cells}closeGroup(e){if(e>this._rowspan){for(let t=0;t<this._cells.length;t++){let l=this._cells[t];l._row==this._rowspan-1&&(l._rowspan=e-l._row)}this._rowspan=e}this._cols=new wjcGrid.ColumnCollection(this._g,this._g.columns.defaultSize),this._rng=new Array(e*this._colspan);for(let e=0;e<this._cells.length;e++){let t=this._cells[e];for(let e=0;e<t._rowspan;e++)for(let l=0;l<t._colspan;l++){let s=(t._row+e)*this._colspan+t._col+l;this._cols.setAt(s,t);let r=new wjcGrid.CellRange(0-e,0-l,0-e+t._rowspan-1,0-l+t._colspan-1);r.isSingleCell||(this._rng[s]=r)}}this._rng[-1]=new wjcGrid.CellRange(0,this._colstart,0,this._colstart+this._colspan-1)}getColumnWidth(e){for(let t=0;t<this._cells.length;t++){let l=this._cells[t];if(l._col==e&&1==l.colspan)return l.width}return null}getMergedRange(e,t,l){if(t<0)return this._rng[-1];let s=e.rows[t],r=null!=s.recordIndex?s.recordIndex:t%this._rowspan,o=l-this._colstart,i=this._rng[r*this._colspan+o];return e.cellType==wjcGrid.CellType.ColumnHeader&&t++,i?new wjcGrid.CellRange(t+i.row,l+i.col,t+i.row2,l+i.col2):null}getBindingColumn(e,t,l){if(t<0)return this;let s=e.rows[t],r=s&&null!=s.recordIndex?s.recordIndex:t%this._rowspan,o=l-this._colstart;return this._cols[r*this._colspan+o]}getColumn(e){return this._cols.getColumn(e)}}exports._CellGroup=_CellGroup;class _MergeManager extends wjcGrid.MergeManager{getMergedRange(e,t,l,s=!0){let r=e.grid;switch(e.cellType){case wjcGrid.CellType.Cell:case wjcGrid.CellType.RowHeader:if(e.rows[t]instanceof wjcGrid.GroupRow)return super.getMergedRange(e,t,l,s)}switch(e.cellType){case wjcGrid.CellType.Cell:case wjcGrid.CellType.ColumnHeader:let s=r._cellGroupsByColumn[l];wjcCore.assert(s instanceof _CellGroup,"Failed to get the group!");let o=e.cellType==wjcGrid.CellType.ColumnHeader?s.getMergedRange(e,t-1,l):s.getMergedRange(e,t,l),i=e.columns.frozen;i&&o&&o.columnSpan>1&&o.col<i&&o.col2>=i&&(o=o.clone(),l<i?o.col2=i-1:o.col=i);let n=e.rows.frozen;return n&&o&&o.rowSpan>1&&e.cellType==wjcGrid.CellType.Cell&&o.row<n&&o.row2>=n&&(o=o.clone(),t<n?o.row2=n-1:o.row=n),o;case wjcGrid.CellType.RowHeader:let a=r._rowsPerItem,c=t-e.rows[t].recordIndex,d=Math.min(c+a-1,e.rows.length-1);return new wjcGrid.CellRange(c,0,d,e.columns.length-1);case wjcGrid.CellType.TopLeft:return new wjcGrid.CellRange(0,0,e.rows.length-1,e.columns.length-1)}return null}}exports._MergeManager=_MergeManager;class _AddNewHandler extends wjcGrid._AddNewHandler{constructor(e){e._addHdl._detach(),super(e)}updateNewRowTemplate(){let e=this._g.editableCollectionView,t=this._g,l=t.rows,s=e&&e.canAddNew&&t.allowAddNew&&!t.isReadOnly,r=!0;for(let e=l.length-t.rowsPerItem;e<l.length;e++)if(!(l[e]instanceof _NewRowTemplate)){r=!1;break}if(s&&!r)for(let e=0;e<t.rowsPerItem;e++){let t=new _NewRowTemplate(e);l.push(t)}if(!s&&r)for(let e=0;e<l.length;e++)l[e]instanceof _NewRowTemplate&&(l.removeAt(e),e--)}}exports._AddNewHandler=_AddNewHandler;class _NewRowTemplate extends wjcGrid._NewRowTemplate{constructor(e){super(),this._idxRecord=e}get recordIndex(){return this._idxRecord}}class MultiRow extends wjcGrid.FlexGrid{constructor(e,t){super(e),this._rowsPerItem=1,this._cellBindingGroups=[],this._centerVert=!0,this._collapsedHeaders=!1,this.collapsedHeadersChanging=new wjcCore.Event,this.collapsedHeadersChanged=new wjcCore.Event,wjcCore.addClass(this.hostElement,"wj-multirow");let l=this.columnHeaders.hostElement.parentElement,s=wjcCore.createElement('<div class="wj-hdr-collapse"><span></span></div>');s.style.display="none",l.appendChild(s),this._btnCollapse=s,this._updateButtonGlyph(),this.addEventListener(s,"mousedown",e=>{switch(this.collapsedHeaders){case null:case!1:this._collapsedHeadersWasNull=null==this.collapsedHeaders,this.collapsedHeaders=!0;break;case!0:this.collapsedHeaders=!!this._collapsedHeadersWasNull&&null}e.preventDefault(),this.focus()},!0),this.autoGenerateColumns=!1,this.mergeManager=new _MergeManager(this);let r=this.hostElement;this.removeEventListener(r,"dragover"),this.removeEventListener(r,"dragleave"),this.removeEventListener(r,"dragdrop"),this._addHdl=new _AddNewHandler(this),this.formatItem.addHandler(this._formatItem,this),this.addEventListener(this.rowHeaders.hostElement,"click",e=>{if(!e.defaultPrevented&&this.selectionMode!=wjcGrid.SelectionMode.None){let t=this.hitTest(e);if(t.panel==this.rowHeaders&&t.row>-1){let t=this.selection,l=this.rows[t.topRow],s=this.selectionMode!=wjcGrid.SelectionMode.Row?this.rows[t.bottomRow]:l;if(l&&null!=l.recordIndex){let r=l.index-l.recordIndex,o=s.index-s.recordIndex+this.rowsPerItem-1,i=this.columns.length-1,n=t.row!=t.topRow?new wjcGrid.CellRange(o,0,r,i):new wjcGrid.CellRange(r,0,o,i);this.select(n),e.preventDefault()}}}},!0),this.initialize(t)}_getProductInfo(){return"H87K,MultiRow"}get layoutDefinition(){return this._layoutDef}set layoutDefinition(e){this._layoutDef=wjcCore.asArray(e),this._rowsPerItem=1,this._cellBindingGroups=this._parseCellGroups(this._layoutDef);for(let e=0;e<this._cellBindingGroups.length;e++){let t=this._cellBindingGroups[e];this._rowsPerItem=Math.max(this._rowsPerItem,t._rowspan)}this._bindGrid(!0)}get rowsPerItem(){return this._rowsPerItem}getBindingColumn(e,t,l){return this._getBindingColumn(e,t,e.columns[l])}getColumn(e){let t,l=this._cellBindingGroups;for(let s=0;s<l.length&&!t;s++)t=l[s].getColumn(e);return t}get centerHeadersVertically(){return this._centerVert}set centerHeadersVertically(e){e!=this._centerVert&&(this._centerVert=wjcCore.asBoolean(e),this.invalidate())}get collapsedHeaders(){return this._collapsedHeaders}set collapsedHeaders(e){if(e!=this._collapsedHeaders){let t=new wjcCore.CancelEventArgs;this.onCollapsedHeadersChanging(t)&&(this._collapsedHeaders=wjcCore.asBoolean(e,!0),this._updateCollapsedHeaders(),this._updateButtonGlyph(),this.onCollapsedHeadersChanged(t))}}get showHeaderCollapseButton(){return""==this._btnCollapse.style.display}set showHeaderCollapseButton(e){e!=this.showHeaderCollapseButton&&(this._btnCollapse.style.display=wjcCore.asBoolean(e)?"":"none")}onCollapsedHeadersChanging(e){return this.collapsedHeadersChanging.raise(this,e),!e.cancel}onCollapsedHeadersChanged(e){this.collapsedHeadersChanged.raise(this,e)}_addBoundRow(e,t){let l=e[t];for(let e=0;e<this._rowsPerItem;e++)this.rows.push(new _MultiRow(l,t,e))}_addNode(e,t,l){this._addBoundRow(e,t)}_bindColumns(){let e=this.columnHeaders.rows,t=this._rowsPerItem+1;for(;e.length>t;)e.removeAt(e.length-1);for(;e.length<t;)e.push(new wjcGrid.Row);this._updateCollapsedHeaders(),this.columns.clear(),this._cellGroupsByColumn={};let l=null,s=this.collectionView;if(s&&s.sourceCollection&&s.sourceCollection.length&&(l=s.sourceCollection[0]),this._cellBindingGroups)for(let e=0;e<this._cellBindingGroups.length;e++){let t=this._cellBindingGroups[e];for(let e=0;e<t._colspan;e++){this._cellGroupsByColumn[this.columns.length]=t;let l=new wjcGrid.Column;for(let s=0;s<t.cells.length;s++){let r=t.cells[s];if(r._col==e){r.width&&(l.width=r.width),r.binding&&(l.binding=r.binding),r.format&&(l.format=r.format),r.aggregate!=wjcCore.Aggregate.None&&(l.aggregate=r.aggregate);break}}this.columns.push(l)}}}_updateCollapsedHeaders(){let e=this.columnHeaders.rows,t=this.collapsedHeaders;e[0].visible=0!=t;for(let l=1;l<e.length;l++)e[l].visible=1!=t}_updateColumnTypes(){super._updateColumnTypes();let e=this.collectionView;if(wjcCore.hasItems(e)){let t=e.items[0];for(let e=0;e<this._cellBindingGroups.length;e++){let l=this._cellBindingGroups[e];for(let e=0;e<l._cols.length;e++){let s=l._cols[e];null==s.dataType&&s._binding&&(s.dataType=wjcCore.getType(s._binding.getValue(t)))}}}}_getBindingColumn(e,t,l){if(e==this.cells||e==this.columnHeaders){let s=this._cellGroupsByColumn[l.index];e==this.columnHeaders&&t--,l=s.getBindingColumn(e,t,l.index)}return l}_cvCollectionChanged(e,t){if(this.autoGenerateColumns&&0==this.columns.length)this._bindGrid(!0);else{let e=wjcCore.NotifyCollectionChangedAction;switch(t.action){case e.Change:this.invalidate();break;case e.Add:if(t.index==this.collectionView.items.length-1){let e=this.rows.length;for(;e>0&&this.rows[e-1]instanceof wjcGrid._NewRowTemplate;)e--;for(let l=0;l<this._rowsPerItem;l++)this.rows.insert(e+l,new _MultiRow(t.item,t.index,l));return}wjcCore.assert(!1,"added item should be the last one.");break;default:this._bindGrid(!1)}}}_parseCellGroups(e){let t=[],l=1;if(e){for(let s=0,r=0;s<e.length;s++){let o=new _CellGroup(this,e[s]);o._colstart=r,r+=o._colspan,l=Math.max(l,o._rowspan),t.push(o)}for(let e=0;e<t.length;e++)t[e].closeGroup(l)}return t}_formatItem(e,t){let l=this._rowsPerItem,s=t.panel.cellType,r=t.panel.rows[t.range.row],o=t.panel.rows[t.range.row2],i=t.cell;if(s==wjcGrid.CellType.ColumnHeader&&wjcCore.toggleClass(i,"wj-group-header",0==t.range.row),s==wjcGrid.CellType.Cell||s==wjcGrid.CellType.ColumnHeader){let e=this._cellGroupsByColumn[t.col];wjcCore.assert(e instanceof _CellGroup,"Failed to get the group!"),wjcCore.toggleClass(i,"wj-group-start",e._colstart==t.range.col),wjcCore.toggleClass(i,"wj-group-end",e._colstart+e._colspan-1==t.range.col2)}if(l>1&&(s!=wjcGrid.CellType.Cell&&s!=wjcGrid.CellType.RowHeader||(wjcCore.toggleClass(i,"wj-record-start",r instanceof _MultiRow&&0==r.recordIndex),wjcCore.toggleClass(i,"wj-record-end",o instanceof _MultiRow&&o.recordIndex==l-1))),this.showAlternatingRows&&wjcCore.toggleClass(i,"wj-alt",r instanceof _MultiRow&&r.dataIndex%2!=0),this._centerVert)if(i.hasChildNodes&&t.range.rowSpan>1){let e=document.createElement("div"),t=document.createRange();wjcCore.setCss(e,{display:"table-cell",verticalAlign:"middle",overflow:"inherit",textOverflow:"inherit"}),t.selectNodeContents(i),t.surroundContents(e),wjcCore.setCss(i,{display:"table",tableLayout:"fixed",paddingTop:0,paddingBottom:0})}else{let e=i.querySelector("input")&&!i.querySelector("input[type=checkbox]")?"0":"";wjcCore.setCss(i,{display:"",tableLayout:"",paddingTop:e,paddingBottom:e})}}_updateButtonGlyph(){let e=this._btnCollapse.querySelector("span");e instanceof HTMLElement&&(e.className=this.collapsedHeaders?"wj-glyph-left":"wj-glyph-down-left")}_getError(e,t,l){if(wjcCore.isFunction(this.itemValidator)&&e==this.rowHeaders)for(let e=0;e<this._rowsPerItem;e++)for(l=0;l<this.columns.length;l++){let s=this.itemValidator(t+e,l);if(s)return s}return super._getError(e,t,l)}}exports.MultiRow=MultiRow;