Skip to content

Commit

Permalink
BaseInput.js: Transfer basic validation from FilterInput.js
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Apr 27, 2023
1 parent dd2b3ca commit f6c0900
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 63 deletions.
60 changes: 59 additions & 1 deletion asset/js/widget/BaseInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
}

this.registerTerm(this.decodeTerm(termData), label.dataset.index);
this.validate(label.firstChild);
});
}

Expand Down Expand Up @@ -317,7 +318,41 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
return this.termsToQueryString(this.usedTerms);
}

checkValidity(input) {
if (input.pattern && ! input.checkValidity()) {
if (! input.value.match(input.pattern)) {
if (input.dataset.invalidMsg) {
input.setCustomValidity(input.dataset.invalidMsg);
}

return false;
}

input.setCustomValidity('');
}

return input.checkValidity();
}

reportValidity(element) {
setTimeout(() => element.reportValidity(), 0);
}

validate(element) {
if (! this.checkValidity(element)) {
this.reportValidity(element);

return false;
}

return true;
}

saveTerm(input, updateDOM = true, force = false) {
if (! this.checkValidity(input)) {
return false;
}

let termIndex = input.parentNode.dataset.index;
let termData = this.readFullTerm(input, termIndex);

Expand Down Expand Up @@ -348,6 +383,18 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
} else {
label.title = '';
}

if (termData.pattern) {
input.pattern = termData.pattern;
delete termData.pattern;

if (termData.invalidMsg) {
input.dataset.invalidMsg = termData.invalidMsg;
delete termData.invalidMsg;
}

this.validate(input);
}
}

termsToQueryString(terms) {
Expand Down Expand Up @@ -640,6 +687,8 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
this.lastCompletedTerm = termData;
this.writePartialTerm(termData.label, input);

this.checkValidity(input);

if (termIndex >= 0) {
this.autoSubmit(input, 'save', { [termIndex]: this.saveTerm(input, false, true) });
} else {
Expand All @@ -660,6 +709,10 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
}

if (! this.hasSyntaxError(input)) {
if (isTerm && ! this.validate(input)) {
return;
}

this.complete(input, { term: termData });
}

Expand Down Expand Up @@ -830,14 +883,19 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
}

onTermFocus(event) {
let input = event.target;

if (input.parentNode.dataset.index >= 0) {
this.validate(input);
}

if (event.detail.scripted) {
// Only request suggestions if the user manually focuses the term
return;
}

this.deselectTerms();

let input = event.target;
if (! this.hasSyntaxError(input) && (
this.completer === null || ! this.completer.isBeingCompleted(input, false)
)) {
Expand Down
78 changes: 16 additions & 62 deletions asset/js/widget/FilterInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,6 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
return termIndex;
}

updateTermData(termData, input) {
super.updateTermData(termData, input);

if (termData.pattern) {
input.pattern = termData.pattern;
delete termData.pattern;

if (termData.invalidMsg) {
input.dataset.invalidMsg = termData.invalidMsg;
delete termData.invalidMsg;
}

if (! this.checkValidity(input)) {
this.reportValidity(input);
}
}
}

readFullTerm(input, termIndex = null) {
let termData = super.readFullTerm(input, termIndex);
if (termData === false) {
Expand Down Expand Up @@ -355,14 +337,6 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
lastTerm.classList.add('last-term');
}

saveTerm(input, updateDOM = true, force = false) {
if (! this.checkValidity(input)) {
return false;
}

return super.saveTerm(input, updateDOM, force);
}

termsToQueryString(terms) {
if (! this.input.form.checkValidity()) {
let filtered = [];
Expand All @@ -386,8 +360,7 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
let termIndex = Number(label.dataset.index);
if (termIndex < this.usedTerms.length - 1) {
// It's not the last term
if (! this.checkValidity(label.firstChild, label.dataset.type, termIndex)) {
this.reportValidity(label.firstChild);
if (! this.validate(label.firstChild)) {
return false;
}
}
Expand Down Expand Up @@ -838,21 +811,13 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
type = input.parentNode.dataset.type;
}

if (input.pattern && ! input.checkValidity()) {
if (! input.value.match(input.pattern)) {
if (input.dataset.invalidMsg) {
input.setCustomValidity(input.dataset.invalidMsg);
}

return false;
}

input.setCustomValidity('');
if (! ['operator', 'logical_operator'].includes(type) && ! super.checkValidity(input)) {
return false;
}

if (! type || type === 'value') {
// type is undefined for the main input, values have no special validity rules
return input.checkValidity();
return true;
}

if (termIndex === null && input.parentNode.dataset.index >= 0) {
Expand Down Expand Up @@ -949,10 +914,6 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
return input.checkValidity();
}

reportValidity(element) {
setTimeout(() => element.reportValidity(), 0);
}

renderSuggestions(suggestions) {
let itemTemplate = $.render('<li><input type="button" tabindex="-1"></li>');

Expand Down Expand Up @@ -1247,22 +1208,19 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {

onTermFocus(event) {
let input = event.target;
let isTerm = input.parentNode.dataset.index >= 0;
let termType = input.parentNode.dataset.type || this.termType;

if (input.parentNode.dataset.index >= 0) {
if (
! this.checkValidity(input, termType)
&& termType !== 'operator'
&& termType !== 'logical_operator'
) {
this.reportValidity(input);
}

if (isTerm) {
this.highlightTerm(input.parentNode);
}

let value = this.readPartialTerm(input);
if (! value && (termType === 'column' || termType === 'value')) {
if (isTerm) {
this.validate(input);
}

// No automatic suggestions without input
return;
}
Expand Down Expand Up @@ -1356,10 +1314,7 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
onCompletion(event) {
super.onCompletion(event);

let input = event.target;
this.checkValidity(input);

if (input.parentNode.dataset.index >= 0) {
if (event.target.parentNode.dataset.index >= 0) {
return;
}

Expand Down Expand Up @@ -1505,12 +1460,11 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
// pass
} else if (termIndex >= 0) {
let value = this.readPartialTerm(input);
if (! this.checkValidity(input)) {
this.reportValidity(input);
// Let inputs also grow upon invalid input
this.updateTermData({ label: value }, input);
return;
} else if (value && ! ['column', 'value'].includes(input.parentNode.dataset.type)) {
if (
value
&& ! ['column', 'value'].includes(input.parentNode.dataset.type)
&& this.checkValidity(input)
) {
this.autoSubmit(input, 'save', { [termIndex]: this.saveTerm(input) });
}
} else if (this.termType === 'operator' || this.termType === 'logical_operator') {
Expand Down

0 comments on commit f6c0900

Please sign in to comment.