/*
    *
    * 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/
    *
    */
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const wjcGrid=require("wijmo/wijmo.grid"),wjcCore=require("wijmo/wijmo"),wjcSelfRef=require("wijmo/wijmo.grid.detail");var wjcSelf=wjcSelfRef||exports;window.wijmo=window.wijmo||{},window.wijmo.grid=window.wijmo.grid||{},window.wijmo.grid.detail=wjcSelf;var DetailVisibilityMode;!function(e){e[e.Code=0]="Code",e[e.Selection=1]="Selection",e[e.ExpandSingle=2]="ExpandSingle",e[e.ExpandMulti=3]="ExpandMulti"}(DetailVisibilityMode=exports.DetailVisibilityMode||(exports.DetailVisibilityMode={}));class FlexGridDetailProvider{constructor(e,i){this._mode=DetailVisibilityMode.ExpandSingle,this._animated=!1,this._g=e,e.mergeManager=new DetailMergeManager(e),e.rowHeaders.hostElement.addEventListener("click",this._hdrClick.bind(this)),e.formatItem.addHandler(this._formatItem,this),e.selectionChanged.addHandler(this._selectionChanged,this),e.resizedRow.addHandler(this._resizedRow,this),e.loadingRows.addHandler(()=>{this.hideDetail()}),e.draggingRow.addHandler((e,i)=>{i.row<e.rows.length-1&&e.rows[i.row+1]instanceof DetailRow&&(i.cancel=!0,this.hideDetail(i.row))}),e.formatItem.addHandler((e,i)=>{i.panel==e.cells&&e.rows[i.row]instanceof DetailRow&&(i.cell.style.left="0")}),i&&wjcCore.copy(this,i)}get grid(){return this._g}get detailVisibilityMode(){return this._mode}set detailVisibilityMode(e){(e=wjcCore.asEnum(e,DetailVisibilityMode))!=this._mode&&(this._mode=e,this.hideDetail(),this._g.invalidate())}get maxHeight(){return this._maxHeight}set maxHeight(e){this._maxHeight=wjcCore.asNumber(e,!0)}get isAnimated(){return this._animated}set isAnimated(e){e!=this._animated&&(this._animated=wjcCore.asBoolean(e))}get createDetailCell(){return this._createDetailCellFn}set createDetailCell(e){this._createDetailCellFn=wjcCore.asFunction(e,!0)}get disposeDetailCell(){return this._disposeDetailCellFn}set disposeDetailCell(e){this._disposeDetailCellFn=wjcCore.asFunction(e,!0)}get rowHasDetail(){return this._rowHasDetailFn}set rowHasDetail(e){this._rowHasDetailFn=wjcCore.asFunction(e,!0)}getDetailRow(e){var i=this._g.rows;return e=this._toIndex(e),i[e]instanceof DetailRow?i[e]:e<i.length-1&&i[e+1]instanceof DetailRow?i[e+1]:null}isDetailVisible(e){return null!=this.getDetailRow(e)}isDetailAvailable(e){this._g.rows;return e=this._toIndex(e),this._hasDetail(e)}hideDetail(e){var i=this._g.rows;if(null!=e){!(i[e=this._toIndex(e)]instanceof DetailRow)&&e<i.length-1&&i[e+1]instanceof DetailRow&&e++;var t=i[e];t instanceof DetailRow&&(this.disposeDetailCell&&this.disposeDetailCell(t),wjcCore.Control.disposeAll(t.detail),i.removeAt(e))}else for(var l=0;l<i.length;l++)i[l]instanceof DetailRow&&this.hideDetail(l)}showDetail(e,i=!1){var t=this._g,l=t.rows;if((e=this._toIndex(e))>0&&l[e]instanceof DetailRow&&e--,i){for(var a=t.selection,s=!1,o=0;o<l.length-1;o++)o!=e&&l[o+1]instanceof DetailRow&&(this.hideDetail(o),o<e&&e--,o<a.row&&(a.row--,a.row2--,s=!0));s&&t.select(a,!1)}if(!this.isDetailVisible(e)&&this._hasDetail(e)){var r=new DetailRow(l[e]);if(r.detail=this._createDetailCell(l[e]),r.detail)if(l.insert(e+1,r),this._animated){var n=r.detail.style;n.transform="translateY(-100%)",n.opacity="0",wjcCore.animate(i=>{i<1?(n.transform="translateY("+(100*-(1-i)).toFixed(0)+"%)",n.opacity=(i*i).toString()):(n.transform="",n.opacity="",wjcCore.Control.invalidateAll(r.detail),t.scrollIntoView(e+1,-1))})}else t.scrollIntoView(e+1,-1)}}_toIndex(e){return e instanceof wjcGrid.Row&&(e=e.index),wjcCore.asNumber(e,!1,!0)}_hdrClick(e){if(!e.defaultPrevented)switch(this._mode){case DetailVisibilityMode.ExpandMulti:case DetailVisibilityMode.ExpandSingle:var i=this._g,t=i.hitTest(e);if(t.row>-1){i.rows[t.row];this.isDetailVisible(t.row)?this.hideDetail(t.row):(i.select(new wjcGrid.CellRange(t.row,0,t.row,i.columns.length-1)),this.showDetail(t.row,this._mode==DetailVisibilityMode.ExpandSingle)),e.preventDefault()}}}_selectionChanged(e,i){this._mode==DetailVisibilityMode.Selection&&(this._toSel&&clearTimeout(this._toSel),this._toSel=setTimeout(()=>{e.selection.row>-1?this.showDetail(e.selection.row,!0):this.hideDetail()},300))}_formatItem(e,i){var t=this._g,l=i.panel.rows[i.row];if(i.panel==t.cells&&l instanceof DetailRow&&null!=l.detail)if(wjcCore.addClass(i.cell,"wj-detail"),i.cell.textContent="",i.cell.style.textAlign="",i.cell.appendChild(l.detail),null==l.height){wjcCore.Control.refreshAll(i.cell);var a=getComputedStyle(i.cell),s=l.detail.scrollHeight+parseInt(a.paddingTop)+parseInt(a.paddingBottom);this._maxHeight>0&&s>this._maxHeight&&(s=this._maxHeight),l.height=s,l.detail.style.height||(l.detail.style.height="100%");var o=l.detail.querySelector(".wj-flexgrid");o&&!o.style.height&&(o.style.height="100%")}else setTimeout(()=>{wjcCore.Control.refreshAll(l.detail)});if(i.panel==t.rowHeaders&&0==i.col&&this._hasDetail(i.row)&&(i.cell.style.cursor="",this._mode==DetailVisibilityMode.ExpandMulti||this._mode==DetailVisibilityMode.ExpandSingle)){var r=t.rows[i.row+1]instanceof DetailRow?"minus":"plus";i.cell.innerHTML='<span class="wj-glyph-'+r+'"></span>',i.cell.children[0].style.pointerEvents="none",i.cell.style.cursor="pointer"}}_resizedRow(e,i){var t=i.panel.rows[i.row];t instanceof DetailRow&&t.detail&&wjcCore.Control.refreshAll(t.detail)}_hasVisibleDetail(e){return!(e instanceof DetailRow||e instanceof wjcGrid.GroupRow||e instanceof wjcGrid._NewRowTemplate)}_hasDetail(e){return!wjcCore.isFunction(this._rowHasDetailFn)||this._rowHasDetailFn(this._g.rows[e])}_createDetailCell(e,i){return this.createDetailCell?this.createDetailCell(e,i):null}}exports.FlexGridDetailProvider=FlexGridDetailProvider;class DetailMergeManager extends wjcGrid.MergeManager{constructor(e){super(e)}getMergedRange(e,i,t,l=!0){switch(e.cellType){case wjcGrid.CellType.Cell:if(e.rows[i]instanceof DetailRow)return new wjcGrid.CellRange(i,0,i,e.columns.length-1);break;case wjcGrid.CellType.RowHeader:if(e.rows[i]instanceof DetailRow)return new wjcGrid.CellRange(i-1,t,i,t);if(i<e.rows.length-1&&e.rows[i+1]instanceof DetailRow)return new wjcGrid.CellRange(i,t,i+1,t)}return super.getMergedRange(e,i,t,l)}}exports.DetailMergeManager=DetailMergeManager;class DetailRow extends wjcGrid.Row{constructor(e){super(),this.isReadOnly=!0}get detail(){return this._detail}set detail(e){this._detail=e}}exports.DetailRow=DetailRow;