From d269a5ea4d591aae467be0b089f5214eb0c057b1 Mon Sep 17 00:00:00 2001 From: Akshita Singh <52077474+singhakshita@users.noreply.github.com> Date: Mon, 26 Apr 2021 15:03:23 +0530 Subject: [PATCH] Common transforms on multiple columns (#3789) Closes #1843. Co-authored-by: Antonin Delpeuch --- .../webapp/modules/core/MOD-INF/controller.js | 1 + .../modules/core/langs/translation-en.json | 1 + .../dialogs/common-transform-dialog.html | 15 ++ .../dialogs/common-transform-dialog.js | 134 ++++++++++++++++++ .../views/data-table/data-table-view.js | 70 +++++++++ 5 files changed, 221 insertions(+) create mode 100644 main/webapp/modules/core/scripts/dialogs/common-transform-dialog.html create mode 100644 main/webapp/modules/core/scripts/dialogs/common-transform-dialog.js diff --git a/main/webapp/modules/core/MOD-INF/controller.js b/main/webapp/modules/core/MOD-INF/controller.js index 85da832d9..503d4e921 100644 --- a/main/webapp/modules/core/MOD-INF/controller.js +++ b/main/webapp/modules/core/MOD-INF/controller.js @@ -492,6 +492,7 @@ function init() { "scripts/dialogs/scatterplot-dialog.js", "scripts/dialogs/templating-exporter-dialog.js", "scripts/dialogs/column-reordering-dialog.js", + "scripts/dialogs/common-transform-dialog.js", "scripts/dialogs/custom-tabular-exporter-dialog.js", "scripts/dialogs/sql-exporter-dialog.js", "scripts/dialogs/expression-column-dialog.js", diff --git a/main/webapp/modules/core/langs/translation-en.json b/main/webapp/modules/core/langs/translation-en.json index 5c6dc8811..a02c23feb 100644 --- a/main/webapp/modules/core/langs/translation-en.json +++ b/main/webapp/modules/core/langs/translation-en.json @@ -439,6 +439,7 @@ "core-views/facet": "Facet", "core-views/edit-cells": "Edit cells", "core-views/edit-column": "Edit column", + "core-views/edit-all-columns": "Edit all columns", "core-views/transpose": "Transpose", "core-views/sort": "Sort", "core-views/collapse-expand": "Collapse/expand columns to make viewing the data more convenient", diff --git a/main/webapp/modules/core/scripts/dialogs/common-transform-dialog.html b/main/webapp/modules/core/scripts/dialogs/common-transform-dialog.html new file mode 100644 index 000000000..d02e4cac4 --- /dev/null +++ b/main/webapp/modules/core/scripts/dialogs/common-transform-dialog.html @@ -0,0 +1,15 @@ +
+
+
+ +
+ +
+ +
+ \ No newline at end of file diff --git a/main/webapp/modules/core/scripts/dialogs/common-transform-dialog.js b/main/webapp/modules/core/scripts/dialogs/common-transform-dialog.js new file mode 100644 index 000000000..0f34310bd --- /dev/null +++ b/main/webapp/modules/core/scripts/dialogs/common-transform-dialog.js @@ -0,0 +1,134 @@ +/* + +Copyright 2010, Google Inc. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + +function commonTransformDialog(expression,label) { + this._createDialog(expression,label); + +} + +commonTransformDialog.prototype._createDialog = function(expression,label) { + var self = this; + var dialog = $(DOM.loadHTML("core", "scripts/dialogs/common-transform-dialog.html")); + this._elmts = DOM.bind(dialog); + this._elmts.dialogHeader.html($.i18n(label)); + this._elmts.selectAllButton.html($.i18n('core-buttons/select-all')); + this._elmts.deselectAllButton.html($.i18n('core-buttons/deselect-all')); + this._elmts.okButton.html($.i18n('core-buttons/ok')); + this._elmts.cancelButton.html($.i18n('core-buttons/cancel')); + + this._level = DialogSystem.showDialog(dialog); + var container = this._elmts.columnContainer; + + for (var i = 0; i < theProject.columnModel.columns.length; i++) { + var col = theProject.columnModel.columns[i]; + var colName = col.name; + var columnIndex = Refine.columnNameToColumnIndex(col.name); + var div = $('
'). + addClass("all-column-transform-dialog-container") + .attr("column", colName) + .appendTo(this._elmts.columnContainer); + $(''). + attr('type', 'checkbox') + .attr("column", colName) + .prop('checked',(i == columnIndex) ? true :false) + .appendTo(div); + $('') + .text(colName) + .appendTo(div); + } + + this._elmts.columnContainer + .find('.all-column-transform-dialog-container') + .click(function() { + container + .find('.all-column-transform-dialog-container') + .removeClass('selected'); + $(this).addClass('selected'); + }); + this._elmts.selectAllButton + .click(function() { + container + .find('input[type="checkbox"]') + .prop('checked',true); + }); + this._elmts.deselectAllButton + .click(function() { + container + .find('input[type="checkbox"]') + .prop('checked',false); + }); + + this._elmts.okButton.click(function() { + self._commit(expression); + self._dismiss(); + }); + this._elmts.cancelButton.click(function() { + self._dismiss(); + }); + + +}; + + +commonTransformDialog.prototype._dismiss = function() { + DialogSystem.dismissUntil(this._level - 1); +}; + + +commonTransformDialog.prototype._commit = function(expression) { + var doTextTransform = function(columnName, expression, onError, repeat, repeatCount) { + Refine.postCoreProcess( + "text-transform", + { + columnName: columnName, + expression: expression, + onError: onError, + repeat: repeat, + repeatCount: repeatCount + }, + null, + { cellsChanged: true } + ); + }; + + this._elmts.columnContainer.find('div').each(function() { + if ($(this).find('input[type="checkbox"]')[0].checked) { + var name = this.getAttribute('column'); + doTextTransform(name,expression, "keep-original", false, ""); + } + }); + + + this._dismiss(); + +}; diff --git a/main/webapp/modules/core/scripts/views/data-table/data-table-view.js b/main/webapp/modules/core/scripts/views/data-table/data-table-view.js index a10362894..a11384a9b 100644 --- a/main/webapp/modules/core/scripts/views/data-table/data-table-view.js +++ b/main/webapp/modules/core/scripts/views/data-table/data-table-view.js @@ -653,6 +653,76 @@ DataTableView.prototype._createMenuForAllColumns = function(elmt) { } }, {}, + { + id: "core/common-transforms", + label: $.i18n('core-views/edit-all-columns'), + submenu: [ + { + id: "core/trim-whitespace", + label: $.i18n('core-views/trim-all'), + click: function() { new commonTransformDialog("value.trim()", "core-views/trim-all"); } + }, + { + id: "core/collapse-whitespace", + label: $.i18n('core-views/collapse-white'), + click: function() { new commonTransformDialog("value.replace(/\\s+/,' ')", "core-views/collapse-white"); } + }, + {}, + { + id: "core/unescape-html-entities", + label: $.i18n('core-views/unescape-html'), + click: function() { new commonTransformDialog("value.unescape('html')","core-views/unescape-html" ); } + }, + { + id: "core/replace-smartquotes", + label: $.i18n('core-views/replace-smartquotes'), + click: function() { new commonTransformDialog("value.replace(/[\u2018\u2019\u201A\u201B\u2039\u203A\u201A]/,\"\\\'\").replace(/[\u201C\u201D\u00AB\u00BB\u201E]/,\"\\\"\")", "core-views/replace-smartquotes"); } + }, + {}, + { + id: "core/to-titlecase", + label: $.i18n('core-views/titlecase'), + click: function() { new commonTransformDialog("value.toTitlecase()", "core-views/titlecase"); } + }, + { + id: "core/to-uppercase", + label: $.i18n('core-views/uppercase'), + click: function() { new commonTransformDialog("value.toUppercase()","core-views/uppercase" ); } + }, + { + id: "core/to-lowercase", + label: $.i18n('core-views/lowercase'), + click: function() { new commonTransformDialog("value.toLowercase()", "core-views/lowercase"); } + }, + {}, + { + id: "core/to-number", + label: $.i18n('core-views/to-number'), + click: function() { new commonTransformDialog("value.toNumber()","core-views/to-number" ); } + }, + { + id: "core/to-date", + label: $.i18n('core-views/to-date'), + click: function() { new commonTransformDialog("value.toDate()","core-views/to-date" ); } + }, + { + id: "core/to-text", + label: $.i18n('core-views/to-text'), + click: function() { new commonTransformDialog("value.toString()","core-views/to-text" ); } + }, + {}, + { + id: "core/to-blank", + label: $.i18n('core-views/blank-out'), + click: function() { new commonTransformDialog("null", "core-views/blank-out"); } + }, + { + id: "core/to-empty", + label: $.i18n('core-views/blank-out-empty'), + click: function() { new commonTransformDialog("\"\"","core-views/blank-out-empty" ); } + } + ] + }, { label: $.i18n('core-views/facet'), id: "core/facets",