-
Notifications
You must be signed in to change notification settings - Fork 1
/
cw-token-field.html
executable file
·149 lines (121 loc) · 4.01 KB
/
cw-token-field.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../cw-autosize-input/cw-autosize-input.html">
<link rel="import" href="cw-options-list.html">
<!--
A tagging/tokenizing input a la http://harvesthq.github.io/chosen/,
http://loopj.com/jquery-tokeninput/, and
http://sliptree.github.io/bootstrap-tokenfield/.
##### Example
<cw-token-field placeholder="Choose a color...">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</cw-token-field>
@element cw-token-field
@homepage http://cletusw.github.io/cw-token-field
-->
<polymer-element name="cw-token-field" on-blur="{{onBlur}}" on-focus="{{onFocus}}" on-tap="{{onTap}}" tabindex="0">
<template>
<link href="cw-token-field.css" rel="stylesheet">
<template repeat="{{selected}}">
<span class="selected">{{text}} <span class="unselect" on-tap="{{unselect}}">x</span></span>
</template>
<input class="{{ { 'maximize-width': selected.length === 0 } | tokenList }}" id="filter" inputValue="{{filterText}}" is="cw-autosize-input" on-input="{{onInput}}" on-keydown="{{onKeydown}}" placeholder="{{_placeholder}}" type="text">
<cw-options-list field="text" filterText="{{filterText}}" hidden?="{{!isOptionsListVisible}}" id="optionsList" on-tap="{{onOptionsTap}}" options="{{options}}" selected="{{selected}}"></cw-options-list>
</template>
<script>
Polymer({
publish: {
/**
* The `placeholder` attribute contains text to display when nothing
* is selected.
*
* @attribute placeholder
* @type String
*/
placeholder: '',
},
_placeholder: '',
filterText: '',
isOptionsListVisible: false,
options: [],
attached: function() {
this.options = Array.prototype.map.call(this.children, function(child) {
return {
selected: false,
text: child.text,
value: child.value
}
});
},
onBlur: function() {
this.isOptionsListVisible = false;
},
onFocus: function() {
this.resetOptionsList();
},
onInput: function() {
this.isOptionsListVisible = true;
},
onKeydown: function(event) {
if (event.keyCode === 13) {
// Enter
this.$.optionsList.selectCurrent();
}
else if (event.keyCode === 8 && this.filterText === '' && this.selected.length > 0) {
// Backspace
var removed = this.selected.pop();
removed.selected = false;
}
else if (event.keyCode === 38) {
// Up
event.preventDefault();
if (!this.isOptionsListVisible) {
this.resetOptionsList();
}
else {
this.$.optionsList.highlightPrevious();
}
}
else if (event.keyCode === 40) {
// Down
event.preventDefault();
if (!this.isOptionsListVisible) {
this.resetOptionsList();
}
else {
this.$.optionsList.highlightNext();
}
}
},
onTap: function() {
this.$.filter.focus();
if (!this.isOptionsListVisible) {
this.resetOptionsList();
}
},
placeholderChanged: function() {
if (this.selected && this.selected.length > 0) {
this._placeholder = '';
}
else {
this._placeholder = this.placeholder;
}
},
resetOptionsList: function() {
this.$.optionsList.highlightFirst();
this.isOptionsListVisible = true;
},
selectedChanged: function() {
this.isOptionsListVisible = false;
this.filterText = '';
this.placeholderChanged();
},
unselect: function(event, detail, sender) {
var model = sender.templateInstance.model;
model.selected = false;
this.selected.splice(this.selected.indexOf(model), 1);
}
});
</script>
</polymer-element>