tabulator-tables
Version:
Interactive table generation JavaScript library
2 lines • 7.05 kB
JavaScript
/* Tabulator v4.3.0 (c) Oliver Folkerd */
var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},Sort=function(t){this.table=t,this.sortList=[],this.changed=!1};Sort.prototype.initializeColumn=function(t,e){var r,o,n=this,s=!1;switch(_typeof(t.definition.sorter)){case"string":n.sorters[t.definition.sorter]?s=n.sorters[t.definition.sorter]:console.warn("Sort Error - No such sorter found: ",t.definition.sorter);break;case"function":s=t.definition.sorter}t.modules.sort={sorter:s,dir:"none",params:t.definition.sorterParams||{},startingDir:t.definition.headerSortStartingDir||"asc",tristate:t.definition.headerSortTristate},!1!==t.definition.headerSort&&(r=t.getElement(),r.classList.add("tabulator-sortable"),o=document.createElement("div"),o.classList.add("tabulator-arrow"),e.appendChild(o),r.addEventListener("click",function(e){var r="",o=[],s=!1;if(t.modules.sort){if(t.modules.sort.tristate)r="none"==t.modules.sort.dir?t.modules.sort.startingDir:t.modules.sort.dir==t.modules.sort.startingDir?"asc"==t.modules.sort.dir?"desc":"asc":"none";else switch(t.modules.sort.dir){case"asc":r="desc";break;case"desc":r="asc";break;default:r=t.modules.sort.startingDir}n.table.options.columnHeaderSortMulti&&(e.shiftKey||e.ctrlKey)?(o=n.getSort(),s=o.findIndex(function(e){return e.field===t.getField()}),s>-1?(o[s].dir=r,s!=o.length-1&&(s=o.splice(s,1)[0],"none"!=r&&o.push(s))):"none"!=r&&o.push({column:t,dir:r}),n.setSort(o)):"none"==r?n.clear():n.setSort(t,r),n.table.rowManager.sorterRefresh(!n.sortList.length)}}))},Sort.prototype.hasChanged=function(){var t=this.changed;return this.changed=!1,t},Sort.prototype.getSort=function(){var t=this,e=[];return t.sortList.forEach(function(t){t.column&&e.push({column:t.column.getComponent(),field:t.column.getField(),dir:t.dir})}),e},Sort.prototype.setSort=function(t,e){var r=this,o=[];Array.isArray(t)||(t=[{column:t,dir:e}]),t.forEach(function(t){var e;e=r.table.columnManager.findColumn(t.column),e?(t.column=e,o.push(t),r.changed=!0):console.warn("Sort Warning - Sort field does not exist and is being ignored: ",t.column)}),r.sortList=o,this.table.options.persistentSort&&this.table.modExists("persistence",!0)&&this.table.modules.persistence.save("sort")},Sort.prototype.clear=function(){this.setSort([])},Sort.prototype.findSorter=function(t){var e,r=this.table.rowManager.activeRows[0],o="string";if(r&&(r=r.getData(),t.getField()))switch(e=t.getFieldValue(r),void 0===e?"undefined":_typeof(e)){case"undefined":o="string";break;case"boolean":o="boolean";break;default:isNaN(e)||""===e?e.match(/((^[0-9]+[a-z]+)|(^[a-z]+[0-9]+))+$/i)&&(o="alphanum"):o="number"}return this.sorters[o]},Sort.prototype.sort=function(t){var e,r=this;e=this.table.options.sortOrderReverse?r.sortList.slice().reverse():r.sortList,r.table.options.dataSorting&&r.table.options.dataSorting.call(r.table,r.getSort()),r.clearColumnHeaders(),r.table.options.ajaxSorting?e.forEach(function(t,e){r.setColumnHeader(t.column,t.dir)}):e.forEach(function(o,n){o.column&&o.column.modules.sort&&(o.column.modules.sort.sorter||(o.column.modules.sort.sorter=r.findSorter(o.column)),r._sortItem(t,o.column,o.dir,e,n)),r.setColumnHeader(o.column,o.dir)}),r.table.options.dataSorted&&r.table.options.dataSorted.call(r.table,r.getSort(),r.table.rowManager.getComponents(!0))},Sort.prototype.clearColumnHeaders=function(){this.table.columnManager.getRealColumns().forEach(function(t){t.modules.sort&&(t.modules.sort.dir="none",t.getElement().setAttribute("aria-sort","none"))})},Sort.prototype.setColumnHeader=function(t,e){t.modules.sort.dir=e,t.getElement().setAttribute("aria-sort",e)},Sort.prototype._sortItem=function(t,e,r,o,n){var s=this,i="function"==typeof e.modules.sort.params?e.modules.sort.params(e.getComponent(),r):e.modules.sort.params;t.sort(function(t,a){var l=s._sortRow(t,a,e,r,i);if(0===l&&n)for(var u=n-1;u>=0&&0===(l=s._sortRow(t,a,o[u].column,o[u].dir,i));u--);return l})},Sort.prototype._sortRow=function(t,e,r,o,n){var s,i,a="asc"==o?t:e,l="asc"==o?e:t;return t=r.getFieldValue(a.getData()),e=r.getFieldValue(l.getData()),t=void 0!==t?t:"",e=void 0!==e?e:"",s=a.getComponent(),i=l.getComponent(),r.modules.sort.sorter.call(this,t,e,s,i,r.getComponent(),o,n)},Sort.prototype.sorters={number:function(t,e,r,o,n,s,i){var a=i.alignEmptyValues,l=i.decimalSeparator||".",u=i.thousandSeparator||",",c=0;if(t=parseFloat(String(t).split(u).join("").split(l).join(".")),e=parseFloat(String(e).split(u).join("").split(l).join(".")),isNaN(t))c=isNaN(e)?0:-1;else{if(!isNaN(e))return t-e;c=1}return("top"===a&&"desc"===s||"bottom"===a&&"asc"===s)&&(c*=-1),c},string:function(t,e,r,o,n,s,i){var a,l=i.alignEmptyValues,u=0;if(t){if(e){switch(_typeof(i.locale)){case"boolean":i.locale&&(a=this.table.modules.localize.getLocale());break;case"string":a=i.locale}return String(t).toLowerCase().localeCompare(String(e).toLowerCase(),a)}u=1}else u=e?-1:0;return("top"===l&&"desc"===s||"bottom"===l&&"asc"===s)&&(u*=-1),u},date:function(t,e,r,o,n,s,i){return i.format||(i.format="DD/MM/YYYY"),this.sorters.datetime.call(this,t,e,r,o,n,s,i)},time:function(t,e,r,o,n,s,i){return i.format||(i.format="hh:mm"),this.sorters.datetime.call(this,t,e,r,o,n,s,i)},datetime:function(t,e,r,o,n,s,i){var a=i.format||"DD/MM/YYYY hh:mm:ss",l=i.alignEmptyValues,u=0;if("undefined"!=typeof moment){if(t=moment(t,a),e=moment(e,a),t.isValid()){if(e.isValid())return t-e;u=1}else u=e.isValid()?-1:0;return("top"===l&&"desc"===s||"bottom"===l&&"asc"===s)&&(u*=-1),u}console.error("Sort Error - 'datetime' sorter is dependant on moment.js")},boolean:function(t,e,r,o,n,s,i){return(!0===t||"true"===t||"True"===t||1===t?1:0)-(!0===e||"true"===e||"True"===e||1===e?1:0)},array:function(t,e,r,o,n,s,i){function a(t){switch(c){case"length":return t.length;case"sum":return t.reduce(function(t,e){return t+e});case"max":return Math.max.apply(null,t);case"min":return Math.min.apply(null,t);case"avg":return t.reduce(function(t,e){return t+e})/t.length}}var l=0,u=0,c=i.type||"length",d=i.alignEmptyValues,m=0;if(Array.isArray(t)){if(Array.isArray(e))return l=t?a(t):0,u=e?a(e):0,l-u;d=1}else d=Array.isArray(e)?-1:0;return("top"===d&&"desc"===s||"bottom"===d&&"asc"===s)&&(m*=-1),m},exists:function(t,e,r,o,n,s,i){return(void 0===t?0:1)-(void 0===e?0:1)},alphanum:function(t,e,r,o,n,s,i){var a,l,u,c,d,m=0,f=/(\d+)|(\D+)/g,p=/\d/,h=i.alignEmptyValues,g=0;if(t||0===t){if(e||0===e){if(isFinite(t)&&isFinite(e))return t-e;if(a=String(t).toLowerCase(),l=String(e).toLowerCase(),a===l)return 0;if(!p.test(a)||!p.test(l))return a>l?1:-1;for(a=a.match(f),l=l.match(f),d=a.length>l.length?l.length:a.length;m<d;)if(u=a[m],c=l[m++],u!==c)return isFinite(u)&&isFinite(c)?("0"===u.charAt(0)&&(u="."+u),"0"===c.charAt(0)&&(c="."+c),u-c):u>c?1:-1;return a.length>l.length}g=1}else g=e||0===e?-1:0;return("top"===h&&"desc"===s||"bottom"===h&&"asc"===s)&&(g*=-1),g}},Tabulator.prototype.registerModule("sort",Sort);