-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.autocomplete.js
506 lines (479 loc) · 18 KB
/
mod.autocomplete.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/* This file is part of cbautocomplete2.
* Copyright © 2010-2013 stiftung kulturserver.de ggmbh <[email protected]>
*
* cbui is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cbui is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with cbui. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* jQuery .autoComplete()
*
* Transforms an <input/> into an autoComplete box
* TODOs
* 1) scroll when using arrow keys and marked element is not visible
* 2) fix ie6 style issues
*
* @Author Andre Mohren
*/
jQuery.fn.autoComplete = function(params) {
/**
* Predefined parameters with their default value
*/
var options = {
/**
* How long has the entered string to be before we start to search?
*/
'minimumKeys': '2',
/**
* How long to wait after pressing a key for start searching?
*/
'waitTime': '1000',
/**
* Is the <inpout/> value already validated at loading time?
*/
'validated': false,
/**
* Which url will be called to get out completion data?
*/
'requestUrl': '/module/jscript/lib/jquery_plugins/autocomplete2/exampleAjax.php',
/**
* Should we close the box and reset the <input/> when its losing focus?
*/
'closeWhenBlur': true,
/**
* Name of the search-param which will be send as _GET to PHP.
*/
'requestParam': 'actualInput',
/**
* Are not validated values allowed?
*/
'allowFreetext': false,
/**
* Where do we put the selected id?
*/
'putIdInto': '',
/**
* Custom callback for click?
*/
'customCallback': false,
/**
* Custom callback final for click?
*/
'customCallbackFinal': false,
/**
* Header
*/
'header': false,
/**
* Footer
*/
'footer': false,
/**
* Free text entry in footer
*/
'footerFreetext' : false,
/**
* Description line in case of free text entry in footer
*/
'footerMissingText' : '',
/**
* Info line in case of free text entry in footer
*/
'footerInfoText' : '',
/**
* Trap "enter" keypress to avoid submitting forms?
*/
'trapEnter': false,
/**
* Provide a callback for showbox
*/
'onShow' : false
};
/*
* All other parameters are passed on as extra request parameters
*/
var extra = {};
for(var param in params) {
if (options[param] != undefined) {
options[param] = params[param];
} else {
extra[param] = params[param];
}
}
options.extraParams = extra;
jQuery(this).each(function() {
//ToDo: save old blur function for later useage
jQuery(this).unbind('blur');
//ToDo: bind only <input/>
//ToDo: compare parameters with old box
//ToDo: icon not over border
//ToDo: scroll when using key-selecting
/**
* Our <input/>
*/
var element = this;
/**
* Used for saving the timeout
*/
var timeout = '';
/**
* HTML Code for the box
*/
var box = jQuery('<div class="__AC_position"><div class="__AC_close"></div><div class="__AC_layer"></div></div>');
var validatingBox = jQuery('<div class="__AC_position"><div class="__AC_layer __AC_validating"></div></div>');
/**
* The default value of the box
*/
var def = jQuery(element).val();
/**
* Used for temporary saving what we have typed into the box
*/
var typed = '';
/**
* Is the box open at the moment?
*/
var boxShown = false;
/**
* Do we mouseover an entry at the moment?
*/
var hoverEntry = false;
/**
* The default state of the box
*/
var isValidated = options['validated'];
/**
* Has the box focus?
*/
var hasFocus = false;
/**
* Is our last saved value freetext?
*/
var isFreetext = false;
/**
* The original value
*/
var originalVal = jQuery(element).val();
/**
* Display the validated state
*/
var validated = function() {
jQuery(element).removeClass('__AC_invalidated __AC_validating __AC_editing __AC_freetext')
.addClass('__AC_validated');
validatingBox.remove();
};
/**
* Display the invalidated state
*/
var invalidated = function() {
jQuery(element).removeClass('__AC_validated __AC_validating __AC_editing __AC_freetext')
.addClass('__AC_invalidated');
validatingBox.remove();
};
/**
* Display the working state
*/
var validating = function() {
jQuery(element).removeClass('__AC_validated __AC_invalidated __AC_editing __AC_freetext')
.addClass('__AC_validating');
validatingBox.remove();
};
/**
* Display the editing state
*/
var editing = function() {
jQuery(element).removeClass('__AC_validated __AC_invalidated __AC_validating __AC_freetext')
.addClass('__AC_editing');
jQuery(element).one('keypress', function(e) {
if (!boxShown && e.which != 13 && e.which != 27) {
showValidatingBox();
}
});
};
/**
* Display the freetext state
*/
var freetext = function() {
jQuery(element).removeClass('__AC_validated __AC_invalidated __AC_validating __AC_editing')
.addClass('__AC_freetext');
validatingBox.remove();
};
var selectEntry = function() {
jQuery(element).val(jQuery(this).attr('title'));
validated();
hideBox();
hoverEntry = false;
typed = def = jQuery(this).attr('title');
isFreetext = false;
if (options['putIdInto'] != "") {
jQuery(options['putIdInto']).val(jQuery(this).attr('id'));
}
if (options['customCallbackFinal']) {
options['customCallbackFinal']();
}
};
/**
* Show box
*/
var showBox = function() {
if (hasFocus || !options['closeWhenBlur']) {
validating();
boxShown = true;
options.extraParams[options.requestParam] = typed;
jQuery.getJSON(
options['requestUrl'],
options.extraParams,
function(JSONdata) {
if (hasFocus || !options['closeWhenBlur']) {
/* handle deprecated format: {data : [{ results : [...] }] */
if (JSONdata.data != undefined && JSONdata.results == undefined) JSONdata = JSONdata.data[0];
box.find('.__AC_layer').html('<div class="__AC_data"></div>');
box.find('.__AC_close').click(function() {
hoverEntry = false;
jQuery(element).val('').blur();
}).hover(function() {
hoverEntry = true;
}, function() {
hoverEntry = false;
}
);
if (JSONdata['header'] != undefined) {
box.find('.__AC_layer').prepend(JSONdata['header']);
} else if (options['header']) {
box.find('.__AC_layer').prepend(jQuery(options['header']).clone(true));
}
if (JSONdata['results'] != undefined) {
var position = box.find('.__AC_data');
for (var record in JSONdata['results']) {
var callback = selectEntry;
if (typeof(JSONdata['results'][record]['customCallback']) !== 'undefined') {
callback = JSONdata['results'][record]['customCallback'];
} else if (options['customCallback']) {
callback = options['customCallback'];
}
jQuery('<div class="__AC_record" title="' +
JSONdata['results'][record]['value'] + '" id="' +
JSONdata['results'][record]['id'] + '">' +
JSONdata['results'][record]['info'] + '</div>')
.appendTo(position).click(callback).hover(function() {
jQuery(this).addClass('__AC_ie8HoverFix');
hoverEntry = true;
jQuery('.__AC_keyhover').removeClass('__AC_keyhover');
isFreetext = false;
}, function() {
jQuery(this).removeClass('__AC_ie8HoverFix');
hoverEntry = false;
});
}
}
var footerFreetext = options['footerFreetext'] || JSONdata['footerFreetext'];
var footerMissingText = options['footerMissingText'] || JSONdata['footerMissingText'];
var footerInfoText = options['footerInfoText'] || JSONdata['footerInfoText'];
if (JSONdata['footer'] != undefined) {
box.find('.__AC_layer').append(JSONdata['footer']);
} else if (options['footer']) {
box.find('.__AC_layer').append(jQuery(options['footer']).clone(true));
} else if (footerFreetext) {
box.find('.__AC_layer').append(jQuery("<div>" + footerMissingText + "</div>").click(function(){
jQuery(element).val("");
jQuery("<div><input/><span>go</span></div>").find("span").click(function(){
var name = jQuery(this).parent().find("input").val();
hideBox();
hoverEntry = false;
if (name != "") {
freetext();
isFreetext = true;
} else {
invalidated();
isFreetext = false;
}
jQuery(element).val(name);
def = typed = name;
jQuery(element).blur();
}).css({
"background-color": "#f0a823",
"color": "#ffffff",
"padding": "2px",
"width": "14px",
"height": "14px",
"display": "block",
"text-align": "center",
"cursor": "pointer",
"margin-left": parseInt(jQuery(".__AC_data").innerWidth()) - 28 + "px"
}).parent().find("input").keyup(function(e){
if (e.which == 13) {
jQuery(this).parent().find("span").click();
}
}).css({
"border": "0px",
"color": "#c0c0c0",
"display": "block",
"float": "left",
"width": parseInt(jQuery(".__AC_data").innerWidth()) - 30 + "px",
"height": "18px"
}).val(footerInfoText).focus(function() {
jQuery(this).unbind("focus").css({
"color": "#000000"
}).val("");
}).parent().hover(function() {
hoverEntry = true;
}, function() {
hoverEntry = false;
}).insertAfter(this);
jQuery(this).remove();
jQuery(".__AC_data").remove()
}).hover(function() {
hoverEntry = true;
}, function() {
hoverEntry = false;
}).css({
"color": "#BB3A11",
"background-color": "#F9F5E1",
"margin-right": "10px",
"margin-top": "10px",
"padding": "2px",
"text-align": "right",
"cursor": "pointer"
}));
}
box.css('width', jQuery(element).outerWidth() + 'px')
jQuery(element).parent().append(box);
editing();
if (options['onShow'] && typeof options['onShow'] === 'function') {
options['onShow']();
}
}
}
);
}
};
/**
* Hide the box
*/
var hideBox = function() {
box.remove();
boxShown = false;
};
var showValidatingBox = function() {
validatingBox.appendTo(jQuery(element).parent())
.css('width', jQuery(element).outerWidth() + 'px');
}
/**
* Function which will be triggered when we focus the box.
*/
jQuery(element).focus(function() {
hasFocus = true;
if (!isValidated && originalVal == jQuery(element).val()) {
jQuery(element).val('');
}
editing();
});
/**
* Leaving the box
*/
jQuery(element).blur(function() {
hasFocus = false;
if (jQuery(element).val() == '') {
hideBox();
jQuery(element).val(originalVal);
if (isValidated) {
validated();
} else {
invalidated();
}
} else if (options['allowFreetext']) {
hideBox();
freetext();
} else if (!hoverEntry && options['closeWhenBlur']) {
hideBox();
if (def != jQuery(element).val()) {
jQuery(element).val(def);
}
if (isFreetext) {
freetext();
} else if (isValidated || def == typed) {
validated();
} else {
jQuery(element).val(def);
invalidated();
}
} else if (def == typed) {
if (isFreetext) {
freetext();
} else {
validated();
}
}
});
/**
* Key enter, has to be done on keypress so that it happens before any submit events
*/
jQuery(element).keypress(function(keyCapture) {
if (keyCapture.which == '13') {
// simulate click to trigger correct callback (possibly from JSONdata)
jQuery('.__AC_keyhover').click();
return !options.trapEnter;
}
return true;
});
/**
* Pressing and button which the box has focus
*/
jQuery(element).keyup(function(keyCapture) {
clearTimeout(timeout);
typed = jQuery(element).val();
/**
* Key escape
*/
if (keyCapture.which == 27) {
hideBox();
jQuery(element).blur();
/**
* Key up-arrow
*/
} else if (keyCapture.which == '38') {
if (jQuery('.__AC_keyhover').length == 0 || jQuery('.__AC_keyhover').prev().length == 0) { // || first
jQuery('.__AC_keyhover').removeClass('__AC_keyhover');
jQuery('.__AC_data div:last-child').addClass('__AC_keyhover');
} else {
jQuery('.__AC_keyhover').removeClass('__AC_keyhover').prev().addClass('__AC_keyhover');
}
/**
* Key down-arrow
*/
} else if (keyCapture.which == '40') {
if (jQuery('.__AC_keyhover').length == 0 || jQuery('.__AC_keyhover').next().length == 0) { // || last
jQuery('.__AC_keyhover').removeClass('__AC_keyhover');
jQuery('.__AC_data div:first-child').addClass('__AC_keyhover');
} else {
jQuery('.__AC_keyhover').removeClass('__AC_keyhover').next().addClass('__AC_keyhover');
}
} else if (typed.length < options['minimumKeys']) {
hideBox();
showValidatingBox();
} else if (def != typed && typed.length >= options['minimumKeys']) {
hideBox();
showValidatingBox();
timeout = window.setTimeout(function() {
showBox();
}, options['waitTime']);
}
});
/**
* Do correct styling of the box at init
*/
if (isValidated) {
validated();
} else {
invalidated();
}
});
};