bootstrap-table-copy-rows.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.join.js'), require('core-js/modules/es.object.assign.js'), require('core-js/modules/es.object.to-string.js'), require('core-js/modules/web.dom-collections.for-each.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.join.js', 'core-js/modules/es.object.assign.js', 'core-js/modules/es.object.to-string.js', 'core-js/modules/web.dom-collections.for-each.js', 'jquery'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(null, null, null, null, null, null, global.jQuery));
  5. })(this, (function (es_array_concat_js, es_array_find_js, es_array_join_js, es_object_assign_js, es_object_toString_js, web_domCollections_forEach_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 _assertThisInitialized(e) {
  12. if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  13. return e;
  14. }
  15. function _callSuper(t, o, e) {
  16. return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e));
  17. }
  18. function _classCallCheck(a, n) {
  19. if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
  20. }
  21. function _defineProperties(e, r) {
  22. for (var t = 0; t < r.length; t++) {
  23. var o = r[t];
  24. o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
  25. }
  26. }
  27. function _createClass(e, r, t) {
  28. return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
  29. writable: !1
  30. }), e;
  31. }
  32. function _createForOfIteratorHelper(r, e) {
  33. var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
  34. if (!t) {
  35. if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e ) {
  36. t && (r = t);
  37. var n = 0,
  38. F = function () {};
  39. return {
  40. s: F,
  41. n: function () {
  42. return n >= r.length ? {
  43. done: !0
  44. } : {
  45. done: !1,
  46. value: r[n++]
  47. };
  48. },
  49. e: function (r) {
  50. throw r;
  51. },
  52. f: F
  53. };
  54. }
  55. throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  56. }
  57. var o,
  58. a = !0,
  59. u = !1;
  60. return {
  61. s: function () {
  62. t = t.call(r);
  63. },
  64. n: function () {
  65. var r = t.next();
  66. return a = r.done, r;
  67. },
  68. e: function (r) {
  69. u = !0, o = r;
  70. },
  71. f: function () {
  72. try {
  73. a || null == t.return || t.return();
  74. } finally {
  75. if (u) throw o;
  76. }
  77. }
  78. };
  79. }
  80. function _get() {
  81. return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) {
  82. var p = _superPropBase(e, t);
  83. if (p) {
  84. var n = Object.getOwnPropertyDescriptor(p, t);
  85. return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value;
  86. }
  87. }, _get.apply(null, arguments);
  88. }
  89. function _getPrototypeOf(t) {
  90. return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) {
  91. return t.__proto__ || Object.getPrototypeOf(t);
  92. }, _getPrototypeOf(t);
  93. }
  94. function _inherits(t, e) {
  95. if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function");
  96. t.prototype = Object.create(e && e.prototype, {
  97. constructor: {
  98. value: t,
  99. writable: !0,
  100. configurable: !0
  101. }
  102. }), Object.defineProperty(t, "prototype", {
  103. writable: !1
  104. }), e && _setPrototypeOf(t, e);
  105. }
  106. function _isNativeReflectConstruct() {
  107. try {
  108. var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
  109. } catch (t) {}
  110. return (_isNativeReflectConstruct = function () {
  111. return !!t;
  112. })();
  113. }
  114. function _possibleConstructorReturn(t, e) {
  115. if (e && ("object" == typeof e || "function" == typeof e)) return e;
  116. if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined");
  117. return _assertThisInitialized(t);
  118. }
  119. function _setPrototypeOf(t, e) {
  120. return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
  121. return t.__proto__ = e, t;
  122. }, _setPrototypeOf(t, e);
  123. }
  124. function _superPropBase(t, o) {
  125. for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t)););
  126. return t;
  127. }
  128. function _toPrimitive(t, r) {
  129. if ("object" != typeof t || !t) return t;
  130. var e = t[Symbol.toPrimitive];
  131. if (void 0 !== e) {
  132. var i = e.call(t, r );
  133. if ("object" != typeof i) return i;
  134. throw new TypeError("@@toPrimitive must return a primitive value.");
  135. }
  136. return (String )(t);
  137. }
  138. function _toPropertyKey(t) {
  139. var i = _toPrimitive(t, "string");
  140. return "symbol" == typeof i ? i : i + "";
  141. }
  142. function _unsupportedIterableToArray(r, a) {
  143. if (r) {
  144. if ("string" == typeof r) return _arrayLikeToArray(r, a);
  145. var t = {}.toString.call(r).slice(8, -1);
  146. 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;
  147. }
  148. }
  149. /**
  150. * @author Homer Glascock <HopGlascock@gmail.com>
  151. * @update zhixin wen <wenzhixin2010@gmail.com>
  152. */
  153. var Utils = $.fn.bootstrapTable.utils;
  154. Object.assign($.fn.bootstrapTable.locales, {
  155. formatCopyRows: function formatCopyRows() {
  156. return 'Copy Rows';
  157. }
  158. });
  159. Object.assign($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  160. Object.assign($.fn.bootstrapTable.defaults.icons, {
  161. copy: {
  162. bootstrap3: 'glyphicon-copy icon-pencil',
  163. bootstrap5: 'bi-clipboard',
  164. materialize: 'content_copy',
  165. 'bootstrap-table': 'icon-copy'
  166. }[$.fn.bootstrapTable.theme] || 'fa-copy'
  167. });
  168. var copyText = function copyText(text) {
  169. var textField = document.createElement('textarea');
  170. $(textField).html(text);
  171. document.body.appendChild(textField);
  172. textField.select();
  173. try {
  174. document.execCommand('copy');
  175. } catch (e) {
  176. console.warn('Oops, unable to copy');
  177. }
  178. $(textField).remove();
  179. };
  180. Object.assign($.fn.bootstrapTable.defaults, {
  181. showCopyRows: false,
  182. copyWithHidden: false,
  183. copyDelimiter: ', ',
  184. copyNewline: '\n',
  185. copyRowsHandler: function copyRowsHandler(text) {
  186. return text;
  187. }
  188. });
  189. Object.assign($.fn.bootstrapTable.columnDefaults, {
  190. ignoreCopy: false,
  191. rawCopy: false
  192. });
  193. $.fn.bootstrapTable.methods.push('copyColumnsToClipboard');
  194. $.BootstrapTable = /*#__PURE__*/function (_$$BootstrapTable) {
  195. function _class() {
  196. _classCallCheck(this, _class);
  197. return _callSuper(this, _class, arguments);
  198. }
  199. _inherits(_class, _$$BootstrapTable);
  200. return _createClass(_class, [{
  201. key: "initToolbar",
  202. value: function initToolbar() {
  203. var _get2;
  204. if (this.options.showCopyRows && this.header.stateField) {
  205. this.buttons = Object.assign(this.buttons, {
  206. copyRows: {
  207. text: this.options.formatCopyRows(),
  208. icon: this.options.icons.copy,
  209. event: this.copyColumnsToClipboard,
  210. attributes: {
  211. 'aria-label': this.options.formatCopyRows(),
  212. title: this.options.formatCopyRows()
  213. }
  214. }
  215. });
  216. }
  217. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  218. args[_key] = arguments[_key];
  219. }
  220. (_get2 = _get(_getPrototypeOf(_class.prototype), "initToolbar", this)).call.apply(_get2, [this].concat(args));
  221. this.$copyButton = this.$toolbar.find('>.columns [name="copyRows"]');
  222. if (this.options.showCopyRows && this.header.stateField) {
  223. this.updateCopyButton();
  224. }
  225. }
  226. }, {
  227. key: "copyColumnsToClipboard",
  228. value: function copyColumnsToClipboard() {
  229. var _this = this;
  230. var rows = [];
  231. var _iterator = _createForOfIteratorHelper(this.getSelections()),
  232. _step;
  233. try {
  234. var _loop = function _loop() {
  235. var row = _step.value;
  236. var cols = [];
  237. _this.options.columns[0].forEach(function (column, index) {
  238. if (column.field !== _this.header.stateField && (!_this.options.copyWithHidden || _this.options.copyWithHidden && column.visible) && !column.ignoreCopy) {
  239. if (row[column.field] !== null) {
  240. var columnValue = column.rawCopy ? row[column.field] : Utils.calculateObjectValue(column, _this.header.formatters[index], [row[column.field], row, index], row[column.field]);
  241. cols.push(columnValue);
  242. }
  243. }
  244. });
  245. rows.push(cols.join(_this.options.copyDelimiter));
  246. };
  247. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  248. _loop();
  249. }
  250. } catch (err) {
  251. _iterator.e(err);
  252. } finally {
  253. _iterator.f();
  254. }
  255. var text = rows.join(this.options.copyNewline);
  256. text = Utils.calculateObjectValue(this.options, this.options.copyRowsHandler, [text], text);
  257. copyText(text);
  258. }
  259. }, {
  260. key: "updateSelected",
  261. value: function updateSelected() {
  262. _get(_getPrototypeOf(_class.prototype), "updateSelected", this).call(this);
  263. this.updateCopyButton();
  264. }
  265. }, {
  266. key: "updateCopyButton",
  267. value: function updateCopyButton() {
  268. if (this.options.showCopyRows && this.header.stateField && this.$copyButton) {
  269. this.$copyButton.prop('disabled', !this.getSelections().length);
  270. }
  271. }
  272. }]);
  273. }($.BootstrapTable);
  274. }));