1 var _typeof = typeof Symbol ===
"function" && typeof Symbol.iterator ===
"symbol" ?
function (obj) {
return typeof obj; } :
function (obj) {
return obj && typeof Symbol ===
"function" && obj.constructor === Symbol && obj !== Symbol.prototype ?
"symbol" : typeof obj; };
5 var Sort =
function Sort(table) {
12 Sort.prototype.initializeColumn =
function (column, content) {
18 switch (_typeof(column.definition.sorter)) {
20 if (
self.sorters[column.definition.sorter]) {
21 sorter =
self.sorters[column.definition.sorter];
23 console.warn(
"Sort Error - No such sorter found: ", column.definition.sorter);
28 sorter = column.definition.sorter;
32 column.modules.sort = {
33 sorter: sorter, dir:
"none",
34 params: column.definition.sorterParams || {},
35 startingDir: column.definition.headerSortStartingDir ||
"asc",
36 tristate: typeof column.definition.headerSortTristate !==
"undefined" ? column.definition.headerSortTristate : this.table.options.headerSortTristate
39 if (typeof column.definition.headerSort ===
"undefined" ?
this.table.options.headerSort !==
false : column.definition.headerSort !==
false) {
41 colEl = column.getElement();
43 colEl.classList.add(
"tabulator-sortable");
45 arrowEl = document.createElement(
"div");
46 arrowEl.classList.add(
"tabulator-arrow");
48 content.appendChild(arrowEl);
51 colEl.addEventListener(
"click",
function (e) {
56 if (column.modules.sort) {
57 if (column.modules.sort.tristate) {
58 if (column.modules.sort.dir ==
"none") {
59 dir = column.modules.sort.startingDir;
61 if (column.modules.sort.dir == column.modules.sort.startingDir) {
62 dir = column.modules.sort.dir ==
"asc" ?
"desc" :
"asc";
68 switch (column.modules.sort.dir) {
78 dir = column.modules.sort.startingDir;
82 if (
self.table.options.columnHeaderSortMulti && (e.shiftKey || e.ctrlKey)) {
83 sorters =
self.getSort();
85 match = sorters.findIndex(
function (sorter) {
86 return sorter.field === column.getField();
90 sorters[match].dir = dir;
92 if (match != sorters.length - 1) {
93 match = sorters.splice(match, 1)[0];
100 sorters.push({ column: column, dir: dir });
105 self.setSort(sorters);
111 self.setSort(column, dir);
115 self.table.rowManager.sorterRefresh(!
self.sortList.length);
122 Sort.prototype.hasChanged =
function () {
123 var changed = this.changed;
124 this.changed =
false;
129 Sort.prototype.getSort =
function () {
133 self.sortList.forEach(
function (item) {
135 sorters.push({ column: item.column.getComponent(), field: item.column.getField(), dir: item.dir });
143 Sort.prototype.setSort =
function (sortList, dir) {
147 if (!Array.isArray(sortList)) {
148 sortList = [{ column: sortList, dir: dir }];
151 sortList.forEach(
function (item) {
154 column =
self.table.columnManager.findColumn(item.column);
157 item.column = column;
158 newSortList.push(item);
161 console.warn(
"Sort Warning - Sort field does not exist and is being ignored: ", item.column);
165 self.sortList = newSortList;
167 if (this.table.options.persistence &&
this.table.modExists(
"persistence",
true) && this.table.modules.persistence.config.sort) {
168 this.table.modules.persistence.save(
"sort");
173 Sort.prototype.clear =
function () {
178 Sort.prototype.findSorter =
function (column) {
179 var row = this.table.rowManager.activeRows[0],
186 field = column.getField();
190 value = column.getFieldValue(row);
192 switch (typeof value ===
"undefined" ?
"undefined" : _typeof(value)) {
202 if (!isNaN(value) && value !==
"") {
205 if (value.match(/((^[0-9]+[a-z]+)|(^[a-z]+[0-9]+))+$/i)) {
214 return this.sorters[sorter];
218 Sort.prototype.sort =
function (data) {
223 sortList = this.table.options.sortOrderReverse ?
self.sortList.slice().reverse() :
self.sortList;
225 if (
self.table.options.dataSorting) {
226 self.table.options.dataSorting.call(
self.table,
self.getSort());
229 self.clearColumnHeaders();
231 if (!
self.table.options.ajaxSorting) {
233 sortList.forEach(
function (item, i) {
235 if (item.column && item.column.modules.sort) {
238 if (!item.column.modules.sort.sorter) {
239 item.column.modules.sort.sorter =
self.findSorter(item.column);
242 self._sortItem(data, item.column, item.dir, sortList, i);
245 self.setColumnHeader(item.column, item.dir);
248 sortList.forEach(
function (item, i) {
249 self.setColumnHeader(item.column, item.dir);
253 if (
self.table.options.dataSorted) {
254 self.table.options.dataSorted.call(
self.table,
self.getSort(),
self.table.rowManager.getComponents(
"active"));
259 Sort.prototype.clearColumnHeaders =
function () {
260 this.table.columnManager.getRealColumns().forEach(
function (column) {
261 if (column.modules.sort) {
262 column.modules.sort.dir =
"none";
263 column.getElement().setAttribute(
"aria-sort",
"none");
269 Sort.prototype.setColumnHeader =
function (column, dir) {
270 column.modules.sort.dir = dir;
271 column.getElement().setAttribute(
"aria-sort", dir);
275 Sort.prototype._sortItem =
function (data, column, dir, sortList, i) {
278 var params = typeof column.modules.sort.params ===
"function" ? column.modules.sort.params(column.getComponent(), dir) : column.modules.sort.params;
280 data.sort(function (a, b) {
282 var result =
self._sortRow(a, b, column, dir, params);
285 if (result === 0 && i) {
286 for (var j = i - 1; j >= 0; j--) {
287 result =
self._sortRow(a, b, sortList[j].column, sortList[j].dir, params);
300 Sort.prototype._sortRow =
function (a, b, column, dir, params) {
301 var el1Comp, el2Comp, colComp;
304 var el1 = dir ==
"asc" ? a : b;
305 var el2 = dir ==
"asc" ? b : a;
307 a = column.getFieldValue(el1.getData());
308 b = column.getFieldValue(el2.getData());
310 a = typeof a !==
"undefined" ? a :
"";
311 b = typeof b !==
"undefined" ? b :
"";
313 el1Comp = el1.getComponent();
314 el2Comp = el2.getComponent();
316 return column.modules.sort.sorter.call(
this, a, b, el1Comp, el2Comp, column.getComponent(), dir, params);
320 Sort.prototype.sorters = {
323 number:
function number(a, b, aRow, bRow, column, dir, params) {
324 var alignEmptyValues = params.alignEmptyValues;
325 var decimal = params.decimalSeparator ||
".";
326 var thousand = params.thousandSeparator ||
",";
329 a = parseFloat(String(a).split(thousand).join(
"").split(decimal).join(
"."));
330 b = parseFloat(String(b).split(thousand).join(
"").split(decimal).join(
"."));
334 emptyAlign = isNaN(b) ? 0 : -1;
335 }
else if (isNaN(b)) {
343 if (alignEmptyValues ===
"top" && dir ===
"desc" || alignEmptyValues ===
"bottom" && dir ===
"asc") {
351 string:
function string(a, b, aRow, bRow, column, dir, params) {
352 var alignEmptyValues = params.alignEmptyValues;
358 emptyAlign = !b ? 0 : -1;
363 switch (_typeof(params.locale)) {
366 locale = this.table.modules.localize.getLocale();
370 locale = params.locale;
374 return String(a).toLowerCase().localeCompare(String(b).toLowerCase(), locale);
378 if (alignEmptyValues ===
"top" && dir ===
"desc" || alignEmptyValues ===
"bottom" && dir ===
"asc") {
386 date:
function date(a, b, aRow, bRow, column, dir, params) {
387 if (!params.format) {
388 params.format =
"DD/MM/YYYY";
391 return this.sorters.datetime.call(
this, a, b, aRow, bRow, column, dir, params);
395 time:
function time(a, b, aRow, bRow, column, dir, params) {
396 if (!params.format) {
397 params.format =
"hh:mm";
400 return this.sorters.datetime.call(
this, a, b, aRow, bRow, column, dir, params);
404 datetime:
function datetime(a, b, aRow, bRow, column, dir, params) {
405 var format = params.format ||
"DD/MM/YYYY hh:mm:ss",
406 alignEmptyValues = params.alignEmptyValues,
409 if (typeof moment !=
"undefined") {
410 a = moment(a, format);
411 b = moment(b, format);
414 emptyAlign = !b.isValid() ? 0 : -1;
415 }
else if (!b.isValid()) {
423 if (alignEmptyValues ===
"top" && dir ===
"desc" || alignEmptyValues ===
"bottom" && dir ===
"asc") {
429 console.error(
"Sort Error - 'datetime' sorter is dependant on moment.js");
434 boolean:
function boolean(a, b, aRow, bRow, column, dir, params) {
435 var el1 = a ===
true || a ===
"true" || a ===
"True" || a === 1 ? 1 : 0;
436 var el2 = b ===
true || b ===
"true" || b ===
"True" || b === 1 ? 1 : 0;
442 array:
function array(a, b, aRow, bRow, column, dir, params) {
445 var type = params.type ||
"length";
446 var alignEmptyValues = params.alignEmptyValues;
449 function calc(value) {
457 return value.reduce(
function (c, d) {
463 return Math.max.apply(null, value);
467 return Math.min.apply(null, value);
471 return value.reduce(
function (c, d) {
479 if (!Array.isArray(a)) {
480 alignEmptyValues = !Array.isArray(b) ? 0 : -1;
481 }
else if (!Array.isArray(b)) {
482 alignEmptyValues = 1;
486 el1 = a ? calc(a) : 0;
487 el2 = b ? calc(b) : 0;
493 if (alignEmptyValues ===
"top" && dir ===
"desc" || alignEmptyValues ===
"bottom" && dir ===
"asc") {
501 exists:
function exists(a, b, aRow, bRow, column, dir, params) {
502 var el1 = typeof a ==
"undefined" ? 0 : 1;
503 var el2 = typeof b ==
"undefined" ? 0 : 1;
509 alphanum:
function alphanum(as, bs, aRow, bRow, column, dir, params) {
518 var alignEmptyValues = params.alignEmptyValues;
522 if (!as && as !== 0) {
523 emptyAlign = !bs && bs !== 0 ? 0 : -1;
524 }
else if (!bs && bs !== 0) {
528 if (isFinite(as) && isFinite(bs))
return as - bs;
529 a = String(as).toLowerCase();
530 b = String(bs).toLowerCase();
531 if (a === b)
return 0;
532 if (!(rd.test(a) && rd.test(b)))
return a > b ? 1 : -1;
535 L = a.length > b.length ? b.length : a.length;
540 if (isFinite(a1) && isFinite(b1)) {
541 if (a1.charAt(0) ===
"0") a1 =
"." + a1;
542 if (b1.charAt(0) ===
"0") b1 =
"." + b1;
544 }
else return a1 > b1 ? 1 : -1;
548 return a.length > b.length;
552 if (alignEmptyValues ===
"top" && dir ===
"desc" || alignEmptyValues ===
"bottom" && dir ===
"asc") {
560 Tabulator.prototype.registerModule(
"sort", Sort);