Skip to content

Commit 52a351f

Browse files
Arthur AgombartArthur Agombart
authored andcommitted
rwk(*): add emit function + split addTag function
1 parent eba49a2 commit 52a351f

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/dev-server.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ class DevServer {
7171
return accumulator;
7272
}
7373

74+
onTagAdd(tag) {
75+
console.log(tag);
76+
}
77+
7478
updateSuggestions(search) {
7579
const newSuggestions = this.searchSuggestions(search);
7680

@@ -96,6 +100,7 @@ angular.module('dev-server', ['angularjs-input-tags'])
96100
suggestions="$ctrl.suggestions"
97101
disabled="$ctrl.disabled"
98102
key-property="code"
103+
on-tag-added="$ctrl.onTagAdd"
99104
display-property="title"
100105
get-suggestions="$ctrl.updateSuggestions"></input-tags>
101106
<pre>

src/input-tags.component.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,52 +37,45 @@ class InputTags {
3737
return tag[this.displayProperty];
3838
}
3939

40-
addTag(tag) {
40+
isTagValid(tag) {
4141
const tagText = this.getTagText(tag);
4242
const key = this.keyProperty || this.displayProperty;
43-
const valid = tagText &&
43+
return tagText &&
4444
this.tags.length <= this.maxLength &&
4545
!this.tags.some(element => element[key] === tag[key]);
46+
}
4647

47-
if (this.onTagAdding) {
48-
this.onTagAdding(tag);
49-
}
48+
addTag(tag) {
49+
const valid = this.isTagValid(tag);
50+
51+
this.emit('onTagAdding', tag);
5052

5153
if (valid) {
5254
this.tags.push(tag);
5355

54-
if (this.onTagAdded) {
55-
this.onTagAdded(tag);
56-
}
57-
} else if (this.onTagAddFailed) {
58-
this.onTagAddFailed(tag);
56+
this.emit('onTagAdded', tag);
57+
} else {
58+
this.emit('onTagAddFailed', tag);
5959
}
6060

6161
return tag;
6262
}
6363

6464
removeTag(tag) {
65-
if (this.onTagRemoving) {
66-
this.onTagRemoving(tag);
67-
}
65+
this.emit('onTagRemoving', tag);
6866

6967
for (let i = this.tags.length - 1; i >= 0; i--) {
7068
if (this.tags[i].code === tag.code) {
7169
this.tags.splice(i, 1);
7270
}
7371
}
7472

75-
if (this.onTagRemoved) {
76-
this.onTagRemoved(tag);
77-
}
78-
73+
this.emit('onTagRemoved', tag);
7974
return tag;
8075
}
8176

8277
inputChange() {
83-
if (this.inputChanged) {
84-
this.inputChanged(this.inputSearch);
85-
}
78+
this.emit('inputChanged', this.inputSearch);
8679
}
8780

8881
triggerFocus() {
@@ -114,6 +107,12 @@ class InputTags {
114107
this.path[this.path.length - 1] :
115108
this.suggestions;
116109
}
110+
111+
emit(action, ...params) {
112+
if (this[action] && typeof this[action] === 'function') {
113+
this[action].apply(this, params);
114+
}
115+
}
117116
}
118117

119118
const InputTagsComponent = {

0 commit comments

Comments
 (0)