This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
jquery.dropdown.js
431 lines (367 loc) · 14.2 KB
/
jquery.dropdown.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
/* globals jQuery, window, document */
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS
module.exports = factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function($) {
var methods = {
options : {
"optionClass": "",
"dropdownClass": "",
"autoinit": false,
"callback": false,
"onSelected": false,
"destroy": function(element) {
this.destroy(element);
},
"dynamicOptLabel": "Add a new option..."
},
init: function(options) {
// Apply user options if user has defined some
if (options) {
options = $.extend(methods.options, options);
} else {
options = methods.options;
}
function initElement($select) {
// Don't do anything if this is not a select or if this select was already initialized
if ($select.data("dropdownjs") || !$select.is("select")) {
return;
}
// Is it a multi select?
var multi = $select.attr("multiple");
// Does it allow to create new options dynamically?
var dynamicOptions = $select.attr("data-dynamic-opts"),
$dynamicInput = $();
// Create the dropdown wrapper
var $dropdown = $("<div></div>");
$dropdown.addClass("dropdownjs").addClass(options.dropdownClass);
$dropdown.data("select", $select);
// Create the fake input used as "select" element and cache it as $input
var $input = $("<input type=text readonly class=fakeinput>");
if ($.material) { $input.data("mdproc", true); }
// Append it to the dropdown wrapper
$dropdown.append($input);
// Create the UL that will be used as dropdown and cache it AS $ul
var $ul = $("<ul></ul>");
$ul.data("select", $select);
// Append it to the dropdown
$dropdown.append($ul);
// Transfer the placeholder attribute
$input.attr("placeholder", $select.attr("placeholder"));
// Loop trough options and transfer them to the dropdown menu
$select.find("option").each(function() {
// Cache $(this)
var $this = $(this);
methods._addOption($ul, $this);
});
// If this select allows dynamic options add the widget
if (dynamicOptions) {
$dynamicInput = $("<li class=dropdownjs-add></li>");
$dynamicInput.append("<input>");
$dynamicInput.find("input").attr("placeholder", options.dynamicOptLabel);
$ul.append($dynamicInput);
}
// Cache the dropdown options
var selectOptions = $dropdown.find("li");
// If is a single select, selected the first one or the last with selected attribute
if (!multi) {
var $selected;
if ($select.find(":selected").length) {
$selected = $select.find(":selected").last();
}
else {
$selected = $select.find("option, li").first();
// $selected = $select.find("option").first();
}
methods._select($dropdown, $selected);
} else {
var selectors = [], val = $select.val()
for (var i in val) {
selectors.push(val[i]);
}
if (selectors.length > 0) {
var $target = $dropdown.find(function() { return $.inArray($(this).data("value"), selectors) !== -1; });
$target.removeClass("selected");
methods._select($dropdown, $target);
}
}
// Transfer the classes of the select to the input dropdown
$input.addClass($select[0].className);
// Hide the old and ugly select
$select.hide().attr("data-dropdownjs", true);
// Bring to life our awesome dropdownjs
$select.after($dropdown);
// Call the callback
if (options.callback) {
options.callback($dropdown);
}
//---------------------------------------//
// DROPDOWN EVENTS //
//---------------------------------------//
// On click, set the clicked one as selected
$ul.on("click", "li:not(.dropdownjs-add)", function(e) {
methods._select($dropdown, $(this));
// trigger change event, if declared on the original selector
$select.change();
});
$ul.on("keydown", "li:not(.dropdownjs-add)", function(e) {
if (e.which === 27) {
$(".dropdownjs > ul > li").attr("tabindex", -1);
return $input.removeClass("focus").blur();
}
if (e.which === 32 && !$(e.target).is("input")) {
methods._select($dropdown, $(this));
return false;
}
});
$ul.on("focus", "li:not(.dropdownjs-add)", function() {
if ($select.is(":disabled")) {
return;
}
$input.addClass("focus");
});
// Add new options when the widget is used
if (dynamicOptions && dynamicOptions.length) {
$dynamicInput.on("keydown", function(e) {
if(e.which !== 13) return;
var $option = $("<option>"),
val = $dynamicInput.find("input").val();
$dynamicInput.find("input").val("");
$option.attr("value", val);
$option.text(val);
$select.append($option);
});
}
// Listen for new added options and update dropdown if needed
$select.on("DOMNodeInserted", function(e) {
var $this = $(e.target);
if (!$this.val().length) return;
methods._addOption($ul, $this);
$ul.find("li").not(".dropdownjs-add").attr("tabindex", 0);
});
$select.on("DOMNodeRemoved", function(e) {
var deletedValue = $(e.target).attr('value');
$ul.find("li").filter(function() { return $(this).data("value") === deletedValue; }).remove();
var $selected;
setTimeout(function () {
if ($select.find(":selected").length) {
$selected = $select.find(":selected").last();
}
else {
$selected = $select.find("option, li").first();
}
methods._select($dropdown, $selected);
}, 100);
});
// Update dropdown when using val, need to use .val("value").trigger("change");
$select.on("change", function(e) {
var $this = $(e.target);
if (!multi) {
var $selected;
if ($select.find(":selected").length) {
$selected = $select.find(":selected").last();
}
else {
$selected = $select.find("option, li").first();
}
methods._select($dropdown, $selected);
} else {
var target = $select.find(":selected"),
values = $(this).val();
// Unselect all options
selectOptions.removeClass("selected");
// Select options
target.each(function () {
var selected = selectOptions.filter(function() { return $.inArray($(this).data("value"), values) !== -1; });
selected.addClass("selected");
});
}
});
// Used to make the dropdown menu more dropdown-ish
$input.on("click focus", function(e) {
e.stopPropagation();
if ($select.is(":disabled")) {
return;
}
$(".dropdownjs > ul > li").attr("tabindex", -1);
$(".dropdownjs > input").not($(this)).removeClass("focus").blur();
$(".dropdownjs > ul > li").not(".dropdownjs-add").attr("tabindex", 0);
// Set height of the dropdown
var coords = {
top: $(this).offset().top - $(document).scrollTop(),
left: $(this).offset().left - $(document).scrollLeft(),
bottom: $(window).height() - ($(this).offset().top - $(document).scrollTop()),
right: $(window).width() - ($(this).offset().left - $(document).scrollLeft())
};
var height = coords.bottom;
// Decide if place the dropdown below or above the input
if (height < 200 && coords.top > coords.bottom) {
height = coords.top;
$ul.attr("placement", $("body").hasClass("rtl") ? "top-right" : "top-left");
} else {
$ul.attr("placement", $("body").hasClass("rtl") ? "bottom-right" : "bottom-left");
}
$(this).next("ul").css("max-height", height - 20);
$(this).addClass("focus");
});
// Close every dropdown on click outside
$(document).on("click", function(e) {
// Don't close the multi dropdown if user is clicking inside it
if (multi && $(e.target).parents(".dropdownjs").length) return;
// Don't close the dropdown if user is clicking inside the dynamic-opts widget
if ($(e.target).parents(".dropdownjs-add").length || $(e.target).is(".dropdownjs-add")) return;
// Close opened dropdowns
$(".dropdownjs > ul > li").attr("tabindex", -1);
if ($(e.target).hasClass('disabled')) {
return;
}
$input.removeClass("focus");
});
}
if (options.autoinit) {
$(document).on("DOMNodeInserted", function(e) {
var $this = $(e.target);
if (!$this.is("select")) {
$this = $this.find('select');
}
$this.each(function() {
if ($(this).is(options.autoinit)) {
initElement($(this));
}
});
});
}
// Loop trough elements
$(this).each(function() {
initElement($(this));
});
},
select: function(target) {
var $target = $(this).find(function() { return $(this).data("value") === target; });
methods._select($(this), $target);
},
_select: function($dropdown, $target) {
if ($target.is(".dropdownjs-add")) return;
// Get dropdown's elements
var $select = $dropdown.data("select"),
$input = $dropdown.find("input.fakeinput");
// Is it a multi select?
var multi = $select.attr("multiple");
// Cache the dropdown options
var selectOptions = $dropdown.find("li");
// Behavior for multiple select
if (multi) {
// Toggle option state
$target.toggleClass("selected");
// Toggle selection of the clicked option in native select
$target.each(function(){
var value = $(this).prop("tagName") === "OPTION" ? $(this).val() : $(this).data("value"),
$selected = $select.find("[value=\"" + value + "\"]");
$selected.prop("selected", $(this).hasClass("selected"));
});
// Add or remove the value from the input
var text = [];
selectOptions.each(function() {
if ($(this).hasClass("selected")) {
text.push($(this).text());
}
});
$input.val(text.join(", "));
}
// Behavior for single select
if (!multi) {
if ($target.hasClass("disabled")) {
return;
}
// Unselect options except the one that will be selected
if ($target.is("li")) {
selectOptions.not($target).removeClass("selected");
}
// Select the selected option
$target.addClass("selected");
// Set the value to the input
$input.val($target.text().trim());
var value = $target.prop("tagName") === "OPTION" ? $target.val() : $target.data("value");
// When val is set below on $select, it will fire change event,
// which ends up back here, make sure to not end up in an infinite loop.
// This is done last so text input is initialized on first load when condition is true.
if (value === $select.val()) {
return;
}
// Set the value to the native select
$select.val(value);
}
// This is used only if Material Design for Bootstrap is selected
if ($.material) {
if ($input.val().trim()) {
$select.removeClass("empty");
} else {
$select.addClass("empty");
}
}
// Call the callback
if (this.options.onSelected) {
this.options.onSelected($target.data("value"));
}
},
_addOption: function($ul, $this) {
// Create the option
var $option = $("<li></li>");
// Style the option
$option.addClass(this.options.optionClass);
// If the option has some text then transfer it
if ($this.text()) {
$option.text($this.text());
}
// Otherwise set the empty label and set it as an empty option
else {
$option.html(" ");
}
// Set the value of the option
$option.data("value", $this.val());
// Will user be able to remove this option?
if ($ul.data("select").attr("data-dynamic-opts")) {
$option.append("<span class=close></span>");
$option.find(".close").on("click", function() {
$option.remove();
$this.remove();
});
}
// Ss it selected?
if ($this.prop("selected")) {
$option.attr("selected", true);
$option.addClass("selected");
}
if ($this.prop("disabled")) {
$option.addClass("disabled");
}
// Append option to our dropdown
if ($ul.find(".dropdownjs-add").length) {
$ul.find(".dropdownjs-add").before($option);
} else {
$ul.append($option);
}
},
destroy: function($e) {
$($e).show().removeAttr('data-dropdownjs').next('.dropdownjs').remove();
}
};
$.fn.dropdown = function(params) {
if( typeof methods[params] == 'function' ) methods[params](this);
if (methods[params]) {
return methods[params].apply(this, Array.prototype.slice.call(arguments,1));
} else if (typeof params === "object" | !params) {
return methods.init.apply(this, arguments);
} else {
$.error("Method " + params + " does not exists on jQuery.dropdown");
}
};
}));