bootstrap-table-reorder-rows.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.index-of.js'), require('core-js/modules/es.array.splice.js'), require('core-js/modules/es.object.assign.js'), require('jquery')) :
  3. typeof define === 'function' && define.amd ? define(['core-js/modules/es.array.concat.js', 'core-js/modules/es.array.index-of.js', 'core-js/modules/es.array.splice.js', 'core-js/modules/es.object.assign.js', 'jquery'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(null, null, null, null, global.jQuery));
  5. })(this, (function (es_array_concat_js, es_array_indexOf_js, es_array_splice_js, es_object_assign_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 _arrayWithoutHoles(r) {
  12. if (Array.isArray(r)) return _arrayLikeToArray(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 _iterableToArray(r) {
  70. if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
  71. }
  72. function _nonIterableSpread() {
  73. throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  74. }
  75. function _possibleConstructorReturn(t, e) {
  76. if (e && ("object" == typeof e || "function" == typeof e)) return e;
  77. if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined");
  78. return _assertThisInitialized(t);
  79. }
  80. function _setPrototypeOf(t, e) {
  81. return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
  82. return t.__proto__ = e, t;
  83. }, _setPrototypeOf(t, e);
  84. }
  85. function _superPropBase(t, o) {
  86. for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t)););
  87. return t;
  88. }
  89. function _toConsumableArray(r) {
  90. return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
  91. }
  92. function _toPrimitive(t, r) {
  93. if ("object" != typeof t || !t) return t;
  94. var e = t[Symbol.toPrimitive];
  95. if (void 0 !== e) {
  96. var i = e.call(t, r );
  97. if ("object" != typeof i) return i;
  98. throw new TypeError("@@toPrimitive must return a primitive value.");
  99. }
  100. return (String )(t);
  101. }
  102. function _toPropertyKey(t) {
  103. var i = _toPrimitive(t, "string");
  104. return "symbol" == typeof i ? i : i + "";
  105. }
  106. function _unsupportedIterableToArray(r, a) {
  107. if (r) {
  108. if ("string" == typeof r) return _arrayLikeToArray(r, a);
  109. var t = {}.toString.call(r).slice(8, -1);
  110. 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;
  111. }
  112. }
  113. /**
  114. * @author: Dennis Hernández
  115. * @update zhixin wen <wenzhixin2010@gmail.com>
  116. */
  117. var rowAttr = function rowAttr(row, index) {
  118. return {
  119. id: "customId_".concat(index)
  120. };
  121. };
  122. Object.assign($.fn.bootstrapTable.defaults, {
  123. reorderableRows: false,
  124. onDragStyle: null,
  125. onDropStyle: null,
  126. onDragClass: 'reorder-rows-on-drag-class',
  127. dragHandle: '>tbody>tr>td:not(.bs-checkbox)',
  128. useRowAttrFunc: false,
  129. // eslint-disable-next-line no-unused-vars
  130. onReorderRowsDrag: function onReorderRowsDrag(row) {
  131. return false;
  132. },
  133. // eslint-disable-next-line no-unused-vars
  134. onReorderRowsDrop: function onReorderRowsDrop(row) {
  135. return false;
  136. },
  137. // eslint-disable-next-line no-unused-vars
  138. onReorderRow: function onReorderRow(newData) {
  139. return false;
  140. },
  141. onDragStop: function onDragStop() {},
  142. onAllowDrop: function onAllowDrop() {
  143. return true;
  144. }
  145. });
  146. Object.assign($.fn.bootstrapTable.events, {
  147. 'reorder-row.bs.table': 'onReorderRow'
  148. });
  149. $.BootstrapTable = /*#__PURE__*/function (_$$BootstrapTable) {
  150. function _class() {
  151. _classCallCheck(this, _class);
  152. return _callSuper(this, _class, arguments);
  153. }
  154. _inherits(_class, _$$BootstrapTable);
  155. return _createClass(_class, [{
  156. key: "init",
  157. value: function init() {
  158. var _this = this,
  159. _get3;
  160. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  161. args[_key] = arguments[_key];
  162. }
  163. if (!this.options.reorderableRows) {
  164. var _get2;
  165. (_get2 = _get(_getPrototypeOf(_class.prototype), "init", this)).call.apply(_get2, [this].concat(args));
  166. return;
  167. }
  168. if (this.options.useRowAttrFunc) {
  169. this.options.rowAttributes = rowAttr;
  170. }
  171. var onPostBody = this.options.onPostBody;
  172. this.options.onPostBody = function () {
  173. setTimeout(function () {
  174. _this.makeRowsReorderable();
  175. onPostBody.call(_this.options, _this.options.data);
  176. }, 1);
  177. };
  178. (_get3 = _get(_getPrototypeOf(_class.prototype), "init", this)).call.apply(_get3, [this].concat(args));
  179. }
  180. }, {
  181. key: "makeRowsReorderable",
  182. value: function makeRowsReorderable() {
  183. var _this2 = this;
  184. this.$el.tableDnD({
  185. onDragStyle: this.options.onDragStyle,
  186. onDropStyle: this.options.onDropStyle,
  187. onDragClass: this.options.onDragClass,
  188. onAllowDrop: function onAllowDrop(hoveredRow, draggedRow) {
  189. return _this2.onAllowDrop(hoveredRow, draggedRow);
  190. },
  191. onDragStop: function onDragStop(table, draggedRow) {
  192. return _this2.onDragStop(table, draggedRow);
  193. },
  194. onDragStart: function onDragStart(table, droppedRow) {
  195. return _this2.onDropStart(table, droppedRow);
  196. },
  197. onDrop: function onDrop(table, droppedRow) {
  198. return _this2.onDrop(table, droppedRow);
  199. },
  200. dragHandle: this.options.dragHandle
  201. });
  202. }
  203. }, {
  204. key: "onDropStart",
  205. value: function onDropStart(table, draggingTd) {
  206. this.$draggingTd = $(draggingTd).css('cursor', 'move');
  207. this.draggingIndex = $(this.$draggingTd.parent()).data('index');
  208. // Call the user defined function
  209. this.options.onReorderRowsDrag(this.data[this.draggingIndex]);
  210. }
  211. }, {
  212. key: "onDragStop",
  213. value: function onDragStop(table, draggedRow) {
  214. var rowIndexDraggedRow = $(draggedRow).data('index');
  215. var draggedRowItem = this.data[rowIndexDraggedRow];
  216. this.options.onDragStop(table, draggedRowItem, draggedRow);
  217. }
  218. }, {
  219. key: "onAllowDrop",
  220. value: function onAllowDrop(hoveredRow, draggedRow) {
  221. var rowIndexDraggedRow = $(draggedRow).data('index');
  222. var rowIndexHoveredRow = $(hoveredRow).data('index');
  223. var draggedRowItem = this.data[rowIndexDraggedRow];
  224. var hoveredRowItem = this.data[rowIndexHoveredRow];
  225. return this.options.onAllowDrop(hoveredRowItem, draggedRowItem, hoveredRow, draggedRow);
  226. }
  227. }, {
  228. key: "onDrop",
  229. value: function onDrop(table) {
  230. this.$draggingTd.css('cursor', '');
  231. var pageNum = this.options.pageNumber;
  232. var pageSize = this.options.pageSize;
  233. var newData = [];
  234. for (var i = 0; i < table.tBodies[0].rows.length; i++) {
  235. var $tr = $(table.tBodies[0].rows[i]);
  236. newData.push(this.data[$tr.data('index')]);
  237. $tr.data('index', i);
  238. }
  239. var draggingRow = this.data[this.draggingIndex];
  240. var droppedIndex = newData.indexOf(this.data[this.draggingIndex]);
  241. var droppedRow = this.data[droppedIndex];
  242. var index = (pageNum - 1) * pageSize + this.options.data.indexOf(this.data[droppedIndex]);
  243. this.options.data.splice(this.options.data.indexOf(draggingRow), 1);
  244. this.options.data.splice(index, 0, draggingRow);
  245. this.initSearch();
  246. if (this.options.sidePagination === 'server') {
  247. this.data = _toConsumableArray(this.options.data);
  248. }
  249. // Call the user defined function
  250. this.options.onReorderRowsDrop(droppedRow);
  251. // Call the event reorder-row
  252. this.trigger('reorder-row', newData, draggingRow, droppedRow);
  253. }
  254. }, {
  255. key: "initSearch",
  256. value: function initSearch() {
  257. this.ignoreInitSort = true;
  258. _get(_getPrototypeOf(_class.prototype), "initSearch", this).call(this);
  259. }
  260. }, {
  261. key: "initSort",
  262. value: function initSort() {
  263. if (this.ignoreInitSort) {
  264. this.ignoreInitSort = false;
  265. return;
  266. }
  267. _get(_getPrototypeOf(_class.prototype), "initSort", this).call(this);
  268. }
  269. }]);
  270. }($.BootstrapTable);
  271. }));