text: // COPYRIGHT © 2021 Esri
  //
  // All rights reserved under the copyright laws of the United States
  // and applicable international laws, treaties, and conventions.
  //
  // This material is licensed for use under the Esri Master License
  // Agreement (MLA), and is bound by the terms of that agreement.
  // You may redistribute and use this code without modification,
  // provided you adhere to the terms of the MLA and include this
  // copyright notice.
  //
  // See use restrictions at http://www.esri.com/legal/pdfs/mla_e204_e300/english
  //
  // For additional information, contact:
  // Environmental Systems Research Institute, Inc.
  // Attn: Contracts and Legal Services Department
  // 380 New York Street
  // Redlands, California, USA 92373
  // USA
  //
  // email: contracts@esri.com
  //
  // See http://js.arcgis.com/3.36/esri/copyright.txt for details.
  
  define(["dojo/_base/lang","dojo/dom-class","dojo/dom-construct","esri/dijit/geoenrichment/ReportPlayer/config","./GridRowStyler","../GridDataUtil","../GridQueryUtil","../gridLayoutCalcUtils/GridLayoutCalculatorQueryUtil","esri/dijit/geoenrichment/utils/DnDUtil","esri/dijit/geoenrichment/utils/TooltipUtil","dojo/i18n!esri/nls/jsapi"],(function(r,e,t,o,n,l,a,i,d,u,c){c=c.geoenrichment.dijit.ReportPlayer.Grid;var s={ORDER_ASC:"asc",ORDER_DES:"desc",isGridSortable:function(r){return!(!r.allowSorting||!(r.viewModel.dynamicReportInfo||r.presetSorting&&!r.ignorePresetSorting)||!s.isGridSortableInStructure(r))},isGridSortableInStructure:function(r){return!(r.isEmptyTable()||r.store.data.length<3||!s.collectHeaderRowInfoFromData(r))},getSorting:function(r){var e=r._sortInfo||r.presetSorting;return e&&{field:e.field,order:e.order}},applySortingToStoreData:function(r){s.isGridSortable(r)&&s._applySortingToStoreData(r)},setSorting:function(r,e,t){if(s.isGridSortable(r)&&e){if(r.presetSorting=null,r._sortInfo={field:e.field,order:e.order},!t||!t.doNotRefresh)return r.refresh();s._makeSortable(r)}},buildSortControls:function(r){s.isGridSortable(r)&&s._makeSortable(r)},getSortRowIndexMapping:function(r){var e=s.getSorting(r);if(!e||!e.order)return null;var t={},o={};return r._getOriginalData().forEach((function(r,e){o[r.id]=e})),r.store.data.forEach((function(r,e){t[e]=o[r.id]})),t},getSortableDataInfo:function(e){function t(t){var o=[],n=s.collectHeaderRowInfoFromData(e,t);if(!n)return null;for(var l=t?e._getOriginalData().slice():r.clone(e.store.data),a=0;a<n.headerRowIndex+1;a++)o.push(l.shift());return{topData:o,sortableData:l}}var o=t();return o&&(o.styleInfo=n.getStyleInfo(t(!0).sortableData,e.columns)),o},_applySortingToStoreData:function(r){var e=s.getSorting(r);if(e&&e.order){r._saveOriginalData();var t=s.getSortableDataInfo(r),o=e.field,a=r.columns.filter((function(r){return r.field===o}))[0];a&&t.sortableData.sort((function(t,o){function n(e){var t=l.getRenderedValue(r,e,a);return"number"==typeof t.numericValue?t.numericValue:t.formattedValue}var i,d=n(t),u=n(o),c=e.order===s.ORDER_DES;return"number"==typeof d||"number"==typeof u?(i=(d=Number(d))>(u=Number(u))?1:d<u?-1:0,c?-i:i):(d=String(d),u=String(u),i=d.localeCompare(u),c?-i:i)})),t.styleInfo&&n.setAlternatingColors(t.sortableData,r.columns,t.styleInfo),r.store.data=t.topData.concat(t.sortableData)}}};return s.collectHeaderRowInfoFromData=function(r,e){var t=e?r._getOriginalData():r.store.data;function o(e){return function(e){for(var o=t[e],n=0,l=0;l<r.columns.length;l++){var a=i.getColumnSpannedFields(r,o,r.columns[l].field);l+=a&&a.length?a.length-1:0,n++}return n}(e)===r.columns.length}for(var n,l=t.length-2,a=0;a<l;a++)if(o(a)){n=a;break}if(!(n>=0))return null;for(a=n+1;a<t.length;a++)if(!o(a))return null;return{headerRowIndex:n}},s._makeSortable=function(r){if(!o.isPlayerOnServer){var n=s._collectHeaderRowInfoWithCells(r);n&&(n.cells.forEach((function(o){if(!o._sortArrow){var n=t.create("div",{class:"sortArrow sortArrowHoverIndicator"},o.domNode),l=o.getFullStyle();n.style[l&&"left"===l.horizontalAlign?"right":"left"]="5px",o._sortArrow=n,r.viewModel.dynamicReportInfo&&(e.add(o.domNode,"esriGEClickable"),d.addNoDragClickHandler(o.contentBlock,(function(e){r&&r.canSortCellFunc&&!r.canSortCellFunc(o)||s._checkCanSortOnHeaderClick(e)&&s._toggleSortForCell(o)})),d.addNoDragClickHandler(n,(function(r){s._toggleSortForCell(o)})))}})),s._updateArrows(r,n))}},s._collectHeaderRowInfoWithCells=function(r){function e(e){return e&&e.length===r.columns.length}for(var t,o,n=r.store.data.length-2,l=0;l<n;l++){var i=a.queryCells(r,{rowIndex:l});if(e(i)){t=i,o=l;break}}if(!t)return null;for(l=o+1;l<r.store.data.length;l++)if(!e(i=a.queryCells(r,{rowIndex:l})))return null;return{cells:t,headerRowIndex:o}},s._toggleSortForCell=function(r){var e=r.parentGrid,t=s.getSorting(e),o=t&&t.field===r.column.field?t.order:null;e.presetSorting=null,e._sortInfo={field:r.column.field,order:null},o?o===s.ORDER_DES?e._sortInfo.order=s.ORDER_ASC:o===s.ORDER_ASC&&(e._sortInfo.order=null):e._sortInfo.order=s.ORDER_DES,e.refresh(),e.onSortingChanged(s.getSorting(e))},s._checkCanSortOnHeaderClick=function(r){if(r&&r.path){if(r.path.some((function(r){return"A"===r.nodeName})))return!1}else if(r&&r.srcElement&&r.srcElement.parentNode&&"A"===r.srcElement.parentNode.nodeName)return!1;return!0},s._updateArrows=function(r,t){t.cells.forEach((function(t){var o=t._sortArrow;if(o){e.remove(o,"sortArrowHoverIndicator sortArrowUp sortArrowDown"),u.setTooltipToNode(o,null);var n=s.getSorting(r);n&&n.field===t.column.field?n.order===s.ORDER_ASC?e.add(o,"sortArrowUp"):n.order===s.ORDER_DES?e.add(o,"sortArrowDown"):(u.setTooltipToNode(o,c.sortTable),e.add(o,"sortArrowHoverIndicator")):(u.setTooltipToNode(o,c.sortTable),e.add(o,"sortArrowHoverIndicator")),setTimeout((function(){o.style.top=(t.getHeight()-o.clientHeight)/2+"px"}),100)}}))},s}));