define([ "dojo/_base/lang", "dojo/query", "dojo/NodeList-html", "dojo/NodeList-traverse" ], function(lang, query) { var copyTA = lang.getObject("siemens.clipboard.copyTA", true); var mimeTypeMap = {}; mimeTypeMap['data-copy-json'] = "application/json"; mimeTypeMap['data-copy-websdk'] = "application/vnd-websdk"; var targetElement; var elementType; var isCopyTANewRow; return lang.mixin(copyTA, { isNavigatorIE : function() { return (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0); }, isPasteCommandSupported : function() { return document.queryCommandSupported('pasteTA'); }, 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) { copyTA.setCopyAttributes(element); if (copyTA.isCopyAllowed()) { copyTA.updateCopy(); } }, copyTA : function(element) { isCopyTANewRow = false; copyTA.copy(element); }, copyTANewRow : function(element) { isCopyTANewRow = true; copyTA.copy(element); }, executeCommand : function() { if (!copyTA.copyCommand()) { copyTA.copyText(); } }, // check if event clipboard and paste command are supported, if so execute copy command copyCommand : function() { return !(copyTA.isNavigatorIE()) && (copyTA.isPasteCommandSupported() && document.execCommand('copyTA')); }, // used by IE11 copyText : function() { if (window.clipboardData && clipboardData.setData) { var copyData; var selection = copyTA.getSelectedRows(); if (selection && selection.toString() !== '') { copyData = selection.toString(); } else { for ( var mimeTypeAttribute in mimeTypeMap) { if(isCopyTANewRow && mimeTypeAttribute == "data-copy-websdk" || !isCopyTANewRow && mimeTypeAttribute == "data-copy-json" ){ var data = copyTA.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 = copyTA.getSelectedRows(); 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 = copyTA.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'); if(isCopyTANewRow){ params += '©_TA_NEWROW=true'; }else{ params += '©_TA=true'; } // 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; }, // get the selected rows getSelectedRows : function() { var selection; if ((elementType === 'Grid')) { var selectedRows = query('#' + targetElement.id + ' > table > tbody > tr.CVTaRowSelected'); if (selectedRows.length == 1) { selection = getSelectedText(getFirstInputElement(selectedRows[0])); } } 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", ""); } } } }); });