1
0
Fork 0
mirror of https://github.com/twbs/bootstrap.git synced 2022-11-09 12:25:43 -05:00
twbs--bootstrap/js/dist/dropdown.js

477 lines
15 KiB
JavaScript
Raw Normal View History

2017-12-22 19:21:54 -05:00
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2017-09-30 17:28:03 -04:00
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
2017-09-06 00:05:12 -04:00
2017-09-30 17:28:03 -04:00
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
2015-05-10 16:47:11 -04:00
/**
* --------------------------------------------------------------------------
2018-01-18 11:21:22 -05:00
* Bootstrap (v4.0.0): dropdown.js
2015-05-10 16:47:11 -04:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
2017-10-25 15:31:55 -04:00
var Dropdown = function ($) {
2015-05-10 16:47:11 -04:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'dropdown';
2018-01-18 11:21:22 -05:00
var VERSION = '4.0.0';
2015-05-10 16:47:11 -04:00
var DATA_KEY = 'bs.dropdown';
2017-09-30 17:28:03 -04:00
var EVENT_KEY = "." + DATA_KEY;
2015-05-13 15:48:34 -04:00
var DATA_API_KEY = '.data-api';
2015-05-10 16:47:11 -04:00
var JQUERY_NO_CONFLICT = $.fn[NAME];
2016-06-04 21:21:15 -04:00
var ESCAPE_KEYCODE = 27; // KeyboardEvent.which value for Escape (Esc) key
2017-09-30 17:28:03 -04:00
2017-01-21 20:50:45 -05:00
var SPACE_KEYCODE = 32; // KeyboardEvent.which value for space key
2017-09-30 17:28:03 -04:00
2017-04-16 16:54:07 -04:00
var TAB_KEYCODE = 9; // KeyboardEvent.which value for tab key
2017-09-30 17:28:03 -04:00
2016-06-04 21:21:15 -04:00
var ARROW_UP_KEYCODE = 38; // KeyboardEvent.which value for up arrow key
2017-09-30 17:28:03 -04:00
2016-06-04 21:21:15 -04:00
var ARROW_DOWN_KEYCODE = 40; // KeyboardEvent.which value for down arrow key
2017-09-30 17:28:03 -04:00
2016-06-04 21:21:15 -04:00
var RIGHT_MOUSE_BUTTON_WHICH = 3; // MouseEvent.which value for the right button (assuming a right-handed mouse)
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
var REGEXP_KEYDOWN = new RegExp(ARROW_UP_KEYCODE + "|" + ARROW_DOWN_KEYCODE + "|" + ESCAPE_KEYCODE);
2015-05-10 16:47:11 -04:00
var Event = {
2017-09-30 17:28:03 -04:00
HIDE: "hide" + EVENT_KEY,
HIDDEN: "hidden" + EVENT_KEY,
SHOW: "show" + EVENT_KEY,
SHOWN: "shown" + EVENT_KEY,
CLICK: "click" + EVENT_KEY,
CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY,
KEYDOWN_DATA_API: "keydown" + EVENT_KEY + DATA_API_KEY,
KEYUP_DATA_API: "keyup" + EVENT_KEY + DATA_API_KEY
2015-05-10 16:47:11 -04:00
};
var ClassName = {
DISABLED: 'disabled',
2017-05-26 23:20:10 -04:00
SHOW: 'show',
DROPUP: 'dropup',
2017-10-29 19:19:14 -04:00
DROPRIGHT: 'dropright',
DROPLEFT: 'dropleft',
2017-05-26 23:20:10 -04:00
MENURIGHT: 'dropdown-menu-right',
2017-12-22 19:21:54 -05:00
MENULEFT: 'dropdown-menu-left',
POSITION_STATIC: 'position-static'
2015-05-10 16:47:11 -04:00
};
var Selector = {
DATA_TOGGLE: '[data-toggle="dropdown"]',
FORM_CHILD: '.dropdown form',
2017-04-16 16:54:07 -04:00
MENU: '.dropdown-menu',
2015-05-10 16:47:11 -04:00
NAVBAR_NAV: '.navbar-nav',
2017-04-16 16:54:07 -04:00
VISIBLE_ITEMS: '.dropdown-menu .dropdown-item:not(.disabled)'
2015-05-10 16:47:11 -04:00
};
2017-05-16 03:59:44 -04:00
var AttachmentMap = {
TOP: 'top-start',
2017-05-26 23:20:10 -04:00
TOPEND: 'top-end',
BOTTOM: 'bottom-start',
2017-10-29 19:19:14 -04:00
BOTTOMEND: 'bottom-end',
RIGHT: 'right-start',
RIGHTEND: 'right-end',
LEFT: 'left-start',
LEFTEND: 'left-end'
2017-05-16 03:59:44 -04:00
};
var Default = {
offset: 0,
2017-12-22 19:21:54 -05:00
flip: true,
2018-02-11 17:53:29 -05:00
boundary: 'scrollParent',
reference: 'toggle'
2017-05-16 03:59:44 -04:00
};
var DefaultType = {
2017-10-15 18:51:44 -04:00
offset: '(number|string|function)',
2017-12-22 19:21:54 -05:00
flip: 'boolean',
2018-02-11 17:53:29 -05:00
boundary: '(string|element)',
reference: '(string|element)'
2017-09-13 01:24:15 -04:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2015-05-10 16:47:11 -04:00
2017-09-13 01:24:15 -04:00
};
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
var Dropdown =
/*#__PURE__*/
function () {
function Dropdown(element, config) {
2015-05-13 15:48:34 -04:00
this._element = element;
2017-05-16 03:59:44 -04:00
this._popper = null;
this._config = this._getConfig(config);
this._menu = this._getMenuElement();
2017-06-18 03:14:35 -04:00
this._inNavbar = this._detectNavbar();
2015-05-13 15:48:34 -04:00
this._addEventListeners();
2018-01-12 01:42:40 -05:00
} // Getters
2015-05-10 16:47:11 -04:00
2015-08-13 00:12:03 -04:00
2017-09-30 17:28:03 -04:00
var _proto = Dropdown.prototype;
2015-05-10 16:47:11 -04:00
2018-01-12 01:42:40 -05:00
// Public
2017-09-30 17:28:03 -04:00
_proto.toggle = function toggle() {
if (this._element.disabled || $(this._element).hasClass(ClassName.DISABLED)) {
return;
}
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
var parent = Dropdown._getParentFromElement(this._element);
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
var isActive = $(this._menu).hasClass(ClassName.SHOW);
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
Dropdown._clearMenus();
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
if (isActive) {
return;
}
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
var relatedTarget = {
relatedTarget: this._element
};
var showEvent = $.Event(Event.SHOW, relatedTarget);
$(parent).trigger(showEvent);
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
if (showEvent.isDefaultPrevented()) {
return;
2017-10-30 15:48:13 -04:00
} // Disable totally Popper.js for Dropdown in Navbar
2017-10-29 19:19:14 -04:00
2017-10-30 15:48:13 -04:00
if (!this._inNavbar) {
/**
* Check for Popper dependency
* Popper - https://popper.js.org
*/
if (typeof Popper === 'undefined') {
2018-01-12 01:42:40 -05:00
throw new TypeError('Bootstrap dropdown require Popper.js (https://popper.js.org)');
2017-10-30 15:48:13 -04:00
}
2017-09-06 00:05:12 -04:00
2018-02-11 17:53:29 -05:00
var referenceElement = this._element;
2017-03-19 22:03:32 -04:00
2018-02-11 17:53:29 -05:00
if (this._config.reference === 'parent') {
referenceElement = parent;
} else if (Util.isElement(this._config.reference)) {
referenceElement = this._config.reference; // Check if it's jQuery element
if (typeof this._config.reference.jquery !== 'undefined') {
referenceElement = this._config.reference[0];
2017-10-30 15:48:13 -04:00
}
2017-12-22 19:21:54 -05:00
} // If boundary is not `scrollParent`, then set position to `static`
// to allow the menu to "escape" the scroll parent's boundaries
// https://github.com/twbs/bootstrap/issues/24251
if (this._config.boundary !== 'scrollParent') {
$(parent).addClass(ClassName.POSITION_STATIC);
2017-09-06 00:05:12 -04:00
}
2015-05-10 16:47:11 -04:00
2018-02-11 17:53:29 -05:00
this._popper = new Popper(referenceElement, this._menu, this._getPopperConfig());
2018-01-12 01:42:40 -05:00
} // If this is a touch-enabled device we add extra
2017-09-30 17:28:03 -04:00
// empty mouseover listeners to the body's immediate children;
// only needed because of broken event delegation on iOS
// https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html
2015-05-10 16:47:11 -04:00
2017-10-30 15:48:13 -04:00
2018-01-12 01:42:40 -05:00
if ('ontouchstart' in document.documentElement && $(parent).closest(Selector.NAVBAR_NAV).length === 0) {
2017-09-30 17:28:03 -04:00
$('body').children().on('mouseover', null, $.noop);
2017-09-06 00:05:12 -04:00
}
2017-09-30 17:28:03 -04:00
this._element.focus();
this._element.setAttribute('aria-expanded', true);
$(this._menu).toggleClass(ClassName.SHOW);
$(parent).toggleClass(ClassName.SHOW).trigger($.Event(Event.SHOWN, relatedTarget));
};
_proto.dispose = function dispose() {
$.removeData(this._element, DATA_KEY);
$(this._element).off(EVENT_KEY);
this._element = null;
this._menu = null;
if (this._popper !== null) {
this._popper.destroy();
2015-05-13 15:48:34 -04:00
2017-10-30 15:48:13 -04:00
this._popper = null;
}
2017-09-30 17:28:03 -04:00
};
2017-05-16 03:59:44 -04:00
2017-09-30 17:28:03 -04:00
_proto.update = function update() {
this._inNavbar = this._detectNavbar();
if (this._popper !== null) {
this._popper.scheduleUpdate();
2017-05-16 03:59:44 -04:00
}
2018-01-12 01:42:40 -05:00
}; // Private
2017-09-30 17:28:03 -04:00
_proto._addEventListeners = function _addEventListeners() {
var _this = this;
$(this._element).on(Event.CLICK, function (event) {
event.preventDefault();
event.stopPropagation();
_this.toggle();
});
};
2017-05-16 03:59:44 -04:00
2017-09-30 17:28:03 -04:00
_proto._getConfig = function _getConfig(config) {
2017-12-22 19:21:54 -05:00
config = _extends({}, this.constructor.Default, $(this._element).data(), config);
2017-09-30 17:28:03 -04:00
Util.typeCheckConfig(NAME, config, this.constructor.DefaultType);
return config;
};
2017-05-16 03:59:44 -04:00
2017-09-30 17:28:03 -04:00
_proto._getMenuElement = function _getMenuElement() {
if (!this._menu) {
var parent = Dropdown._getParentFromElement(this._element);
this._menu = $(parent).find(Selector.MENU)[0];
2017-05-16 03:59:44 -04:00
}
2017-09-30 17:28:03 -04:00
return this._menu;
};
_proto._getPlacement = function _getPlacement() {
var $parentDropdown = $(this._element).parent();
var placement = AttachmentMap.BOTTOM; // Handle dropup
if ($parentDropdown.hasClass(ClassName.DROPUP)) {
placement = AttachmentMap.TOP;
if ($(this._menu).hasClass(ClassName.MENURIGHT)) {
placement = AttachmentMap.TOPEND;
2017-05-26 23:20:10 -04:00
}
2017-10-29 19:19:14 -04:00
} else if ($parentDropdown.hasClass(ClassName.DROPRIGHT)) {
placement = AttachmentMap.RIGHT;
} else if ($parentDropdown.hasClass(ClassName.DROPLEFT)) {
placement = AttachmentMap.LEFT;
2017-09-30 17:28:03 -04:00
} else if ($(this._menu).hasClass(ClassName.MENURIGHT)) {
placement = AttachmentMap.BOTTOMEND;
2017-05-26 23:20:10 -04:00
}
2017-09-30 17:28:03 -04:00
return placement;
};
_proto._detectNavbar = function _detectNavbar() {
return $(this._element).closest('.navbar').length > 0;
};
_proto._getPopperConfig = function _getPopperConfig() {
2017-10-15 18:51:44 -04:00
var _this2 = this;
var offsetConf = {};
if (typeof this._config.offset === 'function') {
offsetConf.fn = function (data) {
2017-12-22 19:21:54 -05:00
data.offsets = _extends({}, data.offsets, _this2._config.offset(data.offsets) || {});
2017-10-15 18:51:44 -04:00
return data;
};
} else {
offsetConf.offset = this._config.offset;
}
2017-09-30 17:28:03 -04:00
var popperConfig = {
placement: this._getPlacement(),
modifiers: {
2017-10-15 18:51:44 -04:00
offset: offsetConf,
2017-09-30 17:28:03 -04:00
flip: {
enabled: this._config.flip
2017-12-22 19:21:54 -05:00
},
preventOverflow: {
boundariesElement: this._config.boundary
2017-06-18 03:14:35 -04:00
}
2017-10-30 15:48:13 -04:00
}
2017-09-30 17:28:03 -04:00
};
return popperConfig;
2018-01-12 01:42:40 -05:00
}; // Static
2017-09-30 17:28:03 -04:00
Dropdown._jQueryInterface = function _jQueryInterface(config) {
return this.each(function () {
var data = $(this).data(DATA_KEY);
var _config = typeof config === 'object' ? config : null;
if (!data) {
data = new Dropdown(this, _config);
$(this).data(DATA_KEY, data);
}
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
2018-01-12 01:42:40 -05:00
throw new TypeError("No method named \"" + config + "\"");
2017-09-06 00:05:12 -04:00
}
2017-06-18 03:14:35 -04:00
2017-09-30 17:28:03 -04:00
data[config]();
2017-09-06 00:05:12 -04:00
}
2017-09-30 17:28:03 -04:00
});
};
Dropdown._clearMenus = function _clearMenus(event) {
if (event && (event.which === RIGHT_MOUSE_BUTTON_WHICH || event.type === 'keyup' && event.which !== TAB_KEYCODE)) {
return;
2017-06-18 03:14:35 -04:00
}
2017-09-30 17:28:03 -04:00
var toggles = $.makeArray($(Selector.DATA_TOGGLE));
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
for (var i = 0; i < toggles.length; i++) {
var parent = Dropdown._getParentFromElement(toggles[i]);
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
var context = $(toggles[i]).data(DATA_KEY);
var relatedTarget = {
relatedTarget: toggles[i]
};
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
if (!context) {
continue;
2017-05-16 03:59:44 -04:00
}
2017-09-30 17:28:03 -04:00
var dropdownMenu = context._menu;
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
if (!$(parent).hasClass(ClassName.SHOW)) {
continue;
}
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
if (event && (event.type === 'click' && /input|textarea/i.test(event.target.tagName) || event.type === 'keyup' && event.which === TAB_KEYCODE) && $.contains(parent, event.target)) {
continue;
}
2016-10-09 20:26:51 -04:00
2017-09-30 17:28:03 -04:00
var hideEvent = $.Event(Event.HIDE, relatedTarget);
$(parent).trigger(hideEvent);
2017-03-19 22:03:32 -04:00
2017-09-30 17:28:03 -04:00
if (hideEvent.isDefaultPrevented()) {
continue;
2018-01-12 01:42:40 -05:00
} // If this is a touch-enabled device we remove the extra
2017-09-30 17:28:03 -04:00
// empty mouseover listeners we added for iOS support
2016-10-09 20:26:51 -04:00
2017-09-30 17:28:03 -04:00
if ('ontouchstart' in document.documentElement) {
$('body').children().off('mouseover', null, $.noop);
2017-09-06 00:05:12 -04:00
}
2017-09-30 17:28:03 -04:00
toggles[i].setAttribute('aria-expanded', 'false');
$(dropdownMenu).removeClass(ClassName.SHOW);
$(parent).removeClass(ClassName.SHOW).trigger($.Event(Event.HIDDEN, relatedTarget));
2015-05-10 16:47:11 -04:00
}
2017-09-30 17:28:03 -04:00
};
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
Dropdown._getParentFromElement = function _getParentFromElement(element) {
var parent;
var selector = Util.getSelectorFromElement(element);
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
if (selector) {
parent = $(selector)[0];
2016-10-09 20:26:51 -04:00
}
2017-09-06 00:05:12 -04:00
2017-09-30 17:28:03 -04:00
return parent || element.parentNode;
2017-12-30 22:41:36 -05:00
}; // eslint-disable-next-line complexity
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
Dropdown._dataApiKeydownHandler = function _dataApiKeydownHandler(event) {
2017-10-28 15:04:47 -04:00
// If not input/textarea:
// - And not a key in REGEXP_KEYDOWN => not a dropdown command
// If input/textarea:
// - If space key => not a dropdown command
// - 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)) {
2017-09-30 17:28:03 -04:00
return;
}
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
event.preventDefault();
event.stopPropagation();
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
if (this.disabled || $(this).hasClass(ClassName.DISABLED)) {
return;
}
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
var parent = Dropdown._getParentFromElement(this);
2016-10-09 20:26:51 -04:00
2017-09-30 17:28:03 -04:00
var isActive = $(parent).hasClass(ClassName.SHOW);
if (!isActive && (event.which !== ESCAPE_KEYCODE || event.which !== SPACE_KEYCODE) || isActive && (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE)) {
if (event.which === ESCAPE_KEYCODE) {
var toggle = $(parent).find(Selector.DATA_TOGGLE)[0];
$(toggle).trigger('focus');
2015-05-10 16:47:11 -04:00
}
2017-09-30 17:28:03 -04:00
$(this).trigger('click');
return;
}
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
var items = $(parent).find(Selector.VISIBLE_ITEMS).get();
2015-05-10 16:47:11 -04:00
2018-01-12 01:42:40 -05:00
if (items.length === 0) {
2017-09-30 17:28:03 -04:00
return;
}
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
var index = items.indexOf(event.target);
2015-08-18 23:28:28 -04:00
2017-09-30 17:28:03 -04:00
if (event.which === ARROW_UP_KEYCODE && index > 0) {
2018-01-12 01:42:40 -05:00
// Up
2017-09-30 17:28:03 -04:00
index--;
}
2015-08-18 23:28:28 -04:00
2017-09-30 17:28:03 -04:00
if (event.which === ARROW_DOWN_KEYCODE && index < items.length - 1) {
2018-01-12 01:42:40 -05:00
// Down
2017-09-30 17:28:03 -04:00
index++;
}
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
if (index < 0) {
index = 0;
2015-05-10 16:47:11 -04:00
}
2017-09-30 17:28:03 -04:00
items[index].focus();
};
_createClass(Dropdown, null, [{
key: "VERSION",
2015-08-13 00:12:03 -04:00
get: function get() {
return VERSION;
}
2017-05-16 03:59:44 -04:00
}, {
2017-09-30 17:28:03 -04:00
key: "Default",
2017-05-16 03:59:44 -04:00
get: function get() {
return Default;
}
}, {
2017-09-30 17:28:03 -04:00
key: "DefaultType",
2017-05-16 03:59:44 -04:00
get: function get() {
return DefaultType;
}
2015-05-10 16:47:11 -04:00
}]);
return Dropdown;
2016-10-09 20:26:51 -04:00
}();
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-10 16:47:11 -04:00
2017-09-30 17:28:03 -04:00
$(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) {
2017-05-16 03:59:44 -04:00
event.preventDefault();
event.stopPropagation();
2017-09-30 17:28:03 -04:00
2017-05-16 03:59:44 -04:00
Dropdown._jQueryInterface.call($(this), 'toggle');
}).on(Event.CLICK_DATA_API, Selector.FORM_CHILD, function (e) {
2015-05-10 16:47:11 -04:00
e.stopPropagation();
2017-07-02 13:40:27 -04:00
});
2015-05-10 16:47:11 -04:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
2017-07-02 13:40:27 -04:00
$.fn[NAME] = Dropdown._jQueryInterface;
2015-05-10 16:47:11 -04:00
$.fn[NAME].Constructor = Dropdown;
2017-09-30 17:28:03 -04:00
2015-05-10 16:47:11 -04:00
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return Dropdown._jQueryInterface;
};
return Dropdown;
2017-10-15 18:51:44 -04:00
}($, Popper);
2017-04-22 02:58:09 -04:00
//# sourceMappingURL=dropdown.js.map