bootstrap-table-reorder-columns.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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.filter.js'), require('core-js/modules/es.array.find.js'), require('core-js/modules/es.object.assign.js'), require('core-js/modules/es.object.entries.js'), require('core-js/modules/es.object.to-string.js'), require('core-js/modules/es.parse-int.js'), require('jquery')) :
  3. typeof define === 'function' && define.amd ? define(['core-js/modules/es.array.concat.js', 'core-js/modules/es.array.filter.js', 'core-js/modules/es.array.find.js', 'core-js/modules/es.object.assign.js', 'core-js/modules/es.object.entries.js', 'core-js/modules/es.object.to-string.js', 'core-js/modules/es.parse-int.js', 'jquery'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(null, null, null, null, null, null, null, global.jQuery));
  5. })(this, (function (es_array_concat_js, es_array_filter_js, es_array_find_js, es_object_assign_js, es_object_entries_js, es_object_toString_js, es_parseInt_js, $) { 'use strict';
  6. function _arrayLikeToArray(r, a) {
  7. (null == a || a > r.length) && (a = r.length);
  8. for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
  9. return n;
  10. }
  11. function _arrayWithHoles(r) {
  12. if (Array.isArray(r)) return r;
  13. }
  14. function _assertThisInitialized(e) {
  15. if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  16. return e;
  17. }
  18. function _callSuper(t, o, e) {
  19. return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e));
  20. }
  21. function _classCallCheck(a, n) {
  22. if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
  23. }
  24. function _defineProperties(e, r) {
  25. for (var t = 0; t < r.length; t++) {
  26. var o = r[t];
  27. o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
  28. }
  29. }
  30. function _createClass(e, r, t) {
  31. return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
  32. writable: !1
  33. }), e;
  34. }
  35. function _get() {
  36. return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) {
  37. var p = _superPropBase(e, t);
  38. if (p) {
  39. var n = Object.getOwnPropertyDescriptor(p, t);
  40. return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value;
  41. }
  42. }, _get.apply(null, arguments);
  43. }
  44. function _getPrototypeOf(t) {
  45. return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) {
  46. return t.__proto__ || Object.getPrototypeOf(t);
  47. }, _getPrototypeOf(t);
  48. }
  49. function _inherits(t, e) {
  50. if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function");
  51. t.prototype = Object.create(e && e.prototype, {
  52. constructor: {
  53. value: t,
  54. writable: !0,
  55. configurable: !0
  56. }
  57. }), Object.defineProperty(t, "prototype", {
  58. writable: !1
  59. }), e && _setPrototypeOf(t, e);
  60. }
  61. function _isNativeReflectConstruct() {
  62. try {
  63. var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
  64. } catch (t) {}
  65. return (_isNativeReflectConstruct = function () {
  66. return !!t;
  67. })();
  68. }
  69. function _iterableToArrayLimit(r, l) {
  70. var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
  71. if (null != t) {
  72. var e,
  73. n,
  74. i,
  75. u,
  76. a = [],
  77. f = !0,
  78. o = !1;
  79. try {
  80. if (i = (t = t.call(r)).next, 0 === l) ; else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
  81. } catch (r) {
  82. o = !0, n = r;
  83. } finally {
  84. try {
  85. if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
  86. } finally {
  87. if (o) throw n;
  88. }
  89. }
  90. return a;
  91. }
  92. }
  93. function _nonIterableRest() {
  94. throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  95. }
  96. function _possibleConstructorReturn(t, e) {
  97. if (e && ("object" == typeof e || "function" == typeof e)) return e;
  98. if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined");
  99. return _assertThisInitialized(t);
  100. }
  101. function _setPrototypeOf(t, e) {
  102. return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
  103. return t.__proto__ = e, t;
  104. }, _setPrototypeOf(t, e);
  105. }
  106. function _slicedToArray(r, e) {
  107. return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
  108. }
  109. function _superPropBase(t, o) {
  110. for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t)););
  111. return t;
  112. }
  113. function _toPrimitive(t, r) {
  114. if ("object" != typeof t || !t) return t;
  115. var e = t[Symbol.toPrimitive];
  116. if (void 0 !== e) {
  117. var i = e.call(t, r );
  118. if ("object" != typeof i) return i;
  119. throw new TypeError("@@toPrimitive must return a primitive value.");
  120. }
  121. return (String )(t);
  122. }
  123. function _toPropertyKey(t) {
  124. var i = _toPrimitive(t, "string");
  125. return "symbol" == typeof i ? i : i + "";
  126. }
  127. function _unsupportedIterableToArray(r, a) {
  128. if (r) {
  129. if ("string" == typeof r) return _arrayLikeToArray(r, a);
  130. var t = {}.toString.call(r).slice(8, -1);
  131. return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
  132. }
  133. }
  134. /**
  135. * @author: Dennis Hernández
  136. * @update: https://github.com/wenzhixin
  137. * @version: v1.2.0
  138. */
  139. $.akottr.dragtable.prototype._restoreState = function (persistObj) {
  140. var i = 0;
  141. for (var _i = 0, _Object$entries = Object.entries(persistObj); _i < _Object$entries.length; _i++) {
  142. var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
  143. field = _Object$entries$_i[0],
  144. value = _Object$entries$_i[1];
  145. var $th = this.originalTable.el.find("th[data-field=\"".concat(field, "\"]"));
  146. if (!$th.length) {
  147. i++;
  148. continue;
  149. }
  150. this.originalTable.startIndex = $th.prevAll().length + 1;
  151. this.originalTable.endIndex = parseInt(value, 10) + 1 - i;
  152. this._bubbleCols();
  153. }
  154. };
  155. // From MDN site, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
  156. var filterFn = function filterFn() {
  157. if (!Array.prototype.filter) {
  158. Array.prototype.filter = function (fun /* , thisArg*/) {
  159. if (this === undefined || this === null) {
  160. throw new TypeError();
  161. }
  162. var t = Object(this);
  163. var len = t.length >>> 0;
  164. if (typeof fun !== 'function') {
  165. throw new TypeError();
  166. }
  167. var res = [];
  168. var thisArg = arguments.length >= 2 ? arguments[1] : undefined;
  169. for (var i = 0; i < len; i++) {
  170. if (i in t) {
  171. var val = t[i];
  172. // NOTE: Technically this should Object.defineProperty at
  173. // the next index, as push can be affected by
  174. // properties on Object.prototype and Array.prototype.
  175. // But this method's new, and collisions should be
  176. // rare, so use the more-compatible alternative.
  177. if (fun.call(thisArg, val, i, t)) {
  178. res.push(val);
  179. }
  180. }
  181. }
  182. return res;
  183. };
  184. }
  185. };
  186. Object.assign($.fn.bootstrapTable.defaults, {
  187. reorderableColumns: false,
  188. maxMovingRows: 10,
  189. // eslint-disable-next-line no-unused-vars
  190. onReorderColumn: function onReorderColumn(headerFields) {
  191. return false;
  192. },
  193. dragaccept: null
  194. });
  195. Object.assign($.fn.bootstrapTable.events, {
  196. 'reorder-column.bs.table': 'onReorderColumn'
  197. });
  198. $.fn.bootstrapTable.methods.push('orderColumns');
  199. $.BootstrapTable = /*#__PURE__*/function (_$$BootstrapTable) {
  200. function _class() {
  201. _classCallCheck(this, _class);
  202. return _callSuper(this, _class, arguments);
  203. }
  204. _inherits(_class, _$$BootstrapTable);
  205. return _createClass(_class, [{
  206. key: "initHeader",
  207. value: function initHeader() {
  208. var _get2;
  209. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  210. args[_key] = arguments[_key];
  211. }
  212. (_get2 = _get(_getPrototypeOf(_class.prototype), "initHeader", this)).call.apply(_get2, [this].concat(args));
  213. if (!this.options.reorderableColumns) {
  214. return;
  215. }
  216. this.makeColumnsReorderable();
  217. }
  218. }, {
  219. key: "_toggleColumn",
  220. value: function _toggleColumn() {
  221. var _get3;
  222. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  223. args[_key2] = arguments[_key2];
  224. }
  225. (_get3 = _get(_getPrototypeOf(_class.prototype), "_toggleColumn", this)).call.apply(_get3, [this].concat(args));
  226. if (!this.options.reorderableColumns) {
  227. return;
  228. }
  229. this.makeColumnsReorderable();
  230. }
  231. }, {
  232. key: "toggleView",
  233. value: function toggleView() {
  234. var _get4;
  235. for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  236. args[_key3] = arguments[_key3];
  237. }
  238. (_get4 = _get(_getPrototypeOf(_class.prototype), "toggleView", this)).call.apply(_get4, [this].concat(args));
  239. if (!this.options.reorderableColumns) {
  240. return;
  241. }
  242. if (this.options.cardView) {
  243. return;
  244. }
  245. this.makeColumnsReorderable();
  246. }
  247. }, {
  248. key: "resetView",
  249. value: function resetView() {
  250. var _get5;
  251. for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
  252. args[_key4] = arguments[_key4];
  253. }
  254. (_get5 = _get(_getPrototypeOf(_class.prototype), "resetView", this)).call.apply(_get5, [this].concat(args));
  255. if (!this.options.reorderableColumns) {
  256. return;
  257. }
  258. this.makeColumnsReorderable();
  259. }
  260. }, {
  261. key: "makeColumnsReorderable",
  262. value: function makeColumnsReorderable() {
  263. var _this = this;
  264. var order = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  265. try {
  266. $(this.$el).dragtable('destroy');
  267. } catch (e) {
  268. // do nothing
  269. }
  270. $(this.$el).dragtable({
  271. maxMovingRows: this.options.maxMovingRows,
  272. dragaccept: this.options.dragaccept,
  273. clickDelay: 200,
  274. dragHandle: '.th-inner',
  275. restoreState: order ? order : this.columnsSortOrder,
  276. beforeStop: function beforeStop(table) {
  277. var sortOrder = {};
  278. table.el.find('th').each(function (i, el) {
  279. sortOrder[$(el).data('field')] = i;
  280. });
  281. _this.columnsSortOrder = sortOrder;
  282. if (_this.options.cookie) {
  283. _this.persistReorderColumnsState(_this);
  284. }
  285. var ths = [];
  286. var formatters = [];
  287. var columns = [];
  288. var columnsHidden = [];
  289. var columnIndex = -1;
  290. var optionsColumns = [];
  291. _this.$header.find('th:not(.detail)').each(function (i, el) {
  292. ths.push($(el).data('field'));
  293. formatters.push($(el).data('formatter'));
  294. });
  295. // Exist columns not shown
  296. if (ths.length < _this.columns.length) {
  297. columnsHidden = _this.columns.filter(function (column) {
  298. return !column.visible;
  299. });
  300. for (var i = 0; i < columnsHidden.length; i++) {
  301. ths.push(columnsHidden[i].field);
  302. formatters.push(columnsHidden[i].formatter);
  303. }
  304. }
  305. for (var _i2 = 0; _i2 < ths.length; _i2++) {
  306. columnIndex = _this.fieldsColumnsIndex[ths[_i2]];
  307. if (columnIndex !== -1) {
  308. _this.fieldsColumnsIndex[ths[_i2]] = _i2;
  309. _this.columns[columnIndex].fieldIndex = _i2;
  310. columns.push(_this.columns[columnIndex]);
  311. }
  312. }
  313. _this.columns = columns;
  314. filterFn(); // Support <IE9
  315. $.each(_this.columns, function (i, column) {
  316. var found = false;
  317. var field = column.field;
  318. _this.options.columns[0].filter(function (item) {
  319. if (!found && item['field'] === field) {
  320. optionsColumns.push(item);
  321. found = true;
  322. return false;
  323. }
  324. return true;
  325. });
  326. });
  327. _this.options.columns[0] = optionsColumns;
  328. _this.header.fields = ths;
  329. _this.header.formatters = formatters;
  330. _this.initHeader();
  331. _this.initToolbar();
  332. _this.initSearchText();
  333. _this.initBody();
  334. _this.resetView();
  335. _this.trigger('reorder-column', ths);
  336. }
  337. });
  338. }
  339. }, {
  340. key: "orderColumns",
  341. value: function orderColumns(order) {
  342. this.columnsSortOrder = order;
  343. this.makeColumnsReorderable();
  344. }
  345. }]);
  346. }($.BootstrapTable);
  347. }));