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; };
5 var Clipboard =
function Clipboard(table) {
8 this.copySelector =
false;
9 this.copySelectorParams = {};
10 this.copyFormatter =
false;
11 this.copyFormatterParams = {};
12 this.pasteParser =
function () {};
13 this.pasteAction =
function () {};
14 this.htmlElement =
false;
20 Clipboard.prototype.initialize =
function () {
23 this.mode = this.table.options.clipboard;
25 if (this.mode ===
true || this.mode ===
"copy") {
26 this.table.element.addEventListener(
"copy",
function (e) {
34 data =
self.generateContent();
36 if (window.clipboardData && window.clipboardData.setData) {
37 window.clipboardData.setData(
'Text', data);
38 }
else if (e.clipboardData && e.clipboardData.setData) {
39 e.clipboardData.setData(
'text/plain', data);
40 if (
self.htmlElement) {
41 e.clipboardData.setData(
'text/html',
self.htmlElement.outerHTML);
43 }
else if (e.originalEvent && e.originalEvent.clipboardData.setData) {
44 e.originalEvent.clipboardData.setData(
'text/plain', data);
45 if (
self.htmlElement) {
46 e.originalEvent.clipboardData.setData(
'text/html',
self.htmlElement.outerHTML);
50 self.table.options.clipboardCopied.call(this.table, data);
57 if (this.mode ===
true || this.mode ===
"paste") {
58 this.table.element.addEventListener(
"paste",
function (e) {
63 this.setPasteParser(this.table.options.clipboardPasteParser);
64 this.setPasteAction(this.table.options.clipboardPasteAction);
67 Clipboard.prototype.processConfig =
function () {
69 columnHeaders:
"groups",
74 if (typeof this.table.options.clipboardCopyHeader !==
"undefined") {
75 config.columnHeaders = this.table.options.clipboardCopyHeader;
76 console.warn(
"DEPRECATION WARNING - clipboardCopyHeader option has been deprecated, please use the columnHeaders property on the clipboardCopyConfig option");
79 if (this.table.options.clipboardCopyConfig) {
80 for (var key in this.table.options.clipboardCopyConfig) {
81 config[key] = this.table.options.clipboardCopyConfig[key];
85 if (config.rowGroups &&
this.table.options.groupBy &&
this.table.modExists(
"groupRows")) {
86 this.config.rowGroups =
true;
89 if (config.columnHeaders) {
90 if ((config.columnHeaders ===
"groups" || config ===
true) && this.table.columnManager.columns.length != this.table.columnManager.columnsByIndex.length) {
91 this.config.columnHeaders =
"groups";
93 this.config.columnHeaders =
"columns";
96 this.config.columnHeaders =
false;
99 if (config.columnCalcs &&
this.table.modExists(
"columnCalcs")) {
100 this.config.columnCalcs =
true;
104 Clipboard.prototype.reset =
function () {
105 this.blocked =
false;
106 this.originalSelectionText =
"";
109 Clipboard.prototype.setPasteAction =
function (action) {
111 switch (typeof action ===
"undefined" ?
"undefined" : _typeof(action)) {
113 this.pasteAction = this.pasteActions[action];
115 if (!this.pasteAction) {
116 console.warn(
"Clipboard Error - No such paste action found:", action);
121 this.pasteAction = action;
126 Clipboard.prototype.setPasteParser =
function (parser) {
127 switch (typeof parser ===
"undefined" ?
"undefined" : _typeof(parser)) {
129 this.pasteParser = this.pasteParsers[parser];
131 if (!this.pasteParser) {
132 console.warn(
"Clipboard Error - No such paste parser found:", parser);
137 this.pasteParser = parser;
142 Clipboard.prototype.paste =
function (e) {
143 var data, rowData, rows;
145 if (this.checkPaseOrigin(e)) {
147 data = this.getPasteData(e);
149 rowData = this.pasteParser.call(
this, data);
154 if (this.table.modExists(
"mutator")) {
155 rowData = this.mutateData(rowData);
158 rows = this.pasteAction.call(
this, rowData);
159 this.table.options.clipboardPasted.call(this.table, data, rowData, rows);
161 this.table.options.clipboardPasteError.call(this.table, data);
166 Clipboard.prototype.mutateData =
function (data) {
170 if (Array.isArray(data)) {
171 data.forEach(
function (row) {
172 output.push(
self.table.modules.mutator.transformRow(row,
"clipboard"));
181 Clipboard.prototype.checkPaseOrigin =
function (e) {
184 if (e.target.tagName !=
"DIV" ||
this.table.modules.edit.currentCell) {
191 Clipboard.prototype.getPasteData =
function (e) {
194 if (window.clipboardData && window.clipboardData.getData) {
195 data = window.clipboardData.getData(
'Text');
196 }
else if (e.clipboardData && e.clipboardData.getData) {
197 data = e.clipboardData.getData(
'text/plain');
198 }
else if (e.originalEvent && e.originalEvent.clipboardData.getData) {
199 data = e.originalEvent.clipboardData.getData(
'text/plain');
205 Clipboard.prototype.copy =
function (selector, selectorParams, formatter, formatterParams,
internal) {
206 var range, sel, textRange;
207 this.blocked =
false;
209 if (this.mode ===
true || this.mode ===
"copy") {
211 if (typeof window.getSelection !=
"undefined" && typeof document.createRange !=
"undefined") {
212 range = document.createRange();
213 range.selectNodeContents(this.table.element);
214 sel = window.getSelection();
216 if (sel.toString() &&
internal) {
217 selector =
"userSelection";
219 selectorParams = sel.toString();
222 sel.removeAllRanges();
224 }
else if (typeof document.selection !=
"undefined" && typeof document.body.createTextRange !=
"undefined") {
225 textRange = document.body.createTextRange();
226 textRange.moveToElementText(this.table.element);
230 this.setSelector(selector);
231 this.copySelectorParams = typeof selectorParams !=
"undefined" && selectorParams != null ? selectorParams : this.config.columnHeaders;
232 this.setFormatter(formatter);
233 this.copyFormatterParams = typeof formatterParams !=
"undefined" && formatterParams != null ? formatterParams : {};
235 document.execCommand(
'copy');
238 sel.removeAllRanges();
243 Clipboard.prototype.setSelector =
function (selector) {
244 selector = selector || this.table.options.clipboardCopySelector;
246 switch (typeof selector ===
"undefined" ?
"undefined" : _typeof(selector)) {
248 if (this.copySelectors[selector]) {
249 this.copySelector = this.copySelectors[selector];
251 console.warn(
"Clipboard Error - No such selector found:", selector);
256 this.copySelector = selector;
261 Clipboard.prototype.setFormatter =
function (formatter) {
263 formatter = formatter || this.table.options.clipboardCopyFormatter;
265 switch (typeof formatter ===
"undefined" ?
"undefined" : _typeof(formatter)) {
267 if (this.copyFormatters[formatter]) {
268 this.copyFormatter = this.copyFormatters[formatter];
270 console.warn(
"Clipboard Error - No such formatter found:", formatter);
275 this.copyFormatter = formatter;
280 Clipboard.prototype.generateContent =
function () {
283 this.htmlElement =
false;
284 data = this.copySelector.call(
this, this.config, this.copySelectorParams);
286 return this.copyFormatter.call(
this, data, this.config, this.copyFormatterParams);
289 Clipboard.prototype.generateSimpleHeaders =
function (columns) {
292 columns.forEach(
function (column) {
293 headers.push(column.definition.title);
299 Clipboard.prototype.generateColumnGroupHeaders =
function (columns) {
304 this.table.columnManager.columns.forEach(
function (column) {
305 var colData = _this.processColumnGroup(column);
308 output.push(colData);
315 Clipboard.prototype.processColumnGroup =
function (column) {
318 var subGroups = column.columns;
322 title: column.definition.title,
326 if (subGroups.length) {
327 groupData.subGroups = [];
330 subGroups.forEach(
function (subGroup) {
331 var subGroupData = _this2.processColumnGroup(subGroup);
334 groupData.width += subGroupData.width;
335 groupData.subGroups.push(subGroupData);
339 if (!groupData.width) {
343 if (column.field && (column.definition.clipboard || column.visible && column.definition.clipboard !==
false)) {
353 Clipboard.prototype.groupHeadersToRows =
function (columns) {
357 function parseColumnGroup(column, level) {
359 if (typeof headers[level] ===
"undefined") {
363 headers[level].push(column.title);
365 if (column.subGroups) {
366 column.subGroups.forEach(
function (subGroup) {
367 parseColumnGroup(subGroup, level + 1);
374 function padColumnheaders() {
377 headers.forEach(
function (title) {
378 var len = title.length;
384 headers.forEach(
function (title) {
385 var len = title.length;
387 for (var i = len; i < max; i++) {
394 columns.forEach(
function (column) {
395 parseColumnGroup(column, 0);
401 Clipboard.prototype.rowsToData =
function (rows, columns, config, params) {
404 rows.forEach(
function (row) {
406 rowData = row instanceof RowComponent ? row.getData(
"clipboard") : row;
408 columns.forEach(
function (column) {
409 var value = column.getFieldValue(rowData);
411 switch (typeof value ===
"undefined" ?
"undefined" : _typeof(value)) {
413 value = JSON.stringify(value);
425 rowArray.push(value);
434 Clipboard.prototype.buildComplexRows =
function (config) {
438 groups = this.table.modules.groupRows.getGroups();
440 groups.forEach(
function (group) {
441 output.push(_this3.processGroupData(group));
447 Clipboard.prototype.processGroupData =
function (group) {
450 var subGroups = group.getSubGroups();
457 if (subGroups.length) {
458 groupData.subGroups = [];
460 subGroups.forEach(
function (subGroup) {
461 groupData.subGroups.push(_this4.processGroupData(subGroup));
464 groupData.rows = group.getRows(
true);
470 Clipboard.prototype.getCalcRow =
function (calcs, columns, selector, pos) {
471 var calcData = calcs[selector];
475 calcData = calcData[pos];
478 if (Object.keys(calcData).length) {
479 return this.rowsToData([calcData], columns);
486 Clipboard.prototype.buildOutput =
function (rows, config, params) {
494 this.table.columnManager.columnsByIndex.forEach(
function (column) {
495 if (column.definition.clipboard || column.visible && column.definition.clipboard !==
false) {
496 columnsByIndex.push(column);
500 if (config.columnHeaders ==
"groups") {
501 columns = this.generateColumnGroupHeaders(this.table.columnManager.columns);
502 output = output.concat(this.groupHeadersToRows(columns));
504 columns = columnsByIndex;
506 output.push(this.generateSimpleHeaders(columns));
509 if (this.config.columnCalcs) {
510 calcs = this.table.getCalcResults();
514 if (this.table.options.clipboardCopyStyled) {
515 this.generateHTML(rows, columns, calcs, config, params);
519 if (config.rowGroups) {
520 rows.forEach(
function (row) {
521 output = output.concat(_this5.parseRowGroupData(row, columnsByIndex, config, params, calcs || {}));
524 if (config.columnCalcs) {
525 output = output.concat(this.getCalcRow(calcs, columnsByIndex,
"top"));
528 output = output.concat(this.rowsToData(rows, columnsByIndex, config, params));
530 if (config.columnCalcs) {
531 output = output.concat(this.getCalcRow(calcs, columnsByIndex,
"bottom"));
538 Clipboard.prototype.parseRowGroupData =
function (group, columns, config, params, calcObj) {
543 groupData.push([group.key]);
545 if (group.subGroups) {
546 group.subGroups.forEach(
function (subGroup) {
547 groupData = groupData.concat(_this6.parseRowGroupData(subGroup, config, params, calcObj[group.key] ? calcObj[group.key].groups || {} : {}));
550 if (config.columnCalcs) {
551 groupData = groupData.concat(this.getCalcRow(calcObj, columns, group.key,
"top"));
554 groupData = groupData.concat(this.rowsToData(group.rows, columns, config, params));
556 if (config.columnCalcs) {
557 groupData = groupData.concat(this.getCalcRow(calcObj, columns, group.key,
"bottom"));
564 Clipboard.prototype.generateHTML =
function (rows, columns, calcs, config, params) {
579 this.htmlElement = document.createElement(
"table");
580 self.mapElementStyles(this.table.element,
this.htmlElement, [
"border-top",
"border-left",
"border-right",
"border-bottom"]);
582 function generateSimpleHeaders() {
583 var headerEl = document.createElement(
"tr");
585 columns.forEach(
function (column) {
586 var columnEl = document.createElement(
"th");
587 columnEl.innerHTML = column.definition.title;
589 self.mapElementStyles(column.getElement(), columnEl, [
"border-top",
"border-left",
"border-right",
"border-bottom",
"background-color",
"color",
"font-weight",
"font-family",
"font-size"]);
591 headerEl.appendChild(columnEl);
594 self.mapElementStyles(
self.table.columnManager.getHeadersElement(), headerEl, [
"border-top",
"border-left",
"border-right",
"border-bottom",
"background-color",
"color",
"font-weight",
"font-family",
"font-size"]);
596 self.htmlElement.appendChild(document.createElement(
"thead").appendChild(headerEl));
599 function generateHeaders(headers) {
601 var headerHolderEl = document.createElement(
"thead");
603 headers.forEach(
function (columns) {
604 var headerEl = document.createElement(
"tr");
606 columns.forEach(
function (column) {
607 var columnEl = document.createElement(
"th");
609 if (column.width > 1) {
610 columnEl.colSpan = column.width;
613 if (column.height > 1) {
614 columnEl.rowSpan = column.height;
617 columnEl.innerHTML = column.title;
619 self.mapElementStyles(column.element, columnEl, [
"border-top",
"border-left",
"border-right",
"border-bottom",
"background-color",
"color",
"font-weight",
"font-family",
"font-size"]);
621 headerEl.appendChild(columnEl);
624 self.mapElementStyles(
self.table.columnManager.getHeadersElement(), headerEl, [
"border-top",
"border-left",
"border-right",
"border-bottom",
"background-color",
"color",
"font-weight",
"font-family",
"font-size"]);
626 headerHolderEl.appendChild(headerEl);
629 self.htmlElement.appendChild(headerHolderEl);
632 function parseColumnGroup(column, level) {
634 var actualColumns = [];
636 if (typeof headers[level] ===
"undefined") {
640 headers[level].push({
644 children: !!column.subGroups,
645 element: column.column.getElement()
648 if (column.subGroups) {
649 column.subGroups.forEach(
function (subGroup) {
650 actualColumns = actualColumns.concat(parseColumnGroup(subGroup, level + 1));
653 return actualColumns;
655 return [column.column];
659 function padVerticalColumnheaders() {
660 headers.forEach(
function (row, index) {
661 row.forEach(
function (header) {
662 if (!header.children) {
663 header.height = headers.length - index;
669 function addCalcRow(calcs, selector, pos) {
670 var calcData = calcs[selector];
674 calcData = calcData[pos];
677 if (Object.keys(calcData).length) {
679 processRows([calcData]);
685 if (config.columnHeaders) {
686 if (config.columnHeaders ==
"groups") {
688 var actualColumns = [];
690 columns.forEach(
function (column) {
691 actualColumns = actualColumns.concat(parseColumnGroup(column, 0));
694 columns = actualColumns;
696 padVerticalColumnheaders();
697 generateHeaders(headers);
699 generateSimpleHeaders();
706 body = document.createElement(
"tbody");
709 if (window.getComputedStyle) {
710 oddRow = this.table.element.querySelector(
".tabulator-row-odd:not(.tabulator-group):not(.tabulator-calcs)");
711 evenRow = this.table.element.querySelector(
".tabulator-row-even:not(.tabulator-group):not(.tabulator-calcs)");
712 calcRow = this.table.element.querySelector(
".tabulator-row.tabulator-calcs");
713 firstRow = this.table.element.querySelector(
".tabulator-row:not(.tabulator-group):not(.tabulator-calcs)");
714 firstGroup = this.table.element.getElementsByClassName(
"tabulator-group")[0];
717 styleCells = firstRow.getElementsByClassName(
"tabulator-cell");
718 firstCell = styleCells[0];
719 lastCell = styleCells[styleCells.length - 1];
723 function processRows(rowArray) {
725 rowArray.forEach(
function (row, i) {
726 var rowEl = document.createElement(
"tr"),
731 if (row instanceof RowComponent) {
732 rowData = row.getData(
"clipboard");
738 columns.forEach(
function (column, j) {
739 var cellEl = document.createElement(
"td"),
740 value = column.getFieldValue(rowData);
742 switch (typeof value ===
"undefined" ?
"undefined" : _typeof(value)) {
744 value = JSON.stringify(value);
756 cellEl.innerHTML = value;
758 if (column.definition.align) {
759 cellEl.style.textAlign = column.definition.align;
762 if (j < columns.length - 1) {
764 self.mapElementStyles(firstCell, cellEl, [
"border-top",
"border-left",
"border-right",
"border-bottom",
"color",
"font-weight",
"font-family",
"font-size"]);
768 self.mapElementStyles(firstCell, cellEl, [
"border-top",
"border-left",
"border-right",
"border-bottom",
"color",
"font-weight",
"font-family",
"font-size"]);
772 rowEl.appendChild(cellEl);
778 if (!(i % 2) && oddRow) {
782 if (i % 2 && evenRow) {
788 self.mapElementStyles(styleRow, rowEl, [
"border-top",
"border-left",
"border-right",
"border-bottom",
"color",
"font-weight",
"font-family",
"font-size",
"background-color"]);
791 body.appendChild(rowEl);
795 function processGroup(group, calcObj) {
796 var groupEl = document.createElement(
"tr"),
797 groupCellEl = document.createElement(
"td");
799 groupCellEl.colSpan = columns.length;
801 groupCellEl.innerHTML = group.key;
803 groupEl.appendChild(groupCellEl);
804 body.appendChild(groupEl);
806 self.mapElementStyles(firstGroup, groupEl, [
"border-top",
"border-left",
"border-right",
"border-bottom",
"color",
"font-weight",
"font-family",
"font-size",
"background-color"]);
808 if (group.subGroups) {
809 group.subGroups.forEach(
function (subGroup) {
810 processGroup(subGroup, calcObj[group.key] ? calcObj[group.key].groups || {} : {});
813 if (config.columnCalcs) {
814 addCalcRow(calcObj, group.key,
"top");
817 processRows(group.rows);
819 if (config.columnCalcs) {
820 addCalcRow(calcObj, group.key,
"bottom");
825 if (config.rowGroups) {
826 rows.forEach(
function (group) {
827 processGroup(group, calcs || {});
830 if (config.columnCalcs) {
831 addCalcRow(calcs,
"top");
836 if (config.columnCalcs) {
837 addCalcRow(calcs,
"bottom");
841 this.htmlElement.appendChild(body);
844 Clipboard.prototype.mapElementStyles =
function (from, to, props) {
847 "background-color":
"backgroundColor",
848 "color":
"fontColor",
849 "font-weight":
"fontWeight",
850 "font-family":
"fontFamily",
851 "font-size":
"fontSize",
852 "border-top":
"borderTop",
853 "border-left":
"borderLeft",
854 "border-right":
"borderRight",
855 "border-bottom":
"borderBottom"
858 if (window.getComputedStyle) {
859 var fromStyle = window.getComputedStyle(from);
861 props.forEach(
function (prop) {
862 to.style[lookup[prop]] = fromStyle.getPropertyValue(prop);
869 Clipboard.prototype.copySelectors = {
870 userSelection:
function userSelection(config, params) {
873 selected:
function selected(config, params) {
876 if (this.table.modExists(
"selectRow",
true)) {
877 rows = this.table.modules.selectRow.getSelectedRows();
880 if (config.rowGroups) {
881 console.warn(
"Clipboard Warning - select coptSelector does not support row groups");
884 return this.buildOutput(rows, config, params);
886 table:
function table(config, params) {
887 if (config.rowGroups) {
888 console.warn(
"Clipboard Warning - table coptSelector does not support row groups");
891 return this.buildOutput(this.table.rowManager.getComponents(), config, params);
893 active:
function active(config, params) {
896 if (config.rowGroups) {
897 rows = this.buildComplexRows(config);
899 rows = this.table.rowManager.getComponents(
"active");
902 return this.buildOutput(rows, config, params);
904 visible:
function visible(config, params) {
907 if (config.rowGroups) {
908 rows = this.buildComplexRows(config);
910 rows = this.table.rowManager.getComponents(
"visible");
913 return this.buildOutput(rows, config, params);
917 Clipboard.prototype.copyFormatters = {
918 raw:
function raw(data, params) {
921 table:
function table(data, params) {
924 data.forEach(
function (row) {
926 row.forEach(
function (value) {
927 if (typeof value ==
"undefined") {
931 value = typeof value ==
"undefined" || value === null ?
"" : value.toString();
933 if (value.match(/\r|\n/)) {
934 value = value.split(
'"').join(
'""');
935 value =
'"' + value +
'"';
940 output.push(newRow.join(
"\t"));
943 return output.join(
"\n");
947 Clipboard.prototype.pasteParsers = {
948 table:
function table(clipboard) {
951 headerFindSuccess =
true,
952 columns = this.table.columnManager.columns,
957 clipboard = clipboard.split(
"\n");
959 clipboard.forEach(
function (row) {
960 data.push(row.split(
"\t"));
963 if (data.length && !(data.length === 1 && data[0].length < 2)) {
967 data[0].forEach(
function (value) {
968 var column = columns.find(
function (column) {
969 return value && column.definition.title && value.trim() && column.definition.title.trim() === value.trim();
973 columnMap.push(column);
975 headerFindSuccess =
false;
980 if (!headerFindSuccess) {
981 headerFindSuccess =
true;
984 data[0].forEach(
function (value) {
985 var column = columns.find(
function (column) {
986 return value && column.field && value.trim() && column.field.trim() === value.trim();
990 columnMap.push(column);
992 headerFindSuccess =
false;
996 if (!headerFindSuccess) {
997 columnMap = this.table.columnManager.columnsByIndex;
1002 if (headerFindSuccess) {
1006 data.forEach(
function (item) {
1009 item.forEach(
function (value, i) {
1011 row[columnMap[i].field] = value;
1025 Clipboard.prototype.pasteActions = {
1026 replace:
function replace(rows) {
1027 return this.table.setData(rows);
1029 update:
function update(rows) {
1030 return this.table.updateOrAddData(rows);
1032 insert:
function insert(rows) {
1033 return this.table.addData(rows);
1037 Tabulator.prototype.registerModule(
"clipboard", Clipboard);