/*
    *
    * Wijmo Library 5.20181.462
    * 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 wjcGrid from"wijmo/wijmo.grid";import*as wjcCore from"wijmo/wijmo";import*as wjcSelfRef from"wijmo/wijmo.grid.multirow";var wjcSelf=wjcSelfRef||exports;window.wijmo=window.wijmo||{},window.wijmo.grid=window.wijmo.grid||{},window.wijmo.grid.multirow=wjcSelf;export 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}};export 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)}};export 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,o=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,o+=t.colspan}this._rowspan=l+1,this._colspan>o&&(this._colspan=o);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){if("cells"==e){if(this._cells=[],wjcCore.isArray(t))for(let e=0;e<t.length;e++){let l=new _Cell(t[e]);!t[e].header&&l.binding&&(t.header=wjcCore.toHeaderCase(l.binding)),this._cells.push(l),this._colspan=Math.max(this._colspan,l.colspan)}return!0}return!1}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 o=new wjcGrid.CellRange(0-e,0-l,0-e+t._rowspan-1,0-l+t._colspan-1);o.isSingleCell||(this._rng[s]=o)}}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],o=null!=s.recordIndex?s.recordIndex:t%this._rowspan,r=l-this._colstart,n=this._rng[o*this._colspan+r];return e.cellType==wjcGrid.CellType.ColumnHeader&&t++,n?new wjcGrid.CellRange(t+n.row,l+n.col,t+n.row2,l+n.col2):null}getBindingColumn(e,t,l){if(t<0)return this;let s=e.rows[t],o=s&&null!=s.recordIndex?s.recordIndex:t%this._rowspan,r=l-this._colstart;return this._cols[o*this._colspan+r]}getColumn(e){return this._cols.getColumn(e)}};export class _MergeManager extends wjcGrid.MergeManager{getMergedRange(e,t,l,s=!0){let o=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=o._cellGroupsByColumn[l];wjcCore.assert(s instanceof _CellGroup,"Failed to get the group!");let r=e.cellType==wjcGrid.CellType.ColumnHeader?s.getMergedRange(e,t-1,l):s.getMergedRange(e,t,l);if(r&&e.columns.frozen){let t=e.columns.frozen;r.col<t&&r.col2>=t&&(l<t?r.col2=t-1:r.col=t)}if(r&&e.rows.frozen&&e.cellType==wjcGrid.CellType.Cell){let l=e.rows.frozen;r.row<l&&r.row2>=l&&(t<l?r.row2=l-1:r.row=l)}return r;case wjcGrid.CellType.RowHeader:let n=o._rowsPerItem,i=t-e.rows[t].recordIndex,a=Math.min(i+n-1,e.rows.length-1);return new wjcGrid.CellRange(i,0,a,e.columns.length-1);case wjcGrid.CellType.TopLeft:return new wjcGrid.CellRange(0,0,e.rows.length-1,e.columns.length-1)}return null}};export 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,o=!0;for(let e=l.length-t.rowsPerItem;e<l.length;e++)if(!(l[e]instanceof _NewRowTemplate)){o=!1;break}if(s&&!o)for(let e=0;e<t.rowsPerItem;e++){let t=new _NewRowTemplate(e);l.push(t)}if(!s&&o)for(let e=0;e<l.length;e++)l[e]instanceof _NewRowTemplate&&(l.removeAt(e),e--)}};class _NewRowTemplate extends wjcGrid._NewRowTemplate{constructor(e){super(),this._idxRecord=e}get recordIndex(){return this._idxRecord}}export 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 o=this.hostElement;this.removeEventListener(o,"dragover"),this.removeEventListener(o,"dragleave"),this.removeEventListener(o,"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 o=l.index-l.recordIndex,r=s.index-s.recordIndex+this.rowsPerItem-1,n=this.columns.length-1,i=t.row!=t.topRow?new wjcGrid.CellRange(r,0,o,n):new wjcGrid.CellRange(o,0,r,n);this.select(i),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=this._cellBindingGroups;for(let l=0;l<t.length;l++){let s=t[l].getColumn(e);if(t[l].getBindingColumn,s)return s}return null}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 o=t.cells[s];if(o._col==e){o.width&&(l.width=o.width),o.binding&&(l.binding=o.binding),o.format&&(l.format=o.format),o.aggregate!=wjcCore.Aggregate.None&&(l.aggregate=o.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 switch(t.action){case wjcCore.NotifyCollectionChangedAction.Change:this.invalidate();break;case wjcCore.NotifyCollectionChangedAction.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,o=0;s<e.length;s++){let r=new _CellGroup(this,e[s]);r._colstart=o,o+=r._colspan,l=Math.max(l,r._rowspan),t.push(r)}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,o=t.panel.rows[t.range.row],r=t.panel.rows[t.range.row2];if(s==wjcGrid.CellType.ColumnHeader&&wjcCore.toggleClass(t.cell,"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(t.cell,"wj-group-start",e._colstart==t.range.col),wjcCore.toggleClass(t.cell,"wj-group-end",e._colstart+e._colspan-1==t.range.col2)}if(l>1&&(s!=wjcGrid.CellType.Cell&&s!=wjcGrid.CellType.RowHeader||(wjcCore.toggleClass(t.cell,"wj-record-start",o instanceof _MultiRow&&0==o.recordIndex),wjcCore.toggleClass(t.cell,"wj-record-end",r instanceof _MultiRow&&r.recordIndex==l-1))),this.showAlternatingRows&&wjcCore.toggleClass(t.cell,"wj-alt",o instanceof _MultiRow&&o.dataIndex%2!=0),this._centerVert)if(t.cell.hasChildNodes&&t.range.rowSpan>1){let e=wjcCore.createElement('<div style="display:table-cell;vertical-align:middle"></div>'),l=document.createRange();l.selectNodeContents(t.cell),l.surroundContents(e),wjcCore.setCss(t.cell,{display:"table",tableLayout:"fixed",paddingTop:0,paddingBottom:0})}else{let e=t.cell.querySelector("input")?"0px":"";wjcCore.setCss(t.cell,{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")}};