Skip to content

Commit d17b807

Browse files
TermInput: Add attr data-term-separator and code for term read-only mode
1 parent be6b59a commit d17b807

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

asset/js/widget/TermInput.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
define(["BaseInput"], function (BaseInput) {
1+
define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
22

33
"use strict";
44

55
class TermInput extends BaseInput {
66
constructor(input) {
77
super(input);
88

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

14+
bind() {
15+
if (this.isReadOnlyMode()) {
16+
$(this.termContainer).on('click', '[data-index]', this.onTermClick, this);
17+
}
18+
19+
return super.bind();
20+
}
21+
1422
reset() {
1523
super.reset();
1624

@@ -61,6 +69,32 @@ define(["BaseInput"], function (BaseInput) {
6169
super.complete(input, data);
6270
}
6371

72+
renderTerm(termData, termIndex) {
73+
if (! this.isReadOnlyMode()) {
74+
return super.renderTerm(termData, termIndex);
75+
}
76+
77+
let label = $.render(
78+
'<label><input type="button" class="term-badge"><i class="fa fa-trash trash-icon"></i></label>'
79+
);
80+
81+
if (termData.class) {
82+
label.classList.add(termData.class);
83+
}
84+
85+
if (termData.title) {
86+
label.title = termData.title;
87+
}
88+
89+
label.dataset.label = termData.label;
90+
label.dataset.search = termData.search;
91+
label.dataset.index = termIndex;
92+
93+
label.firstChild.value = termData.label;
94+
95+
return label;
96+
}
97+
6498
/**
6599
* Event listeners
66100
*/
@@ -122,6 +156,12 @@ define(["BaseInput"], function (BaseInput) {
122156
}
123157
}
124158
}
159+
160+
onTermClick(event) {
161+
let termIndex = event.target.parentNode.dataset.index;
162+
this.removeTerm(event.target.parentNode);
163+
this.moveFocusForward(termIndex - 1);
164+
}
125165
}
126166

127167
return TermInput;

0 commit comments

Comments
 (0)