Skip to content

Commit 9961b93

Browse files
committed
ignore-space-improvements
1 parent 896fe23 commit 9961b93

File tree

1 file changed

+56
-46
lines changed

1 file changed

+56
-46
lines changed

asset/js/widget/TermInput.js

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
88

99
this.separator = this.input.dataset.termSeparator || ' ';
1010
this.ignoreSpaceUntil = null;
11-
this.ignoreSpaceSince = null;
1211
}
1312

1413
bind() {
@@ -23,36 +22,54 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
2322
super.reset();
2423

2524
this.ignoreSpaceUntil = null;
26-
this.ignoreSpaceSince = null;
25+
}
26+
27+
readPartialTerm(input) {
28+
let value = super.readPartialTerm(input);
29+
if (value && this.ignoreSpaceUntil && value[0] === this.ignoreSpaceUntil) {
30+
value = value.slice(1);
31+
if (value.slice(-1) === this.ignoreSpaceUntil) {
32+
value = value.slice(0, -1);
33+
}
34+
}
35+
36+
return value;
2737
}
2838

2939
readFullTerm(input, termIndex = null) {
3040
let termData = super.readFullTerm(input, termIndex);
31-
if (this.ignoreSpaceUntil !== null && termData.label[this.ignoreSpaceSince] === this.ignoreSpaceUntil) {
32-
if (termData.label.length - 1 === this.ignoreSpaceSince
33-
|| termData.label.slice(-1) !== this.ignoreSpaceUntil
34-
|| (this.ignoreSpaceSince === 0 && (termData.label.length < 2
35-
|| termData.label.slice(0, 1) !== this.ignoreSpaceUntil)
36-
)
37-
) {
41+
if (termData && this.ignoreSpaceUntil !== null && input.value[0] === this.ignoreSpaceUntil) {
42+
if (input.value.slice(-1) !== this.ignoreSpaceUntil || input.value.length < 2) {
3843
return false;
3944
}
45+
46+
this.ignoreSpaceUntil = null;
4047
}
4148

4249
return termData;
4350
}
4451

45-
addTerm(termData, termIndex = null) {
46-
if (this.ignoreSpaceUntil !== null) {
47-
if (this.ignoreSpaceSince === 0 && termData.label[this.ignoreSpaceSince] === this.ignoreSpaceUntil) {
48-
termData.label = termData.label.slice(1, -1);
52+
hasSyntaxError(input) {
53+
if (input === this.input && this.ignoreSpaceUntil !== null) {
54+
if (input.value === this.ignoreSpaceUntil) {
55+
return true;
4956
}
57+
}
5058

51-
this.ignoreSpaceUntil = null;
52-
this.ignoreSpaceSince = null;
59+
return super.hasSyntaxError(input);
60+
}
61+
62+
termsToQueryString(terms) {
63+
let quoted = [];
64+
for (const termData of terms) {
65+
if (termData.search.indexOf(this.separator) >= 0) {
66+
quoted.push({ ...termData, search: '"' + termData.search + '"' });
67+
} else {
68+
quoted.push(termData);
69+
}
5370
}
5471

55-
super.addTerm(termData, termIndex);
72+
return super.termsToQueryString(quoted);
5673
}
5774

5875
complete(input, data) {
@@ -95,7 +112,29 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
95112
super.onSubmit(event);
96113

97114
this.ignoreSpaceUntil = null;
98-
this.ignoreSpaceSince = null;
115+
}
116+
117+
onInput(event) {
118+
let label = event.target.parentNode;
119+
if (label.dataset.index >= 0) {
120+
return;
121+
}
122+
123+
let input = event.target;
124+
let firstChar = input.value[0];
125+
126+
if (this.ignoreSpaceUntil !== null) {
127+
// Reset if the user changes/removes the source char
128+
if (firstChar !== this.ignoreSpaceUntil) {
129+
this.ignoreSpaceUntil = null;
130+
}
131+
}
132+
133+
if (this.ignoreSpaceUntil === null && (firstChar === "'" || firstChar === '"')) {
134+
this.ignoreSpaceUntil = firstChar;
135+
}
136+
137+
super.onInput(event);
99138
}
100139

101140
onKeyDown(event) {
@@ -121,34 +160,6 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
121160
}
122161
}
123162

124-
onKeyUp(event) {
125-
super.onKeyUp(event);
126-
127-
let label = event.target.parentNode;
128-
if (label.dataset.index >= 0) {
129-
return;
130-
}
131-
132-
if (this.ignoreSpaceUntil !== null) {
133-
// Reset if the user changes/removes the source char
134-
let value = event.target.value;
135-
if (value[this.ignoreSpaceSince] !== this.ignoreSpaceUntil) {
136-
this.ignoreSpaceUntil = null;
137-
this.ignoreSpaceSince = null;
138-
}
139-
}
140-
141-
let input = event.target;
142-
let firstChar = this.readPartialTerm(input).charAt(0);
143-
144-
if (firstChar === "'" || firstChar === '"') {
145-
if (this.ignoreSpaceUntil === null) {
146-
this.ignoreSpaceUntil = firstChar;
147-
this.ignoreSpaceSince = input.selectionStart - 1;
148-
}
149-
}
150-
}
151-
152163
onTermClick(event) {
153164
let termIndex = event.target.parentNode.dataset.index;
154165
this.removeTerm(event.target.parentNode);
@@ -167,7 +178,6 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
167178
event.preventDefault();
168179

169180
this.ignoreSpaceUntil = null;
170-
this.ignoreSpaceSince = null;
171181
}
172182
}
173183
}

0 commit comments

Comments
 (0)