bootstrap-table-editable.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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.index-of.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.regexp.exec.js'), require('core-js/modules/es.string.replace.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.index-of.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.regexp.exec.js', 'core-js/modules/es.string.replace.js', 'jquery'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(null, null, null, null, null, null, null, null, global.jQuery));
  5. })(this, (function (es_array_concat_js, es_array_find_js, es_array_indexOf_js, es_object_assign_js, es_object_entries_js, es_object_toString_js, es_regexp_exec_js, es_string_replace_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 _createForOfIteratorHelper(r, e) {
  36. var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
  37. if (!t) {
  38. if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e ) {
  39. t && (r = t);
  40. var n = 0,
  41. F = function () {};
  42. return {
  43. s: F,
  44. n: function () {
  45. return n >= r.length ? {
  46. done: !0
  47. } : {
  48. done: !1,
  49. value: r[n++]
  50. };
  51. },
  52. e: function (r) {
  53. throw r;
  54. },
  55. f: F
  56. };
  57. }
  58. throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  59. }
  60. var o,
  61. a = !0,
  62. u = !1;
  63. return {
  64. s: function () {
  65. t = t.call(r);
  66. },
  67. n: function () {
  68. var r = t.next();
  69. return a = r.done, r;
  70. },
  71. e: function (r) {
  72. u = !0, o = r;
  73. },
  74. f: function () {
  75. try {
  76. a || null == t.return || t.return();
  77. } finally {
  78. if (u) throw o;
  79. }
  80. }
  81. };
  82. }
  83. function _get() {
  84. return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) {
  85. var p = _superPropBase(e, t);
  86. if (p) {
  87. var n = Object.getOwnPropertyDescriptor(p, t);
  88. return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value;
  89. }
  90. }, _get.apply(null, arguments);
  91. }
  92. function _getPrototypeOf(t) {
  93. return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) {
  94. return t.__proto__ || Object.getPrototypeOf(t);
  95. }, _getPrototypeOf(t);
  96. }
  97. function _inherits(t, e) {
  98. if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function");
  99. t.prototype = Object.create(e && e.prototype, {
  100. constructor: {
  101. value: t,
  102. writable: !0,
  103. configurable: !0
  104. }
  105. }), Object.defineProperty(t, "prototype", {
  106. writable: !1
  107. }), e && _setPrototypeOf(t, e);
  108. }
  109. function _isNativeReflectConstruct() {
  110. try {
  111. var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
  112. } catch (t) {}
  113. return (_isNativeReflectConstruct = function () {
  114. return !!t;
  115. })();
  116. }
  117. function _iterableToArrayLimit(r, l) {
  118. var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
  119. if (null != t) {
  120. var e,
  121. n,
  122. i,
  123. u,
  124. a = [],
  125. f = !0,
  126. o = !1;
  127. try {
  128. 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);
  129. } catch (r) {
  130. o = !0, n = r;
  131. } finally {
  132. try {
  133. if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
  134. } finally {
  135. if (o) throw n;
  136. }
  137. }
  138. return a;
  139. }
  140. }
  141. function _nonIterableRest() {
  142. throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  143. }
  144. function _possibleConstructorReturn(t, e) {
  145. if (e && ("object" == typeof e || "function" == typeof e)) return e;
  146. if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined");
  147. return _assertThisInitialized(t);
  148. }
  149. function _setPrototypeOf(t, e) {
  150. return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
  151. return t.__proto__ = e, t;
  152. }, _setPrototypeOf(t, e);
  153. }
  154. function _slicedToArray(r, e) {
  155. return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
  156. }
  157. function _superPropBase(t, o) {
  158. for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t)););
  159. return t;
  160. }
  161. function _toPrimitive(t, r) {
  162. if ("object" != typeof t || !t) return t;
  163. var e = t[Symbol.toPrimitive];
  164. if (void 0 !== e) {
  165. var i = e.call(t, r );
  166. if ("object" != typeof i) return i;
  167. throw new TypeError("@@toPrimitive must return a primitive value.");
  168. }
  169. return (String )(t);
  170. }
  171. function _toPropertyKey(t) {
  172. var i = _toPrimitive(t, "string");
  173. return "symbol" == typeof i ? i : i + "";
  174. }
  175. function _unsupportedIterableToArray(r, a) {
  176. if (r) {
  177. if ("string" == typeof r) return _arrayLikeToArray(r, a);
  178. var t = {}.toString.call(r).slice(8, -1);
  179. 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;
  180. }
  181. }
  182. /* eslint-disable no-unused-vars */
  183. /**
  184. * @author zhixin wen <wenzhixin2010@gmail.com>
  185. * extensions: https://github.com/vitalets/x-editable
  186. */
  187. var Utils = $.fn.bootstrapTable.utils;
  188. Object.assign($.fn.bootstrapTable.defaults, {
  189. editable: true,
  190. onEditableInit: function onEditableInit() {
  191. return false;
  192. },
  193. onEditableSave: function onEditableSave(field, row, rowIndex, oldValue, $el) {
  194. return false;
  195. },
  196. onEditableShown: function onEditableShown(field, row, $el, editable) {
  197. return false;
  198. },
  199. onEditableHidden: function onEditableHidden(field, row, $el, reason) {
  200. return false;
  201. }
  202. });
  203. Object.assign($.fn.bootstrapTable.columnDefaults, {
  204. alwaysUseFormatter: false
  205. });
  206. Object.assign($.fn.bootstrapTable.events, {
  207. 'editable-init.bs.table': 'onEditableInit',
  208. 'editable-save.bs.table': 'onEditableSave',
  209. 'editable-shown.bs.table': 'onEditableShown',
  210. 'editable-hidden.bs.table': 'onEditableHidden'
  211. });
  212. $.BootstrapTable = /*#__PURE__*/function (_$$BootstrapTable) {
  213. function _class() {
  214. _classCallCheck(this, _class);
  215. return _callSuper(this, _class, arguments);
  216. }
  217. _inherits(_class, _$$BootstrapTable);
  218. return _createClass(_class, [{
  219. key: "initTable",
  220. value: function initTable() {
  221. var _this = this;
  222. _get(_getPrototypeOf(_class.prototype), "initTable", this).call(this);
  223. if (!this.options.editable) {
  224. return;
  225. }
  226. this.editedCells = [];
  227. $.each(this.columns, function (i, column) {
  228. if (!column.editable) {
  229. return;
  230. }
  231. var editableOptions = {};
  232. var editableDataPrefix = 'editable-';
  233. var processDataOptions = function processDataOptions(key, value) {
  234. // Replace camel case with dashes.
  235. var dashKey = key.replace(/([A-Z])/g, function ($1) {
  236. return "-".concat($1.toLowerCase());
  237. });
  238. if (dashKey.indexOf(editableDataPrefix) === 0) {
  239. editableOptions[dashKey.replace(editableDataPrefix, 'data-')] = value;
  240. }
  241. };
  242. var formatterIsSet = column.formatter ? true : false;
  243. $.each(_this.options, processDataOptions);
  244. column.formatter = column.formatter || function (value) {
  245. return value;
  246. };
  247. column._formatter = column._formatter ? column._formatter : column.formatter;
  248. column.formatter = function (value, row, index, field) {
  249. var result = Utils.calculateObjectValue(column, column._formatter, [value, row, index, field], value);
  250. result = typeof result === 'undefined' || result === null ? _this.options.undefinedText : result;
  251. if (_this.options.uniqueId !== undefined && !column.alwaysUseFormatter) {
  252. var uniqueId = Utils.getItemField(row, _this.options.uniqueId, false);
  253. if ($.inArray(column.field + uniqueId, _this.editedCells) !== -1) {
  254. result = value;
  255. }
  256. }
  257. $.each(column, processDataOptions);
  258. var editableOpts = Utils.calculateObjectValue(column, column.editable, [index, row], {});
  259. var noEditFormatter = editableOpts.hasOwnProperty('noEditFormatter') && editableOpts.noEditFormatter(value, row, index, field);
  260. if (noEditFormatter) {
  261. return noEditFormatter;
  262. }
  263. var editableDataMarkup = '';
  264. $.each(editableOptions, function (key, value) {
  265. editableDataMarkup += " ".concat(key, "=\"").concat(value, "\"");
  266. });
  267. return "<a href=\"javascript:void(0)\"\n data-name=\"".concat(column.field, "\"\n data-pk=\"").concat(row[_this.options.idField], "\"\n data-value=\"").concat(value || '', "\"\n ").concat(editableDataMarkup, ">").concat(formatterIsSet ? result : '', "</a>"); // expand all data-editable-XXX
  268. };
  269. });
  270. }
  271. }, {
  272. key: "initBody",
  273. value: function initBody(fixedScroll) {
  274. var _this2 = this;
  275. _get(_getPrototypeOf(_class.prototype), "initBody", this).call(this, fixedScroll);
  276. if (!this.options.editable) {
  277. return;
  278. }
  279. $.each(this.columns, function (i, column) {
  280. if (!column.editable) {
  281. return;
  282. }
  283. var data = _this2.getData({
  284. escape: true
  285. });
  286. var $field = _this2.$body.find("a[data-name=\"".concat(column.field, "\"]"));
  287. $field.each(function (i, element) {
  288. var $element = $(element);
  289. var $tr = $element.closest('tr');
  290. var index = $tr.data('index');
  291. var row = data[index];
  292. var editableOpts = Utils.calculateObjectValue(column, column.editable, [index, row, $element], {});
  293. $element.editable(editableOpts);
  294. });
  295. $field.off('save').on('save', function (_ref, _ref2) {
  296. var currentTarget = _ref.currentTarget;
  297. var submitValue = _ref2.submitValue;
  298. var $this = $(currentTarget);
  299. var data = _this2.getData();
  300. var rowIndex = $this.parents('tr[data-index]').data('index');
  301. var row = data[rowIndex];
  302. var oldValue = row[column.field];
  303. if (_this2.options.uniqueId !== undefined && !column.alwaysUseFormatter) {
  304. var uniqueId = Utils.getItemField(row, _this2.options.uniqueId, false);
  305. if ($.inArray(column.field + uniqueId, _this2.editedCells) === -1) {
  306. _this2.editedCells.push(column.field + uniqueId);
  307. }
  308. }
  309. submitValue = Utils.escapeHTML(submitValue);
  310. $this.data('value', submitValue);
  311. row[column.field] = submitValue;
  312. _this2.trigger('editable-save', column.field, row, rowIndex, oldValue, $this);
  313. _this2.initBody();
  314. });
  315. $field.off('shown').on('shown', function (_ref3, editable) {
  316. var currentTarget = _ref3.currentTarget;
  317. var $this = $(currentTarget);
  318. var data = _this2.getData();
  319. var rowIndex = $this.parents('tr[data-index]').data('index');
  320. var row = data[rowIndex];
  321. _this2.trigger('editable-shown', column.field, row, $this, editable);
  322. });
  323. $field.off('hidden').on('hidden', function (_ref4, reason) {
  324. var currentTarget = _ref4.currentTarget;
  325. var $this = $(currentTarget);
  326. var data = _this2.getData();
  327. var rowIndex = $this.parents('tr[data-index]').data('index');
  328. var row = data[rowIndex];
  329. _this2.trigger('editable-hidden', column.field, row, $this, reason);
  330. });
  331. });
  332. this.trigger('editable-init');
  333. }
  334. }, {
  335. key: "getData",
  336. value: function getData(params) {
  337. var data = _get(_getPrototypeOf(_class.prototype), "getData", this).call(this, params);
  338. if (params && params.escape) {
  339. var _iterator = _createForOfIteratorHelper(data),
  340. _step;
  341. try {
  342. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  343. var row = _step.value;
  344. for (var _i = 0, _Object$entries = Object.entries(row); _i < _Object$entries.length; _i++) {
  345. var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
  346. key = _Object$entries$_i[0],
  347. value = _Object$entries$_i[1];
  348. row[key] = Utils.unescapeHTML(value);
  349. }
  350. }
  351. } catch (err) {
  352. _iterator.e(err);
  353. } finally {
  354. _iterator.f();
  355. }
  356. }
  357. return data;
  358. }
  359. }]);
  360. }($.BootstrapTable);
  361. }));