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

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