Add menu for searching and replacing
Add a new menu "Replace" in "Edit Cells" > "Common Transforms" and a new interaction box for entering parameters. Under the hood this is a wrapper around GREL replace() function. The behavior is the same as replace() except the automatic backslah escaping of "Replace with" parameter. The 2 main parameters in the box are 2 textaras : "Find" and "Replace with" 2 cumulative options are available for "Find" parameter : "Case insensitive" and "Regular Expression" 1 option is available for "Replace with" parameter : "Do not escape backslash automatically" A validity control of the pattern is made when "Regular expression" is checked.
This commit is contained in:
parent
2676c60fb2
commit
338fa585d3
@ -633,6 +633,7 @@
|
||||
"to-text": "To text",
|
||||
"blank-out": "To null",
|
||||
"blank-out-empty": "To empty string",
|
||||
"replace": "Replace",
|
||||
"fill-down": "Fill down",
|
||||
"blank-down": "Blank down",
|
||||
"split-cells": "Split multi-valued cells",
|
||||
@ -674,7 +675,17 @@
|
||||
"rows": "rows",
|
||||
"records": "records",
|
||||
"show": "Show",
|
||||
"hide": "Hide"
|
||||
"hide": "Hide",
|
||||
"text-to-find": "Find:",
|
||||
"replacement-text": "Replace with:",
|
||||
"case-insensitive": "case insensitive",
|
||||
"finding-info1": "Leave blank to add the remplacement string after each character.",
|
||||
"finding-info2": "Check \"regular expression\" to find special characters (new lines, tabulations...) or complex patterns.",
|
||||
"finding-options": "Finding options:",
|
||||
"replacement-options": "Replacement options:",
|
||||
"replacement-info": "If \"regular expression\" option is checked and finding pattern contains groups delimited with parentheses, $0 will return the complete string matching the pattern, and $1, $2... the 1st, 2d... group.",
|
||||
"replace-dont-escape": "Do not escape backslash (\\) automatically.",
|
||||
"warning-regex": "Invalid regular expression."
|
||||
},
|
||||
"core-buttons": {
|
||||
"cancel": "Cancel",
|
||||
|
@ -132,6 +132,139 @@ DataTableColumnHeaderUI.extendMenu(function(column, columnHeaderUI, menu) {
|
||||
}
|
||||
};
|
||||
|
||||
var doReplace = function() {
|
||||
function isValidPattern(p) {
|
||||
// check if a string can be used as a regexp pattern
|
||||
// parameters :
|
||||
// p : a string without beginning and trailing "/"
|
||||
|
||||
// we need a manual check for unescaped /
|
||||
// the GREL replace function cannot contain a pattern with unescaped /
|
||||
// but javascript Regexp accepts it and auto escape it
|
||||
var pos = p.replace(/\\\//g,'').indexOf("/");
|
||||
if (pos != -1) {
|
||||
alert($.i18n._('core-views')["warning-regex"] + " : " + p);
|
||||
return 0;}
|
||||
try {
|
||||
var pattern = new RegExp(p);
|
||||
return 1;
|
||||
} catch (e) {
|
||||
alert($.i18n._('core-views')["warning-regex"] + " : " + p);
|
||||
return 0;}
|
||||
}
|
||||
function escapeInputString(s) {
|
||||
// if the textarea input is used as a plain string
|
||||
// user input | result after escaping
|
||||
// 4 characters : A\tA | 5 characters : A\\tA
|
||||
// 4 characters : A\nA | 5 characters : A\\nA
|
||||
// 1 characters : \ | 2 characters : \\
|
||||
// 1 character : ' | 2 characters : \'
|
||||
// 1 character : " | 2 characters : \"
|
||||
// new line | 2 characters : \n
|
||||
// tab | 2 characters : \t
|
||||
// parameters :
|
||||
// s : a string from a textarea
|
||||
if (typeof s != 'string') return ""
|
||||
// convert new lines and tabs manually typed in the textarea into \n and \t and suppress other printable characters, escape \ ' and "
|
||||
return s.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/\t/g, '\\t').replace(/[\x00-\x1F\x80-\x9F]/g, '').replace(/'/g, "\\'").replace(/"/g, '\\"')
|
||||
function hex(c) {
|
||||
// non used
|
||||
var v = '0' + c.charCodeAt(0).toString(16);
|
||||
return '\\x' + v.substr(v.length - 2);
|
||||
}
|
||||
}
|
||||
function escapeInputRegex(s) {
|
||||
// if the textarea input is used as a regex pattern
|
||||
// suppress new lines and tabs manually typed in the textarea, and other non printable characters
|
||||
// no need to escape \ or /
|
||||
// parameters :
|
||||
// s : a string from a textarea
|
||||
return (typeof s == 'string') ? s.replace(/[\x00-\x1F\x80-\x9F]/g, '') : ""
|
||||
}
|
||||
function stringToRegex(s) {
|
||||
// converts a plain string to regex, in order to use the i flag
|
||||
// escaping is needed to force the regex processor to interpret every character litteraly
|
||||
// parameters :
|
||||
// s : a string from a textarea, preprocessed with escapeInputString
|
||||
if (typeof s != 'string') return ""
|
||||
// no need to escape \ : already escaped by escapeInputString
|
||||
var EscapeCharsRegex = /[-|{}()[\]^$+*?.]/g;
|
||||
// FIXME escaping of / directly by adding / or \/ or // in EscapeCharsRegex don't work...
|
||||
return s.replace(EscapeCharsRegex, '\\$&').replace(/\//g, '\\/');
|
||||
}
|
||||
function escapeReplacementString(dont_escape,s) {
|
||||
// in replacement string, the GREL replace function handle in a specific way the Java special escape sequences
|
||||
// cf https://docs.oracle.com/javase/tutorial/java/data/characters.html
|
||||
// Escape Sequence | Java documentation | GREL replace function
|
||||
// \t | tab | tabs
|
||||
// \n | new line | new line
|
||||
// \r | carriage return | <blank>
|
||||
// \b | backspace | b
|
||||
// \f | formfeed | f
|
||||
// \' | ' | ''
|
||||
// \" | " | ""
|
||||
// \\ | \ | \
|
||||
// GREL replace function returns an error if a \ is not followed by an other character
|
||||
// it could be unexpected for the user, so the replace menu escape the replacement string, but gives the possibility to choose the default behavior
|
||||
// if dont_escape = 0, a complete escaping is done
|
||||
// if dont_escape = 1, no escaping is done, but a slight cleaning
|
||||
// parameters :
|
||||
// replace_dont_escape : 0 or 1
|
||||
// s : a string from a textarea
|
||||
if (typeof s != 'string') {return "";}
|
||||
var temp = s;
|
||||
if (dont_escape == 0) {temp = temp.replace(/\\/g, '\\\\\\\\');}
|
||||
// convert newlines and tabs manually typed in the textarea into \n and \t and suppress other non printable characters
|
||||
return temp.replace(/\n/g, '\\n').replace(/\t/g, '\\t').replace(/[\x00-\x1F\x80-\x9F]/g,'');
|
||||
}
|
||||
var frame = $(DOM.loadHTML("core", "scripts/views/data-table/replace-dialog.html"));
|
||||
var elmts = DOM.bind(frame);
|
||||
elmts.dialogHeader.text($.i18n._('core-views')["replace"]);
|
||||
elmts.or_views_text_to_find.text($.i18n._('core-views')["text-to-find"]);
|
||||
elmts.or_views_replacement.text($.i18n._('core-views')["replacement-text"]);
|
||||
elmts.or_views_finding_options.text($.i18n._('core-views')["finding-options"]);
|
||||
elmts.or_views_finding_info1.text($.i18n._('core-views')["finding-info1"]);
|
||||
elmts.or_views_finding_info2.text($.i18n._('core-views')["finding-info2"]);
|
||||
elmts.or_views_replacement_info.text($.i18n._('core-views')["replacement-info"]);
|
||||
elmts.or_views_replacement_options.text($.i18n._('core-views')["replacement-options"]);
|
||||
elmts.or_views_find_regExp.text($.i18n._('core-views')["reg-exp"]);
|
||||
elmts.or_views_find_case_insensitive.text($.i18n._('core-views')["case-insensitive"]);
|
||||
elmts.or_views_replace_dont_escape.text($.i18n._('core-views')["replace-dont-escape"]);
|
||||
elmts.okButton.html($.i18n._('core-buttons')["ok"]);
|
||||
elmts.cancelButton.text($.i18n._('core-buttons')["cancel"]);
|
||||
var level = DialogSystem.showDialog(frame);
|
||||
var dismiss = function() { DialogSystem.dismissUntil(level - 1); };
|
||||
elmts.cancelButton.click(dismiss);
|
||||
elmts.okButton.click(function() {
|
||||
var text_to_find = elmts.text_to_findTextarea[0].value;
|
||||
var replacement_text = elmts.replacementTextarea[0].value;
|
||||
var replace_dont_escape = elmts.replace_dont_escapeInput[0].checked;
|
||||
var find_regex = elmts.find_regexInput[0].checked;
|
||||
var find_case_insensitive = elmts.find_case_insensitiveInput[0].checked;
|
||||
replacement_text = escapeReplacementString(replace_dont_escape, replacement_text)
|
||||
if (find_regex) {
|
||||
text_to_find = escapeInputRegex(text_to_find);
|
||||
if (!isValidPattern (text_to_find)) {return}
|
||||
text_to_find = "/"+text_to_find+"/";
|
||||
if (find_case_insensitive) {text_to_find = text_to_find + "i"}
|
||||
}
|
||||
else {
|
||||
text_to_find = escapeInputString(text_to_find);
|
||||
if (find_case_insensitive) {
|
||||
text_to_find = stringToRegex(text_to_find);
|
||||
if (!isValidPattern (text_to_find)) {return}
|
||||
text_to_find = "/"+text_to_find+"/i";
|
||||
}
|
||||
else {
|
||||
text_to_find = '"'+text_to_find+'"';
|
||||
}
|
||||
}
|
||||
expression = 'value.replace('+text_to_find+',"'+replacement_text+'")';
|
||||
doTextTransform(expression, "keep-original", false, "");
|
||||
dismiss();
|
||||
});
|
||||
};
|
||||
|
||||
var doSplitMultiValueCells = function() {
|
||||
|
||||
var frame = $(DOM.loadHTML("core", "scripts/views/data-table/split-multi-valued-cells-dialog.html"));
|
||||
@ -273,6 +406,12 @@ DataTableColumnHeaderUI.extendMenu(function(column, columnHeaderUI, menu) {
|
||||
id: "core/to-empty",
|
||||
label: $.i18n._('core-views')["blank-out-empty"],
|
||||
click: function() { doTextTransform("\"\"", "keep-original", false, ""); }
|
||||
},
|
||||
{},
|
||||
{
|
||||
id: "core/replace",
|
||||
label: $.i18n._('core-views')["replace"],
|
||||
click: doReplace
|
||||
}
|
||||
]
|
||||
},
|
||||
|
@ -0,0 +1,45 @@
|
||||
<div class="dialog-frame" style="width: 600px;">
|
||||
<div class="dialog-border">
|
||||
<div class="dialog-header" bind="dialogHeader"></div>
|
||||
<div class="dialog-body" bind="dialogBody">
|
||||
<div class="grid-layout layout-looser layout-full">
|
||||
<table>
|
||||
<tr>
|
||||
<td>
|
||||
<div class="grid-layout layout-tighter">
|
||||
<table>
|
||||
<tr>
|
||||
<td >
|
||||
<h3 bind="or_views_text_to_find"></h3>
|
||||
<p bind="or_views_finding_options"></p>
|
||||
<input type="checkbox" bind="find_case_insensitiveInput" id="$find-case" />
|
||||
<label for="$find-case" bind="or_views_find_case_insensitive"></label>
|
||||
<input type="checkbox" bind="find_regexInput" id="$find-regex" />
|
||||
<label for="$find-regex" bind="or_views_find_regExp"></label>
|
||||
<p bind="or_views_finding_info1"></p>
|
||||
<p bind="or_views_finding_info2"></p>
|
||||
<textarea style="width: 100%;" bind="text_to_findTextarea"></textarea>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<h3 bind="or_views_replacement"></h3>
|
||||
<p bind="or_views_replacement_options"></p>
|
||||
<input type="checkbox" bind="replace_dont_escapeInput" id="$replace-escape" />
|
||||
<label for="$replace-escape" bind="or_views_replace_dont_escape"></label>
|
||||
<p bind="or_views_replacement_info"></p>
|
||||
<textarea style="width: 100%;" bind="replacementTextarea"></textarea>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</td>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<div class="dialog-footer" bind="dialogFooter">
|
||||
<button class="button" bind="okButton"></button>
|
||||
<button class="button" bind="cancelButton"></button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
Loading…
Reference in New Issue
Block a user