-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patht-autosuggest.html
381 lines (340 loc) · 11.1 KB
/
t-autosuggest.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-material/paper-material.html">
<link rel="import" href="../iron-input/iron-input.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout-classes.html">
<link rel="import" href="../iron-dropdown/iron-dropdown-scroll-manager.html">
<link rel="import" href="../t-loader/t-loader.html">
<link rel="import" href="../paper-input/paper-input-error.html">
<link rel="import" href="../t-input/t-input-container.html">
<link rel="import" href="t-autosuggest-import.html">
<link rel="import" href="t-autosuggest-style.html">
<!--
`<t-autosuggest>` is a ajax driven autosuggestion component.
<t-autosuggest data-url="http://localhost:3000/components/formComponents/t-autosuggest/data.json">
</t-autosuggest>
-->
<dom-module id="t-autosuggest">
<template>
<style include="t-autosuggest-style">
*,
::content *{
-webkit-text-size-adjust: none;
}
</style>
<style include="iron-flex"></style>
<t-input-container fire-blur="{{validateOnBlur}}" no-label-float="[[noLabelFloat]]" auto-validate$="[[autoValidate]]" disabled$="[[disabled]]" invalid="[[invalid]]">
<div id="mainBody" class="layout vertical " style="width:100%">
<div class="layout horizontal center">
<div class="flex label-container">
<label hidden$="[[!label]]">[[label]]</label>
<paper-material class="flex">
<input class="suggestions"
id="autoSuggest"
is="iron-input"
disabled$="[[disabled]]"
placeholder="{{placeholder}}"
data-autocomplete$="{{dataUrl}}"
data-autocomplete-method="GET"
data-autocomplete-type="JSON"
data-autocomplete-limit="10"
data-autocomplete-param-name$="{{param}}"
data-autocomplete-no-result$="{{emptyMessage}}"
on-mouseUp="_onFocus"
bind-value$="[[selectedValue]]"
on-search="_onClear"
on-tap="_focusOnInput"
autocapitalize="off" autocorrect="off" autocomplete="off"> <!-- code to cancel populate in safari -->
<div id="clearContainer">
<iron-icon id="clearButton" icon="close"></iron-icon>
</div>
</paper-material>
</div>
<span id="cancel" on-tap="_cancel">Cancel</span>
</div>
<div id="append" class="flex">
<div id="spinner" class="horizontal layout flex center-justified" hidden>
<t-loader active></t-loader>
</div>
<div id="dropBox" class="autocomplete flex"></div>
</div>
</div>
<template is="dom-if" if="[[errorMessage]]">
<paper-input-error class="error empty-error">[[errorMessage]]</paper-input-error>
</template>
<template is="dom-if" if="[[errorMessage]]">
<paper-input-error class="invalid-error error">[[invalidErrorMessage]]</paper-input-error>
</template>
</t-input-container>
</template>
</dom-module>
<script>
Polymer({
is: 't-autosuggest',
properties: {
/**
* Name property
*/
name: {
type: String,
value: '',
reflectToAttribute: true
},
/**
* No label float is to make the label either on the body or on the top.
*/
noLabelFloat: {
type: Boolean,
reflectToAttribute: true,
value: false,
observer: '_checkLabel'
},
/**
* Api url of the autosuggestion engine.
*/
dataUrl: {
type: String,
value: '',
reflectToAttribute: true
},
/**
* Search parameter.
*/
param: {
type: String,
value: "query",
reflectToAttribute: true
},
/**
* No matching result message.
*/
emptyMessage: {
type: String,
value: "No matches found.",
reflectToAttribute: true
},
/**
* Selected value from the autosuggest.
*/
selectedValue: {
type: String,
value: '',
notify: true,
observer:'_checkFloatedLabel',
reflectToAttribute: true
},
/**
* Selected item from the autosuggest.
*/
selectedItem: {
type: Object,
// value:'test',
notify: true,
observer:'_selectedItemChanged',
value: function() {
return null;
}
},
/**
* Disable autosuggest.
*/
disabled: {
type: Boolean,
value: false,
reflectToAttribute: true
},
/**
* Disable autosuggest.
*/
isInvalid: {
type: Boolean,
value: false,
reflectToAttribute: true
},
/**
* Label of the autosuggest.
*/
label: {
type: String,
value: "",
reflectToAttribute: true,
observer: '_checkLabel'
},
/**
* Put as required
*/
required: {
type: Boolean,
value: false,
reflectToAttribute: true
},
/**
* Set to true to fire validations on blur
*/
validateOnBlur: {
type: Boolean,
value: true
},
/**
* Token param
*/
tokenParam: {
type: String,
value: "",
reflectToAttribute: true
},
/**
* errorMessage is to put the error msg
*/
errorMessage: {
type: String,
reflectToAttribute: true,
value: 'Please select a value!'
},
invalidErrorMessage:{
type:String,
value:'Invalid value'
},
caching: {
type: Boolean,
reflectToAttribute: true,
value: false
},
subType: {
type: String,
value: ""
},
queryParams: {
type: String,
value: ""
},
minimumCharacters: {
type: Number,
value: 3
}
},
listeners:{
'tap':'simpleTap',
'clearContainer.tap':'_clear'
},
attached: function() {
AutoComplete({
method: "POST"
}, this);
var component = this,
//to handle the scrolling position when focused
scrollTop;
this.$.autoSuggest.addEventListener('focus',function(){
function allFunction(){
//to scroll to top when focused for ios
scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
component.classList.add('focused');
document.body.scrollTop = document.documentElement.scrollTop = 0;
}
setTimeout(allFunction, 10);
});
},
_focusOnInput: function(){
this.querySelector('#autoSuggest').blur();
this.querySelector('#autoSuggest').focus();
},
_focusingOut: function(){
var component = this,
//to handle the scrolling position when focused
scrollTop;
function allFunction(){
Polymer.IronDropdownScrollManager.removeScrollLock(component);
if (component.selectedItem) {
component.selectedValue = component.selectedItem[component.tokenParam];
}
component.classList.remove('focused');
component.$.dropBox.className = "autocomplete ";
component.$.spinner.hidden = true;
//to scroll to previous position when focusout
document.body.scrollTop = document.documentElement.scrollTop = scrollTop;
component.fire('blur');
component.querySelector('#autoSuggest').blur();
if(component.required && component.validateOnBlur){
component.validate();
}
}
setTimeout(allFunction, 10);
},
_cancel:function(){
// if(this.lastSelectedItem){
// this.selectedItem = this.lastSelectedItem;
// }
this.querySelector('#autoSuggest').value = this.selectedValue;
this._focusingOut();
},
_clear:function(event){
event.stopPropagation();
this.clear();
},
_onFocus: function(event) {
Polymer.IronDropdownScrollManager.pushScrollLock(this);
this.$.autoSuggest.select();
event.stopImmediatePropagation();
return false;
},
_checkLabel: function() {
this.placeholder = this.noLabelFloat ? this.label : "";
},
validate: function() {
if (this.required) {
if(!this.selectedValue == true ){
this.classList.add('no-value');
this.classList.remove('invalid-value');
}else{
if(this.isInvalid){
this.classList.add('invalid-value');
this.classList.remove('no-value');
return false;
}
}
if(!this.selectedValue == true || this.isInvalid)
{
this.isInvalid = false;
return false;
}
}
this.classList.remove('no-value');
this.classList.remove('invalid-value');
return true;
},
_closeLambda: function() {
var item = this;
this.$.dropBox.className = "autocomplete ";
},
_onClear: function(event){
this.selectedValue = "";
this.selectedItem = null;
},
_selectedItemChanged: function () {
if (this.selectedItem != undefined) {
this.selectedValue = this.selectedItem[this.tokenParam];
this._focusingOut();
this.validate();
}
},
_checkFloatedLabel: function() {
if (this.$.autoSuggest.value.length > 0) {
this.classList.add('label-is-floating');
} else {
this.classList.remove('label-is-floating');
}
},
simpleTap:function(e){
if (!e) var e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
},
clear: function() {
this.classList.add('focused');
this.selectedValue = "";
//this.selectedItem = null;
this.$.autoSuggest.value = "";
this.classList.remove('label-is-floating');
}
})
</script>