Common transforms on multiple columns (#3789)

Closes #1843.
Co-authored-by: Antonin Delpeuch <antonin@delpeuch.eu>
This commit is contained in:
Akshita Singh 2021-04-26 15:03:23 +05:30 committed by GitHub
parent 109956d235
commit d269a5ea4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 221 additions and 0 deletions

View File

@ -492,6 +492,7 @@ function init() {
"scripts/dialogs/scatterplot-dialog.js", "scripts/dialogs/scatterplot-dialog.js",
"scripts/dialogs/templating-exporter-dialog.js", "scripts/dialogs/templating-exporter-dialog.js",
"scripts/dialogs/column-reordering-dialog.js", "scripts/dialogs/column-reordering-dialog.js",
"scripts/dialogs/common-transform-dialog.js",
"scripts/dialogs/custom-tabular-exporter-dialog.js", "scripts/dialogs/custom-tabular-exporter-dialog.js",
"scripts/dialogs/sql-exporter-dialog.js", "scripts/dialogs/sql-exporter-dialog.js",
"scripts/dialogs/expression-column-dialog.js", "scripts/dialogs/expression-column-dialog.js",

View File

@ -439,6 +439,7 @@
"core-views/facet": "Facet", "core-views/facet": "Facet",
"core-views/edit-cells": "Edit cells", "core-views/edit-cells": "Edit cells",
"core-views/edit-column": "Edit column", "core-views/edit-column": "Edit column",
"core-views/edit-all-columns": "Edit all columns",
"core-views/transpose": "Transpose", "core-views/transpose": "Transpose",
"core-views/sort": "Sort", "core-views/sort": "Sort",
"core-views/collapse-expand": "Collapse/expand columns to make viewing the data more convenient", "core-views/collapse-expand": "Collapse/expand columns to make viewing the data more convenient",

View File

@ -0,0 +1,15 @@
<div class="dialog-frame" style="width: 400px;">
<div class="dialog-header" bind="dialogHeader"></div>
<div class="dialog-body" bind="dialogBody">
<td width="100%">
<div id="checkedColumn" class="column-reordering-dialog-column-container" bind="columnContainer"></div>
</td>
</div>
<div class="all-column-transform-dialog-container dialog-footer" bind="dialogFooter">
<button class="button" bind="selectAllButton"></button>
<button class="button" bind="deselectAllButton"></button>
<button class="button" bind="okButton"></button>
<button class="button" bind="cancelButton"></button>
</div>
</div>

View File

@ -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 = $('<div>').
addClass("all-column-transform-dialog-container")
.attr("column", colName)
.appendTo(this._elmts.columnContainer);
$('<input>').
attr('type', 'checkbox')
.attr("column", colName)
.prop('checked',(i == columnIndex) ? true :false)
.appendTo(div);
$('<span>')
.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();
};

View File

@ -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'), label: $.i18n('core-views/facet'),
id: "core/facets", id: "core/facets",