smart-webcomponents-angular
Version:
[](https://jqwidgets.com/license/)
6 lines (4 loc) • 6.38 kB
JavaScript
/* Smart UI v20.0.2 (2024-09-06)
Copyright (c) 2011-2024 jQWidgets.
License: https://htmlelements.com/license/ */ //
Smart.Utilities.Assign("Grid.Sort",class{clearSort(){const t=this;if(!t._isSorting&&t.dataSource&&t._sortedColumns){t._isSorting=!0,t.dataSource.clearSort(),t._sortedColumns||(t._sortedColumns=[]);for(let e=0;e<t._sortedColumns.length;e++){const r=t._sortedColumns[e],o=t.columnByDataField[r.dataField];o&&(o.setProperty("sortOrder",null),o.setProperty("sortIndex",null))}t._sortedColumns=[],t.rowById=[];for(let e=0;e<t.dataSource.length;e++){const r=t.rows[e],o=t.dataSource[e];r&&(r.data=o,o&&o.$&&(r.id=o.$.id,r._updateData()),t.rowById[r.id]=r)}t._recycle(),t._refreshHeaderBar(),t._isSorting=!1}}getSortedColumns(){const t=this,e=[];if(t._sortedColumns)for(let r=0;r<t._sortedColumns.length;r++){const o=t._sortedColumns[r];e[o.dataField]={sortOrder:o.sortOrder,sortIndex:o.sortIndex},e.length++}return e}addSort(t,e){this.sortBy(t,e)}removeSort(t){this.sortBy(t,null)}refreshSort(){this._refreshSort(this._sortedColumns)}_refreshSort(t){const e=this;if(e._isSorting||!t)return;if(e._suppressSort)return;if(!1===e.sorting.maintainSort)return;const r=[],o=[],s=[];e._isSorting=!0;let n=[];for(let a=0;a<t.length;a++){const d=t[a],i=e.columnByDataField[d.dataField];i&&(i.setProperty("sortOrder",d.sortOrder),i.setProperty("sortIndex",a),i.sortComparator&&(n[i.dataField]=i.sortComparator),r.push(d.dataField),o.push(d.sortOrder),s.push(d.dataType||"string"))}!function(){if(e.dataSource&&(e.dataSource.sortComparators=n),e.dataSource&&e.dataSource.virtualDataSource)e._virtualDataRequest("sort");else{if(e.dataSource.sortBy(r,s,o),e.dataSource.boundHierarchy)e._refreshRowHierarchy(),e.dataSource.groupBy.length>0&&(e._refreshRowHierarchy(!0,!0),e.refresh());else{e.rowById=[];for(let t=0;t<e.dataSource.length;t++){const r=e.rows[t],o=e.dataSource[t];r.data=o,r.id=o.$.id,r._updateData(),e.rowById[r.id]=r}}e._recycle()}e._renderConditionalFormatting(!0)}(),e._refreshHeaderBar(),e._isSorting=!1,e._sortedColumns=t}sortBy(t,e){const r=this,o=r.columnByDataField[t],s=[],n=[],a=[],d=void 0===e;if(r._isSorting||!o)return;r._isSorting=!0,"descending"===e&&(e="desc"),void 0!==e&&"ascending"!==e||(e="asc");const i=function(t){t&&(t.setProperty("sortOrder",null),t.setProperty("sortIndex",null))},l=function(){if(r._sortedColumns.length>0)for(let t=0;t<r._sortedColumns.length;t++){const e=r._sortedColumns[t],o=r.columnByDataField[e.dataField];i(o)}r._sortedColumns=[]};if(null===o)return l(),void(r._isSorting=!1);if(!r.sorting.enabled||!r.dataSource||!o.allowSort||r._sortAnimation)return void(r._isSorting=!1);i(o),r._sortedColumns||(r._sortedColumns=[]);let u="string";const c=r.dataSource&&r.dataSource.dataFields?r.dataSource.dataFields:r.dataSourceSettings.dataFields;if(c)for(let e=0;e<c.length;e++){const r=c[e];if(r.name===t){u=r.dataType;break}}let m=!0;for(let s=0;s<r._sortedColumns.length;s++){const n=r._sortedColumns[s];if(n.dataField===t)if(m=!1,n.sortIndex=o.sortIndex,d){if("asc"===n.sortOrder)n.sortOrder="desc",e="desc";else if("desc"===n.sortOrder){r.sorting.sortToggleThreeStates?(r._sortedColumns.splice(s,1),i(o),e=null):(n.sortOrder="asc",e="asc");break}}else n.sortOrder=e,null===e&&(r._sortedColumns.splice(s,1),i(o))}m&&("one"===r.sorting.mode&&l(),"many"===r.sorting.mode&&!1===r._canSort&&l(),null!==e&&r._sortedColumns.push({dataField:t,sortOrder:e,sortIndex:o.sortIndex,dataType:u})),o.setProperty("sortOrder",e),r._sortedColumns.sort(((t,e)=>"string"==typeof t.sortIndex&&"string"==typeof e.sortIndex?0:"number"==typeof t.sortIndex&&"string"==typeof e.sortIndex?-1:"string"==typeof t.sortIndex&&"number"==typeof e.sortIndex?1:"number"==typeof t.sortIndex&&"number"==typeof e.sortIndex?t.sortIndex-e.sortIndex:void 0));const f=[];for(let t=0;t<r._sortedColumns.length;t++){const e=r._sortedColumns[t];s.push(e.dataField),n.push(e.sortOrder),a.push(e.dataType||"string");const o=r.columnByDataField[e.dataField];o.sortComparator&&(f[o.dataField]=o.sortComparator)}const _=function(){if(r.dataSource.sortComparators=f,r.dataSource&&r.dataSource.virtualDataSource)r._virtualDataRequest("sort");else{if(r.dataSource.sortBy(s,a,n),r.dataSource.boundHierarchy)r._refreshRowHierarchy(),r.dataSource.groupBy.length>0&&r._refreshRowHierarchy(!0,!0),r.refresh();else{r.rowById=[];const t=r.rows.canNotify;r.rows.canNotify=!1;for(let t=0;t<r.dataSource.length;t++){const e=r.rows[t],o=r.dataSource[t];e&&(e.data=o,o&&o.$&&(e.id=o.$.id,e._updateData()),r._filters&&r._filters.length>0&&(e.filtered=void 0===e.data.$.filtered||e.data.$.filtered),r.rowById[e.id]=e)}if(r.rows.canNotify=t,r._filters&&r._filters.length>0&&r.refresh(),r.selection&&r.selection.enabled&&r._selection){r._selection.indexes=[];const t=r._selection.selectionRect;t&&(delete r._selection.selectionRect,t.parentNode.removeChild(t)),r._refreshCellSelectionRect()}}r._recycle()}const t=[],e=[];for(let o=0;o<r._sortedColumns.length;o++){const s=r.columnByDataField[r._sortedColumns[o].dataField];s&&(s.setProperty("sortIndex",o),t.push(s),e.push(s.sortIndex),r._sortedColumns[o].sortIndex=o)}r._refreshHeaderBar(),r._renderConditionalFormatting(!0),r.$.fireEvent("sort",{sortDataFields:s,sortOrders:n,sortDataTypes:a,sortIndexes:e,columns:t,data:r._sortedColumns}),r._onSort&&r._onSort()};if(r.appearance.allowSortAnimation){let t=[],e=[];r.rows.canNotify=!1,r._sortAnimation=!0;const o=function(){for(let e=0;e<r._rowElements.length;e++){const o=r._rowElements[e];o.classList.remove("smart-grid-sort-animation"),r.removeTransformMoveStyle(o),o.offsetHeight>0&&t.push(o.offsetTop)}};o(),r._sortTimer=setTimeout((function(){o(),r._sortAnimation=!1,r.rows.canNotify=!0}),r.appearance.sortAnimationDuration),r._sortTimer2=setTimeout((function(){_()}),r.appearance.sortAnimationDuration/2);for(let o=0;o<t.length;o++){const s=r._rowElements[o];s.classList.remove("smart-grid-sort-animation"),r.removeTransformMoveStyle(s);let n=Math.floor(Math.random()*t.length-1+1);for(;e[n];)n=Math.floor(Math.random()*t.length-1+1);e[n]=!0,r.addTransformMoveStyle(s,"0ms",0,-s.offsetTop+t[n],0,.5),s.classList.add("smart-grid-sort-animation"),setTimeout((function(){r.addTransformMoveStyle(s,r.appearance.sortAnimationDuration+"ms",0,0,0,1)})),setTimeout((function(){s.classList.remove("smart-grid-sort-animation")}),r.appearance.sortAnimationDuration)}}else _();r._isSorting=!1}});