From 0ce51ee2f49f83c1f53590803f069d1c43c635d9 Mon Sep 17 00:00:00 2001 From: George Gelashvili and Tira Odhner Date: Mon, 3 Apr 2017 14:15:38 -0400 Subject: [PATCH 02/11] Refactor copyData --- web/pgadmin/static/js/selection/copy_data.js | 45 ++++++++++++++++++++++ .../static/js/selection/tests/copy_data_spec.js | 35 +++++++++++++++++ .../sqleditor/templates/sqleditor/js/sqleditor.js | 44 ++------------------- 3 files changed, 83 insertions(+), 41 deletions(-) create mode 100644 web/pgadmin/static/js/selection/copy_data.js create mode 100644 web/pgadmin/static/js/selection/tests/copy_data_spec.js diff --git a/web/pgadmin/static/js/selection/copy_data.js b/web/pgadmin/static/js/selection/copy_data.js new file mode 100644 index 00000000..586e2fc5 --- /dev/null +++ b/web/pgadmin/static/js/selection/copy_data.js @@ -0,0 +1,45 @@ +define(['jquery', 'underscore', 'sources/selection/clipboard'], function ($, _, clipboard) { + var copyData = function () { + var self = this, grid, data, rows, selection, copied_text = '', copied_data = ''; + self.copied_rows = []; + + // Disable copy button + $("#btn-copy-row").prop('disabled', true); + // Enable paste button + if (self.can_edit) { + $("#btn-paste-row").prop('disabled', false); + } + + grid = self.slickgrid; + selection = grid.getSelectionModel().getSelectedRanges(); + data = grid.getData(); + rows = grid.getSelectedRows(); + // Iterate over all the selected rows & fetch data + for (var i = 0; i < rows.length; i += 1) { + var idx = rows[i], + _rowData = data[idx], + _values = []; + self.copied_rows.push(_rowData); + // Convert it as CSV for clipboard + for (var j = 0; j < self.columns.length; j += 1) { + + var val = _rowData[self.columns[j].pos]; + if(val && _.isObject(val)) + val = "'" + JSON.stringify(val) + "'"; + else if(val && typeof val != "number" && typeof true != "boolean") + val = "'" + val.toString() + "'"; + else if (_.isNull(val) || _.isUndefined(val)) + val = ''; + _values.push(val); + } + // Append to main text string + if (_values.length > 0) + copied_text += _values.toString() + "\n"; + } + + // If there is something to set into clipboard + if (copied_text) + clipboard.copyTextToClipboard(copied_text); + }; + return copyData +}); \ No newline at end of file diff --git a/web/pgadmin/static/js/selection/tests/copy_data_spec.js b/web/pgadmin/static/js/selection/tests/copy_data_spec.js new file mode 100644 index 00000000..4ad6cb0a --- /dev/null +++ b/web/pgadmin/static/js/selection/tests/copy_data_spec.js @@ -0,0 +1,35 @@ +define( + ["jquery", + "slickgrid/slick.grid", + "slickgrid/slick.rowselectionmodel", + "sources/selection/copy_data", + "sources/selection/clipboard" + ], + function ($, SlickGrid, RowSelectionModel, copyData, clipboard) { + describe('copyData', function () { + it('copies selected rows', function () { + var data = [{"flavor":"leopard","id":"1","color":"purple"}, + {"flavor":"lion","id":"2","color":"sand"}, + {"flavor":"puma","id":"3","color":"jet"}]; + var columns = [{"name":"id","label":"id
numeric","cell":"number","can_edit":false,"type":"numeric"}, + {"name":"flavor","label":"flavor
character varying","cell":"string","can_edit":false,"type":"character varying"}, + {"name":"color","label":"size
numeric","cell":"number","can_edit":false,"type":"numeric"}]; + var gridContainer = $("
"); + $("body").append(gridContainer); + var grid = new Slick.Grid("#grid", data, columns, {}); + grid.setSelectionModel(new Slick.RowSelectionModel({selectActiveRow: false})); + + + var sqlEditor = {slickgrid: grid, columns: columns}; + spyOn(clipboard, 'copyTextToClipboard'); + spyOn(grid, 'getSelectedRows').and.returnValue([0, 2]); + + copyData.apply(sqlEditor); + + expect(sqlEditor.copied_rows.length).toBe(2); + expect(clipboard.copyTextToClipboard).toHaveBeenCalled(); + + }) + }) + } +); \ No newline at end of file diff --git a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js index 40974d9b..c8049b62 100644 --- a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js +++ b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js @@ -3,6 +3,7 @@ define( 'jquery', 'underscore', 'underscore.string', 'alertify', 'pgadmin', 'backbone', 'backgrid', 'codemirror', 'pgadmin.misc.explain', 'sources/selection/column_selector', 'sources/selection/clipboard', + 'sources/selection/copy_data', 'slickgrid', 'bootstrap', 'pgadmin.browser', 'wcdocker', 'codemirror/mode/sql/sql', 'codemirror/addon/selection/mark-selection', @@ -27,7 +28,7 @@ define( 'slickgrid/slick.grid' ], function( - $, _, S, alertify, pgAdmin, Backbone, Backgrid, CodeMirror, pgExplain, ColumnSelector, clipboard + $, _, S, alertify, pgAdmin, Backbone, Backgrid, CodeMirror, pgExplain, ColumnSelector, clipboard, copyData ) { /* Return back, this has been called more than once */ if (pgAdmin.SqlEditor) @@ -2951,46 +2952,7 @@ define( }, // This function will copy the selected row. - _copy_row: function() { - var self = this, grid, data, rows, copied_text = ''; - - self.copied_rows = []; - - // Disable copy button - $("#btn-copy-row").prop('disabled', true); - // Enable paste button - if(self.can_edit) { - $("#btn-paste-row").prop('disabled', false); - } - - grid = self.slickgrid; - data = grid.getData(); - rows = grid.getSelectedRows(); - // Iterate over all the selected rows & fetch data - for (var i = 0; i < rows.length; i += 1) { - var idx = rows[i], - _rowData = data[idx], - _values = []; - self.copied_rows.push(_rowData); - // Convert it as CSV for clipboard - for (var j = 0; j < self.columns.length; j += 1) { - var val = _rowData[self.columns[j].pos]; - if(val && _.isObject(val)) - val = "'" + JSON.stringify(val) + "'"; - else if(val && typeof val != "number" && typeof true != "boolean") - val = "'" + val.toString() + "'"; - else if (_.isNull(val) || _.isUndefined(val)) - val = ''; - _values.push(val); - } - // Append to main text string - if(_values.length > 0) - copied_text += _values.toString() + "\n"; - } - // If there is something to set into clipboard - if(copied_text) - clipboard.copyTextToClipboard(copied_text); - }, + _copy_row: copyData, // This function will paste the selected row. _paste_row: function() { -- 2.12.0