Built files from Bizgaze WebServer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

clipboard.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  2. /* Tabulator v4.8.2 (c) Oliver Folkerd */
  3. var Clipboard = function Clipboard(table) {
  4. this.table = table;
  5. this.mode = true;
  6. this.pasteParser = function () {};
  7. this.pasteAction = function () {};
  8. this.customSelection = false;
  9. this.rowRange = false;
  10. this.blocked = true; //block copy actions not originating from this command
  11. };
  12. Clipboard.prototype.initialize = function () {
  13. var _this = this;
  14. this.mode = this.table.options.clipboard;
  15. this.rowRange = this.table.options.clipboardCopyRowRange;
  16. if (this.mode === true || this.mode === "copy") {
  17. this.table.element.addEventListener("copy", function (e) {
  18. var plain, html, list;
  19. if (!_this.blocked) {
  20. e.preventDefault();
  21. if (_this.customSelection) {
  22. plain = _this.customSelection;
  23. if (_this.table.options.clipboardCopyFormatter) {
  24. plain = _this.table.options.clipboardCopyFormatter("plain", plain);
  25. }
  26. } else {
  27. var list = _this.table.modules.export.generateExportList(_this.rowRange, _this.table.options.clipboardCopyStyled, _this.table.options.clipboardCopyConfig, "clipboard");
  28. html = _this.table.modules.export.genereateHTMLTable(list);
  29. plain = html ? _this.generatePlainContent(list) : "";
  30. if (_this.table.options.clipboardCopyFormatter) {
  31. plain = _this.table.options.clipboardCopyFormatter("plain", plain);
  32. html = _this.table.options.clipboardCopyFormatter("html", html);
  33. }
  34. }
  35. if (window.clipboardData && window.clipboardData.setData) {
  36. window.clipboardData.setData('Text', plain);
  37. } else if (e.clipboardData && e.clipboardData.setData) {
  38. e.clipboardData.setData('text/plain', plain);
  39. if (html) {
  40. e.clipboardData.setData('text/html', html);
  41. }
  42. } else if (e.originalEvent && e.originalEvent.clipboardData.setData) {
  43. e.originalEvent.clipboardData.setData('text/plain', plain);
  44. if (html) {
  45. e.originalEvent.clipboardData.setData('text/html', html);
  46. }
  47. }
  48. _this.table.options.clipboardCopied.call(_this.table, plain, html);
  49. _this.reset();
  50. }
  51. });
  52. }
  53. if (this.mode === true || this.mode === "paste") {
  54. this.table.element.addEventListener("paste", function (e) {
  55. _this.paste(e);
  56. });
  57. }
  58. this.setPasteParser(this.table.options.clipboardPasteParser);
  59. this.setPasteAction(this.table.options.clipboardPasteAction);
  60. };
  61. Clipboard.prototype.reset = function () {
  62. this.blocked = false;
  63. this.originalSelectionText = "";
  64. };
  65. Clipboard.prototype.generatePlainContent = function (list) {
  66. var output = [];
  67. list.forEach(function (row) {
  68. var rowData = [];
  69. row.columns.forEach(function (col) {
  70. var value = "";
  71. if (col) {
  72. if (row.type === "group") {
  73. col.value = col.component.getKey();
  74. }
  75. if (col.value === null) {
  76. value = "";
  77. } else {
  78. switch (_typeof(col.value)) {
  79. case "object":
  80. value = JSON.stringify(col.value);
  81. break;
  82. case "undefined":
  83. value = "";
  84. break;
  85. default:
  86. value = col.value;
  87. }
  88. }
  89. }
  90. rowData.push(value);
  91. });
  92. output.push(rowData.join("\t"));
  93. });
  94. return output.join("\n");
  95. };
  96. Clipboard.prototype.copy = function (range, internal) {
  97. var range, sel, textRange;
  98. this.blocked = false;
  99. this.customSelection = false;
  100. if (this.mode === true || this.mode === "copy") {
  101. this.rowRange = range || this.table.options.clipboardCopyRowRange;
  102. if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
  103. range = document.createRange();
  104. range.selectNodeContents(this.table.element);
  105. sel = window.getSelection();
  106. if (sel.toString() && internal) {
  107. this.customSelection = sel.toString();
  108. }
  109. sel.removeAllRanges();
  110. sel.addRange(range);
  111. } else if (typeof document.selection != "undefined" && typeof document.body.createTextRange != "undefined") {
  112. textRange = document.body.createTextRange();
  113. textRange.moveToElementText(this.table.element);
  114. textRange.select();
  115. }
  116. document.execCommand('copy');
  117. if (sel) {
  118. sel.removeAllRanges();
  119. }
  120. }
  121. };
  122. //PASTE EVENT HANDLING
  123. Clipboard.prototype.setPasteAction = function (action) {
  124. switch (typeof action === "undefined" ? "undefined" : _typeof(action)) {
  125. case "string":
  126. this.pasteAction = this.pasteActions[action];
  127. if (!this.pasteAction) {
  128. console.warn("Clipboard Error - No such paste action found:", action);
  129. }
  130. break;
  131. case "function":
  132. this.pasteAction = action;
  133. break;
  134. }
  135. };
  136. Clipboard.prototype.setPasteParser = function (parser) {
  137. switch (typeof parser === "undefined" ? "undefined" : _typeof(parser)) {
  138. case "string":
  139. this.pasteParser = this.pasteParsers[parser];
  140. if (!this.pasteParser) {
  141. console.warn("Clipboard Error - No such paste parser found:", parser);
  142. }
  143. break;
  144. case "function":
  145. this.pasteParser = parser;
  146. break;
  147. }
  148. };
  149. Clipboard.prototype.paste = function (e) {
  150. var data, rowData, rows;
  151. if (this.checkPaseOrigin(e)) {
  152. data = this.getPasteData(e);
  153. rowData = this.pasteParser.call(this, data);
  154. if (rowData) {
  155. e.preventDefault();
  156. if (this.table.modExists("mutator")) {
  157. rowData = this.mutateData(rowData);
  158. }
  159. rows = this.pasteAction.call(this, rowData);
  160. this.table.options.clipboardPasted.call(this.table, data, rowData, rows);
  161. } else {
  162. this.table.options.clipboardPasteError.call(this.table, data);
  163. }
  164. }
  165. };
  166. Clipboard.prototype.mutateData = function (data) {
  167. var self = this,
  168. output = [];
  169. if (Array.isArray(data)) {
  170. data.forEach(function (row) {
  171. output.push(self.table.modules.mutator.transformRow(row, "clipboard"));
  172. });
  173. } else {
  174. output = data;
  175. }
  176. return output;
  177. };
  178. Clipboard.prototype.checkPaseOrigin = function (e) {
  179. var valid = true;
  180. if (e.target.tagName != "DIV" || this.table.modules.edit.currentCell) {
  181. valid = false;
  182. }
  183. return valid;
  184. };
  185. Clipboard.prototype.getPasteData = function (e) {
  186. var data;
  187. if (window.clipboardData && window.clipboardData.getData) {
  188. data = window.clipboardData.getData('Text');
  189. } else if (e.clipboardData && e.clipboardData.getData) {
  190. data = e.clipboardData.getData('text/plain');
  191. } else if (e.originalEvent && e.originalEvent.clipboardData.getData) {
  192. data = e.originalEvent.clipboardData.getData('text/plain');
  193. }
  194. return data;
  195. };
  196. Clipboard.prototype.pasteParsers = {
  197. table: function table(clipboard) {
  198. var data = [],
  199. success = false,
  200. headerFindSuccess = true,
  201. columns = this.table.columnManager.columns,
  202. columnMap = [],
  203. rows = [];
  204. //get data from clipboard into array of columns and rows.
  205. clipboard = clipboard.split("\n");
  206. clipboard.forEach(function (row) {
  207. data.push(row.split("\t"));
  208. });
  209. if (data.length && !(data.length === 1 && data[0].length < 2)) {
  210. success = true;
  211. //check if headers are present by title
  212. data[0].forEach(function (value) {
  213. var column = columns.find(function (column) {
  214. return value && column.definition.title && value.trim() && column.definition.title.trim() === value.trim();
  215. });
  216. if (column) {
  217. columnMap.push(column);
  218. } else {
  219. headerFindSuccess = false;
  220. }
  221. });
  222. //check if column headers are present by field
  223. if (!headerFindSuccess) {
  224. headerFindSuccess = true;
  225. columnMap = [];
  226. data[0].forEach(function (value) {
  227. var column = columns.find(function (column) {
  228. return value && column.field && value.trim() && column.field.trim() === value.trim();
  229. });
  230. if (column) {
  231. columnMap.push(column);
  232. } else {
  233. headerFindSuccess = false;
  234. }
  235. });
  236. if (!headerFindSuccess) {
  237. columnMap = this.table.columnManager.columnsByIndex;
  238. }
  239. }
  240. //remove header row if found
  241. if (headerFindSuccess) {
  242. data.shift();
  243. }
  244. data.forEach(function (item) {
  245. var row = {};
  246. item.forEach(function (value, i) {
  247. if (columnMap[i]) {
  248. row[columnMap[i].field] = value;
  249. }
  250. });
  251. rows.push(row);
  252. });
  253. return rows;
  254. } else {
  255. return false;
  256. }
  257. }
  258. };
  259. Clipboard.prototype.pasteActions = {
  260. replace: function replace(rows) {
  261. return this.table.setData(rows);
  262. },
  263. update: function update(rows) {
  264. return this.table.updateOrAddData(rows);
  265. },
  266. insert: function insert(rows) {
  267. return this.table.addData(rows);
  268. }
  269. };
  270. Tabulator.prototype.registerModule("clipboard", Clipboard);