diff --git a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js index cb6f417..2228030 100644 --- a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js +++ b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js @@ -175,7 +175,8 @@ define( "click #btn-explain-costs": "on_explain_costs", "click #btn-explain-buffers": "on_explain_buffers", "click #btn-explain-timing": "on_explain_timing", - "change .limit": "on_limit_change" + "change .limit": "on_limit_change", + "keyup": "keyAction" }, // This function is used to render the template. @@ -787,7 +788,14 @@ define( $('#editor-panel').removeClass('sql-editor-busy-fetching'); }, - // Callback function for copy button click. + /* + * Callback function for copy button click. + * This function performs two operations: + * 1. Trigger the signal to copy_row function to copy the + * variable into the object to further use for paste row. + * 2. Trigger the signal to clipboard function to perform + * copy row to clipboard operation. + */ on_copy_row: function() { var self = this; @@ -797,6 +805,13 @@ define( self, self.handler ); + + // Trigger copy to clipboard signal + self.handler.trigger( + 'pgadmin-sqleditor:button:copy_to_clipboard', + self, + self.handler + ); }, // Callback function for paste button click. @@ -1010,6 +1025,20 @@ define( self, self.handler ); + }, + + /* + * Common function to define the key events + * and bind those events with the callbacks. + */ + keyAction: function(ev) { + if (ev.ctrlKey) { + if (ev.keyCode == 67) { + // char c + // Copy selected row to clipboard. + this.on_copy_row(); + } + } } }); @@ -1083,6 +1112,7 @@ define( self.on('pgadmin-sqleditor:button:remove_filter', self._remove_filter, self); self.on('pgadmin-sqleditor:button:apply_filter', self._apply_filter, self); self.on('pgadmin-sqleditor:button:copy_row', self._copy_row, self); + self.on('pgadmin-sqleditor:button:copy_to_clipboard', self._copy_to_clipboard, self); self.on('pgadmin-sqleditor:button:paste_row', self._paste_row_callback, self); self.on('pgadmin-sqleditor:button:limit', self._set_limit, self); self.on('pgadmin-sqleditor:button:flash', self._refresh, self); @@ -2227,6 +2257,86 @@ define( if ('selected_model' in self) self.copied_model = self.selected_model; }, + /* + * It copies the content of element passed from + * first character to the last character to set + * into the clipboard. + */ + selectContent: function(elem) { + var range = document.createRange (); + range.setStart (elem, 0); + range.setEndAfter (elem.lastChild, 0); + + var selection = window.getSelection (); + selection.removeAllRanges (); + selection.addRange (range); + }, + /* + * convert the selected row into csv format. create a + * temporary element containing csv data and then + * insert it into document and then copy the + * csv row on the clipboard with command + * document.execCommand ("copy", false, null); + * Now remove the temp element. + */ + _copy_to_clipboard: function() { + // fetch selected model + if (this.selected_model) { + var item = this.selected_model; + } + else { + return; + } + + var copied_row = document.createElement('a'), + clip_array = [], + csv; + if (this.columns != undefined) { + var keys = _.pluck(this.columns, 'name'), + csv_col = _.indexBy(this.columns, 'name'), + item_val; + _.map(keys, function(key) { + var cell = csv_col [key].cell, + // suppose you want to preserve custom formatters + formatter = cell.prototype && cell.prototype.formatter; + + if (formatter && formatter.fromRaw) { + item_val = formatter.fromRaw(item.get(key), item); + } + else { + item_val = item.get(key); + } + if (item_val) { + if(cell == 'string') { + item_val = '"' + item_val + '"'; + } + clip_array.push(item_val); + } + }); + csv = clip_array.join(';'); + } + + // Create a temporary element to store data to be copied. + copied_row.innerHTML = csv; + document.body.appendChild(copied_row); + copied_row.contentEditable = true; + // pass element to function for processing + this.selectContent(copied_row); + + try { + document.execCommand ("copy", false, null); + } catch (e) { + try { + netscape.security.PrivilegeManager.enablePrivilege ( + "UniversalXPConnect" + ); + document.execCommand ("copy", false, null); + } catch (e) { + alert ("Your browser doesn't allow clipboard access! " + + "Please copy the data manually."); + } + } + }, // This function will paste the selected row. _paste_row: function() {