|
| 1 | +/* |
| 2 | + Original Plugin by Osvaldas Valutis, www.osvaldas.info |
| 3 | + http://osvaldas.info/drop-down-navigation-responsive-and-touch-friendly |
| 4 | + Available for use under the MIT License |
| 5 | + */ |
| 6 | +/** |
| 7 | + * jquery-doubleTapToGo plugin |
| 8 | + * Copyright 2017 DACHCOM.DIGITAL AG |
| 9 | + * @author Marco Rieser |
| 10 | + * @author Volker Andres |
| 11 | + * @author Stefan Hagspiel |
| 12 | + * @version 3.0.0 |
| 13 | + * @see https://github.com/dachcom-digital/jquery-doubleTapToGo |
| 14 | + */ |
| 15 | +(function ($, window, document, undefined) { |
| 16 | + 'use strict'; |
| 17 | + var pluginName = 'doubleTapToGo', |
| 18 | + defaults = { |
| 19 | + automatic: true, |
| 20 | + selectorClass: 'doubletap', |
| 21 | + selectorChain: 'li:has(ul)' |
| 22 | + }; |
| 23 | + |
| 24 | + function DoubleTapToGo (element, options) { |
| 25 | + this.element = element; |
| 26 | + this.settings = $.extend({}, defaults, options); |
| 27 | + this._defaults = defaults; |
| 28 | + this._name = pluginName; |
| 29 | + this.init(); |
| 30 | + } |
| 31 | + |
| 32 | + $.extend(DoubleTapToGo.prototype, { |
| 33 | + preventClick: false, |
| 34 | + currentTap: $(), |
| 35 | + init: function () { |
| 36 | + var self = this; |
| 37 | + |
| 38 | + if (window.ontouchstart === undefined && !window.navigator.msMaxTouchPoints && !window.navigator.userAgent.toLowerCase().match(/windows phone os 7/i)) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + if (window.navigator.msPointerEnabled) { |
| 43 | + $(this.element).get(0).addEventListener('MSPointerDown', self._tap.bind(self), false); |
| 44 | + } else if (window.navigator.pointerEnabled) { |
| 45 | + $(this.element).get(0).addEventListener('pointerdown', self._tap.bind(self), false); |
| 46 | + } |
| 47 | + |
| 48 | + $(this.element) |
| 49 | + .on('click', '.' + this.settings.selectorClass, this._click.bind(this)) |
| 50 | + .on('touchstart', '.' + this.settings.selectorClass, this._tap.bind(this)) |
| 51 | + .on('remove', this._destroy.bind(this)); |
| 52 | + |
| 53 | + this._addSelectors(); |
| 54 | + }, |
| 55 | + |
| 56 | + _addSelectors: function () { |
| 57 | + if (this.settings.automatic !== true) { |
| 58 | + return; |
| 59 | + } |
| 60 | + $(this.element) |
| 61 | + .find(this.settings.selectorChain) |
| 62 | + .addClass(this.settings.selectorClass); |
| 63 | + }, |
| 64 | + |
| 65 | + _click: function (event) { |
| 66 | + if (this.preventClick) { |
| 67 | + event.preventDefault(); |
| 68 | + } else { |
| 69 | + this.currentTap = $(); |
| 70 | + } |
| 71 | + }, |
| 72 | + |
| 73 | + _tap: function (event) { |
| 74 | + var $target = $(event.target).closest('li'); |
| 75 | + if (!$target.hasClass(this.settings.selectorClass)) { |
| 76 | + this.preventClick = false; |
| 77 | + return; |
| 78 | + } |
| 79 | + if ($target.get(0) === this.currentTap.get(0)) { |
| 80 | + this.preventClick = false; |
| 81 | + return; |
| 82 | + } |
| 83 | + this.preventClick = true; |
| 84 | + this.currentTap = $target; |
| 85 | + event.stopPropagation(); |
| 86 | + }, |
| 87 | + |
| 88 | + _destroy: function () { |
| 89 | + $(this.element).off(); |
| 90 | + if (window.navigator.msPointerEnabled) { |
| 91 | + $(this.element).get(0).removeEventListener('MSPointerDown', this._tap); |
| 92 | + } |
| 93 | + if (window.navigator.pointerEnabled) { |
| 94 | + $(this.element).get(0).removeEventListener('pointerdown', this._tap); |
| 95 | + } |
| 96 | + }, |
| 97 | + |
| 98 | + reset: function () { |
| 99 | + this.currentTap = $(); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + $.fn[pluginName] = function (options) { |
| 104 | + var args = arguments, |
| 105 | + returns; |
| 106 | + if (options === undefined || typeof options === 'object') { |
| 107 | + return this.each(function () { |
| 108 | + if (!$.data(this, pluginName)) { |
| 109 | + $.data(this, pluginName, new DoubleTapToGo(this, options)); |
| 110 | + } |
| 111 | + }); |
| 112 | + } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') { |
| 113 | + this.each(function () { |
| 114 | + var instance = $.data(this, pluginName), |
| 115 | + methodName = (options === 'destroy' ? '_destroy' : options); |
| 116 | + if (instance instanceof DoubleTapToGo && typeof instance[methodName] === 'function') { |
| 117 | + returns = instance[methodName].apply(instance, Array.prototype.slice.call(args, 1)); |
| 118 | + } |
| 119 | + if (options === 'destroy') { |
| 120 | + $.data(this, pluginName, null); |
| 121 | + } |
| 122 | + }); |
| 123 | + return returns !== undefined ? returns : this; |
| 124 | + } |
| 125 | + }; |
| 126 | +})(jQuery, window, document); |
0 commit comments