Skip to content

Commit

Permalink
Introduce new dataset Attributes
Browse files Browse the repository at this point in the history
'data-term-separator', 'data-term-mode' and 'data-term-direction'
  • Loading branch information
sukhwinder33445 committed May 4, 2022
1 parent a5fc0e2 commit fe42110
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
47 changes: 40 additions & 7 deletions asset/js/widget/BaseInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
$(this.termContainer).on('keyup', '[data-label]', this.onKeyUp, this);
$(this.termContainer).on('focusout', '[data-index]', this.onTermFocusOut, this);
$(this.termContainer).on('focusin', '[data-index]', this.onTermFocus, this);
$(this.termContainer).on('click', '[data-index]', this.onTermClick, this);

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

renderTerm(termData, termIndex) {
let label = $.render('<label><input type="text"></label>');
let inputContainer;
if (this.isReadOnlyMode()) {
inputContainer = '<label><input type="button"><i class="icon-cancel term-icon"></i></label>';
} else {
inputContainer = '<label><input type="input"></label>';
}

let label = $.render(inputContainer);

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

toFocus.selectionStart = toFocus.selectionEnd = 0;
if (! this.isReadOnlyMode()) {
toFocus.selectionStart = toFocus.selectionEnd = 0;
}

$(toFocus).focus();

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

toFocus.selectionStart = toFocus.selectionEnd = toFocus.value.length;
if (! this.isReadOnlyMode()) {
toFocus.selectionStart = toFocus.selectionEnd = toFocus.value.length;
}

$(toFocus).focus();

return toFocus;
Expand Down Expand Up @@ -657,6 +671,8 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
}
}

let arrowUpOrLeft = this.isTermDirectionVertical() ? 'ArrowUp' : 'ArrowLeft';
let arrowDownOrRight = this.isTermDirectionVertical() ? 'ArrowDown' : 'ArrowRight';
let removedTerms;
switch (event.key) {
case ' ':
Expand Down Expand Up @@ -723,14 +739,17 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
this.saveTerm(input, false);
}
break;
case 'ArrowLeft':
if (input.selectionStart === 0 && this.hasTerms()) {
case arrowUpOrLeft:
if ((input.selectionStart === 0 || input.selectionStart === null && this.isReadOnlyMode())
&& this.hasTerms()) {
event.preventDefault();
this.moveFocusBackward();
}
break;
case 'ArrowRight':
if (input.selectionStart === input.value.length && this.hasTerms()) {
case arrowDownOrRight:
if ((input.selectionStart === input.value.length
|| input.selectionStart === null && this.isReadOnlyMode())
&& this.hasTerms()) {
event.preventDefault();
this.moveFocusForward();
}
Expand Down Expand Up @@ -812,6 +831,20 @@ define(["../notjQuery", "Completer"], function ($, Completer) {
}
}

onTermClick(event) {
if (this.isReadOnlyMode()) {
this.removeTerm(event.target.parentNode);
}
}

isReadOnlyMode() {
return this.input.dataset.termMode === 'read-only'
}

isTermDirectionVertical() {
return this.input.dataset.termDirection === 'vertical'
}

onButtonClick(event) {
if (! this.hasSyntaxError()) {
// Register current input value, otherwise it's not included
Expand Down
2 changes: 1 addition & 1 deletion asset/js/widget/TermInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ define(["BaseInput"], function (BaseInput) {
constructor(input) {
super(input);

this.separator = ' ';
this.separator = this.input.dataset.termSeparator ? this.input.dataset.termSeparator : ' ';
this.ignoreSpaceUntil = null;
this.ignoreSpaceSince = null;
}
Expand Down

0 comments on commit fe42110

Please sign in to comment.