diff options
Diffstat (limited to 'js/dist/dropdown.js')
-rw-r--r-- | js/dist/dropdown.js | 268 |
1 files changed, 172 insertions, 96 deletions
diff --git a/js/dist/dropdown.js b/js/dist/dropdown.js index 32aff75c1b..fb620d385b 100644 --- a/js/dist/dropdown.js +++ b/js/dist/dropdown.js @@ -4,14 +4,16 @@ * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) */ (function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery'), require('popper.js'), require('./util.js')) : - typeof define === 'function' && define.amd ? define(['jquery', 'popper.js', './util.js'], factory) : - (global = global || self, global.Dropdown = factory(global.jQuery, global.Popper, global.Util)); -}(this, function ($, Popper, Util) { 'use strict'; - - $ = $ && $.hasOwnProperty('default') ? $['default'] : $; + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/data.js'), require('./dom/eventHandler.js'), require('./dom/manipulator.js'), require('popper.js'), require('./dom/selectorEngine.js')) : + typeof define === 'function' && define.amd ? define(['./dom/data.js', './dom/eventHandler.js', './dom/manipulator.js', 'popper.js', './dom/selectorEngine.js'], factory) : + (global = global || self, global.Dropdown = factory(global.Data, global.EventHandler, global.Manipulator, global.Popper, global.SelectorEngine)); +}(this, function (Data, EventHandler, Manipulator, Popper, SelectorEngine) { 'use strict'; + + Data = Data && Data.hasOwnProperty('default') ? Data['default'] : Data; + EventHandler = EventHandler && EventHandler.hasOwnProperty('default') ? EventHandler['default'] : EventHandler; + Manipulator = Manipulator && Manipulator.hasOwnProperty('default') ? Manipulator['default'] : Manipulator; Popper = Popper && Popper.hasOwnProperty('default') ? Popper['default'] : Popper; - Util = Util && Util.hasOwnProperty('default') ? Util['default'] : Util; + SelectorEngine = SelectorEngine && SelectorEngine.hasOwnProperty('default') ? SelectorEngine['default'] : SelectorEngine; function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { @@ -64,6 +66,62 @@ } /** + * -------------------------------------------------------------------------- + * Bootstrap (v4.3.1): util/index.js + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) + * -------------------------------------------------------------------------- + */ + var jQuery = window.jQuery; // Shoutout AngusCroll (https://goo.gl/pxwQGp) + + var toType = function toType(obj) { + return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase(); + }; + + var getSelectorFromElement = function getSelectorFromElement(element) { + var selector = element.getAttribute('data-target'); + + if (!selector || selector === '#') { + var hrefAttr = element.getAttribute('href'); + selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : ''; + } + + try { + return document.querySelector(selector) ? selector : null; + } catch (err) { + return null; + } + }; + + var isElement = function isElement(obj) { + return (obj[0] || obj).nodeType; + }; + + var typeCheckConfig = function typeCheckConfig(componentName, config, configTypes) { + Object.keys(configTypes).forEach(function (property) { + var expectedTypes = configTypes[property]; + var value = config[property]; + var valueType = value && isElement(value) ? 'element' : toType(value); + + if (!new RegExp(expectedTypes).test(valueType)) { + throw new Error(componentName.toUpperCase() + ": " + ("Option \"" + property + "\" provided type \"" + valueType + "\" ") + ("but expected type \"" + expectedTypes + "\".")); + } + }); + }; + + var makeArray = function makeArray(nodeList) { + if (!nodeList) { + return []; + } + + return [].slice.call(nodeList); + }; + + + var noop = function noop() { + return function () {}; + }; + + /** * ------------------------------------------------------------------------ * Constants * ------------------------------------------------------------------------ @@ -74,7 +132,6 @@ var DATA_KEY = 'bs.dropdown'; var EVENT_KEY = "." + DATA_KEY; var DATA_API_KEY = '.data-api'; - var JQUERY_NO_CONFLICT = $.fn[NAME]; var ESCAPE_KEYCODE = 27; // KeyboardEvent.which value for Escape (Esc) key var SPACE_KEYCODE = 32; // KeyboardEvent.which value for space key @@ -157,6 +214,8 @@ this._inNavbar = this._detectNavbar(); this._addEventListeners(); + + Data.setData(element, DATA_KEY, this); } // Getters @@ -164,13 +223,13 @@ // Public _proto.toggle = function toggle() { - if (this._element.disabled || $(this._element).hasClass(ClassName.DISABLED)) { + if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED)) { return; } var parent = Dropdown._getParentFromElement(this._element); - var isActive = $(this._menu).hasClass(ClassName.SHOW); + var isActive = this._menu.classList.contains(ClassName.SHOW); Dropdown._clearMenus(); @@ -181,10 +240,9 @@ var relatedTarget = { relatedTarget: this._element }; - var showEvent = $.Event(Event.SHOW, relatedTarget); - $(parent).trigger(showEvent); + var showEvent = EventHandler.trigger(parent, Event.SHOW, relatedTarget); - if (showEvent.isDefaultPrevented()) { + if (showEvent.defaultPrevented) { return; } // Disable totally Popper.js for Dropdown in Navbar @@ -195,14 +253,14 @@ * Popper - https://popper.js.org */ if (typeof Popper === 'undefined') { - throw new TypeError('Bootstrap\'s dropdowns require Popper.js (https://popper.js.org/)'); + throw new TypeError('Bootstrap\'s dropdowns require Popper.js (https://popper.js.org)'); } var referenceElement = this._element; if (this._config.reference === 'parent') { referenceElement = parent; - } else if (Util.isElement(this._config.reference)) { + } else if (isElement(this._config.reference)) { referenceElement = this._config.reference; // Check if it's jQuery element if (typeof this._config.reference.jquery !== 'undefined') { @@ -214,7 +272,7 @@ if (this._config.boundary !== 'scrollParent') { - $(parent).addClass(ClassName.POSITION_STATIC); + parent.classList.add(ClassName.POSITION_STATIC); } this._popper = new Popper(referenceElement, this._menu, this._getPopperConfig()); @@ -224,65 +282,66 @@ // https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html - if ('ontouchstart' in document.documentElement && $(parent).closest(Selector.NAVBAR_NAV).length === 0) { - $(document.body).children().on('mouseover', null, $.noop); + if ('ontouchstart' in document.documentElement && !makeArray(SelectorEngine.closest(parent, Selector.NAVBAR_NAV)).length) { + makeArray(document.body.children).forEach(function (elem) { + return EventHandler.on(elem, 'mouseover', null, noop()); + }); } this._element.focus(); this._element.setAttribute('aria-expanded', true); - $(this._menu).toggleClass(ClassName.SHOW); - $(parent).toggleClass(ClassName.SHOW).trigger($.Event(Event.SHOWN, relatedTarget)); + Manipulator.toggleClass(this._menu, ClassName.SHOW); + Manipulator.toggleClass(parent, ClassName.SHOW); + EventHandler.trigger(parent, Event.SHOWN, relatedTarget); }; _proto.show = function show() { - if (this._element.disabled || $(this._element).hasClass(ClassName.DISABLED) || $(this._menu).hasClass(ClassName.SHOW)) { + if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED) || this._menu.classList.contains(ClassName.SHOW)) { return; } + var parent = Dropdown._getParentFromElement(this._element); + var relatedTarget = { relatedTarget: this._element }; - var showEvent = $.Event(Event.SHOW, relatedTarget); + var showEvent = EventHandler.trigger(parent, Event.SHOW, relatedTarget); - var parent = Dropdown._getParentFromElement(this._element); - - $(parent).trigger(showEvent); - - if (showEvent.isDefaultPrevented()) { + if (showEvent.defaultPrevented) { return; } - $(this._menu).toggleClass(ClassName.SHOW); - $(parent).toggleClass(ClassName.SHOW).trigger($.Event(Event.SHOWN, relatedTarget)); + Manipulator.toggleClass(this._menu, ClassName.SHOW); + Manipulator.toggleClass(parent, ClassName.SHOW); + EventHandler.trigger(parent, Event.SHOWN, relatedTarget); }; _proto.hide = function hide() { - if (this._element.disabled || $(this._element).hasClass(ClassName.DISABLED) || !$(this._menu).hasClass(ClassName.SHOW)) { + if (this._element.disabled || this._element.classList.contains(ClassName.DISABLED) || !this._menu.classList.contains(ClassName.SHOW)) { return; } + var parent = Dropdown._getParentFromElement(this._element); + var relatedTarget = { relatedTarget: this._element }; - var hideEvent = $.Event(Event.HIDE, relatedTarget); - - var parent = Dropdown._getParentFromElement(this._element); + var hideEvent = EventHandler.trigger(parent, Event.HIDE, relatedTarget); - $(parent).trigger(hideEvent); - - if (hideEvent.isDefaultPrevented()) { + if (hideEvent.defaultPrevented) { return; } - $(this._menu).toggleClass(ClassName.SHOW); - $(parent).toggleClass(ClassName.SHOW).trigger($.Event(Event.HIDDEN, relatedTarget)); + Manipulator.toggleClass(this._menu, ClassName.SHOW); + Manipulator.toggleClass(parent, ClassName.SHOW); + EventHandler.trigger(parent, Event.HIDDEN, relatedTarget); }; _proto.dispose = function dispose() { - $.removeData(this._element, DATA_KEY); - $(this._element).off(EVENT_KEY); + Data.removeData(this._element, DATA_KEY); + EventHandler.off(this._element, EVENT_KEY); this._element = null; this._menu = null; @@ -305,7 +364,7 @@ _proto._addEventListeners = function _addEventListeners() { var _this = this; - $(this._element).on(Event.CLICK, function (event) { + EventHandler.on(this._element, Event.CLICK, function (event) { event.preventDefault(); event.stopPropagation(); @@ -314,8 +373,8 @@ }; _proto._getConfig = function _getConfig(config) { - config = _objectSpread({}, this.constructor.Default, $(this._element).data(), config); - Util.typeCheckConfig(NAME, config, this.constructor.DefaultType); + config = _objectSpread({}, this.constructor.Default, Manipulator.getDataAttributes(this._element), config); + typeCheckConfig(NAME, config, this.constructor.DefaultType); return config; }; @@ -324,7 +383,7 @@ var parent = Dropdown._getParentFromElement(this._element); if (parent) { - this._menu = parent.querySelector(Selector.MENU); + this._menu = SelectorEngine.findOne(Selector.MENU, parent); } } @@ -332,20 +391,20 @@ }; _proto._getPlacement = function _getPlacement() { - var $parentDropdown = $(this._element.parentNode); + var parentDropdown = this._element.parentNode; var placement = AttachmentMap.BOTTOM; // Handle dropup - if ($parentDropdown.hasClass(ClassName.DROPUP)) { + if (parentDropdown.classList.contains(ClassName.DROPUP)) { placement = AttachmentMap.TOP; - if ($(this._menu).hasClass(ClassName.MENURIGHT)) { + if (this._menu.classList.contains(ClassName.MENURIGHT)) { placement = AttachmentMap.TOPEND; } - } else if ($parentDropdown.hasClass(ClassName.DROPRIGHT)) { + } else if (parentDropdown.classList.contains(ClassName.DROPRIGHT)) { placement = AttachmentMap.RIGHT; - } else if ($parentDropdown.hasClass(ClassName.DROPLEFT)) { + } else if (parentDropdown.classList.contains(ClassName.DROPLEFT)) { placement = AttachmentMap.LEFT; - } else if ($(this._menu).hasClass(ClassName.MENURIGHT)) { + } else if (this._menu.classList.contains(ClassName.MENURIGHT)) { placement = AttachmentMap.BOTTOMEND; } @@ -353,7 +412,7 @@ }; _proto._detectNavbar = function _detectNavbar() { - return $(this._element).closest('.navbar').length > 0; + return Boolean(SelectorEngine.closest(this._element, '.navbar')); }; _proto._getOffset = function _getOffset() { @@ -398,24 +457,27 @@ } // Static ; - Dropdown._jQueryInterface = function _jQueryInterface(config) { - return this.each(function () { - var data = $(this).data(DATA_KEY); + Dropdown._dropdownInterface = function _dropdownInterface(element, config) { + var data = Data.getData(element, DATA_KEY); - var _config = typeof config === 'object' ? config : null; + var _config = typeof config === 'object' ? config : null; - if (!data) { - data = new Dropdown(this, _config); - $(this).data(DATA_KEY, data); + if (!data) { + data = new Dropdown(element, _config); + } + + if (typeof config === 'string') { + if (typeof data[config] === 'undefined') { + throw new Error("No method named \"" + config + "\""); } - if (typeof config === 'string') { - if (typeof data[config] === 'undefined') { - throw new TypeError("No method named \"" + config + "\""); - } + data[config](); + } + }; - data[config](); - } + Dropdown._jQueryInterface = function _jQueryInterface(config) { + return this.each(function () { + Dropdown._dropdownInterface(this, config); }); }; @@ -424,12 +486,12 @@ return; } - var toggles = [].slice.call(document.querySelectorAll(Selector.DATA_TOGGLE)); + var toggles = makeArray(SelectorEngine.find(Selector.DATA_TOGGLE)); for (var i = 0, len = toggles.length; i < len; i++) { var parent = Dropdown._getParentFromElement(toggles[i]); - var context = $(toggles[i]).data(DATA_KEY); + var context = Data.getData(toggles[i], DATA_KEY); var relatedTarget = { relatedTarget: toggles[i] }; @@ -444,44 +506,45 @@ var dropdownMenu = context._menu; - if (!$(parent).hasClass(ClassName.SHOW)) { + if (!parent.classList.contains(ClassName.SHOW)) { continue; } - if (event && (event.type === 'click' && /input|textarea/i.test(event.target.tagName) || event.type === 'keyup' && event.which === TAB_KEYCODE) && $.contains(parent, event.target)) { + if (event && (event.type === 'click' && /input|textarea/i.test(event.target.tagName) || event.type === 'keyup' && event.which === TAB_KEYCODE) && parent.contains(event.target)) { continue; } - var hideEvent = $.Event(Event.HIDE, relatedTarget); - $(parent).trigger(hideEvent); + var hideEvent = EventHandler.trigger(parent, Event.HIDE, relatedTarget); - if (hideEvent.isDefaultPrevented()) { + if (hideEvent.defaultPrevented) { continue; } // If this is a touch-enabled device we remove the extra // empty mouseover listeners we added for iOS support if ('ontouchstart' in document.documentElement) { - $(document.body).children().off('mouseover', null, $.noop); + makeArray(document.body.children).forEach(function (elem) { + return EventHandler.off(elem, 'mouseover', null, noop()); + }); } toggles[i].setAttribute('aria-expanded', 'false'); - $(dropdownMenu).removeClass(ClassName.SHOW); - $(parent).removeClass(ClassName.SHOW).trigger($.Event(Event.HIDDEN, relatedTarget)); + dropdownMenu.classList.remove(ClassName.SHOW); + parent.classList.remove(ClassName.SHOW); + EventHandler.trigger(parent, Event.HIDDEN, relatedTarget); } }; Dropdown._getParentFromElement = function _getParentFromElement(element) { var parent; - var selector = Util.getSelectorFromElement(element); + var selector = getSelectorFromElement(element); if (selector) { - parent = document.querySelector(selector); + parent = SelectorEngine.findOne(selector); } return parent || element.parentNode; - } // eslint-disable-next-line complexity - ; + }; Dropdown._dataApiKeydownHandler = function _dataApiKeydownHandler(event) { // If not input/textarea: @@ -491,34 +554,34 @@ // - If key is other than escape // - If key is not up or down => not a dropdown command // - If trigger inside the menu => not a dropdown command - if (/input|textarea/i.test(event.target.tagName) ? event.which === SPACE_KEYCODE || event.which !== ESCAPE_KEYCODE && (event.which !== ARROW_DOWN_KEYCODE && event.which !== ARROW_UP_KEYCODE || $(event.target).closest(Selector.MENU).length) : !REGEXP_KEYDOWN.test(event.which)) { + if (/input|textarea/i.test(event.target.tagName) ? event.which === SPACE_KEYCODE || event.which !== ESCAPE_KEYCODE && (event.which !== ARROW_DOWN_KEYCODE && event.which !== ARROW_UP_KEYCODE || SelectorEngine.closest(event.target, Selector.MENU)) : !REGEXP_KEYDOWN.test(event.which)) { return; } event.preventDefault(); event.stopPropagation(); - if (this.disabled || $(this).hasClass(ClassName.DISABLED)) { + if (this.disabled || this.classList.contains(ClassName.DISABLED)) { return; } var parent = Dropdown._getParentFromElement(this); - var isActive = $(parent).hasClass(ClassName.SHOW); + var isActive = parent.classList.contains(ClassName.SHOW); if (!isActive || isActive && (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE)) { if (event.which === ESCAPE_KEYCODE) { - var toggle = parent.querySelector(Selector.DATA_TOGGLE); - $(toggle).trigger('focus'); + EventHandler.trigger(SelectorEngine.findOne(Selector.DATA_TOGGLE, parent), 'focus'); } - $(this).trigger('click'); + Dropdown._clearMenus(); + return; } - var items = [].slice.call(parent.querySelectorAll(Selector.VISIBLE_ITEMS)); + var items = makeArray(SelectorEngine.find(Selector.VISIBLE_ITEMS, parent)); - if (items.length === 0) { + if (!items.length) { return; } @@ -541,6 +604,10 @@ items[index].focus(); }; + Dropdown._getInstance = function _getInstance(element) { + return Data.getData(element, DATA_KEY); + }; + _createClass(Dropdown, null, [{ key: "VERSION", get: function get() { @@ -567,27 +634,36 @@ */ - $(document).on(Event.KEYDOWN_DATA_API, Selector.DATA_TOGGLE, Dropdown._dataApiKeydownHandler).on(Event.KEYDOWN_DATA_API, Selector.MENU, Dropdown._dataApiKeydownHandler).on(Event.CLICK_DATA_API + " " + Event.KEYUP_DATA_API, Dropdown._clearMenus).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) { + EventHandler.on(document, Event.KEYDOWN_DATA_API, Selector.DATA_TOGGLE, Dropdown._dataApiKeydownHandler); + EventHandler.on(document, Event.KEYDOWN_DATA_API, Selector.MENU, Dropdown._dataApiKeydownHandler); + EventHandler.on(document, Event.CLICK_DATA_API, Dropdown._clearMenus); + EventHandler.on(document, Event.KEYUP_DATA_API, Dropdown._clearMenus); + EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) { event.preventDefault(); event.stopPropagation(); - Dropdown._jQueryInterface.call($(this), 'toggle'); - }).on(Event.CLICK_DATA_API, Selector.FORM_CHILD, function (e) { - e.stopPropagation(); + Dropdown._dropdownInterface(this, 'toggle'); + }); + EventHandler.on(document, Event.CLICK_DATA_API, Selector.FORM_CHILD, function (e) { + return e.stopPropagation(); }); /** * ------------------------------------------------------------------------ * jQuery * ------------------------------------------------------------------------ + * add .dropdown to jQuery only if jQuery is present */ - $.fn[NAME] = Dropdown._jQueryInterface; - $.fn[NAME].Constructor = Dropdown; + if (typeof jQuery !== 'undefined') { + var JQUERY_NO_CONFLICT = jQuery.fn[NAME]; + jQuery.fn[NAME] = Dropdown._jQueryInterface; + jQuery.fn[NAME].Constructor = Dropdown; - $.fn[NAME].noConflict = function () { - $.fn[NAME] = JQUERY_NO_CONFLICT; - return Dropdown._jQueryInterface; - }; + jQuery.fn[NAME].noConflict = function () { + jQuery.fn[NAME] = JQUERY_NO_CONFLICT; + return Dropdown._jQueryInterface; + }; + } return Dropdown; |