Skip to content

Commit d0f4a75

Browse files
committed
BaseInput.js: Enhance pasted input processing
1 parent 376cc1b commit d0f4a75

File tree

3 files changed

+65
-33
lines changed

3 files changed

+65
-33
lines changed

asset/js/widget/BaseInput.js

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,22 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
180180
// Reset the data input, otherwise the value remains and is sent continuously with subsequent requests
181181
this.dataInput.value = '';
182182

183-
for (const termIndex of Object.keys(changedTerms)) {
183+
if (changedTerms === 'bogus') {
184+
return;
185+
}
186+
187+
let changedIndices = Object.keys(changedTerms);
188+
if (! changedIndices.length) {
189+
// Perform a partial reset. this.reset() empties the termContainer, which isn't desired here
190+
this.usedTerms = [];
191+
this.lastCompletedTerm = null;
192+
193+
this.registerTerms();
194+
this.togglePlaceholder();
195+
this.termInput.value = '';
196+
}
197+
198+
for (const termIndex of changedIndices) {
184199
let label = this.termContainer.querySelector(`[data-index="${ termIndex }"]`);
185200
if (! label) {
186201
continue;
@@ -563,26 +578,34 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
563578
return 'noAutoSubmit' in this.input.dataset;
564579
}
565580

566-
autoSubmit(input, changeType, changedTerms) {
581+
autoSubmit(input, changeType, data) {
567582
if (this.shouldNotAutoSubmit()) {
568583
return;
569584
}
570585

571-
if (changeType === 'save') {
586+
if (changeType === 'save' && 'terms' in data) {
572587
// Replace old term data with the new one, as required by the backend
573-
for (const termIndex of Object.keys(changedTerms)) {
574-
changedTerms[termIndex] = this.usedTerms[termIndex];
588+
for (const termIndex of Object.keys(data['terms'])) {
589+
data['terms'][termIndex] = this.usedTerms[termIndex];
575590
}
576591
}
577592

578-
if (Object.keys(changedTerms).length) {
579-
this.dataInput.value = JSON.stringify({
580-
type: changeType,
581-
terms: changedTerms
582-
});
593+
if (changeType === 'remove' && ! Object.keys(data['terms']).length) {
594+
return;
595+
}
583596

584-
$(this.input.form).trigger('submit', { submittedBy: input });
597+
this.dataInput.value = JSON.stringify({
598+
type: changeType,
599+
...data
600+
});
601+
602+
let eventData = { submittedBy: input };
603+
if (changeType === 'paste') {
604+
// Ensure that what's pasted is also transmitted as value
605+
eventData['terms'] = this.termsToQueryString(data['terms']) + this.separator + data['input'];
585606
}
607+
608+
$(this.input.form).trigger('submit', eventData);
586609
}
587610

588611
submitTerms(terms) {
@@ -691,9 +714,9 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
691714
this.checkValidity(input);
692715

693716
if (termIndex >= 0) {
694-
this.autoSubmit(input, 'save', { [termIndex]: this.saveTerm(input, false, true) });
717+
this.autoSubmit(input, 'save', { terms: { [termIndex]: this.saveTerm(input, false, true) } });
695718
} else {
696-
this.autoSubmit(input, 'exchange', this.exchangeTerm());
719+
this.autoSubmit(input, 'exchange', { terms: this.exchangeTerm() });
697720
this.togglePlaceholder();
698721
}
699722
}
@@ -718,7 +741,7 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
718741
}
719742

720743
if (! isTerm) {
721-
this.autoSubmit(this.input, 'remove', this.clearSelectedTerms());
744+
this.autoSubmit(this.input, 'remove', { terms: this.clearSelectedTerms() });
722745
this.togglePlaceholder();
723746
}
724747
}
@@ -774,7 +797,7 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
774797
}
775798

776799
this.togglePlaceholder();
777-
this.autoSubmit(input, 'remove', removedTerms);
800+
this.autoSubmit(input, 'remove', { terms: removedTerms });
778801
break;
779802
case 'Delete':
780803
removedTerms = this.clearSelectedTerms();
@@ -795,7 +818,7 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
795818
}
796819

797820
this.togglePlaceholder();
798-
this.autoSubmit(input, 'remove', removedTerms);
821+
this.autoSubmit(input, 'remove', { terms: removedTerms });
799822
break;
800823
case 'Enter':
801824
if (termIndex >= 0) {
@@ -847,7 +870,7 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
847870

848871
break;
849872
case 'Delete':
850-
this.autoSubmit(event.target, 'remove', this.clearSelectedTerms());
873+
this.autoSubmit(event.target, 'remove', { terms: this.clearSelectedTerms() });
851874
this.togglePlaceholder();
852875
break;
853876
}
@@ -873,10 +896,11 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
873896
if (this.readPartialTerm(input)) {
874897
let previousTerm = this.saveTerm(input);
875898
if (previousTerm !== false) {
876-
this.autoSubmit(input, 'save', { [termIndex]: previousTerm });
899+
this.autoSubmit(input, 'save', { terms: { [termIndex]: previousTerm } });
877900
}
878901
} else {
879-
this.autoSubmit(input, 'remove', { [termIndex]: this.removeTerm(input.parentNode) });
902+
this.autoSubmit(
903+
input, 'remove', { terms: { [termIndex]: this.removeTerm(input.parentNode) } });
880904
}
881905
}
882906
}, 0);
@@ -923,11 +947,14 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
923947
}
924948

925949
onPaste(event) {
926-
if (this.shouldNotAutoSubmit() || this.hasTerms() || this.input.value) {
950+
if (this.shouldNotAutoSubmit() || this.input.value) {
927951
return;
928952
}
929953

930-
this.autoSubmit(this.input, 'paste', [event.clipboardData.getData('text/plain')]);
954+
this.autoSubmit(this.input, 'paste', {
955+
input: event.clipboardData.getData('text/plain'),
956+
terms: this.usedTerms
957+
});
931958

932959
event.preventDefault();
933960
}
@@ -953,7 +980,7 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
953980

954981
if (event.type === 'cut') {
955982
this.clearPartialTerm(this.input);
956-
this.autoSubmit(this.input, 'remove', this.clearSelectedTerms());
983+
this.autoSubmit(this.input, 'remove', { terms: this.clearSelectedTerms() });
957984
this.togglePlaceholder();
958985
}
959986
}

asset/js/widget/FilterInput.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -970,11 +970,16 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
970970
return label;
971971
}
972972

973-
autoSubmit(input, changeType, changedTerms) {
973+
autoSubmit(input, changeType, data) {
974974
if (this.shouldNotAutoSubmit()) {
975975
return;
976976
}
977977

978+
let changedTerms = [];
979+
if ('terms' in data) {
980+
changedTerms = data['terms'];
981+
}
982+
978983
let changedIndices = Object.keys(changedTerms).sort((a, b) => a - b);
979984
if (! changedIndices.length) {
980985
return;
@@ -987,7 +992,7 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
987992
lastTermAt = changedIndices.pop();
988993
if (changedTerms[lastTermAt].type === 'value') {
989994
if (! changedIndices.length) {
990-
changedTerms = {
995+
data['terms'] = {
991996
...{
992997
[lastTermAt - 2]: this.usedTerms[lastTermAt - 2],
993998
[lastTermAt - 1]: this.usedTerms[lastTermAt - 1]
@@ -1030,7 +1035,7 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
10301035

10311036
if (valueAt === updateAt) {
10321037
if (changedIndices.length === 1) {
1033-
changedTerms = {
1038+
data['terms'] = {
10341039
...{
10351040
[valueAt - 2]: this.usedTerms[valueAt - 2],
10361041
[valueAt - 1]: this.usedTerms[valueAt - 1]
@@ -1058,7 +1063,7 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
10581063
return;
10591064
}
10601065

1061-
super.autoSubmit(input, changeType, changedTerms);
1066+
super.autoSubmit(input, changeType, data);
10621067
}
10631068

10641069
encodeTerm(termData) {
@@ -1307,7 +1312,7 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
13071312
}
13081313
}
13091314

1310-
this.autoSubmit(this.input, 'remove', this.removeRange(labels));
1315+
this.autoSubmit(this.input, 'remove', { terms: this.removeRange(labels) });
13111316
this.togglePlaceholder();
13121317
}
13131318

@@ -1421,7 +1426,7 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
14211426

14221427
if (newTerm !== null) {
14231428
let label = this.insertTerm(newTerm, termIndex + 1);
1424-
this.autoSubmit(label.firstChild, 'insert', { [termIndex + 1]: newTerm });
1429+
this.autoSubmit(label.firstChild, 'insert', { terms: { [termIndex + 1]: newTerm } });
14251430
this.complete(label.firstChild, { term: newTerm });
14261431
$(label.firstChild).focus({ scripted: true });
14271432
event.preventDefault();
@@ -1432,13 +1437,13 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
14321437
this.togglePlaceholder();
14331438
} else if (operators.exactMatch) {
14341439
if (termType !== operators[0].type) {
1435-
this.autoSubmit(input, 'exchange', this.exchangeTerm());
1440+
this.autoSubmit(input, 'exchange', { terms: this.exchangeTerm() });
14361441
} else {
14371442
this.clearPartialTerm(input);
14381443
}
14391444

14401445
this.addTerm({ ...operators[0] });
1441-
this.autoSubmit(input, 'add', { [this.usedTerms.length - 1]: operators[0] });
1446+
this.autoSubmit(input, 'add', { terms: { [this.usedTerms.length - 1]: operators[0] } });
14421447
this.togglePlaceholder();
14431448
event.preventDefault();
14441449
} else if (termType === 'operator') {
@@ -1484,7 +1489,7 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
14841489
if (isTerm && input.checkValidity()) {
14851490
let value = this.readPartialTerm(input);
14861491
if (value && ! ['column', 'value'].includes(input.parentNode.dataset.type)) {
1487-
this.autoSubmit(input, 'save', { [termIndex]: this.saveTerm(input) });
1492+
this.autoSubmit(input, 'save', { terms: { [termIndex]: this.saveTerm(input) } });
14881493
}
14891494
}
14901495
}

asset/js/widget/TermInput.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ define(["BaseInput"], function (BaseInput) {
131131
if (Object.keys(addedTerms).length) {
132132
this.togglePlaceholder();
133133
event.preventDefault();
134-
this.autoSubmit(this.input, 'exchange', addedTerms);
134+
this.autoSubmit(this.input, 'exchange', { terms: addedTerms });
135135
}
136136
}
137137

@@ -169,7 +169,7 @@ define(["BaseInput"], function (BaseInput) {
169169
if (Object.keys(addedTerms).length) {
170170
this.togglePlaceholder();
171171
event.preventDefault();
172-
this.autoSubmit(this.input, 'exchange', addedTerms);
172+
this.autoSubmit(this.input, 'exchange', { terms: addedTerms });
173173
this.ignoreSpaceUntil = null;
174174

175175
return;

0 commit comments

Comments
 (0)