From bed984f5a90e974e2546b493d616dfe930de1358 Mon Sep 17 00:00:00 2001 From: Benoit Rapidel Date: Mon, 9 Nov 2020 16:04:36 +0100 Subject: [PATCH 1/4] Add fieldValidator property for input widget Allows to filter value against specified chars --- src/client/widgets/basics/input.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/client/widgets/basics/input.js b/src/client/widgets/basics/input.js index 41960923..a5dcb6fb 100644 --- a/src/client/widgets/basics/input.js +++ b/src/client/widgets/basics/input.js @@ -19,7 +19,11 @@ class Input extends Canvas { unit: {type: 'string', value: '', help: 'Unit will be appended to the displayed widget\'s value (it doesn\'t affect osc messages)'}, }, class_specific: { - asYouType: {type: 'boolean', value: false, help: 'Set to `true` to make the input send its value at each keystroke'} + asYouType: {type: 'boolean', value: false, help: 'Set to `true` to make the input send its value at each keystroke'}, + fieldValidator: {type: 'string|boolean', value: false, help: [ + 'Set to `abc` to make the input reject all characters, except a, b or c', + '`0-9` will only allow integer to be entered', + '`a-z` will only allow lowercase characters']} } }) @@ -37,6 +41,7 @@ class Input extends Canvas { this.stringValue = '' this.focused = false this.tabKeyBlur = false + this.fieldValidator = false if (this.getProp('vertical')) this.widget.classList.add('vertical') if (this.getProp('align') === 'left') this.widget.classList.add('left') @@ -53,6 +58,7 @@ class Input extends Canvas { this.tabKeyBlur = false }) var asYouType = this.getProp('asYouType') + this.fieldValidator = new RegExp('[^'+this.getProp('fieldValidator')+']', 'g') this.input.addEventListener('keydown', (e)=>{ if (e.keyCode === 13) this.blur() // enter else if (e.keyCode === 27) this.blur(false) // esc @@ -95,6 +101,9 @@ class Input extends Canvas { inputChange() { + if (this.fieldValidator) { + this.input.value = this.input.value.replace(this.fieldValidator, ''); + } this.setValue(this.input.value, {sync:true, send:true}) } From 04f75fc30d3601c2352b5c38f632115e0d5ff07b Mon Sep 17 00:00:00 2001 From: Benoit Rapidel Date: Mon, 9 Nov 2020 18:48:36 +0100 Subject: [PATCH 2/4] Initialize fieldValidator with null instead of false --- src/client/widgets/basics/input.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/widgets/basics/input.js b/src/client/widgets/basics/input.js index a5dcb6fb..1953c262 100644 --- a/src/client/widgets/basics/input.js +++ b/src/client/widgets/basics/input.js @@ -41,7 +41,7 @@ class Input extends Canvas { this.stringValue = '' this.focused = false this.tabKeyBlur = false - this.fieldValidator = false + this.fieldValidator = null if (this.getProp('vertical')) this.widget.classList.add('vertical') if (this.getProp('align') === 'left') this.widget.classList.add('left') From 0bb7a2bdf65c0c5ed6e7f5bc68578bc4bb29b699 Mon Sep 17 00:00:00 2001 From: Benoit Rapidel Date: Mon, 9 Nov 2020 18:49:49 +0100 Subject: [PATCH 3/4] Only assign RegExp if property is different from false --- src/client/widgets/basics/input.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/client/widgets/basics/input.js b/src/client/widgets/basics/input.js index 1953c262..e85955f8 100644 --- a/src/client/widgets/basics/input.js +++ b/src/client/widgets/basics/input.js @@ -58,7 +58,9 @@ class Input extends Canvas { this.tabKeyBlur = false }) var asYouType = this.getProp('asYouType') - this.fieldValidator = new RegExp('[^'+this.getProp('fieldValidator')+']', 'g') + if (this.getProp('fieldValidator')) { + this.fieldValidator = new RegExp('[^'+this.getProp('fieldValidator')+']', 'g') + } this.input.addEventListener('keydown', (e)=>{ if (e.keyCode === 13) this.blur() // enter else if (e.keyCode === 27) this.blur(false) // esc From 9228d39e7203cfbff185db9de349108e06cbce96 Mon Sep 17 00:00:00 2001 From: bordun Date: Mon, 9 Nov 2020 19:14:53 +0100 Subject: [PATCH 4/4] input: tweak fieldValidator implementation --- src/client/widgets/basics/input.js | 34 +++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/client/widgets/basics/input.js b/src/client/widgets/basics/input.js index e85955f8..35393efc 100644 --- a/src/client/widgets/basics/input.js +++ b/src/client/widgets/basics/input.js @@ -20,10 +20,13 @@ class Input extends Canvas { }, class_specific: { asYouType: {type: 'boolean', value: false, help: 'Set to `true` to make the input send its value at each keystroke'}, - fieldValidator: {type: 'string|boolean', value: false, help: [ - 'Set to `abc` to make the input reject all characters, except a, b or c', - '`0-9` will only allow integer to be entered', - '`a-z` will only allow lowercase characters']} + fieldValidator: {type: 'string', value: '', help: [ + 'Regular expression: if the submitted value doesn\'t match the regular expression, it will be reset to the last valid value.', + 'If leading and trailing slashes are omitted, they will be added automatically and the flag will be set to "gm"', + 'Examples:', + '- `^[0-9]*$` accepts digits only, any number of them', + '- `/^[a-z\s]{0,10}$/i` accept between 0 and 10 alphabetic characters and spaces (case insensitive)', + ]} } }) @@ -58,9 +61,22 @@ class Input extends Canvas { this.tabKeyBlur = false }) var asYouType = this.getProp('asYouType') - if (this.getProp('fieldValidator')) { - this.fieldValidator = new RegExp('[^'+this.getProp('fieldValidator')+']', 'g') + + if (this.getProp('fieldValidator') !== '') { + var fieldValidator = String(this.getProp('fieldValidator')), + flags = fieldValidator.match(/^\/.*\/.*$/) ? fieldValidator.split('/').pop() : 'gm', + validator = fieldValidator.match(/^\/.*\/.*$/) ? fieldValidator.replace(/^\/(.*)\/.*$/, '$1') : fieldValidator + + if (!validator.match(/^\^.*\$$/)) validator = '^' + validator + '$' + + try { + this.fieldValidator = new RegExp(validator, flags) + } catch(err) { + console.error((this.getProp('id') || this.props.id) + '.' + propName + ':\n') + console.error(err) + } } + this.input.addEventListener('keydown', (e)=>{ if (e.keyCode === 13) this.blur() // enter else if (e.keyCode === 27) this.blur(false) // esc @@ -103,9 +119,11 @@ class Input extends Canvas { inputChange() { - if (this.fieldValidator) { - this.input.value = this.input.value.replace(this.fieldValidator, ''); + if (this.fieldValidator && !this.input.value.match(this.fieldValidator)) { + this.input.value = this.value + return } + this.setValue(this.input.value, {sync:true, send:true}) }