bootstrap-table-multiple-sort.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('core-js/modules/es.array.concat.js'), require('core-js/modules/es.array.find.js'), require('core-js/modules/es.array.includes.js'), require('core-js/modules/es.array.index-of.js'), require('core-js/modules/es.array.map.js'), require('core-js/modules/es.array.slice.js'), require('core-js/modules/es.array.sort.js'), require('core-js/modules/es.array.splice.js'), require('core-js/modules/es.object.assign.js'), require('core-js/modules/es.object.to-string.js'), require('core-js/modules/es.parse-float.js'), require('core-js/modules/es.regexp.to-string.js'), require('jquery')) :
  3. typeof define === 'function' && define.amd ? define(['core-js/modules/es.array.concat.js', 'core-js/modules/es.array.find.js', 'core-js/modules/es.array.includes.js', 'core-js/modules/es.array.index-of.js', 'core-js/modules/es.array.map.js', 'core-js/modules/es.array.slice.js', 'core-js/modules/es.array.sort.js', 'core-js/modules/es.array.splice.js', 'core-js/modules/es.object.assign.js', 'core-js/modules/es.object.to-string.js', 'core-js/modules/es.parse-float.js', 'core-js/modules/es.regexp.to-string.js', 'jquery'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(null, null, null, null, null, null, null, null, null, null, null, null, global.jQuery));
  5. })(this, (function (es_array_concat_js, es_array_find_js, es_array_includes_js, es_array_indexOf_js, es_array_map_js, es_array_slice_js, es_array_sort_js, es_array_splice_js, es_object_assign_js, es_object_toString_js, es_parseFloat_js, es_regexp_toString_js, $) { 'use strict';
  6. function _typeof(o) {
  7. "@babel/helpers - typeof";
  8. return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
  9. return typeof o;
  10. } : function (o) {
  11. return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
  12. }, _typeof(o);
  13. }
  14. /**
  15. * @author Nadim Basalamah <dimbslmh@gmail.com>
  16. * @version: v1.1.0
  17. * @update: ErwannNevou <https://github.com/ErwannNevou>
  18. */
  19. var isSingleSort = false;
  20. var Utils = $.fn.bootstrapTable.utils;
  21. Object.assign($.fn.bootstrapTable.defaults.icons, {
  22. plus: {
  23. bootstrap3: 'glyphicon-plus',
  24. bootstrap4: 'fa-plus',
  25. bootstrap5: 'bi-plus',
  26. semantic: 'fa-plus',
  27. materialize: 'plus',
  28. foundation: 'fa-plus',
  29. bulma: 'fa-plus',
  30. 'bootstrap-table': 'icon-plus'
  31. }[$.fn.bootstrapTable.theme] || 'fa-clock',
  32. minus: {
  33. bootstrap3: 'glyphicon-minus',
  34. bootstrap4: 'fa-minus',
  35. bootstrap5: 'bi-dash',
  36. semantic: 'fa-minus',
  37. materialize: 'minus',
  38. foundation: 'fa-minus',
  39. bulma: 'fa-minus',
  40. 'bootstrap-table': 'icon-minus'
  41. }[$.fn.bootstrapTable.theme] || 'fa-clock',
  42. sort: {
  43. bootstrap3: 'glyphicon-sort',
  44. bootstrap4: 'fa-sort',
  45. bootstrap5: 'bi-arrow-down-up',
  46. semantic: 'fa-sort',
  47. materialize: 'sort',
  48. foundation: 'fa-sort',
  49. bulma: 'fa-sort',
  50. 'bootstrap-table': 'icon-sort-amount-asc'
  51. }[$.fn.bootstrapTable.theme] || 'fa-clock'
  52. });
  53. var theme = {
  54. bootstrap3: {
  55. html: {
  56. multipleSortModal: "\n <div class=\"modal fade\" id=\"%s\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"%sLabel\" aria-hidden=\"true\">\n <div class=\"modal-dialog\">\n <div class=\"modal-content\">\n <div class=\"modal-header\">\n <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>\n <h4 class=\"modal-title\" id=\"%sLabel\">%s</h4>\n </div>\n <div class=\"modal-body\">\n <div class=\"bootstrap-table\">\n <div class=\"fixed-table-toolbar\">\n <div class=\"bars\">\n <button type=\"button\" class=\"toolbar-btn-add btn btn-default\">%s %s</button>\n <button type=\"button\" class=\"toolbar-btn-delete btn btn-default\" disabled>%s %s</button>\n </div>\n </div>\n <div class=\"fixed-table-container\">\n <table class=\"table\">\n <thead>\n <tr>\n <th></th>\n <th><div class=\"th-inner\">%s</div></th>\n <th><div class=\"th-inner\">%s</div></th>\n </tr>\n </thead>\n <tbody></tbody>\n </table>\n </div>\n </div>\n </div>\n <div class=\"modal-footer\">\n <button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">%s</button>\n <button type=\"button\" class=\"btn btn-primary multi-sort-order-button\">%s</button>\n </div>\n </div>\n </div>\n </div>\n ",
  57. multipleSortButton: '<button class="multi-sort %s" type="button" data-toggle="modal" data-target="#%s" title="%s">%s</button>',
  58. multipleSortSelect: '<select class="%s %s form-control">'
  59. }
  60. },
  61. bootstrap4: {
  62. html: {
  63. multipleSortModal: "\n <div class=\"modal fade\" id=\"%s\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"%sLabel\" aria-hidden=\"true\">\n <div class=\"modal-dialog\" role=\"document\">\n <div class=\"modal-content\">\n <div class=\"modal-header\">\n <h5 class=\"modal-title\" id=\"%sLabel\">%s</h5>\n <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\">\n <span aria-hidden=\"true\">&times;</span>\n </button>\n </div>\n <div class=\"modal-body\">\n <div class=\"bootstrap-table\">\n <div class=\"fixed-table-toolbar\">\n <div class=\"bars pb-3\">\n <button type=\"button\" class=\"toolbar-btn-add btn btn-secondary\">%s %s</button>\n <button type=\"button\" class=\"toolbar-btn-delete btn btn-secondary\" disabled>%s %s</button>\n </div>\n </div>\n <div class=\"fixed-table-container\">\n <table class=\"table\">\n <thead>\n <tr>\n <th></th>\n <th><div class=\"th-inner\">%s</div></th>\n <th><div class=\"th-inner\">%s</div></th>\n </tr>\n </thead>\n <tbody></tbody>\n </table>\n </div>\n </div>\n </div>\n <div class=\"modal-footer\">\n <button type=\"button\" class=\"btn btn-secondary\" data-dismiss=\"modal\">%s</button>\n <button type=\"button\" class=\"btn btn-primary multi-sort-order-button\">%s</button>\n </div>\n </div>\n </div>\n </div>\n ",
  64. multipleSortButton: '<button class="multi-sort %s" type="button" data-toggle="modal" data-target="#%s" title="%s">%s</button>',
  65. multipleSortSelect: '<select class="%s %s form-control">'
  66. }
  67. },
  68. bootstrap5: {
  69. html: {
  70. multipleSortModal: "\n <div class=\"modal fade\" id=\"%s\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"%sLabel\" aria-hidden=\"true\">\n <div class=\"modal-dialog\" role=\"document\">\n <div class=\"modal-content\">\n <div class=\"modal-header\">\n <h5 class=\"modal-title\" id=\"%sLabel\">%s</h5>\n <button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"modal\" aria-label=\"Close\"></button>\n </div>\n <div class=\"modal-body\">\n <div class=\"bootstrap-table\">\n <div class=\"fixed-table-toolbar\">\n <div class=\"bars pb-3\">\n <button type=\"button\" class=\"toolbar-btn-add btn btn-secondary\">%s %s</button>\n <button type=\"button\" class=\"toolbar-btn-delete btn btn-secondary\" disabled>%s %s</button>\n </div>\n </div>\n <div class=\"fixed-table-container\">\n <table class=\"table\">\n <thead>\n <tr>\n <th></th>\n <th><div class=\"th-inner\">%s</div></th>\n <th><div class=\"th-inner\">%s</div></th>\n </tr>\n </thead>\n <tbody></tbody>\n </table>\n </div>\n </div>\n </div>\n <div class=\"modal-footer\">\n <button type=\"button\" class=\"btn btn-secondary\" data-bs-dismiss=\"modal\">%s</button>\n <button type=\"button\" class=\"btn btn-primary multi-sort-order-button\">%s</button>\n </div>\n </div>\n </div>\n </div>\n ",
  71. multipleSortButton: '<button class="multi-sort %s" type="button" data-bs-toggle="modal" data-bs-target="#%s" title="%s">%s</button>',
  72. multipleSortSelect: '<select class="%s %s form-control">'
  73. }
  74. },
  75. semantic: {
  76. html: {
  77. multipleSortModal: "\n <div class=\"ui modal tiny\" id=\"%s\" aria-labelledby=\"%sLabel\" aria-hidden=\"true\">\n <i class=\"close icon\"></i>\n <div class=\"header\" id=\"%sLabel\">\n %s\n </div>\n <div class=\"image content\">\n <div class=\"bootstrap-table\">\n <div class=\"fixed-table-toolbar\">\n <div class=\"bars pb-3\">\n <button type=\"button\" class=\"toolbar-btn-add ui button\">%s %s</button>\n <button type=\"button\" class=\"toolbar-btn-delete ui button\" disabled>%s %s</button>\n </div>\n </div>\n <div class=\"fixed-table-container\">\n <table class=\"table\">\n <thead>\n <tr>\n <th></th>\n <th><div class=\"th-inner\">%s</div></th>\n <th><div class=\"th-inner\">%s</div></th>\n </tr>\n </thead>\n <tbody></tbody>\n </table>\n </div>\n </div>\n </div>\n <div class=\"actions\">\n <div class=\"ui button deny\">%s</div>\n <div class=\"ui button approve multi-sort-order-button\">%s</div>\n </div>\n </div>\n ",
  78. multipleSortButton: '<button class="multi-sort %s" type="button" data-toggle="modal" data-target="#%s" title="%s">%s</button>',
  79. multipleSortSelect: '<select class="%s %s">'
  80. }
  81. },
  82. materialize: {
  83. html: {
  84. multipleSortModal: "\n <div id=\"%s\" class=\"modal\" aria-labelledby=\"%sLabel\" aria-hidden=\"true\">\n <div class=\"modal-content\" id=\"%sLabel\">\n <h4>%s</h4>\n <div class=\"bootstrap-table\">\n <div class=\"fixed-table-toolbar\">\n <div class=\"bars pb-3\">\n <button type=\"button\" class=\"toolbar-btn-add waves-effect waves-light btn\">%s %s</button>\n <button type=\"button\" class=\"toolbar-btn-delete waves-effect waves-light btn\" disabled>%s %s</button>\n </div>\n </div>\n <div class=\"fixed-table-container\">\n <table class=\"table\">\n <thead>\n <tr>\n <th></th>\n <th><div class=\"th-inner\">%s</div></th>\n <th><div class=\"th-inner\">%s</div></th>\n </tr>\n </thead>\n <tbody></tbody>\n </table>\n </div>\n </div>\n <div class=\"modal-footer\">\n <a href=\"javascript:void(0)\" class=\"modal-close waves-effect waves-light btn\">%s</a>\n <a href=\"javascript:void(0)\" class=\"modal-close waves-effect waves-light btn multi-sort-order-button\">%s</a>\n </div>\n </div>\n </div>\n ",
  85. multipleSortButton: '<a class="multi-sort %s modal-trigger" href="#%s" type="button" data-toggle="modal" title="%s">%s</a>',
  86. multipleSortSelect: '<select class="%s %s browser-default">'
  87. }
  88. },
  89. foundation: {
  90. html: {
  91. multipleSortModal: "\n <div class=\"reveal\" id=\"%s\" data-reveal aria-labelledby=\"%sLabel\" aria-hidden=\"true\">\n <div id=\"%sLabel\">\n <h1>%s</h1>\n <div class=\"bootstrap-table\">\n <div class=\"fixed-table-toolbar\">\n <div class=\"bars padding-bottom-2\">\n <button type=\"button\" class=\"toolbar-btn-add waves-effect waves-light button\">%s %s</button>\n <button type=\"button\" class=\"toolbar-btn-delete waves-effect waves-light button\" disabled>%s %s</button>\n </div>\n </div>\n <div class=\"fixed-table-container\">\n <table class=\"table\">\n <thead>\n <tr>\n <th></th>\n <th><div class=\"th-inner\">%s</div></th>\n <th><div class=\"th-inner\">%s</div></th>\n </tr>\n </thead>\n <tbody></tbody>\n </table>\n </div>\n </div>\n\n <button class=\"waves-effect waves-light button\" data-close aria-label=\"Close modal\" type=\"button\">\n <span aria-hidden=\"true\">%s</span>\n </button>\n <button class=\"waves-effect waves-light button multi-sort-order-button\" data-close aria-label=\"Order\" type=\"button\">\n <span aria-hidden=\"true\">%s</span>\n </button>\n </div>\n </div>\n ",
  92. multipleSortButton: '<button class="multi-sort %s" data-open="%s" title="%s">%s</button>',
  93. multipleSortSelect: '<select class="%s %s browser-default">'
  94. }
  95. },
  96. bulma: {
  97. html: {
  98. multipleSortModal: "\n <div class=\"modal\" id=\"%s\" aria-labelledby=\"%sLabel\" aria-hidden=\"true\">\n <div class=\"modal-background\"></div>\n <div class=\"modal-content\" id=\"%sLabel\">\n <div class=\"box\">\n <h2>%s</h2>\n <div class=\"bootstrap-table\">\n <div class=\"fixed-table-toolbar\">\n <div class=\"bars padding-bottom-2\">\n <button type=\"button\" class=\"toolbar-btn-add waves-effect waves-light button\">%s %s</button>\n <button type=\"button\" class=\"toolbar-btn-delete waves-effect waves-light button\" disabled>%s %s</button>\n </div>\n </div>\n <div class=\"fixed-table-container\">\n <table class=\"table\">\n <thead>\n <tr>\n <th></th>\n <th><div class=\"th-inner\">%s</div></th>\n <th><div class=\"th-inner\">%s</div></th>\n </tr>\n </thead>\n <tbody></tbody>\n </table>\n </div>\n </div>\n <button type=\"button\" class=\"waves-effect waves-light button\" data-close>%s</button>\n <button type=\"button\" class=\"waves-effect waves-light button multi-sort-order-button\" data-close>%s</button>\n </div>\n </div>\n </div>\n ",
  99. multipleSortButton: '<button class="multi-sort %s" data-target="%s" title="%s">%s</button>',
  100. multipleSortSelect: '<select class="%s %s browser-default">'
  101. }
  102. },
  103. 'bootstrap-table': {
  104. html: {
  105. multipleSortModal: "\n <div class=\"modal\" id=\"%s\" aria-labelledby=\"%sLabel\" aria-hidden=\"true\">\n <div class=\"modal-background\"></div>\n <div class=\"modal-content\" id=\"%sLabel\">\n <div class=\"box\">\n <h2>%s</h2>\n <div class=\"bootstrap-table\">\n <div class=\"fixed-table-toolbar\">\n <div class=\"bars padding-bottom-2\">\n <button type=\"button\" class=\"toolbar-btn-add btn\">%s %s</button>\n <button type=\"button\" class=\"toolbar-btn-delete btn\" disabled>%s %s</button>\n </div>\n </div>\n <div class=\"fixed-table-container\">\n <table class=\"table\">\n <thead>\n <tr>\n <th></th>\n <th><div class=\"th-inner\">%s</div></th>\n <th><div class=\"th-inner\">%s</div></th>\n </tr>\n </thead>\n <tbody></tbody>\n </table>\n </div>\n </div>\n <div class=\"mt-30\">\n <button type=\"button\" class=\"btn\" data-close>%s</button>\n <button type=\"button\" class=\"btn multi-sort-order-button\" data-close>%s</button>\n </div>\n </div>\n </div>\n </div>\n ",
  106. multipleSortButton: '<button class="multi-sort %s" data-target="%s" title="%s">%s</button>',
  107. multipleSortSelect: '<select class="%s %s browser-default">'
  108. }
  109. }
  110. }[$.fn.bootstrapTable.theme];
  111. var showSortModal = function showSortModal(that) {
  112. var _selector = that.sortModalSelector;
  113. var _id = "#".concat(_selector);
  114. var o = that.options;
  115. if (!$(_id).hasClass('modal')) {
  116. var sModal = Utils.sprintf(theme.html.multipleSortModal, _selector, _selector, _selector, that.options.formatMultipleSort(), Utils.sprintf(that.constants.html.icon, o.iconsPrefix, o.icons.plus), that.options.formatAddLevel(), Utils.sprintf(that.constants.html.icon, o.iconsPrefix, o.icons.minus), that.options.formatDeleteLevel(), that.options.formatColumn(), that.options.formatOrder(), that.options.formatCancel(), that.options.formatSort());
  117. $('body').append($(sModal));
  118. that.$sortModal = $(_id);
  119. var $rows = that.$sortModal.find('tbody > tr');
  120. that.$sortModal.off('click', '.toolbar-btn-add').on('click', '.toolbar-btn-add', function () {
  121. var total = that.$sortModal.find('.multi-sort-name:first option').length;
  122. var current = that.$sortModal.find('tbody tr').length;
  123. if (current < total) {
  124. current++;
  125. that.addLevel();
  126. that.setButtonStates();
  127. }
  128. });
  129. that.$sortModal.off('click', '.toolbar-btn-delete').on('click', '.toolbar-btn-delete', function () {
  130. var total = that.$sortModal.find('.multi-sort-name:first option').length;
  131. var current = that.$sortModal.find('tbody tr').length;
  132. if (current > 1 && current <= total) {
  133. current--;
  134. that.$sortModal.find('tbody tr:last').remove();
  135. that.setButtonStates();
  136. }
  137. });
  138. that.$sortModal.off('click', '.multi-sort-order-button').on('click', '.multi-sort-order-button', function () {
  139. var $rows = that.$sortModal.find('tbody > tr');
  140. var $alert = that.$sortModal.find('div.alert');
  141. var fields = [];
  142. var results = [];
  143. var sortPriority = $.map($rows, function (row) {
  144. var $row = $(row);
  145. var name = $row.find('.multi-sort-name').val();
  146. var order = $row.find('.multi-sort-order').val();
  147. fields.push(name);
  148. return {
  149. sortName: name,
  150. sortOrder: order
  151. };
  152. });
  153. var sorted_fields = fields.sort();
  154. for (var i = 0; i < fields.length - 1; i++) {
  155. if (sorted_fields[i + 1] === sorted_fields[i]) {
  156. results.push(sorted_fields[i]);
  157. }
  158. }
  159. if (results.length > 0) {
  160. if ($alert.length === 0) {
  161. $alert = "<div class=\"alert alert-danger\" role=\"alert\"><strong>".concat(that.options.formatDuplicateAlertTitle(), "</strong> ").concat(that.options.formatDuplicateAlertDescription(), "</div>");
  162. $($alert).insertBefore(that.$sortModal.find('.bars'));
  163. }
  164. } else {
  165. if ($alert.length === 1) {
  166. $($alert).remove();
  167. }
  168. if (['bootstrap3', 'bootstrap4', 'bootstrap5'].includes($.fn.bootstrapTable.theme)) {
  169. that.$sortModal.modal('hide');
  170. }
  171. that.multiSort(sortPriority);
  172. }
  173. });
  174. if (that.options.sortPriority === null || that.options.sortPriority.length === 0) {
  175. if (that.options.sortName) {
  176. that.options.sortPriority = [{
  177. sortName: that.options.sortName,
  178. sortOrder: that.options.sortOrder
  179. }];
  180. }
  181. }
  182. if (that.options.sortPriority !== null && that.options.sortPriority.length > 0) {
  183. if ($rows.length < that.options.sortPriority.length && _typeof(that.options.sortPriority) === 'object') {
  184. for (var i = 0; i < that.options.sortPriority.length; i++) {
  185. that.addLevel(i, that.options.sortPriority[i]);
  186. }
  187. }
  188. } else {
  189. that.addLevel(0);
  190. }
  191. that.setButtonStates();
  192. }
  193. };
  194. $.fn.bootstrapTable.methods.push('multipleSort');
  195. $.fn.bootstrapTable.methods.push('multiSort');
  196. Object.assign($.fn.bootstrapTable.defaults, {
  197. showMultiSort: false,
  198. showMultiSortButton: true,
  199. multiSortStrictSort: false,
  200. sortPriority: null,
  201. onMultipleSort: function onMultipleSort() {
  202. return false;
  203. }
  204. });
  205. Object.assign($.fn.bootstrapTable.events, {
  206. 'multiple-sort.bs.table': 'onMultipleSort'
  207. });
  208. Object.assign($.fn.bootstrapTable.locales, {
  209. formatMultipleSort: function formatMultipleSort() {
  210. return 'Multiple Sort';
  211. },
  212. formatAddLevel: function formatAddLevel() {
  213. return 'Add Level';
  214. },
  215. formatDeleteLevel: function formatDeleteLevel() {
  216. return 'Delete Level';
  217. },
  218. formatColumn: function formatColumn() {
  219. return 'Column';
  220. },
  221. formatOrder: function formatOrder() {
  222. return 'Order';
  223. },
  224. formatSortBy: function formatSortBy() {
  225. return 'Sort by';
  226. },
  227. formatThenBy: function formatThenBy() {
  228. return 'Then by';
  229. },
  230. formatSort: function formatSort() {
  231. return 'Sort';
  232. },
  233. formatCancel: function formatCancel() {
  234. return 'Cancel';
  235. },
  236. formatDuplicateAlertTitle: function formatDuplicateAlertTitle() {
  237. return 'Duplicate(s) detected!';
  238. },
  239. formatDuplicateAlertDescription: function formatDuplicateAlertDescription() {
  240. return 'Please remove or change any duplicate column.';
  241. },
  242. formatSortOrders: function formatSortOrders() {
  243. return {
  244. asc: 'Ascending',
  245. desc: 'Descending'
  246. };
  247. }
  248. });
  249. Object.assign($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  250. var BootstrapTable = $.fn.bootstrapTable.Constructor;
  251. var _initToolbar = BootstrapTable.prototype.initToolbar;
  252. var _destroy = BootstrapTable.prototype.destroy;
  253. BootstrapTable.prototype.initToolbar = function () {
  254. var _this = this;
  255. this.showToolbar = this.showToolbar || this.options.showMultiSort;
  256. var that = this;
  257. var sortModalSelector = Utils.getEventName('sort-modal', this.$el.attr('id'));
  258. var sortModalId = "#".concat(sortModalSelector);
  259. var $multiSortBtn = this.$toolbar.find('div.multi-sort');
  260. var o = this.options;
  261. this.$sortModal = $(sortModalId);
  262. this.sortModalSelector = sortModalSelector;
  263. if (that.options.sortPriority !== null) {
  264. that.onMultipleSort();
  265. }
  266. if (this.options.showMultiSort && this.options.showMultiSortButton) {
  267. this.buttons = Object.assign(this.buttons, {
  268. multipleSort: {
  269. html: Utils.sprintf(theme.html.multipleSortButton, that.constants.buttonsClass, that.sortModalSelector, this.options.formatMultipleSort(), Utils.sprintf(that.constants.html.icon, o.iconsPrefix, o.icons.sort))
  270. }
  271. });
  272. }
  273. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  274. args[_key] = arguments[_key];
  275. }
  276. _initToolbar.apply(this, Array.prototype.slice.apply(args));
  277. if (that.options.sidePagination === 'server' && !isSingleSort && that.options.sortPriority !== null) {
  278. var t = that.options.queryParams;
  279. that.options.queryParams = function (params) {
  280. params.multiSort = that.options.sortPriority;
  281. return t(params);
  282. };
  283. }
  284. if (this.options.showMultiSort) {
  285. if (!$multiSortBtn.length && this.options.showMultiSortButton) {
  286. if ($.fn.bootstrapTable.theme === 'semantic') {
  287. this.$toolbar.find('.multi-sort').on('click', function () {
  288. $(sortModalId).modal('show');
  289. });
  290. } else if ($.fn.bootstrapTable.theme === 'materialize') {
  291. this.$toolbar.find('.multi-sort').on('click', function () {
  292. $(sortModalId).modal();
  293. });
  294. } else if ($.fn.bootstrapTable.theme === 'bootstrap-table') {
  295. this.$toolbar.find('.multi-sort').on('click', function () {
  296. $(sortModalId).addClass('show');
  297. });
  298. } else if ($.fn.bootstrapTable.theme === 'foundation') {
  299. this.$toolbar.find('.multi-sort').on('click', function () {
  300. if (!_this.foundationModal) {
  301. // eslint-disable-next-line no-undef
  302. _this.foundationModal = new Foundation.Reveal($(sortModalId));
  303. }
  304. _this.foundationModal.open();
  305. });
  306. } else if ($.fn.bootstrapTable.theme === 'bulma') {
  307. this.$toolbar.find('.multi-sort').on('click', function () {
  308. $('html').toggleClass('is-clipped');
  309. $(sortModalId).toggleClass('is-active');
  310. $('button[data-close]').one('click', function () {
  311. $('html').toggleClass('is-clipped');
  312. $(sortModalId).toggleClass('is-active');
  313. });
  314. });
  315. }
  316. showSortModal(that);
  317. }
  318. this.$el.on('sort.bs.table', function () {
  319. isSingleSort = true;
  320. });
  321. this.$el.on('multiple-sort.bs.table', function () {
  322. isSingleSort = false;
  323. });
  324. this.$el.on('load-success.bs.table', function () {
  325. if (!isSingleSort && that.options.sortPriority !== null && _typeof(that.options.sortPriority) === 'object' && that.options.sidePagination !== 'server') {
  326. that.onMultipleSort();
  327. }
  328. });
  329. this.$el.on('column-switch.bs.table', function (field, checked) {
  330. if (that.options.sortPriority !== null && that.options.sortPriority.length > 0) {
  331. for (var i = 0; i < that.options.sortPriority.length; i++) {
  332. if (that.options.sortPriority[i].sortName === checked) {
  333. that.options.sortPriority.splice(i, 1);
  334. }
  335. }
  336. that.assignSortableArrows();
  337. }
  338. that.$sortModal.remove();
  339. showSortModal(that);
  340. });
  341. this.$el.on('reset-view.bs.table', function () {
  342. if (!isSingleSort && that.options.sortPriority !== null && _typeof(that.options.sortPriority) === 'object') {
  343. that.assignSortableArrows();
  344. }
  345. });
  346. }
  347. };
  348. BootstrapTable.prototype.destroy = function () {
  349. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  350. args[_key2] = arguments[_key2];
  351. }
  352. _destroy.apply(this, Array.prototype.slice.apply(args));
  353. if (this.options.showMultiSort) {
  354. this.enableCustomSort = false;
  355. this.$sortModal.remove();
  356. }
  357. };
  358. BootstrapTable.prototype.multipleSort = function () {
  359. var that = this;
  360. if (!isSingleSort && that.options.sortPriority !== null && _typeof(that.options.sortPriority) === 'object' && that.options.sidePagination !== 'server') {
  361. that.onMultipleSort();
  362. }
  363. };
  364. BootstrapTable.prototype.onMultipleSort = function () {
  365. var that = this;
  366. var cmp = function cmp(x, y) {
  367. return x > y ? 1 : x < y ? -1 : 0;
  368. };
  369. var arrayCmp = function arrayCmp(a, b) {
  370. var arr1 = [];
  371. var arr2 = [];
  372. for (var i = 0; i < that.options.sortPriority.length; i++) {
  373. var fieldName = that.options.sortPriority[i].sortName;
  374. var fieldIndex = that.header.fields.indexOf(fieldName);
  375. var sorterName = that.header.sorters[that.header.fields.indexOf(fieldName)];
  376. if (that.header.sortNames[fieldIndex]) {
  377. fieldName = that.header.sortNames[fieldIndex];
  378. }
  379. var order = that.options.sortPriority[i].sortOrder === 'desc' ? -1 : 1;
  380. var aa = Utils.getItemField(a, fieldName);
  381. var bb = Utils.getItemField(b, fieldName);
  382. var value1 = $.fn.bootstrapTable.utils.calculateObjectValue(that.header, sorterName, [aa, bb, a, b]);
  383. var value2 = $.fn.bootstrapTable.utils.calculateObjectValue(that.header, sorterName, [bb, aa, b, a]);
  384. if (value1 !== undefined && value2 !== undefined) {
  385. arr1.push(order * value1);
  386. arr2.push(order * value2);
  387. continue;
  388. }
  389. if (aa === undefined || aa === null) aa = '';
  390. if (bb === undefined || bb === null) bb = '';
  391. if ($.isNumeric(aa) && $.isNumeric(bb)) {
  392. aa = parseFloat(aa);
  393. bb = parseFloat(bb);
  394. } else {
  395. aa = aa.toString();
  396. bb = bb.toString();
  397. if (that.options.multiSortStrictSort) {
  398. aa = aa.toLowerCase();
  399. bb = bb.toLowerCase();
  400. }
  401. }
  402. arr1.push(order * cmp(aa, bb));
  403. arr2.push(order * cmp(bb, aa));
  404. }
  405. return cmp(arr1, arr2);
  406. };
  407. this.enableCustomSort = true;
  408. this.data.sort(function (a, b) {
  409. return arrayCmp(a, b);
  410. });
  411. this.initBody();
  412. this.assignSortableArrows();
  413. this.trigger('multiple-sort');
  414. };
  415. BootstrapTable.prototype.addLevel = function (index, sortPriority) {
  416. var text = index === 0 ? this.options.formatSortBy() : this.options.formatThenBy();
  417. this.$sortModal.find('tbody').append($('<tr>').append($('<td>').text(text)).append($('<td>').append($(Utils.sprintf(theme.html.multipleSortSelect, this.constants.classes.paginationDropdown, 'multi-sort-name')))).append($('<td>').append($(Utils.sprintf(theme.html.multipleSortSelect, this.constants.classes.paginationDropdown, 'multi-sort-order')))));
  418. var $multiSortName = this.$sortModal.find('.multi-sort-name').last();
  419. var $multiSortOrder = this.$sortModal.find('.multi-sort-order').last();
  420. $.each(this.columns, function (i, column) {
  421. if (column.sortable === false || column.visible === false) {
  422. return true;
  423. }
  424. $multiSortName.append("<option value=\"".concat(column.field, "\">").concat(column.title, "</option>"));
  425. });
  426. $.each(this.options.formatSortOrders(), function (value, order) {
  427. $multiSortOrder.append("<option value=\"".concat(value, "\">").concat(order, "</option>"));
  428. });
  429. if (sortPriority !== undefined) {
  430. $multiSortName.find("option[value=\"".concat(sortPriority.sortName, "\"]")).attr('selected', true);
  431. $multiSortOrder.find("option[value=\"".concat(sortPriority.sortOrder, "\"]")).attr('selected', true);
  432. }
  433. };
  434. BootstrapTable.prototype.assignSortableArrows = function () {
  435. var that = this;
  436. var headers = that.$header.find('th');
  437. for (var i = 0; i < headers.length; i++) {
  438. for (var c = 0; c < that.options.sortPriority.length; c++) {
  439. if ($(headers[i]).data('field') === that.options.sortPriority[c].sortName) {
  440. $(headers[i]).find('.sortable').removeClass('desc asc').addClass(that.options.sortPriority[c].sortOrder);
  441. }
  442. }
  443. }
  444. };
  445. BootstrapTable.prototype.setButtonStates = function () {
  446. var total = this.$sortModal.find('.multi-sort-name:first option').length;
  447. var current = this.$sortModal.find('tbody tr').length;
  448. if (current === total) {
  449. this.$sortModal.find('.toolbar-btn-add').attr('disabled', 'disabled');
  450. }
  451. if (current > 1) {
  452. this.$sortModal.find('.toolbar-btn-delete').removeAttr('disabled');
  453. }
  454. if (current < total) {
  455. this.$sortModal.find('.toolbar-btn-add').removeAttr('disabled');
  456. }
  457. if (current === 1) {
  458. this.$sortModal.find('.toolbar-btn-delete').attr('disabled', 'disabled');
  459. }
  460. };
  461. BootstrapTable.prototype.multiSort = function (sortPriority) {
  462. var _this2 = this;
  463. this.options.sortPriority = sortPriority;
  464. this.options.sortName = undefined;
  465. if (this.options.sidePagination === 'server') {
  466. var queryParams = this.options.queryParams;
  467. this.options.queryParams = function (params) {
  468. params.multiSort = _this2.options.sortPriority;
  469. return $.fn.bootstrapTable.utils.calculateObjectValue(_this2.options, queryParams, [params]);
  470. };
  471. isSingleSort = false;
  472. this.initServer(this.options.silentSort);
  473. }
  474. this.onMultipleSort();
  475. };
  476. }));