Skip to content

Commit fe42110

Browse files
Introduce new dataset Attributes
'data-term-separator', 'data-term-mode' and 'data-term-direction'
1 parent a5fc0e2 commit fe42110

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

asset/js/widget/BaseInput.js

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
5656
$(this.termContainer).on('keyup', '[data-label]', this.onKeyUp, this);
5757
$(this.termContainer).on('focusout', '[data-index]', this.onTermFocusOut, this);
5858
$(this.termContainer).on('focusin', '[data-index]', this.onTermFocus, this);
59+
$(this.termContainer).on('click', '[data-index]', this.onTermClick, this);
5960

6061
// Copy/Paste
6162
$(this.input).on('paste', this.onPaste, this);
@@ -468,7 +469,14 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
468469
}
469470

470471
renderTerm(termData, termIndex) {
471-
let label = $.render('<label><input type="text"></label>');
472+
let inputContainer;
473+
if (this.isReadOnlyMode()) {
474+
inputContainer = '<label><input type="button"><i class="icon-cancel term-icon"></i></label>';
475+
} else {
476+
inputContainer = '<label><input type="input"></label>';
477+
}
478+
479+
let label = $.render(inputContainer);
472480

473481
if (termData.class) {
474482
label.classList.add(termData.class);
@@ -539,7 +547,10 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
539547
toFocus = this.input;
540548
}
541549

542-
toFocus.selectionStart = toFocus.selectionEnd = 0;
550+
if (! this.isReadOnlyMode()) {
551+
toFocus.selectionStart = toFocus.selectionEnd = 0;
552+
}
553+
543554
$(toFocus).focus();
544555

545556
return toFocus;
@@ -562,7 +573,10 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
562573
toFocus = this.input;
563574
}
564575

565-
toFocus.selectionStart = toFocus.selectionEnd = toFocus.value.length;
576+
if (! this.isReadOnlyMode()) {
577+
toFocus.selectionStart = toFocus.selectionEnd = toFocus.value.length;
578+
}
579+
566580
$(toFocus).focus();
567581

568582
return toFocus;
@@ -657,6 +671,8 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
657671
}
658672
}
659673

674+
let arrowUpOrLeft = this.isTermDirectionVertical() ? 'ArrowUp' : 'ArrowLeft';
675+
let arrowDownOrRight = this.isTermDirectionVertical() ? 'ArrowDown' : 'ArrowRight';
660676
let removedTerms;
661677
switch (event.key) {
662678
case ' ':
@@ -723,14 +739,17 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
723739
this.saveTerm(input, false);
724740
}
725741
break;
726-
case 'ArrowLeft':
727-
if (input.selectionStart === 0 && this.hasTerms()) {
742+
case arrowUpOrLeft:
743+
if ((input.selectionStart === 0 || input.selectionStart === null && this.isReadOnlyMode())
744+
&& this.hasTerms()) {
728745
event.preventDefault();
729746
this.moveFocusBackward();
730747
}
731748
break;
732-
case 'ArrowRight':
733-
if (input.selectionStart === input.value.length && this.hasTerms()) {
749+
case arrowDownOrRight:
750+
if ((input.selectionStart === input.value.length
751+
|| input.selectionStart === null && this.isReadOnlyMode())
752+
&& this.hasTerms()) {
734753
event.preventDefault();
735754
this.moveFocusForward();
736755
}
@@ -812,6 +831,20 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
812831
}
813832
}
814833

834+
onTermClick(event) {
835+
if (this.isReadOnlyMode()) {
836+
this.removeTerm(event.target.parentNode);
837+
}
838+
}
839+
840+
isReadOnlyMode() {
841+
return this.input.dataset.termMode === 'read-only'
842+
}
843+
844+
isTermDirectionVertical() {
845+
return this.input.dataset.termDirection === 'vertical'
846+
}
847+
815848
onButtonClick(event) {
816849
if (! this.hasSyntaxError()) {
817850
// Register current input value, otherwise it's not included

asset/js/widget/TermInput.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ define(["BaseInput"], function (BaseInput) {
66
constructor(input) {
77
super(input);
88

9-
this.separator = ' ';
9+
this.separator = this.input.dataset.termSeparator ? this.input.dataset.termSeparator : ' ';
1010
this.ignoreSpaceUntil = null;
1111
this.ignoreSpaceSince = null;
1212
}

0 commit comments

Comments
 (0)