-
Notifications
You must be signed in to change notification settings - Fork 52
/
jquery.minimalect.js
655 lines (575 loc) · 22.6 KB
/
jquery.minimalect.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/************************************
MINIMALECT 0.9
A minimalistic select replacement
jQuery 1.7+ required.
Developed by @groenroos
http://www.groenroos.fi
Github: http://git.io/Xedg9w
Licensed under the MIT license.
************************************/
;(function ( $, window, document, undefined ) {
var pluginName = "minimalect",
defaults = {
// settings
theme: "", // name of the theme used
reset: false,
transition: "fade",
transition_time: 150,
remove_empty_option: true,
searchable: true, // whether or not the combobox functionality is enabled
ajax: null, // URL to an external resource
debug: false, // whether to be verbose in the console
live: true, // whether to automatically detect changes
// messages
placeholder: "Select a choice", // default placeholder when nothing is selected
empty: "No results match your keyword.", // error message when nothing matches the filter search term
error_message: "There was a problem with the request.", // error message when the AJAX call fails
// classes
class_container: "minict_wrapper", // wrapper div for the element
class_dropdown: "minict_dropdown", // dropdown element
class_group: "minict_group", // list item for an optgroup
class_empty: "minict_empty", // "No results" message
class_active: "active", // applied to wrapper when the dropdown is displayed
class_disabled: "disabled", // applied to list elements that are disabled
class_selected: "selected", // the currently selected item in the dropdown
class_hidden: "hidden", // an item that doesn't match the filter search term
class_highlighted: "highlighted", // item highlighted by keyboard navigation
class_first: "minict_first", // first visible element
class_last: "minict_last", // last visible element
class_reset: "minict_reset", // reset link
// callbacks
beforeinit: function(){}, // called before Minimalect is initialized
afterinit: function(){}, // called right after Minimalect is initialized
onchange: function(){}, // called whenever the user changes the selected value
onopen: function(){}, // called when the dropdown is displayed
onclose: function(){}, // called when the dropdown is hidden
onfilter: function(){} // called every time the filter has been activated
};
// The actual plugin constructor
function Plugin( element, options ) {
this.element = $(element);
this.options = $.extend( {}, defaults, options );
this._defaults = defaults;
this._name = pluginName;
this.label = $('[for="'+this.element.attr('id')+'"]').attr('for', 'minict_'+this.element.attr('id'));
this._init();
}
Plugin.prototype = {
// INITIALIZATION
_init: function() {
// before init callback
this.options.beforeinit();
// PREPWORK
var op = this.options,
m = this;
if(op.ajax)
op.live = false;
// create the wrapper
this.wrapper = $('<div class="'+op.class_container+'"></div>');
// hide the original select and add the wrapper
this.element.hide().after(this.wrapper);
// apply the current theme to the wrapper
if(op.theme) this.wrapper.addClass(op.theme);
// reflect disabled status
if(this.element.prop("disabled"))
this.wrapper.addClass(op.class_disabled);
// create and add the input
this.input = $(
'<span '+
(op.searchable ? 'contenteditable="true"' : '') +
' data-placeholder="'+(this.element.find("option[selected]").text() || this.element.attr("placeholder") || (op.placeholder != null) ? op.placeholder : this.element.find("option:first").text())+
'" '+ (this.element.is('[tabindex]') ? ('tabindex='+this.element.attr('tabindex')) : '') +'>'+
(this.element.find("option[selected]").html() || "")+
'</span>'
).appendTo(this.wrapper);
// add the reset link, if it's wanted
if(op.reset)
this.reset = $('<a href="#" class="'+op.class_reset+'">×</a>').appendTo(this.wrapper);
// parse the select itself, and create the dropdown markup
this.ul = $('<ul class="'+op.class_dropdown+'">'+this._parseSelect()+'<li class="'+op.class_empty+'">'+op.empty+'</li></ul>').appendTo(this.wrapper);
this.items = this.wrapper.find('li');
// if it's preselected, select the option itself as well
if(this.element.find("option[selected]").length) {
this._showResetLink();
this.items.filter('[data-value="'+this.element.find("option[selected]").val()+'"]').addClass(op.class_selected);
}
// BIND EVENTS
// hide dropdown when you click elsewhere
$(document).on("click touch", function(){ m._hideChoices(m.wrapper) });
// hide dropdown when moving focus outside it
$("*").not(this.wrapper).not(this.wrapper.find('*')).on("focus", function(){ m._hideChoices(m.wrapper) });
// toggle dropdown when you click on the dropdown itself
this.wrapper.on("click touch", function(e){
e.stopPropagation();
// only close the dropdown when it's not disabled and not multiselect
if(!m.element.prop("multiple") && !m.element.prop("disabled"))
m._toggleChoices()
});
// toggle dropdown when you click on the associated label, if present
this.label.on("click touch", function(e){ e.stopPropagation(); m.input.trigger('focus') });
// select choice when you click on it
this.wrapper.on("click touch", "li:not(."+op.class_group+", ."+op.class_empty+", ."+op.class_disabled+")", function(){ m._selectChoice($(this)) });
// stop the dropdown from closing when you click on a group or empty placeholder
this.wrapper.on("click touch", "li."+op.class_group+", li."+op.class_empty+", li."+op.class_disabled, function(e){
e.stopPropagation();
m.input.focus();
});
// if the original is focused or blurred manually, mimic it
// also handle the custom update event
this.element.on("focus", function(){
m.element.blur();
m._showChoices();
})
.on("blur", m._hideChoices)
.on("update", m.update);
// bind reset only if it's there
if(op.reset){
this.wrapper.on("click touch", "a."+op.class_reset, function(e){
e.stopPropagation();
m._resetChoice();
return false;
});
}
// key bindings for the input element
this.input.on("focus click touch", function(e){
e.stopPropagation();
if(!m.element.prop("disabled")) m._showChoices(); else m.input.blur();
}).on("keydown", function(e){
// keyboard navigation
switch(e.keyCode) {
// up
case 38:
e.preventDefault();
m._navigateChoices('up');
break;
// down
case 40:
e.preventDefault();
m._navigateChoices('down');
break;
// enter
case 13:
// tab
case 9:
// select the highlighted choice
if(m.items.filter("."+op.class_highlighted).length)
m._selectChoice(m.items.filter("."+op.class_highlighted));
// or if there is none, select the first choice after filtering
else if(m.input.text())
m._selectChoice(m.items.not("."+op.class_group+", ."+op.class_empty).filter(':visible').first());
if(e.keyCode===13){
e.preventDefault();
m._hideChoices(m.wrapper);
}
break;
// escape
case 27:
e.preventDefault();
// close the select and don't change the value
m._hideChoices(m.wrapper);
break;
}
}).on("keyup", function(e){
// if we're not navigating, filter
if($.inArray(e.keyCode, [38, 40, 13, 9, 27]) === -1){
m._filterChoices();
}
});
// if mutation observing is supported
if(window.MutationObserver){
// observe the original for DOM changes so they can be reflected
this.observer = new MutationObserver(function( mutations ) {
// if there were changes...
if(mutations.length > 0){
// ...reparse the select
m.ul.html(m._parseSelect()+'<li class="'+op.class_empty+'">'+op.empty+'</li>');
m.items = m.wrapper.find('li');
if(m.options.debug) console.log("Minimalect detected a DOM change for ", m.element);
}
});
this.observer.observe(m.element[0], {childList: true});
}
// poll the original for changes
if(op.live){
// set cache to the original value
var prevval = this.element.val();
// set a tight interval to check for the original
setInterval(function(){
// if we're out of date
if(prevval != m.element.val() && m.element.val() != null && m.element.val() != "") {
// update cache
prevval = m.element.val();
// update selection
if(typeof prevval == "array") {
prevval.each(function(k,v){
m._selectChoice(m.wrapper.find("li[data-value='"+v+"']"));
});
} else {
m._selectChoice(m.wrapper.find("li[data-value='"+prevval+"']"));
}
} else if (m.element.val() == null || m.element.val() == "") {
// update cache
prevval = m.element.val();
// if it was empty, let's clear it
m.items.removeClass(m.options.class_selected);
m.input.text('').attr('data-placeholder', m.options.placeholder);
}
// let's also check for disabled
if(m.element.prop("disabled"))
m.wrapper.addClass(op.class_disabled);
else
m.wrapper.removeClass(op.class_disabled);
}, 100);
}
// after init callback
op.afterinit();
},
// PRIVATE METHODS
// navigate with a keyboard
// dr - direction we're going, either "up" or "down"
_navigateChoices: function(dr) {
var m = this,
wr = this.wrapper, // jQuery reference for the wrapper
op = this.options, // options object
items = this.items;
// list all the elements that aren't navigatable
var ignored = "."+op.class_hidden+", ."+op.class_empty+", ."+op.class_group;
if(!items.filter("."+op.class_highlighted).length) { // if nothing is selected, select the first or last
if(dr === 'up') {
items.not(ignored).last().addClass(op.class_highlighted);
} else if (dr === 'down') {
items.not(ignored).first().addClass(op.class_highlighted);
}
return false;
} else { // if something is selected...
// ...remove current selection...
cur = items.filter("."+op.class_highlighted);
cur.removeClass(op.class_highlighted);
// ...and figure out the next one
if(dr === 'up') {
if(items.not(ignored).first()[0] != cur[0]) { // if we're not at the first
cur.prevAll("li").not(ignored).first().addClass(op.class_highlighted); // highlight the prev
// make sure it's visible in a scrollable list
var offset = items.filter("."+op.class_highlighted).offset().top - this.ul.offset().top + this.ul.scrollTop();
if (this.ul.scrollTop() > offset)
this.ul.scrollTop(offset);
} else { // if we are at the first
items.not(ignored).last().addClass(op.class_highlighted); // highlight the last
// make sure it's visible in a scrollable list
this.ul.scrollTop(this.ul.height());
}
} else if (dr === 'down') {
if(items.not(ignored).last()[0] != cur[0]) { // if we're not at the last
cur.nextAll("li").not(ignored).first().addClass(op.class_highlighted); // highlight the next
// make sure it's visible in a scrollable list
var ddbottom = this.ul.height(),
libottom = items.filter("."+op.class_highlighted).offset().top - this.ul.offset().top + items.filter("."+op.class_highlighted).outerHeight();
if (ddbottom < libottom)
this.ul.scrollTop(this.ul.scrollTop() + libottom - ddbottom);
} else { // if we are at the last
items.not(ignored).first().addClass(op.class_highlighted); // highlight the first
// make sure it's visible in a scrollable list
this.ul.scrollTop(0);
}
}
}
},
// parse the entire select based on whether it has optgroups or not, and return the new markup
_parseSelect: function() {
var m = this, ulcontent = "";
if( !this.element.find("optgroup").length ) { // if we don't have groups
// just parse the elements regularly
ulcontent = this._parseElements( this.element.html() );
} else { // if we have groups
// parse each group separately
this.element.find("optgroup").each(function(){
// create a group element
ulcontent += '<li class="'+m.options.class_group+'">'+$(this).attr("label")+'</li>';
// and add its children
ulcontent += m._parseElements( $(this).html() );
});
}
return ulcontent;
},
// turn option elements into li elements
// elhtml - HTML containing the options
_parseElements: function(elhtml) {
var m = this, readyhtml = "";
// go through each option
$( $.trim(elhtml) ).filter("option").each(function(){
var $el = $(this);
if ($el.attr('value') === '' && m.options.remove_empty_option) return;
// create an li with a data attribute containing its value
readyhtml += '<li data-value="'+$el.val().replace(/"/g, """)+'" class="'+($el.attr("class") || "")+($el.prop("disabled") ? " "+m.options.class_disabled : "")+'">'+$el.text()+'</li>';
});
// spit it out
return readyhtml;
},
// toggle the visibility of the dropdown
_toggleChoices: function(){
(!this.wrapper.hasClass(this.options.class_active)) ? this._showChoices() : this._hideChoices(this.wrapper);
},
// show the dropdown
// cb - callback before the animation plays
_showChoices: function(cb){
var m = this,
wr = this.wrapper, // jQuery reference for the wrapper
op = this.options; // options object
if (!wr.hasClass(op.class_active)){
// keep the first and last classes up to date
this._updateFirstLast(false);
// close all other open minimalects
$("."+op.class_container).each(function(){ //todo this doesn't work if the container classes are different
if($(this)[0] !== wr[0])
m._hideChoices($(this));
});
// internal callback
if(typeof cb === 'function') cb.call();
// add the active class
wr.addClass(op.class_active);
switch(op.transition) {
case "fade":
this.ul.fadeIn(op.transition_time);
break;
default:
this.ul.show();
break;
}
// make the input editable
this.input.text("").focus();
// hide the reset link
this._hideResetLink();
// callback
this.options.onopen();
} else {
// internal callback
if(typeof cb === 'function') cb.call();
}
},
_resetDropdown: function(cb){
var op = this.options; // options object
// reset the filtered elements
this.items.removeClass(op.class_hidden);
// hide the empty error message
this.wrapper.find("."+op.class_empty).hide();
// reset keyboard navigation
this.items.filter("."+op.class_highlighted).removeClass(op.class_highlighted);
// internal callback
if(typeof cb === 'function') cb.call();
},
// hide the dropdown
// wr - jQuery reference for the wrapper
// cb - callback for after the animation has played
_hideChoices: function(wr, cb){
var op = this.options, // options object
to = op.transition_time, // timeout for the transition to finish
m = this;
if (wr.hasClass(op.class_active)){
// remove the active class and fade out
wr.removeClass(op.class_active);
switch(op.transition) {
case "fade":
wr.children("ul").fadeOut(op.transition_time);
break;
default:
wr.children("ul").hide();
to = 0;
break;
}
// set a timeout for clearing the field, so there's no flickering
setTimeout(function(){
// reset filters
m._resetDropdown(cb);
// blur the input
m.input.blur();
// reset it
if(m.input.attr("data-placeholder") != op.placeholder) {
// if we have a previously selected value, restore that
m.input.text(m.input.attr("data-placeholder"));
} else if(!m.items.filter("."+op.class_selected).length) {
// if we have no selection, empty it to show placeholder
m.input.text("");
}
}, to);
// show the reset link
m._showResetLink();
// callback
op.onclose();
} else {
// internal callback
if(typeof cb === 'function') cb.call();
}
},
// filter choices based on user input
_filterChoices: function(){
var wr = this.wrapper, // jQuery reference for the wrapper
op = this.options, // options object
m = this;
if(op.ajax) {
// if we're searching from ajax
$.post(op.ajax, {"q": this.input.text()})
.success(function(data){
// we got a response
if(op.debug) console.log("Minimalect received ", data, " for query '"+m.input.text()+"' in ", m.element);
if(data.length) {
// if we have results
var new_html = "";
$.each(data, function(k, choice){
// parse each data point to an option in the original
new_html += '<option value="'+choice.value+'">'+choice.name+'</option>';
});
// populate original element
m.element.html(new_html);
// parse and display it
m.ul.html(m._parseSelect()+'<li class="'+op.class_empty+'">'+op.empty+'</li>');
wr.find("."+op.class_empty).hide();
// refresh internal cache
m.items = wr.find('li');
// callback, results found
m.options.onfilter(true);
} else {
// show a "no results" placeholder if there's nothing to show
m.ul.html('<li class="'+op.class_empty+'">'+op.empty+'</li>');
wr.find("."+op.class_empty).show();
// tell the console if debug mode is on
if(op.debug) console.log("Minimalect didn't find any results for '"+m.input.text()+"' from ", m.element);
// callback, no results found
m.options.onfilter(false);
}
})
.error(function(data){
// show feedback for the user
wr.find("."+op.class_empty).text(op.error_message);
wr.find("li").not("."+op.class_empty).addClass(op.class_hidden);
wr.find("."+op.class_empty).show();
// tell the console if debug mode is on
if(op.debug) console.error("Minimalect's AJAX query failed for ", m.element, " - came back with ", data);
});
} else {
// traditional filtering
// get the filter value, escape regex chars (thanks Andrew Clover!)
var filter = this.input.text().replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
// reset keyboard navigation
this.items.filter("."+op.class_highlighted).removeClass(op.class_highlighted);
// filter through each option
this.items.not(op.class_group).each(function(){
// if there's no match (or if it's disabled), hide it. otherwise, unhide it
if ($(this).text().search(new RegExp(filter, "i")) < 0 || $(this).hasClass(op.class_disabled))
$(this).addClass(op.class_hidden);
else
$(this).removeClass(op.class_hidden);
});
// make sure optgroups with no choices are hidden
// sort of a kludge since we have no hierarchy
this.items.filter("."+op.class_group).removeClass(op.class_hidden).each(function(){
nextlis = $(this).nextAll("li").not("."+op.class_hidden+", ."+op.class_empty);
if(nextlis.first().hasClass(op.class_group) || !nextlis.length) $(this).addClass(op.class_hidden);
});
// show a "no results" placeholder if there's nothing to show
wr.find("."+op.class_empty).hide();
if(!this.items.not("."+op.class_hidden+", ."+op.class_empty).length) {
wr.find("."+op.class_empty).show();
// tell the console if debug mode is on
if(op.debug) console.log("Minimalect didn't find any results for '"+this.input.text()+"' from ", this.element);
// callback, no results found
this.options.onfilter(false);
} else {
// callback, results found
this.options.onfilter(true);
}
// keep the first and last classes up to date
this._updateFirstLast(true);
}
},
// select the choice defined
// ch - jQuery reference for the li element the user has chosen
_selectChoice: function(ch){
var el = this.element, // jQuery reference for the original select element
op = this.options, // options object
vals = [],
names = [];
// if it's disabled, au revoir
if(ch.hasClass(this.options.class_disabled)) return false;
if(!op.live)
this.items = this.wrapper.find('li');
// apply the selected class
if(!this.element.prop("multiple"))
this.items.removeClass(op.class_selected);
ch.addClass(op.class_selected);
this.items.filter("."+op.class_selected).each(function(){
vals.push($(this).data("value"));
names.push($(this).text());
});
// show it up in the input
this.input.text(names.join(", ")).attr("data-placeholder", names.join(", "));
// if the selected choice is different
if(el.val() != ch.data("value") || el.val() != vals){
// update the original select element
el.val(vals);
// call original select change event
el.trigger("change");
}
this._showResetLink();
// callback
this.options.onchange.call(this, ch.data("value"), ch.text());
},
// clear the select
_resetChoice: function() {
this.element.val('').trigger("change");
this._hideResetLink();
},
// show the reset link if options.reset is true
_showResetLink: function() {
if(this.input.text().length > 0 || this.ul.find("li."+this.options.class_selected).length > 0)
this.options.reset && this.reset.show();
},
// hide the reset link if options.reset is true
_hideResetLink: function() {
this.options.reset && this.reset.hide();
},
// keep the first and last classes up-to-date
// vi - whether we want to count visibility or not
_updateFirstLast: function(vi){
var wr = this.wrapper, // jQuery reference for the wrapper
op = this.options; // options object
wr.find("."+op.class_first+", ."+op.class_last).removeClass(op.class_first+" "+op.class_last);
if(vi) {
this.items.filter(":visible").first().addClass(op.class_first);
this.items.filter(":visible").last().addClass(op.class_last);
} else {
this.items.first().addClass(op.class_first);
this.items.not("."+op.class_empty).last().addClass(op.class_last);
}
},
// PUBLIC METHODS
// uninit Minimalect
destroy: function(){
// remove (and automatically unbind) all Minimalect stuff
this.wrapper.remove();
// display and unhook the original
this.element.off("change focus blur").show();
// stop listening for changes
if (window.MutationObserver)
this.observer.disconnect();
// if debug mode is on, let them know upstairs
if(this.options.debug) console.log("Minimalect destroyed for ", this.element);
},
// update Minimalect's choice from the original select
update: function(){
// reparse the select
this.ul.html(this._parseSelect()+'<li class="'+this.options.class_empty+'">'+this.options.empty+'</li>');
}
};
$.fn[pluginName] = function ( options, argument ) {
return this.each(function () {
if ($.isFunction(Plugin.prototype[options]) && options.charAt(0) != "_") {
if(arguments.length == 1)
$.data(this, 'plugin_' + pluginName)[options]();
else
$.data(this, 'plugin_' + pluginName)[options](argument);
} else if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new Plugin( this, options ));
}
});
};
})( jQuery, window, document );