From bf1d16cdb71edc1a11898f54dd2925fba36bcc32 Mon Sep 17 00:00:00 2001 From: Peter Knut Date: Wed, 2 Oct 2024 11:08:13 +0200 Subject: [PATCH] Hide edited value if selected function will not use it --- adminer/include/adminer.inc.php | 2 +- adminer/include/functions.inc.php | 11 +++-- adminer/static/functions.js | 75 ++++++++++++++++++++----------- editor/include/adminer.inc.php | 6 ++- plugins/plugin.php | 2 +- 5 files changed, 61 insertions(+), 35 deletions(-) diff --git a/adminer/include/adminer.inc.php b/adminer/include/adminer.inc.php index d887cd4c..277e4fbe 100644 --- a/adminer/include/adminer.inc.php +++ b/adminer/include/adminer.inc.php @@ -751,7 +751,7 @@ class Adminer { * @param string * @return string custom input field or empty string for default */ - function editInput($table, $field, $attrs, $value) { + function editInput($table, $field, $attrs, $value, $function) { if ($field["type"] == "enum") { return (isset($_GET["select"]) ? " " : "") . ($field["null"] ? " " : "") diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index 692c3c94..9f5cb210 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -963,7 +963,7 @@ function input($field, $value, $function) { echo ""; if ($field["type"] == "enum") { - echo h($functions[""]) . "" . $adminer->editInput($_GET["edit"], $field, $attrs, $value); + echo h($functions[""]) . "" . $adminer->editInput($_GET["edit"], $field, $attrs, $value, $function); } else { $has_function = (in_array($function, $functions) || isset($functions[$function])); echo (count($functions) > 1 @@ -972,7 +972,7 @@ function input($field, $value, $function) { . script("qsl('select').onchange = functionChange;", "") : h(reset($functions)) ) . ''; - $input = $adminer->editInput($_GET["edit"], $field, $attrs, $value); // usage in call is without a table + $input = $adminer->editInput($_GET["edit"], $field, $attrs, $value, $function); // usage in call is without a table if ($input != "") { echo $input; } elseif (preg_match('~bool~', $field["type"])) { @@ -1006,7 +1006,8 @@ function input($field, $value, $function) { // type='date' and type='time' display localized value which may be confusing, type='datetime' uses 'T' as date and time separator echo " 20 ? " size='40'" : "") . "$attrs>" ; @@ -1020,9 +1021,7 @@ function input($field, $value, $function) { } $first++; } - if ($first) { - echo script("mixin(qsl('td'), {onchange: partial(skipOriginal, $first), oninput: function () { this.onchange(); }});"); - } + echo script("mixin(qsl('td'), {onchange: partial(skipOriginal, $first), oninput: function () { this.onchange(); }});"); } } diff --git a/adminer/static/functions.js b/adminer/static/functions.js index c5d8107f..5f62992d 100644 --- a/adminer/static/functions.js +++ b/adminer/static/functions.js @@ -547,37 +547,62 @@ function editingKeydown(event) { return true; } -/** Disable maxlength for functions -* @this HTMLSelectElement -*/ +/** + * Disables maxlength for functions and manages value visibility. + * + * @this HTMLSelectElement + */ function functionChange() { - var input = this.form[this.name.replace(/^function/, 'fields')]; - if (input) { // undefined with the set data type - if (selectValue(this)) { - if (input.origType === undefined) { - input.origType = input.type; - input.origMaxLength = input.getAttribute('data-maxlength'); - } - input.removeAttribute('data-maxlength'); - input.type = 'text'; - } else if (input.origType) { - input.type = input.origType; - if (input.origMaxLength >= 0) { - input.setAttribute('data-maxlength', input.origMaxLength); - } - } - oninput({target: input}); + const input = this.form[this.name.replace(/^function/, 'fields')]; + const value = selectValue(this); + + // Undefined with the set data type. + if (!input) { + helpClose(); + return; } + + if (value) { + if (input.origType === undefined) { + input.origType = input.type; + input.origMaxLength = input.getAttribute('data-maxlength'); + } + + input.removeAttribute('data-maxlength'); + input.type = 'text'; + } else if (input.origType) { + input.type = input.origType; + if (input.origMaxLength >= 0) { + input.setAttribute('data-maxlength', input.origMaxLength); + } + } + + // Hide input value if it will be not used by selected function. + if (value === "NULL" || value === "now") { + if (input.value !== "") { + input.dataset.lastValue = input.value; + input.value = ""; + } + } else if (input.dataset.lastValue) { + input.value = input.dataset.lastValue; + } + + oninput({target: input}); + helpClose(); } -/** Skip 'original' when typing -* @param number -* @this HTMLTableCellElement -*/ +/** + * Unset 'original', 'NULL' and 'now' functions when typing. + * + * @param first number + * @this HTMLTableCellElement + */ function skipOriginal(first) { - var fnSelect = this.previousSibling.firstChild; - if (fnSelect.selectedIndex < first) { + const fnSelect = this.previousSibling.firstChild; + const value = selectValue(fnSelect); + + if (fnSelect.selectedIndex < first || value === "NULL" || value === "now") { fnSelect.selectedIndex = first; } } diff --git a/editor/include/adminer.inc.php b/editor/include/adminer.inc.php index 4d6d68f3..be9d7436 100644 --- a/editor/include/adminer.inc.php +++ b/editor/include/adminer.inc.php @@ -483,7 +483,7 @@ ORDER BY ORDINAL_POSITION", null, "") as $row) { //! requires MySQL 5 return $return; } - function editInput($table, $field, $attrs, $value) { + function editInput($table, $field, $attrs, $value, $function) { if ($field["type"] == "enum") { return (isset($_GET["select"]) ? " " : "") . enum_input("radio", $attrs, $field, ($value || isset($_GET["select"]) ? $value : 0), ($field["null"] ? "" : null)) @@ -511,7 +511,9 @@ qsl('div').onclick = whisperClick;", "") $hint = lang('[yyyy]-mm-dd') . ($hint ? " [$hint]" : ""); } if ($hint) { - return " ($hint)"; //! maxlength + return " ($hint)"; //! maxlength } if (preg_match('~_(md5|sha1)$~i', $field["field"])) { return ""; diff --git a/plugins/plugin.php b/plugins/plugin.php index 6defead8..ec7d9c04 100644 --- a/plugins/plugin.php +++ b/plugins/plugin.php @@ -335,7 +335,7 @@ class AdminerPlugin extends Adminer { return $this->_applyPlugin(__FUNCTION__, $args); } - function editInput($table, $field, $attrs, $value) { + function editInput($table, $field, $attrs, $value, $function) { $args = func_get_args(); return $this->_applyPlugin(__FUNCTION__, $args); }