/*
    *
    * 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 wjcCore=require("wijmo/wijmo"),wjcGrid=require("wijmo/wijmo.grid");var __glob="undefined"!=typeof window?window:self;const wjcSelfRef=require("wijmo/wijmo.grid.detail");var wjcSelf=wjcSelfRef||exports;__glob.wijmo=__glob.wijmo||{},__glob.wijmo.grid=__glob.wijmo.grid||{},__glob.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.updatedView.addHandler(()=>{this._handleFixedCells()}),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,o=!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--,o=!0));o&&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){l.insert(e+1,t),i.autoSizeRow(e+1);let o=this._maxHeight;if(wjcCore.isNumber(o)&&t.renderHeight>o&&(t.height=o),this._animated){let l=t.detail.style;l.transform="translateY(-100%)",l.opacity="0",wjcCore.animate(o=>{o<1?(l.transform="translateY("+(100*-(1-o)).toFixed(0)+"%)",l.opacity=(o*o).toString()):(l.transform="",l.opacity="",wjcCore.Control.invalidateAll(t.detail),i.scrollIntoView(e+1,-1))})}else i.scrollIntoView(e+1,-1,!0)}}}_handleFixedCells(){let e=this._g,t=e.hostElement;if(e.frozenRows||e.frozenColumns){let i=wjcCore.Control.getControl(t.querySelector(".wj-flexgrid"));if(i instanceof wjcGrid.FlexGrid&&(i.frozenRows||i.frozenColumns)){wjcCore.setCss([e._eTL,e._eBL,e._eCHdr,e._eCFtr,e._eRHdr,e._eMarquee],{zIndex:"6"});let t=e.hostElement.querySelectorAll(".wj-frozen");for(let i=0;i<t.length;i++){let l=t[i];if(wjcCore.closest(l,".wj-flexgrid")==e.hostElement){let e=parseInt(l.style.zIndex);l.style.zIndex=(e%3+3).toString()}}}}}_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){if(this.isDetailVisible(e))return this.hideDetail(e),!0;if(this._hasDetail(e)){let t=this._g;return t.select(new wjcGrid.CellRange(e,0,e,t.columns.length-1)),this.showDetail(e,this._mode==DetailVisibilityMode.ExpandSingle),!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),o=l.detail.scrollHeight+i;this._maxHeight>0&&o>this._maxHeight&&(o=this._maxHeight),l.height=o,l.detail.style.height||(l.detail.style.height="100%");let a=l.detail.querySelector(".wj-flexgrid");a&&!a.style.height&&(a.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,o=l?"minus":"plus",a=FlexGridDetailProvider._WJC_DETAIL;e.innerHTML='<div class="wj-btn wj-btn-glyph '+a+'" role="button" tabindex="-1"><span class="wj-glyph-'+o+'"></span></div>';let r=e.children[0],s=wjcCore.culture.FlexGridDetailProvider.ariaLabels.toggleDetail;wjcCore.setAriaLabel(r,s),wjcCore.setAttribute(r,"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,o=Math.min(l.length,l.frozen);return i<o?new wjcGrid.CellRange(t,0,t,o-1):new wjcGrid.CellRange(t,o,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;