forked from cheonhyangzhang/paper-tags-input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper-tags-input.html
executable file
·338 lines (291 loc) · 9.38 KB
/
paper-tags-input.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../iron-input/iron-input.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="tag-item.html">
<!--
paper-tags-input is a list of tags with a single-line text field with Material Design styling.
<paper-tags-input></paper-tags-input>
Use a label property to show the label for tags input and placeholder property for the placeholder in paper-input element.
<paper-tags-input label="Tags" placeholder="Please enter the tag"></paper-tags-input>
It may include an optional error message. For now, the tags list is case sensitive. So Taeyeon and taeyeon will not be considered as duplicate.
<paper-tags-input empty-error-message="Tag could not be empty"></paper-tags-input>
<paper-tags-input duplicate-error-message="Tag could not be duplicate"></paper-tags-input>
Tags list are stored in the property of tags.
<paper-tags-input tags="[[tags_list]]"></paper-tags-input>
<b>Styling</b>
For the color of tags, it is using theme color --light-primary-color by default.
But you can change the color by applying custom colors.
<paper-tags-input tag-color="red" font-color="#FFFFFF" tags="[[tags_list]]"></paper-tags-input>
<b>Internaltional</b>
paper-tags-input work with all unicode languages, such as Chinese, Korean, Japanese, German, Russian, Arabic, etc.
<b>Installation</b>
Install with bower
bower install paper-tags-input
If you want to save it in bower.json file, remember to add flag --save
bower install --save paper-tags-input
@demo
-->
<dom-module id="paper-tags-input">
<style>
:host {
display: block;
box-sizing: border-box;
font-family: 'Roboto', 'Noto', sans-serif;
}
.clearfix:after {
content: " ";
display: block;
height: 0;
clear: both;
}
#tag-input .paper-input-container-0{
padding: 0px;
}
#tag-input .paper-input-container-0 .floated-label-placeholder{
display: none;
}
input{
height: 18px !important;
color: #525252;
/*font-family: monospace;*/
font-size: 15px !important;
line-height: 14px;
font-stretch: normal;
font-style: normal;
font-variant: normal;
font-weight: 300;
}
paper-input-error{
font-size: 11px !important;
}
</style>
<template>
<!--<h3>[[label]]</h3> -->
<div class="clearfix" id="tags-items" style="margin-top:3px;">
<template is="dom-repeat" items="[[tags]]">
<tag-item font-color="[[fontColor]]" tag-color="[[tagColor]]" size="[[size]]" tags="{{tags}}" enable-remove="[[enableRemove]]" index="[[index]]" tagvalue="[[item]]"></tag-item>
</template>
</div>
<template is="dom-if" if="[[enableAdd]]">
<paper-input id="tag-input" no-label-input error-message="[[emptyErrorMessage]]" on-keydown="_keyDown" value="{{input_value}}" style="margin-top:2px;margin-bottom:16px;"></paper-input>
</template>
</template>
</dom-module>
<script>
Polymer({
is: 'paper-tags-input',
properties: {
/**
* list of String that holds tags. Values in this array should be identical and avoids duplicates due to the definition of tags.
*/
tags:{
type:Array,
value:[],
notify:true,
// observer: 'nameChanged'
},
/**
* the CSS color for the tag, e.g. #e5e5e5
*/
tagColor:String,
/**
* the CSS color for the text and remove button, e.g. #e5e5e5
*/
fontColor:String,
/**
* size of the tags, possible values: small, medium, large
*/
size:{
type:String,
value:'large'
},
/**
* The error message that will show up if a user tries to add an empty tag.
*/
emptyErrorMessage:{
type:String,
value:"Tag cannot be empty"
},
commaErrorMessage:{
type:String,
value:"Comma not allowed"
},
/**
* Boolean flag for whether add tag operation is enabled.
*/
enableAdd:{
type:Boolean,
value:true
},
/**
* Boolean flag for whether remove tag operation is enabled.
*/
enableRemove:{
type:Boolean,
value:true
},
/**
* The error message that will show up if a user tries to add a duplicate tag.
*/
duplicateErrorMessage:{
type:String,
value:"Tag must not be identical"
},
/**
* The label that will show up as the label for tags input.
*/
label:{
type:String,
value:''
},
/**
* The placeholder for the paper-input element.
*/
placeholder:{
type:String,
value:'Enter tags'
},
/**
* Internal variable to hold the value of paper-input.
*/
input_value: {
type:String,
notify:true,
value:"",
},
name:{
type:String,
value:''
},
},
// Element Lifecycle
ready: function() {
this.tags=[];
// `ready` is called after all elements have been configured, but
// propagates bottom-up. This element's children are ready, but parents
// are not.
//
// This is the point where you should make modifications to the DOM (when
// necessary), or kick off any processes the element wants to perform.
},
attached: function() {
// `attached` fires once the element and its parents have been inserted
// into a document.
//
// This is a good place to perform any work related to your element's
// visual state or active behavior (measuring sizes, beginning animations,
// loading resources, etc).
},
detached: function() {
// The analog to `attached`, `detached` fires when the element has been
// removed from a document.
//
// Use this to clean up anything you did in `attached`.
},
// Element Behavior
/**
* _addTag is the internal method to add a new tag to the list
*
* @param {string} the tag that needs to be added.
*/
_addTag: function(tag){
// console.log(tag)
// return
if (this.enableAdd == false){
return;
}
if (typeof(this.tags) == 'undefined'){
this.tags = [];
}
// this.tags.push(tag);
// this.tags = this.tags.slice();
this.push('tags',tag)
this.tags = this.tags.slice();
},
/**
* _getInputVal is the internal method to get the value of paper-input. It performs a trim on the value.
*
* @return {string} returns the trimmed version of paper-input value.
*/
_getInputVal: function(){
return this.input_value.trim();
},
/**
* _focusOut is fired when input lose focus
*
* @param {object} event object
*/
_focusOut: function(e) {
var tag = this._getInputVal();
if (typeof(tag) != undefined && tag != ""){
this._addTag(this._getInputVal());
this.input_value = "";
}
},
/**
* _keyDown is fired when user enters a key in the input
*
* @param {object} event object
*/
_keyDown: function(e) {
// console.log(e)
var keyVal = e.which;
if (keyVal == 13 || keyVal == 188 ){
if (this._getInputVal() == ""){
var input = Polymer.dom(this.root).querySelector('#tag-input');
// console.log(input)
if (keyVal == 188)
input.errorMessage = this.commaErrorMessage;
else
input.errorMessage = this.emptyErrorMessage;
input.invalid=true;
e.preventDefault();
}
else if (this.tags.indexOf(this._getInputVal()) >= 0){
var input = Polymer.dom(this.root).querySelector('#tag-input');
// console.log(input)
input.errorMessage = this.duplicateErrorMessage;
input.invalid=true;
e.preventDefault();
}
else{
this._addTag(this._getInputVal());
this.input_value = "";
e.preventDefault();
}
}
else{
var input = Polymer.dom(this.root).querySelector('#tag-input');
input.invalid=false;
}
},
addTag: function(tag){
// console.log('tag'+tag);
if (this.enableAdd == false){
return;
}
if (typeof(this.tags) == 'undefined'){
this.tags = [];
}
if (this.tags.indexOf(tag) === -1)
this.push('tags',tag)
this.tags = this.tags.slice();
},
nameChanged: function(newFirstName, oldFirstName) {
// this.lastName could be undefined!
console.log('new name:', newFirstName, this.lastName);
}
});
</script>