define([ "dojo/_base/lang", "dojo/query", "dojo/NodeList-html", "dojo/NodeList-traverse" ], function(lang, query) { var copy = lang.getObject("siemens.clipboard.copy", true); var mimeTypeMap = {}; mimeTypeMap['data-copy-websdk'] = "application/vnd-websdk"; mimeTypeMap['data-copy-text-html'] = "text/html"; mimeTypeMap['data-copy-text'] = "text/plain"; var targetElement; var elementType; return lang.mixin(copy, { isNavigatorIE : function() { return (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0); }, isPasteCommandSupported : function() { return document.queryCommandSupported('paste'); }, setCopyAttributes : function(element) { targetElement = query(element); var activeElement = query(document.activeElement); var parentInlineFilter = activeElement.parents('tr.CVInlineFilter'), parentToolbar = activeElement .parents('div.gridToolbarCell'); if (parentInlineFilter.length > 0 || parentToolbar.length > 0) { targetElement = activeElement; } elementType = targetElement.attr('data-context').toString(); }, // try to copy using event.clipboardData; if not supported copy using window.clipboardData copy : function(element) { copy.setCopyAttributes(element); if (copy.isCopyAllowed()) { copy.updateCopy(); copy.executeCommand(); } }, executeCommand : function() { if (!copy.copyCommand()) { copy.copyText(); } }, // check if event clipboard and paste command are supported, if so execute copy command copyCommand : function() { return !(copy.isNavigatorIE()) && (copy.isPasteCommandSupported() && document.execCommand('copy')); }, // used by IE11 copyText : function() { if (window.clipboardData && clipboardData.setData) { var copyData; var selection = copy.getSelection(); if (selection && selection.toString() !== '') { copyData = selection.toString(); } else { for ( var mimeTypeAttribute in mimeTypeMap) { var data = copy.getCopyData(mimeTypeAttribute); if (data) { copyData = data; break; } } } if (copyData) { clipboardData.setData("Text", copyData.toString()); } } }, // used by QT browser copyData : function(e) { // if a text is highlighted, only store the selection var selection = copy.getSelection(); if (selection && selection.toString() !== '') { e.clipboardData.setData('text/plain', selection.toString()); } else { // go through all stored data and set the appropriate mime type on the clipboard for ( var mimeTypeAttribute in mimeTypeMap) { var copyData = copy.getCopyData(mimeTypeAttribute); if (copyData) { e.clipboardData.setData(mimeTypeMap[mimeTypeAttribute], copyData); } } } }, // only needed by grids currently, in case selection is not contiguous; could be extended to other components in // the future. isCopyAllowed : function() { if (elementType === 'Grid') { if (targetElement.attr('data-copy-allowed').toString() === 'false') { window.top.addMessage('Copy can only be used on connected cells with no gaps', 'Error', 'CopyNotAllowed', '5000'); return false; } } return true; }, updateCopy : function() { var params = 'GRID_ID=' + targetElement.attr('id'); // update the css of the copied cells display_sendAction('GRID_SELECTION', 'UPDATE_COPY', params); }, // return the copy data from an element's mime type attribute getCopyData : function(mimeTypeAttribute) { return targetElement.attr(mimeTypeAttribute); }, // get the current selection, for grids factor if only a single cell is selected (otherwise, it doesn't count) getSelection : function() { var selection; if (elementType === 'Grid') { var selectedCells = query('#' + targetElement.id + ' > table > tbody > tr > td.CVTaCellSelected'); if (selectedCells.length == 1) { selection = getSelectedText(getFirstInputElement(selectedCells[0])); } } else { selection = getSelectedText(targetElement); } return selection; }, clearClipboardData : function(e) { if (e.clipboardData) { if (e.clipboardData.clearData) { e.clipboardData.clearData(); } else if (e.clipboardData.setData) { for ( var mimeTypeAttribute in mimeTypeMap) { e.clipboardData.setData(mimeTypeMap[mimeTypeAttribute], ""); } } } else if (window.clipboardData) { if (clipboardData.clearData) { clipboardData.clearData(); } else { clipboardData.setData("Text", ""); } } } }); });