|
9 | 9 | * /,'
|
10 | 10 | * /'
|
11 | 11 | *
|
12 |
| - * Selectric ϟ v1.10.1 (Sep 22 2016) - http://lcdsantos.github.io/jQuery-Selectric/ |
| 12 | + * Selectric ϟ v1.10.1 (Sep 23 2016) - http://lcdsantos.github.io/jQuery-Selectric/ |
13 | 13 | *
|
14 | 14 | * Copyright (c) 2016 Leonardo Santos; MIT License
|
15 | 15 | *
|
|
45 | 45 |
|
46 | 46 | var pluginName = 'selectric';
|
47 | 47 | var classList = 'Input Items Open Disabled TempShow HideSelect Wrapper Focus Hover Responsive Above Scroll Group GroupLabel';
|
48 |
| - var bindSufix = '.sl'; |
| 48 | + var eventNamespaceSuffix = '.sl'; |
49 | 49 |
|
50 | 50 | var chars = ['a', 'e', 'i', 'o', 'u', 'n', 'c', 'y'];
|
51 | 51 | var diacritics = [
|
|
249 | 249 | _this.classes = _this.getClassNames();
|
250 | 250 |
|
251 | 251 | // Create elements
|
252 |
| - var input = $('<input/>', { 'class': _this.classes.input, 'readonly': _this.utils.isMobile() }); |
253 |
| - var items = $('<div/>', { 'class': _this.classes.items, 'tabindex': -1 }); |
254 |
| - var itemsScroll = $('<div/>', { 'class': _this.classes.scroll }); |
255 |
| - var wrapper = $('<div/>', { 'class': _this.classes.prefix, 'html': _this.options.arrowButtonMarkup }); |
256 |
| - var label = $('<span/>', { 'class': 'label' }); |
257 |
| - var outerWrapper = _this.$element.wrap('<div/>').parent().append(wrapper.prepend(label), items, input); |
| 252 | + var input = $('<input/>', { 'class': _this.classes.input, 'readonly': _this.utils.isMobile() }); |
| 253 | + var items = $('<div/>', { 'class': _this.classes.items, 'tabindex': -1 }); |
| 254 | + var itemsScroll = $('<div/>', { 'class': _this.classes.scroll }); |
| 255 | + var wrapper = $('<div/>', { 'class': _this.classes.prefix, 'html': _this.options.arrowButtonMarkup }); |
| 256 | + var label = $('<span/>', { 'class': 'label' }); |
| 257 | + var outerWrapper = _this.$element.wrap('<div/>').parent().append(wrapper.prepend(label), items, input); |
| 258 | + var hideSelectWrapper = $('<div/>', { 'class': _this.classes.hideselect }); |
258 | 259 |
|
259 | 260 | _this.elements = {
|
260 | 261 | input : input,
|
|
265 | 266 | outerWrapper : outerWrapper
|
266 | 267 | };
|
267 | 268 |
|
| 269 | + if (_this.options.nativeOnMobile && _this.utils.isMobile()) { |
| 270 | + _this.elements.input = undefined; |
| 271 | + hideSelectWrapper.addClass(_this.classes.prefix + '-is-native'); |
| 272 | + |
| 273 | + _this.$element.on('change', function () { |
| 274 | + _this.refresh(); |
| 275 | + }); |
| 276 | + } |
| 277 | + |
268 | 278 | _this.$element
|
269 | 279 | .on(_this.eventTriggers)
|
270 |
| - .wrap('<div class="' + _this.classes.hideselect + '"/>'); |
| 280 | + .wrap(hideSelectWrapper); |
271 | 281 |
|
272 | 282 | _this.originalTabindex = _this.$element.prop('tabindex');
|
273 | 283 | _this.$element.prop('tabindex', false);
|
|
554 | 564 | .add(_this.$element)
|
555 | 565 | .add(_this.elements.outerWrapper)
|
556 | 566 | .add(_this.elements.input)
|
557 |
| - .off(bindSufix); |
| 567 | + .off(eventNamespaceSuffix); |
558 | 568 |
|
559 |
| - _this.elements.outerWrapper.on('mouseenter' + bindSufix + ' mouseleave' + bindSufix, function(e) { |
| 569 | + _this.elements.outerWrapper.on('mouseenter' + eventNamespaceSuffix + ' mouseleave' + eventNamespaceSuffix, function(e) { |
560 | 570 | $(this).toggleClass(_this.classes.hover, e.type === 'mouseenter');
|
561 | 571 |
|
562 | 572 | // Delay close effect when openOnHover is true
|
|
572 | 582 | });
|
573 | 583 |
|
574 | 584 | // Toggle open/close
|
575 |
| - _this.elements.wrapper.on('click' + bindSufix, function(e) { |
| 585 | + _this.elements.wrapper.on('click' + eventNamespaceSuffix, function(e) { |
576 | 586 | _this.state.opened ? _this.close() : _this.open(e);
|
577 | 587 | });
|
578 | 588 |
|
579 |
| - // Translate original element focus event to dummy input |
580 |
| - _this.$element.on('focus' + bindSufix, function() { |
581 |
| - _this.elements.input.focus(); |
582 |
| - }); |
583 |
| - |
584 |
| - _this.elements.input |
585 |
| - .prop({ tabindex: _this.originalTabindex, disabled: false }) |
586 |
| - .on('keydown' + bindSufix, $.proxy(_this.handleKeys, _this)) |
587 |
| - .on('focusin' + bindSufix, function(e) { |
588 |
| - _this.elements.outerWrapper.addClass(_this.classes.focus); |
| 589 | + // Translate original element focus event to dummy input. |
| 590 | + // Disabled on mobile devices because the default option list isn't |
| 591 | + // shown due the fact that hidden input gets focused |
| 592 | + if ( !(_this.options.nativeOnMobile && _this.utils.isMobile()) ) { |
| 593 | + _this.$element.on('focus' + eventNamespaceSuffix, function() { |
| 594 | + _this.elements.input.focus(); |
| 595 | + }); |
589 | 596 |
|
590 |
| - // Prevent the flicker when focusing out and back again in the browser window |
591 |
| - _this.elements.input.one('blur', function() { |
592 |
| - _this.elements.input.blur(); |
593 |
| - }); |
| 597 | + _this.elements.input |
| 598 | + .prop({ tabindex: _this.originalTabindex, disabled: false }) |
| 599 | + .on('keydown' + eventNamespaceSuffix, $.proxy(_this.handleKeys, _this)) |
| 600 | + .on('focusin' + eventNamespaceSuffix, function(e) { |
| 601 | + _this.elements.outerWrapper.addClass(_this.classes.focus); |
594 | 602 |
|
595 |
| - if ( _this.options.openOnFocus && !_this.state.opened ) { |
596 |
| - _this.open(e); |
597 |
| - } |
598 |
| - }) |
599 |
| - .on('focusout' + bindSufix, function() { |
600 |
| - _this.elements.outerWrapper.removeClass(_this.classes.focus); |
601 |
| - }) |
602 |
| - .on('input propertychange', function() { |
603 |
| - var val = _this.elements.input.val(); |
604 |
| - |
605 |
| - // Clear search |
606 |
| - clearTimeout(_this.resetStr); |
607 |
| - _this.resetStr = setTimeout(function() { |
608 |
| - _this.elements.input.val(''); |
609 |
| - }, _this.options.keySearchTimeout); |
610 |
| - |
611 |
| - if ( val.length ) { |
612 |
| - // Search in select options |
613 |
| - $.each(_this.items, function(i, elm) { |
614 |
| - if ( new RegExp('^' + _this.utils.escapeRegExp(val), 'i').test(elm.slug) && !elm.disabled ) { |
615 |
| - _this.highlight(i); |
616 |
| - return false; |
617 |
| - } |
| 603 | + // Prevent the flicker when focusing out and back again in the browser window |
| 604 | + _this.elements.input.one('blur', function() { |
| 605 | + _this.elements.input.blur(); |
618 | 606 | });
|
619 |
| - } |
620 |
| - }); |
| 607 | + |
| 608 | + if ( _this.options.openOnFocus && !_this.state.opened ) { |
| 609 | + _this.open(e); |
| 610 | + } |
| 611 | + }) |
| 612 | + .on('focusout' + eventNamespaceSuffix, function() { |
| 613 | + _this.elements.outerWrapper.removeClass(_this.classes.focus); |
| 614 | + }) |
| 615 | + .on('input propertychange', function() { |
| 616 | + var val = _this.elements.input.val(); |
| 617 | + |
| 618 | + // Clear search |
| 619 | + clearTimeout(_this.resetStr); |
| 620 | + _this.resetStr = setTimeout(function() { |
| 621 | + _this.elements.input.val(''); |
| 622 | + }, _this.options.keySearchTimeout); |
| 623 | + |
| 624 | + if ( val.length ) { |
| 625 | + // Search in select options |
| 626 | + $.each(_this.items, function(i, elm) { |
| 627 | + if ( new RegExp('^' + _this.utils.escapeRegExp(val), 'i').test(elm.slug) && !elm.disabled ) { |
| 628 | + _this.highlight(i); |
| 629 | + return false; |
| 630 | + } |
| 631 | + }); |
| 632 | + } |
| 633 | + }); |
| 634 | + } |
621 | 635 |
|
622 | 636 | _this.$li.on({
|
623 | 637 | // Prevent <input> blur on Chrome
|
|
793 | 807 | open: function(e) {
|
794 | 808 | var _this = this;
|
795 | 809 |
|
| 810 | + if ( _this.options.nativeOnMobile && _this.utils.isMobile()) { |
| 811 | + return false; |
| 812 | + } |
| 813 | + |
796 | 814 | _this.utils.triggerCallback('BeforeOpen', _this);
|
797 | 815 |
|
798 | 816 | if ( e ) {
|
|
822 | 840 | // Delayed binds events on Document to make label clicks work
|
823 | 841 | setTimeout(function() {
|
824 | 842 | $doc
|
825 |
| - .on('click' + bindSufix, $.proxy(_this.close, _this)) |
826 |
| - .on('scroll' + bindSufix, $.proxy(_this.isInViewport, _this)); |
| 843 | + .on('click' + eventNamespaceSuffix, $.proxy(_this.close, _this)) |
| 844 | + .on('scroll' + eventNamespaceSuffix, $.proxy(_this.isInViewport, _this)); |
827 | 845 | }, 1);
|
828 | 846 |
|
829 | 847 | _this.isInViewport();
|
830 | 848 |
|
831 | 849 | // Prevent window scroll when using mouse wheel inside items box
|
832 | 850 | if ( _this.options.preventWindowScroll ) {
|
833 | 851 | /* istanbul ignore next */
|
834 |
| - $doc.on('mousewheel' + bindSufix + ' DOMMouseScroll' + bindSufix, '.' + _this.classes.scroll, function(e) { |
| 852 | + $doc.on('mousewheel' + eventNamespaceSuffix + ' DOMMouseScroll' + eventNamespaceSuffix, '.' + _this.classes.scroll, function(e) { |
835 | 853 | var orgEvent = e.originalEvent;
|
836 | 854 | var scrollTop = $(this).scrollTop();
|
837 | 855 | var deltaY = 0;
|
|
862 | 880 | _this.utils.triggerCallback('BeforeClose', _this);
|
863 | 881 |
|
864 | 882 | // Remove custom events on document
|
865 |
| - $doc.off(bindSufix); |
| 883 | + $doc.off(eventNamespaceSuffix); |
866 | 884 |
|
867 | 885 | // Remove visible class to hide options box
|
868 | 886 | _this.elements.outerWrapper.removeClass(_this.classes.open);
|
|
986 | 1004 | _this.$element.removeData(pluginName).removeData('value');
|
987 | 1005 | }
|
988 | 1006 |
|
989 |
| - _this.$element.prop('tabindex', _this.originalTabindex).off(bindSufix).off(_this.eventTriggers).unwrap().unwrap(); |
| 1007 | + _this.$element.prop('tabindex', _this.originalTabindex).off(eventNamespaceSuffix).off(_this.eventTriggers).unwrap().unwrap(); |
990 | 1008 |
|
991 | 1009 | _this.state.enabled = false;
|
992 | 1010 | }
|
|
1050 | 1068 | keySearchTimeout : 500,
|
1051 | 1069 | arrowButtonMarkup : '<b class="button">▾</b>',
|
1052 | 1070 | disableOnMobile : true,
|
| 1071 | + nativeOnMobile : true, |
1053 | 1072 | openOnFocus : true,
|
1054 | 1073 | openOnHover : false,
|
1055 | 1074 | hoverIntentTimeout : 500,
|
|
0 commit comments