1 | (function ($) {
|
2 | $(document).ready(function() {
|
3 |
|
4 |
|
5 | Materialize.updateTextFields = function() {
|
6 | var input_selector = 'input[type=text], input[type=password], input[type=email], input[type=url], input[type=tel], input[type=number], input[type=search], textarea';
|
7 | $(input_selector).each(function(index, element) {
|
8 | var $this = $(this);
|
9 | if ($(element).val().length > 0 || element.autofocus || $this.attr('placeholder') !== undefined) {
|
10 | $this.siblings('label').addClass('active');
|
11 | } else if ($(element)[0].validity) {
|
12 | $this.siblings('label').toggleClass('active', $(element)[0].validity.badInput === true);
|
13 | } else {
|
14 | $this.siblings('label').removeClass('active');
|
15 | }
|
16 | });
|
17 | };
|
18 |
|
19 |
|
20 | var input_selector = 'input[type=text], input[type=password], input[type=email], input[type=url], input[type=tel], input[type=number], input[type=search], textarea';
|
21 |
|
22 |
|
23 | $(document).on('change', input_selector, function () {
|
24 | if($(this).val().length !== 0 || $(this).attr('placeholder') !== undefined) {
|
25 | $(this).siblings('label').addClass('active');
|
26 | }
|
27 | validate_field($(this));
|
28 | });
|
29 |
|
30 |
|
31 | $(document).ready(function() {
|
32 | Materialize.updateTextFields();
|
33 | });
|
34 |
|
35 |
|
36 | $(document).on('reset', function(e) {
|
37 | var formReset = $(e.target);
|
38 | if (formReset.is('form')) {
|
39 | formReset.find(input_selector).removeClass('valid').removeClass('invalid');
|
40 | formReset.find(input_selector).each(function () {
|
41 | if ($(this).attr('value') === '') {
|
42 | $(this).siblings('label').removeClass('active');
|
43 | }
|
44 | });
|
45 |
|
46 |
|
47 | formReset.find('select.initialized').each(function () {
|
48 | var reset_text = formReset.find('option[selected]').text();
|
49 | formReset.siblings('input.select-dropdown').val(reset_text);
|
50 | });
|
51 | }
|
52 | });
|
53 |
|
54 |
|
55 | $(document).on('focus', input_selector, function () {
|
56 | $(this).siblings('label, .prefix').addClass('active');
|
57 | });
|
58 |
|
59 | $(document).on('blur', input_selector, function () {
|
60 | var $inputElement = $(this);
|
61 | var selector = ".prefix";
|
62 |
|
63 | if ($inputElement.val().length === 0 && $inputElement[0].validity.badInput !== true && $inputElement.attr('placeholder') === undefined) {
|
64 | selector += ", label";
|
65 | }
|
66 |
|
67 | $inputElement.siblings(selector).removeClass('active');
|
68 |
|
69 | validate_field($inputElement);
|
70 | });
|
71 |
|
72 | window.validate_field = function(object) {
|
73 | var hasLength = object.attr('data-length') !== undefined;
|
74 | var lenAttr = parseInt(object.attr('data-length'));
|
75 | var len = object.val().length;
|
76 |
|
77 | if (object.val().length === 0 && object[0].validity.badInput === false) {
|
78 | if (object.hasClass('validate')) {
|
79 | object.removeClass('valid');
|
80 | object.removeClass('invalid');
|
81 | }
|
82 | }
|
83 | else {
|
84 | if (object.hasClass('validate')) {
|
85 |
|
86 | if ((object.is(':valid') && hasLength && (len <= lenAttr)) || (object.is(':valid') && !hasLength)) {
|
87 | object.removeClass('invalid');
|
88 | object.addClass('valid');
|
89 | }
|
90 | else {
|
91 | object.removeClass('valid');
|
92 | object.addClass('invalid');
|
93 | }
|
94 | }
|
95 | }
|
96 | };
|
97 |
|
98 |
|
99 | var radio_checkbox = 'input[type=radio], input[type=checkbox]';
|
100 | $(document).on('keyup.radio', radio_checkbox, function(e) {
|
101 |
|
102 | if (e.which === 9) {
|
103 | $(this).addClass('tabbed');
|
104 | var $this = $(this);
|
105 | $this.one('blur', function(e) {
|
106 |
|
107 | $(this).removeClass('tabbed');
|
108 | });
|
109 | return;
|
110 | }
|
111 | });
|
112 |
|
113 |
|
114 | var hiddenDiv = $('.hiddendiv').first();
|
115 | if (!hiddenDiv.length) {
|
116 | hiddenDiv = $('<div class="hiddendiv common"></div>');
|
117 | $('body').append(hiddenDiv);
|
118 | }
|
119 | var text_area_selector = '.materialize-textarea';
|
120 |
|
121 | function textareaAutoResize($textarea) {
|
122 |
|
123 |
|
124 | var fontFamily = $textarea.css('font-family');
|
125 | var fontSize = $textarea.css('font-size');
|
126 | var lineHeight = $textarea.css('line-height');
|
127 |
|
128 | if (fontSize) { hiddenDiv.css('font-size', fontSize); }
|
129 | if (fontFamily) { hiddenDiv.css('font-family', fontFamily); }
|
130 | if (lineHeight) { hiddenDiv.css('line-height', lineHeight); }
|
131 |
|
132 | if ($textarea.attr('wrap') === "off") {
|
133 | hiddenDiv.css('overflow-wrap', "normal")
|
134 | .css('white-space', "pre");
|
135 | }
|
136 |
|
137 | hiddenDiv.text($textarea.val() + '\n');
|
138 | var content = hiddenDiv.html().replace(/\n/g, '<br>');
|
139 | hiddenDiv.html(content);
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | if ($textarea.is(':visible')) {
|
146 | hiddenDiv.css('width', $textarea.width());
|
147 | }
|
148 | else {
|
149 | hiddenDiv.css('width', $(window).width()/2);
|
150 | }
|
151 |
|
152 | $textarea.css('height', hiddenDiv.height());
|
153 | }
|
154 |
|
155 | $(text_area_selector).each(function () {
|
156 | var $textarea = $(this);
|
157 | if ($textarea.val().length) {
|
158 | textareaAutoResize($textarea);
|
159 | }
|
160 | });
|
161 |
|
162 | $('body').on('keyup keydown autoresize', text_area_selector, function () {
|
163 | textareaAutoResize($(this));
|
164 | });
|
165 |
|
166 |
|
167 | $(document).on('change', '.file-field input[type="file"]', function () {
|
168 | var file_field = $(this).closest('.file-field');
|
169 | var path_input = file_field.find('input.file-path');
|
170 | var files = $(this)[0].files;
|
171 | var file_names = [];
|
172 | for (var i = 0; i < files.length; i++) {
|
173 | file_names.push(files[i].name);
|
174 | }
|
175 | path_input.val(file_names.join(", "));
|
176 | path_input.trigger('change');
|
177 | });
|
178 |
|
179 | |
180 |
|
181 |
|
182 |
|
183 | var range_type = 'input[type=range]';
|
184 | var range_mousedown = false;
|
185 | var left;
|
186 |
|
187 | $(range_type).each(function () {
|
188 | var thumb = $('<span class="thumb"><span class="value"></span></span>');
|
189 | $(this).after(thumb);
|
190 | });
|
191 |
|
192 | var range_wrapper = '.range-field';
|
193 | $(document).on('change', range_type, function(e) {
|
194 | var thumb = $(this).siblings('.thumb');
|
195 | thumb.find('.value').html($(this).val());
|
196 | });
|
197 |
|
198 | $(document).on('input mousedown touchstart', range_type, function(e) {
|
199 | var thumb = $(this).siblings('.thumb');
|
200 | var width = $(this).outerWidth();
|
201 |
|
202 |
|
203 | if (thumb.length <= 0) {
|
204 | thumb = $('<span class="thumb"><span class="value"></span></span>');
|
205 | $(this).after(thumb);
|
206 | }
|
207 |
|
208 |
|
209 | thumb.find('.value').html($(this).val());
|
210 |
|
211 | range_mousedown = true;
|
212 | $(this).addClass('active');
|
213 |
|
214 | if (!thumb.hasClass('active')) {
|
215 | thumb.velocity({ height: "30px", width: "30px", top: "-20px", marginLeft: "-15px"}, { duration: 300, easing: 'easeOutExpo' });
|
216 | }
|
217 |
|
218 | if (e.type !== 'input') {
|
219 | if(e.pageX === undefined || e.pageX === null){
|
220 | left = e.originalEvent.touches[0].pageX - $(this).offset().left;
|
221 | }
|
222 | else{
|
223 | left = e.pageX - $(this).offset().left;
|
224 | }
|
225 | if (left < 0) {
|
226 | left = 0;
|
227 | }
|
228 | else if (left > width) {
|
229 | left = width;
|
230 | }
|
231 | thumb.addClass('active').css('left', left);
|
232 | }
|
233 |
|
234 | thumb.find('.value').html($(this).val());
|
235 | });
|
236 |
|
237 | $(document).on('mouseup touchend', range_wrapper, function() {
|
238 | range_mousedown = false;
|
239 | $(this).removeClass('active');
|
240 | });
|
241 |
|
242 | $(document).on('mousemove touchmove', range_wrapper, function(e) {
|
243 | var thumb = $(this).children('.thumb');
|
244 | var left;
|
245 | if (range_mousedown) {
|
246 | if (!thumb.hasClass('active')) {
|
247 | thumb.velocity({ height: '30px', width: '30px', top: '-20px', marginLeft: '-15px'}, { duration: 300, easing: 'easeOutExpo' });
|
248 | }
|
249 | if (e.pageX === undefined || e.pageX === null) {
|
250 | left = e.originalEvent.touches[0].pageX - $(this).offset().left;
|
251 | }
|
252 | else{
|
253 | left = e.pageX - $(this).offset().left;
|
254 | }
|
255 | var width = $(this).outerWidth();
|
256 |
|
257 | if (left < 0) {
|
258 | left = 0;
|
259 | }
|
260 | else if (left > width) {
|
261 | left = width;
|
262 | }
|
263 | thumb.addClass('active').css('left', left);
|
264 | thumb.find('.value').html(thumb.siblings(range_type).val());
|
265 | }
|
266 | });
|
267 |
|
268 | $(document).on('mouseout touchleave', range_wrapper, function() {
|
269 | if (!range_mousedown) {
|
270 |
|
271 | var thumb = $(this).children('.thumb');
|
272 |
|
273 | if (thumb.hasClass('active')) {
|
274 | thumb.velocity({ height: '0', width: '0', top: '10px', marginLeft: '-6px'}, { duration: 100 });
|
275 | }
|
276 | thumb.removeClass('active');
|
277 | }
|
278 | });
|
279 |
|
280 | |
281 |
|
282 |
|
283 | $.fn.autocomplete = function (options) {
|
284 |
|
285 | var defaults = {
|
286 | data: {},
|
287 | limit: Infinity,
|
288 | onAutocomplete: null
|
289 | };
|
290 |
|
291 | options = $.extend(defaults, options);
|
292 |
|
293 | return this.each(function() {
|
294 | var $input = $(this);
|
295 | var data = options.data,
|
296 | count = 0,
|
297 | activeIndex = 0,
|
298 | oldVal,
|
299 | $inputDiv = $input.closest('.input-field');
|
300 |
|
301 |
|
302 | if (!$.isEmptyObject(data)) {
|
303 | var $autocomplete = $('<ul class="autocomplete-content dropdown-content"></ul>');
|
304 | var $oldAutocomplete;
|
305 |
|
306 |
|
307 |
|
308 | if ($inputDiv.length) {
|
309 | $oldAutocomplete = $inputDiv.children('.autocomplete-content.dropdown-content').first();
|
310 | if (!$oldAutocomplete.length) {
|
311 | $inputDiv.append($autocomplete);
|
312 | }
|
313 | } else {
|
314 | $oldAutocomplete = $input.next('.autocomplete-content.dropdown-content');
|
315 | if (!$oldAutocomplete.length) {
|
316 | $input.after($autocomplete);
|
317 | }
|
318 | }
|
319 | if ($oldAutocomplete.length) {
|
320 | $autocomplete = $oldAutocomplete;
|
321 | }
|
322 |
|
323 |
|
324 | var highlight = function(string, $el) {
|
325 | var img = $el.find('img');
|
326 | var matchStart = $el.text().toLowerCase().indexOf("" + string.toLowerCase() + ""),
|
327 | matchEnd = matchStart + string.length - 1,
|
328 | beforeMatch = $el.text().slice(0, matchStart),
|
329 | matchText = $el.text().slice(matchStart, matchEnd + 1),
|
330 | afterMatch = $el.text().slice(matchEnd + 1);
|
331 | $el.html("<span>" + beforeMatch + "<span class='highlight'>" + matchText + "</span>" + afterMatch + "</span>");
|
332 | if (img.length) {
|
333 | $el.prepend(img);
|
334 | }
|
335 | };
|
336 |
|
337 |
|
338 | var resetCurrentElement = function() {
|
339 | activeIndex = 0;
|
340 | $autocomplete.find('.active').removeClass('active');
|
341 | }
|
342 |
|
343 |
|
344 | $input.off('keyup.autocomplete').on('keyup.autocomplete', function (e) {
|
345 |
|
346 | count = 0;
|
347 |
|
348 |
|
349 | if (e.which === 13 ||
|
350 | e.which === 38 ||
|
351 | e.which === 40) {
|
352 | return;
|
353 | }
|
354 |
|
355 | var val = $input.val().toLowerCase();
|
356 |
|
357 |
|
358 | if (oldVal !== val) {
|
359 | $autocomplete.empty();
|
360 | resetCurrentElement();
|
361 |
|
362 | if (val !== '') {
|
363 | for(var key in data) {
|
364 | if (data.hasOwnProperty(key) &&
|
365 | key.toLowerCase().indexOf(val) !== -1 &&
|
366 | key.toLowerCase() !== val) {
|
367 |
|
368 | if (count >= options.limit) {
|
369 | break;
|
370 | }
|
371 |
|
372 | var autocompleteOption = $('<li></li>');
|
373 | if (!!data[key]) {
|
374 | autocompleteOption.append('<img src="'+ data[key] +'" class="right circle"><span>'+ key +'</span>');
|
375 | } else {
|
376 | autocompleteOption.append('<span>'+ key +'</span>');
|
377 | }
|
378 |
|
379 | $autocomplete.append(autocompleteOption);
|
380 | highlight(val, autocompleteOption);
|
381 | count++;
|
382 | }
|
383 | }
|
384 | }
|
385 | }
|
386 |
|
387 |
|
388 | oldVal = val;
|
389 | });
|
390 |
|
391 | $input.off('keydown.autocomplete').on('keydown.autocomplete', function (e) {
|
392 |
|
393 | var keyCode = e.which,
|
394 | liElement,
|
395 | numItems = $autocomplete.children('li').length,
|
396 | $active = $autocomplete.children('.active').first();
|
397 |
|
398 |
|
399 | if (keyCode === 13) {
|
400 | liElement = $autocomplete.children('li').eq(activeIndex);
|
401 | if (liElement.length) {
|
402 | liElement.click();
|
403 | e.preventDefault();
|
404 | }
|
405 | return;
|
406 | }
|
407 |
|
408 |
|
409 | if ( keyCode === 38 || keyCode === 40 ) {
|
410 | e.preventDefault();
|
411 |
|
412 | if (keyCode === 38 &&
|
413 | activeIndex > 0) {
|
414 | activeIndex--;
|
415 | }
|
416 |
|
417 | if (keyCode === 40 &&
|
418 | activeIndex < (numItems - 1) &&
|
419 | $active.length) {
|
420 | activeIndex++;
|
421 | }
|
422 |
|
423 | $active.removeClass('active');
|
424 | $autocomplete.children('li').eq(activeIndex).addClass('active');
|
425 | }
|
426 | });
|
427 |
|
428 |
|
429 | $autocomplete.on('click', 'li', function () {
|
430 | var text = $(this).text().trim();
|
431 | $input.val(text);
|
432 | $input.trigger('change');
|
433 | $autocomplete.empty();
|
434 | resetCurrentElement();
|
435 |
|
436 |
|
437 | if (typeof(options.onAutocomplete) === "function") {
|
438 | options.onAutocomplete.call(this, text);
|
439 | }
|
440 | });
|
441 | }
|
442 | });
|
443 | };
|
444 |
|
445 | });
|
446 |
|
447 | |
448 |
|
449 |
|
450 | $.fn.material_select = function (callback) {
|
451 | $(this).each(function(){
|
452 | var $select = $(this);
|
453 |
|
454 | if ($select.hasClass('browser-default')) {
|
455 | return;
|
456 | }
|
457 |
|
458 | var multiple = $select.attr('multiple') ? true : false,
|
459 | lastID = $select.data('select-id');
|
460 |
|
461 | if (lastID) {
|
462 | $select.parent().find('span.caret').remove();
|
463 | $select.parent().find('input').remove();
|
464 |
|
465 | $select.unwrap();
|
466 | $('ul#select-options-'+lastID).remove();
|
467 | }
|
468 |
|
469 |
|
470 | if(callback === 'destroy') {
|
471 | $select.data('select-id', null).removeClass('initialized');
|
472 | return;
|
473 | }
|
474 |
|
475 | var uniqueID = Materialize.guid();
|
476 | $select.data('select-id', uniqueID);
|
477 | var wrapper = $('<div class="select-wrapper"></div>');
|
478 | wrapper.addClass($select.attr('class'));
|
479 | var options = $('<ul id="select-options-' + uniqueID +'" class="dropdown-content select-dropdown ' + (multiple ? 'multiple-select-dropdown' : '') + '"></ul>'),
|
480 | selectChildren = $select.children('option, optgroup'),
|
481 | valuesSelected = [],
|
482 | optionsHover = false;
|
483 |
|
484 | var label = $select.find('option:selected').html() || $select.find('option:first').html() || "";
|
485 |
|
486 |
|
487 |
|
488 | var appendOptionWithIcon = function(select, option, type) {
|
489 |
|
490 | var disabledClass = (option.is(':disabled')) ? 'disabled ' : '';
|
491 | var optgroupClass = (type === 'optgroup-option') ? 'optgroup-option ' : '';
|
492 |
|
493 |
|
494 | var icon_url = option.data('icon');
|
495 | var classes = option.attr('class');
|
496 | if (!!icon_url) {
|
497 | var classString = '';
|
498 | if (!!classes) classString = ' class="' + classes + '"';
|
499 |
|
500 |
|
501 | if (type === 'multiple') {
|
502 | options.append($('<li class="' + disabledClass + '"><img alt="" src="' + icon_url + '"' + classString + '><span><input type="checkbox"' + disabledClass + '/><label></label>' + option.html() + '</span></li>'));
|
503 | } else {
|
504 | options.append($('<li class="' + disabledClass + optgroupClass + '"><img alt="" src="' + icon_url + '"' + classString + '><span>' + option.html() + '</span></li>'));
|
505 | }
|
506 | return true;
|
507 | }
|
508 |
|
509 |
|
510 | if (type === 'multiple') {
|
511 | options.append($('<li class="' + disabledClass + '"><span><input type="checkbox"' + disabledClass + '/><label></label>' + option.html() + '</span></li>'));
|
512 | } else {
|
513 | options.append($('<li class="' + disabledClass + optgroupClass + '"><span>' + option.html() + '</span></li>'));
|
514 | }
|
515 | };
|
516 |
|
517 |
|
518 | if (selectChildren.length) {
|
519 | selectChildren.each(function() {
|
520 | if ($(this).is('option')) {
|
521 |
|
522 | if (multiple) {
|
523 | appendOptionWithIcon($select, $(this), 'multiple');
|
524 |
|
525 | } else {
|
526 | appendOptionWithIcon($select, $(this));
|
527 | }
|
528 | } else if ($(this).is('optgroup')) {
|
529 |
|
530 | var selectOptions = $(this).children('option');
|
531 | options.append($('<li class="optgroup"><span>' + $(this).attr('label') + '</span></li>'));
|
532 |
|
533 | selectOptions.each(function() {
|
534 | appendOptionWithIcon($select, $(this), 'optgroup-option');
|
535 | });
|
536 | }
|
537 | });
|
538 | }
|
539 |
|
540 | options.find('li:not(.optgroup)').each(function (i) {
|
541 | $(this).click(function (e) {
|
542 |
|
543 | if (!$(this).hasClass('disabled') && !$(this).hasClass('optgroup')) {
|
544 | var selected = true;
|
545 |
|
546 | if (multiple) {
|
547 | $('input[type="checkbox"]', this).prop('checked', function(i, v) { return !v; });
|
548 | selected = toggleEntryFromArray(valuesSelected, $(this).index(), $select);
|
549 | $newSelect.trigger('focus');
|
550 | } else {
|
551 | options.find('li').removeClass('active');
|
552 | $(this).toggleClass('active');
|
553 | $newSelect.val($(this).text());
|
554 | }
|
555 |
|
556 | activateOption(options, $(this));
|
557 | $select.find('option').eq(i).prop('selected', selected);
|
558 |
|
559 | $select.trigger('change');
|
560 | if (typeof callback !== 'undefined') callback();
|
561 | }
|
562 |
|
563 | e.stopPropagation();
|
564 | });
|
565 | });
|
566 |
|
567 |
|
568 | $select.wrap(wrapper);
|
569 |
|
570 | var dropdownIcon = $('<span class="caret">▼</span>');
|
571 | if ($select.is(':disabled'))
|
572 | dropdownIcon.addClass('disabled');
|
573 |
|
574 |
|
575 | var sanitizedLabelHtml = label.replace(/"/g, '"');
|
576 |
|
577 | var $newSelect = $('<input type="text" class="select-dropdown" readonly="true" ' + (($select.is(':disabled')) ? 'disabled' : '') + ' data-activates="select-options-' + uniqueID +'" value="'+ sanitizedLabelHtml +'"/>');
|
578 | $select.before($newSelect);
|
579 | $newSelect.before(dropdownIcon);
|
580 |
|
581 | $newSelect.after(options);
|
582 |
|
583 | if (!$select.is(':disabled')) {
|
584 | $newSelect.dropdown({'hover': false, 'closeOnClick': false});
|
585 | }
|
586 |
|
587 |
|
588 | if ($select.attr('tabindex')) {
|
589 | $($newSelect[0]).attr('tabindex', $select.attr('tabindex'));
|
590 | }
|
591 |
|
592 | $select.addClass('initialized');
|
593 |
|
594 | $newSelect.on({
|
595 | 'focus': function (){
|
596 | if ($('ul.select-dropdown').not(options[0]).is(':visible')) {
|
597 | $('input.select-dropdown').trigger('close');
|
598 | }
|
599 | if (!options.is(':visible')) {
|
600 | $(this).trigger('open', ['focus']);
|
601 | var label = $(this).val();
|
602 | if (multiple && label.indexOf(',') >= 0) {
|
603 | label = label.split(',')[0];
|
604 | }
|
605 |
|
606 | var selectedOption = options.find('li').filter(function() {
|
607 | return $(this).text().toLowerCase() === label.toLowerCase();
|
608 | })[0];
|
609 | activateOption(options, selectedOption, true);
|
610 | }
|
611 | },
|
612 | 'click': function (e){
|
613 | e.stopPropagation();
|
614 | }
|
615 | });
|
616 |
|
617 | $newSelect.on('blur', function() {
|
618 | if (!multiple) {
|
619 | $(this).trigger('close');
|
620 | }
|
621 | options.find('li.selected').removeClass('selected');
|
622 | });
|
623 |
|
624 | options.hover(function() {
|
625 | optionsHover = true;
|
626 | }, function () {
|
627 | optionsHover = false;
|
628 | });
|
629 |
|
630 | $(window).on({
|
631 | 'click': function () {
|
632 | multiple && (optionsHover || $newSelect.trigger('close'));
|
633 | }
|
634 | });
|
635 |
|
636 |
|
637 | if (multiple) {
|
638 | $select.find("option:selected:not(:disabled)").each(function () {
|
639 | var index = $(this).index();
|
640 |
|
641 | toggleEntryFromArray(valuesSelected, index, $select);
|
642 | options.find("li").eq(index).find(":checkbox").prop("checked", true);
|
643 | });
|
644 | }
|
645 |
|
646 | |
647 |
|
648 |
|
649 |
|
650 |
|
651 |
|
652 | var activateOption = function(collection, newOption, firstActivation) {
|
653 | if (newOption) {
|
654 | collection.find('li.selected').removeClass('selected');
|
655 | var option = $(newOption);
|
656 | option.addClass('selected');
|
657 | if (!multiple || !!firstActivation) {
|
658 | options.scrollTo(option);
|
659 | }
|
660 | }
|
661 | };
|
662 |
|
663 |
|
664 |
|
665 | var filterQuery = [],
|
666 | onKeyDown = function(e){
|
667 |
|
668 | if(e.which == 9){
|
669 | $newSelect.trigger('close');
|
670 | return;
|
671 | }
|
672 |
|
673 |
|
674 | if(e.which == 40 && !options.is(':visible')){
|
675 | $newSelect.trigger('open');
|
676 | return;
|
677 | }
|
678 |
|
679 |
|
680 | if(e.which == 13 && !options.is(':visible')){
|
681 | return;
|
682 | }
|
683 |
|
684 | e.preventDefault();
|
685 |
|
686 |
|
687 | var letter = String.fromCharCode(e.which).toLowerCase(),
|
688 | nonLetters = [9,13,27,38,40];
|
689 | if (letter && (nonLetters.indexOf(e.which) === -1)) {
|
690 | filterQuery.push(letter);
|
691 |
|
692 | var string = filterQuery.join(''),
|
693 | newOption = options.find('li').filter(function() {
|
694 | return $(this).text().toLowerCase().indexOf(string) === 0;
|
695 | })[0];
|
696 |
|
697 | if (newOption) {
|
698 | activateOption(options, newOption);
|
699 | }
|
700 | }
|
701 |
|
702 |
|
703 | if (e.which == 13) {
|
704 | var activeOption = options.find('li.selected:not(.disabled)')[0];
|
705 | if(activeOption){
|
706 | $(activeOption).trigger('click');
|
707 | if (!multiple) {
|
708 | $newSelect.trigger('close');
|
709 | }
|
710 | }
|
711 | }
|
712 |
|
713 |
|
714 | if (e.which == 40) {
|
715 | if (options.find('li.selected').length) {
|
716 | newOption = options.find('li.selected').next('li:not(.disabled)')[0];
|
717 | } else {
|
718 | newOption = options.find('li:not(.disabled)')[0];
|
719 | }
|
720 | activateOption(options, newOption);
|
721 | }
|
722 |
|
723 |
|
724 | if (e.which == 27) {
|
725 | $newSelect.trigger('close');
|
726 | }
|
727 |
|
728 |
|
729 | if (e.which == 38) {
|
730 | newOption = options.find('li.selected').prev('li:not(.disabled)')[0];
|
731 | if(newOption)
|
732 | activateOption(options, newOption);
|
733 | }
|
734 |
|
735 |
|
736 | setTimeout(function(){ filterQuery = []; }, 1000);
|
737 | };
|
738 |
|
739 | $newSelect.on('keydown', onKeyDown);
|
740 | });
|
741 |
|
742 | function toggleEntryFromArray(entriesArray, entryIndex, select) {
|
743 | var index = entriesArray.indexOf(entryIndex),
|
744 | notAdded = index === -1;
|
745 |
|
746 | if (notAdded) {
|
747 | entriesArray.push(entryIndex);
|
748 | } else {
|
749 | entriesArray.splice(index, 1);
|
750 | }
|
751 |
|
752 | select.siblings('ul.dropdown-content').find('li').eq(entryIndex).toggleClass('active');
|
753 |
|
754 |
|
755 | select.find('option').eq(entryIndex).prop('selected', notAdded);
|
756 | setValueToInput(entriesArray, select);
|
757 |
|
758 | return notAdded;
|
759 | }
|
760 |
|
761 | function setValueToInput(entriesArray, select) {
|
762 | var value = '';
|
763 |
|
764 | for (var i = 0, count = entriesArray.length; i < count; i++) {
|
765 | var text = select.find('option').eq(entriesArray[i]).text();
|
766 |
|
767 | i === 0 ? value += text : value += ', ' + text;
|
768 | }
|
769 |
|
770 | if (value === '') {
|
771 | value = select.find('option:disabled').eq(0).text();
|
772 | }
|
773 |
|
774 | select.siblings('input.select-dropdown').val(value);
|
775 | }
|
776 | };
|
777 |
|
778 | }( jQuery ));
|