/*
    *
    * 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/
    *
    */
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const wjcCore=require("wijmo/wijmo"),wjcGrid=require("wijmo/wijmo.grid"),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,wjcCore._addCultureInfo("FlexGridDetailProvider",{ariaLabels:{toggleDetail:"Toggle Row Detail"}});var KeyAction;!function(e){e[e.None=0]="None",e[e.ToggleDetail=1]="ToggleDetail"}(KeyAction=exports.KeyAction||(exports.KeyAction={}));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,t){this._mode=DetailVisibilityMode.ExpandSingle,this._animated=!1,this._keyActionEnter=KeyAction.None,this._g=e,e.mergeManager=new DetailMergeManager(e),e.rowHeaders.hostElement.addEventListener("click",this._hdrClick.bind(this)),e.rowHeaders.hostElement.addEventListener("mousedown",t=>{let i=e.editableCollectionView;(e.activeEditor||i&&i.currentEditItem)&&(this._hdrClick(t),t.preventDefault())}),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,t)=>{t.row<e.rows.length-1&&e.rows[t.row+1]instanceof DetailRow&&(t.cancel=!0,this.hideDetail(t.row))}),e.hostElement.addEventListener("keydown",e=>{if(e.keyCode==wjcCore.Key.Enter&&this._keyActionEnter==KeyAction.ToggleDetail){let t=this._g.selection.row;this._toggleRowDetail(t)&&e.preventDefault()}},!0),t&&wjcCore.copy(this,t)}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 keyActionEnter(){return this._keyActionEnter}set keyActionEnter(e){this._keyActionEnter=wjcCore.asEnum(e,KeyAction)}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){e=this._toIndex(e);let t=this._g.rows;return t[e]instanceof DetailRow?t[e]:e<t.length-1&&t[e+1]instanceof DetailRow?t[e+1]:null}isDetailVisible(e){return null!=this.getDetailRow(e)}isDetailAvailable(e){return e=this._toIndex(e),this._hasDetail(e)}hideDetail(e){let t=this._g.rows;if(null==e){for(let e=0;e<t.length;e++)t[e]instanceof DetailRow&&this.hideDetail(e);return}!(t[e=this._toIndex(e)]instanceof DetailRow)&&e<t.length-1&&t[e+1]instanceof DetailRow&&e++;let i=t[e];i instanceof DetailRow&&(this.disposeDetailCell&&this.disposeDetailCell(i),wjcCore.Control.disposeAll(i.detail),t.removeAt(e))}showDetail(e,t=!1){let i=this._g,l=i.rows;if((e=this._toIndex(e))>0&&l[e]instanceof DetailRow&&e--,t){let t=i.selection,a=!1;for(let i=0;i<l.length-1;i++)i!=e&&l[i+1]instanceof DetailRow&&(this.hideDetail(i),i<e&&e--,i<t.row&&(t.row--,t.row2--,a=!0));a&&i.select(t,!1)}if(!this.isDetailVisible(e)&&this._hasDetail(e)){let t=new DetailRow(l[e]);if(t.detail=this._createDetailCell(l[e]),t.detail)if(l.insert(e+1,t),this._animated){let l=t.detail.style;l.transform="translateY(-100%)",l.opacity="0",wjcCore.animate(a=>{a<1?(l.transform="translateY("+(100*-(1-a)).toFixed(0)+"%)",l.opacity=(a*a).toString()):(l.transform="",l.opacity="",wjcCore.Control.invalidateAll(t.detail),i.scrollIntoView(e+1,-1))})}else i.scrollIntoView(e+1,-1)}}_toIndex(e){return e instanceof wjcGrid.Row&&(e=e.index),wjcCore.asNumber(e,!1,!0)}_hdrClick(e){if(!e.defaultPrevented&&0==e.button&&wjcCore.closestClass(e.target,FlexGridDetailProvider._WJC_DETAIL))switch(this._mode){case DetailVisibilityMode.ExpandMulti:case DetailVisibilityMode.ExpandSingle:let t=this._g,i=t.hitTest(e.target);i.panel||(i=t.hitTest(e)),i.panel&&this._toggleRowDetail(i.row)&&e.preventDefault()}}_toggleRowDetail(e){if(e>-1&&this._hasDetail(e)){if(this.isDetailVisible(e))this.hideDetail(e);else{let t=this._g;t.select(new wjcGrid.CellRange(e,0,e,t.columns.length-1)),this.showDetail(e,this._mode==DetailVisibilityMode.ExpandSingle)}return!0}return!1}_selectionChanged(e,t){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,t){let i=this._g,l=t.panel.rows[t.row];if(t.panel==i.cells&&l instanceof DetailRow&&null!=l.detail&&t.col==i.cells.columns.frozen)if(wjcCore.addClass(t.cell,"wj-detail"),t.cell.textContent="",t.cell.style.textAlign="",t.cell.appendChild(l.detail),null==l.height){wjcCore.Control.refreshAll(t.cell);let e=getComputedStyle(t.cell),i=parseInt(e.paddingTop)+parseInt(e.paddingBottom),a=l.detail.scrollHeight+i;this._maxHeight>0&&a>this._maxHeight&&(a=this._maxHeight),l.height=a,l.detail.style.height||(l.detail.style.height="100%");let o=l.detail.querySelector(".wj-flexgrid");o&&!o.style.height&&(o.style.height="100%")}else setTimeout(()=>{wjcCore.Control.refreshAll(l.detail)});if(t.panel==i.rowHeaders&&0==t.col&&this._hasDetail(t.row))switch(t.cell.style.cursor="",this._mode){case DetailVisibilityMode.ExpandMulti:case DetailVisibilityMode.ExpandSingle:let e=t.cell,l=i.rows[t.row+1]instanceof DetailRow,a=l?"minus":"plus",o=FlexGridDetailProvider._WJC_DETAIL;e.innerHTML='<button class="wj-btn wj-btn-glyph '+o+'" type="button" tabindex="-1"><span class="wj-glyph-'+a+'"></span></button>';let s=e.children[0],r=wjcCore.culture.FlexGridDetailProvider.ariaLabels.toggleDetail;wjcCore.setAriaLabel(s,r),wjcCore.setAttribute(s,"aria-expanded",l)}}_resizedRow(e,t){let i=t.panel.rows[t.row];i instanceof DetailRow&&i.detail&&wjcCore.Control.refreshAll(i.detail)}_hasDetail(e){let t=this._g.rows[e];return wjcCore.isFunction(this._rowHasDetailFn)?this._rowHasDetailFn(t):this._isRegularRow(t)}_isRegularRow(e){return!(e instanceof wjcGrid.GroupRow||e instanceof wjcGrid._NewRowTemplate)}_createDetailCell(e,t){return this.createDetailCell?this.createDetailCell(e,t):null}}FlexGridDetailProvider._WJC_DETAIL="wj-elem-detail",exports.FlexGridDetailProvider=FlexGridDetailProvider;class DetailMergeManager extends wjcGrid.MergeManager{constructor(e){super(e)}getMergedRange(e,t,i,l=!0){switch(e.cellType){case wjcGrid.CellType.Cell:if(e.rows[t]instanceof DetailRow){let l=e.columns,a=Math.min(l.length,l.frozen);return i<a?new wjcGrid.CellRange(t,0,t,a-1):new wjcGrid.CellRange(t,a,t,l.length-1)}break;case wjcGrid.CellType.RowHeader:if(e.rows[t]instanceof DetailRow)return new wjcGrid.CellRange(t-1,i,t,i);if(t<e.rows.length-1&&e.rows[t+1]instanceof DetailRow)return new wjcGrid.CellRange(t,i,t+1,i)}return super.getMergedRange(e,t,i,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;