/
jquery.tokenizeforms.js
415 lines (370 loc) · 11.3 KB
/
jquery.tokenizeforms.js
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* Inspiration for some of this code goes out to:
*
* jquery.tokeninput.js
* on github at: http://github.com/loopj/jQuery-Tokenizing-Autocomplete-Plugin
*
* --
* The rest of the credit goes to the collective world that works on this.
* Feel free to fork this at: http://github.com/m3talsmith/jquery-tokenizeforms
* --
* Licensed under GPLv3: http://www.gnu.org/licenses/gpl-3.0.html
* --
* Todo's:
*
* - Add mouse handlers for results list
* - Check for any IE bugs
* - Add nice animations
* - Add Data Cache Expiration
* - Add Query Bypassing
* - Add query term highlighting
* - Add auto select result list item on queryterm whendropdown is shown
*
* --
* Status: 95% Complete
*
* --
* Notes: Added a monkey patch for Javascript to check for array duplicates.
*
* Usage:
* [1,2,3].includes(2) // true
* [1,2,3].includes(4) // false
*
*/
/* Array Monkey Patching */
Array.prototype.includes = function(value) {
var length = this.length;
for(var i=0; i < length; i++) {
if(this[i] == value) { return true; }
}
return false;
}
/* Plugin code */
$.fn.tokenizeInput = function(options) {
var options = $.extend(options);
// make sure critical default values are not null
if(options.url == undefined){options.url = "";}
if(options.queryToken == undefined){options.queryToken = "tokenizeForm";}
// --
return this.each(function(){
var token_list = $.TokenList(this, options);
});
}
$.TokenList = function(input_field, options) {
// need to enter some keycode for keyboard operation first
var key = {
backspace: 8,
tab: 9,
enter: 13,
esc: 27,
left: 37,
up: 38,
right: 39,
down: 40,
comma: 188,
apple: 91
};
// --
// callbacks
var onSelectToken = function(){return true;};
var onDeselectToken = function(){return true;};
var onNewToken = function(){return true;};
var onRemoveToken = function(){return true;};
// --
// check for callbacks in options
if(options.onSelectToken) { onSelectToken = options.onSelectToken; }
if(options.onDeselectToken) { onDeselectToken = options.onDeselectToken; }
if(options.onNewToken) { onNewToken = options.onNewToken; }
if(options.onRemoveToken) { onRemoveToken = options.onRemoveToken; }
// --
// set other options up
var url = options.url;
var queryToken = options.queryToken;
var input_cache = [];
if(options.cache_data) {input_cache = cache_data};
// --
var input_field = $(input_field);
var parent = $(input_field).parent();
var token_list = $("<div />")
.attr("id", input_field.attr("id") + "_token_list")
.addClass("token-list")
.appendTo(parent)
.click(function(){false_input_field.focus();});
var tokens = [];
var token_selected = false;
var result_list = $("<div />")
.attr("id", input_field.attr("id") + "_result_list")
.addClass("query-results")
.appendTo(parent);
hide_dropdown();
var results = [];
var selected_result = "";
var disabled_form_submit = false;
var dropdown_hidden = false;
if(input_field.val() != "") {
$(input_field.val().split(",")).each(function(){tokens.push(this);});
}
$(tokens).each(function(){
create_token(this);
});
var focused = false;
var false_input_field = $("<input type='text' />")
.addClass("token-input-field")
.css({
id: "false-input-field"
})
.focus(function(){
if(token_selected) { deselect_token(); }
if($(this).val().length > 0){
focused = true;
run_query($(this).val());
show_dropdown();
}
})
.blur(function(){
hide_dropdown();
if(token_selected) { deselect_token(); }
focused = false;
})
.keydown(function(event){
switch(event.keyCode) {
case key.apple:
break;
case key.tab:
hide_dropdown();
if(token_selected) { deselect_token(); }
break;
// break default tab use for this field
// if(options.tabCompletion){event.preventDefault();}
// --
case key.enter:
disable_form_submit();
if(selected_result != "") { create_token(selected_result.html()); }
else { create_token(String($(this).val()).replace(",","")); }
$(this).appendTo(token_list).focus().val("");
if(token_selected) { unselect_token(); }
clear_results();
hide_dropdown();
// event.stopImmediatePropagation();
event.preventDefault();
enable_form_submit();
return false;
break;
case key.down:
select_next_result();
// event.stopImmediatePropagation();
event.preventDefault();
return false;
break;
case key.up:
select_previous_result();
event.preventDefault();
return false;
break;
}
})
.keyup(function(event){
switch(event.keyCode) {
case key.apple:
case key.up:
case key.down:
event.preventDefault();
break;
case key.comma:
create_token(String($(this).val()).replace(",",""));
$(this).appendTo(token_list).focus().val("");
if(token_selected) { unselect_token(); }
return false;
break;
case key.backspace:
if(token_selected){ remove_token(); }
else if($(this).val().length <= 0 && !token_selected) { select_token(); hide_dropdown(); }
else if($(this).val().length > 0) { run_query($(this).val()); }
break;
case key.esc:
hide_dropdown();
$(this).blur();
break;
default:
if(token_selected) { deselect_token(); }
$(this).focus();
run_query($(this).val());
}
});
false_input_field.appendTo(token_list);
input_field.focus(function(){false_input_field.focus();});
input_field.css("display", "none");
// fix a race condition with the enter key
init_form_submit();
function init_form_submit() {
var form = token_list.parents().find("form:first");
form.bind("submit", function(){
if(disabled_form_submit){return false;}
return true;
});
}
function disable_form_submit() {
disabled_form_submit = true;
}
function enable_form_submit() {
disabled_form_submit = false;
}
// --
function create_token(value) {
var duplicate_token = false;
if(token_list.find("span").length >= 1) {
token_list.find("span").each(function(){
if($(this).text() == value) { duplicate_token = true; }
});
}
if(!duplicate_token) {
tokens.push(value);
var token = $("<span>" + value + "</span>")
.addClass("token-complete")
.css("cursor", "pointer")
.click(function(){
var token = $(this);
clear_selected_tokens();
token
.removeClass("token-complete")
.addClass("token-selected");
remove_token();
})
.appendTo(token_list);
input_field.val(tokens.join(","));
onNewToken();
}
}
function deselect_token() {
token_list.find(".token-selected:first")
.removeClass("token-selected")
.addClass("token-complete");
token_selected = false;
onDeselectToken();
}
function select_token() {
token_list.find(".token-complete:last")
.removeClass("token-complete")
.addClass("token-selected");
token_selected = true;
onSelectToken();
}
function remove_token() {
token_list.find(".token-selected:first").remove();
token_selected = false;
tokens = [];
token_list.find(".token-complete").each(function() {
tokens.push($(this).text());
});
input_field.val(tokens.join(","));
clear_results();
hide_dropdown();
onRemoveToken();
}
function clear_selected_tokens() {
token_list.find(".token-selected").each(function(){
$(this)
.addClass("token-complete")
.removeClass("token-selected");
});
}
// Marked for depreciation
function remove_token_immediately() {
select_token();
remove_token();
}
// --
function show_dropdown() {
result_list.show(); // I really should animate this
dropdown_hidden = false;
}
function hide_dropdown() {
result_list.hide(); // I really should animate this
dropdown_hidden = true;
}
function clear_results() {
results = [];
result_list.empty(); // I really should animate this
selected_result = "";
}
function select_next_result() {
if(selected_result == "") {
selected_result = result_list.children(":first");
} else if(selected_result.next().html() != null) {
selected_result.removeClass("query-result-selected");
selected_result = selected_result.next();
}
selected_result.addClass("query-result-selected");
/*
.click(function(){
create_token($(this).html());
false_input_field.appendTo(token_list).focus().val("");
if(token_selected) { unselect_token(); }
clear_results();
hide_dropdown();
})
*/
}
function select_previous_result() {
if(selected_result != "" && result_list.children().index(selected_result.prev()) >= 0) {
selected_result.removeClass("query-result-selected");
selected_result = selected_result.prev();
selected_result.addClass("query-result-selected");
}
}
function populate_dropdown(json) {
clear_results();
$(json).each(function(){
name = this.name;
if(!results.includes(name)) {
results.push(name);
var result = $("<span>" + name + "</span>")
.addClass("query-result-token")
.appendTo(result_list);
}
});
if(dropdown_hidden){show_dropdown();}
}
function populate_cache_from_json(json) {
$(json).each(function(){
input_cache.push(this.name);
});
}
function find_in_cache(query) {
var cache_matches = [];
$(input_cache).each(function(index) {
if(input_cache[index].match(query)) { cache_matches.push({name:input_cache[index]}); }
});
return cache_matches;
}
function find_in_database(query) {
// setup simple variables for complex builds
var queryDelimiter = options.url.indexOf("?") < 0 ? "?" : "&";
var merged_url = url + (options.disableQueryToken != undefined ? query : queryDelimeter + queryToken + "=" + query);
var type = options.method != undefined ? options.method : "get";
$.ajax({
type: type,
url: merged_url,
dataType: "json",
success: function(json, status) {
populate_cache_from_json(json);
if(focused) { populate_dropdown(json); }
else { focused = false; }
}
// error: function(request, status, errorMessage) {alert(errorMessage);},
// complete: function(request, status) {alert(status);}
});
}
function run_query(query){
if(input_cache.length > 0) {
var cached_results = find_in_cache(query);
if(cached_results.length > 0) {
populate_dropdown(cached_results);
} else {
find_in_database(query);
}
} else {
find_in_database(query);
}
}
return token_list;
}