0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-04 16:24:22 +01:00
Bootstrap/js/dist/dropdown.js

416 lines
13 KiB
JavaScript
Raw Normal View History

2017-05-16 09:59:44 +02:00
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
2016-10-10 02:26:51 +02:00
var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2015-05-10 22:47:11 +02:00
/**
* --------------------------------------------------------------------------
2017-01-06 17:38:04 +01:00
* Bootstrap (v4.0.0-alpha.6): dropdown.js
2015-05-10 22:47:11 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
2016-10-10 02:26:51 +02:00
var Dropdown = function ($) {
2015-05-10 22:47:11 +02:00
2017-05-16 09:59:44 +02:00
/**
* Check for Popper dependency
* Popper - https://popper.js.org
*/
if (typeof Popper === 'undefined') {
throw new Error('Bootstrap dropdown require Popper.js (https://popper.js.org)');
}
2015-05-10 22:47:11 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'dropdown';
2017-01-06 17:38:04 +01:00
var VERSION = '4.0.0-alpha.6';
2015-05-10 22:47:11 +02:00
var DATA_KEY = 'bs.dropdown';
2015-05-13 21:48:34 +02:00
var EVENT_KEY = '.' + DATA_KEY;
var DATA_API_KEY = '.data-api';
2015-05-10 22:47:11 +02:00
var JQUERY_NO_CONFLICT = $.fn[NAME];
2016-06-05 03:21:15 +02:00
var ESCAPE_KEYCODE = 27; // KeyboardEvent.which value for Escape (Esc) key
2017-01-22 02:50:45 +01:00
var SPACE_KEYCODE = 32; // KeyboardEvent.which value for space key
2017-04-16 22:54:07 +02:00
var TAB_KEYCODE = 9; // KeyboardEvent.which value for tab key
2016-06-05 03:21:15 +02:00
var ARROW_UP_KEYCODE = 38; // KeyboardEvent.which value for up arrow key
var ARROW_DOWN_KEYCODE = 40; // KeyboardEvent.which value for down arrow key
var RIGHT_MOUSE_BUTTON_WHICH = 3; // MouseEvent.which value for the right button (assuming a right-handed mouse)
2017-04-16 22:54:07 +02:00
var REGEXP_KEYDOWN = new RegExp(ARROW_UP_KEYCODE + '|' + ARROW_DOWN_KEYCODE + '|' + ESCAPE_KEYCODE);
2015-05-10 22:47:11 +02:00
var Event = {
2015-05-13 21:48:34 +02:00
HIDE: 'hide' + EVENT_KEY,
HIDDEN: 'hidden' + EVENT_KEY,
SHOW: 'show' + EVENT_KEY,
SHOWN: 'shown' + EVENT_KEY,
CLICK: 'click' + EVENT_KEY,
2015-08-13 06:12:03 +02:00
CLICK_DATA_API: 'click' + EVENT_KEY + DATA_API_KEY,
2017-04-16 22:54:07 +02:00
KEYDOWN_DATA_API: 'keydown' + EVENT_KEY + DATA_API_KEY,
KEYUP_DATA_API: 'keyup' + EVENT_KEY + DATA_API_KEY
2015-05-10 22:47:11 +02:00
};
var ClassName = {
DISABLED: 'disabled',
2017-05-27 05:20:10 +02:00
SHOW: 'show',
DROPUP: 'dropup',
MENURIGHT: 'dropdown-menu-right',
MENULEFT: 'dropdown-menu-left'
2015-05-10 22:47:11 +02:00
};
var Selector = {
DATA_TOGGLE: '[data-toggle="dropdown"]',
FORM_CHILD: '.dropdown form',
2017-04-16 22:54:07 +02:00
MENU: '.dropdown-menu',
2015-05-10 22:47:11 +02:00
NAVBAR_NAV: '.navbar-nav',
2017-04-16 22:54:07 +02:00
VISIBLE_ITEMS: '.dropdown-menu .dropdown-item:not(.disabled)'
2015-05-10 22:47:11 +02:00
};
2017-05-16 09:59:44 +02:00
var AttachmentMap = {
TOP: 'top-start',
2017-05-27 05:20:10 +02:00
TOPEND: 'top-end',
BOTTOM: 'bottom-start',
BOTTOMEND: 'bottom-end'
2017-05-16 09:59:44 +02:00
};
var Default = {
placement: AttachmentMap.BOTTOM,
offset: 0,
flip: true
};
var DefaultType = {
placement: 'string',
offset: '(number|string)',
flip: 'boolean'
};
2015-05-10 22:47:11 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2016-10-10 02:26:51 +02:00
var Dropdown = function () {
2017-05-16 09:59:44 +02:00
function Dropdown(element, config) {
2015-05-10 22:47:11 +02:00
_classCallCheck(this, Dropdown);
2015-05-13 21:48:34 +02:00
this._element = element;
2017-05-16 09:59:44 +02:00
this._popper = null;
this._config = this._getConfig(config);
this._menu = this._getMenuElement();
2015-05-13 21:48:34 +02:00
this._addEventListeners();
2015-05-10 22:47:11 +02:00
}
2015-08-13 06:12:03 +02:00
// getters
2016-10-10 02:26:51 +02:00
// public
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
Dropdown.prototype.toggle = function toggle() {
2017-05-16 09:59:44 +02:00
if (this._element.disabled || $(this._element).hasClass(ClassName.DISABLED)) {
return;
2016-10-10 02:26:51 +02:00
}
2015-05-10 22:47:11 +02:00
2017-05-16 09:59:44 +02:00
var parent = Dropdown._getParentFromElement(this._element);
var isActive = $(this._menu).hasClass(ClassName.SHOW);
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
Dropdown._clearMenus();
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
if (isActive) {
2017-05-16 09:59:44 +02:00
return;
2016-10-10 02:26:51 +02:00
}
2015-05-10 22:47:11 +02:00
2016-11-26 00:00:23 +01:00
var relatedTarget = {
2017-05-16 09:59:44 +02:00
relatedTarget: this._element
2016-11-26 00:00:23 +01:00
};
2016-10-10 02:26:51 +02:00
var showEvent = $.Event(Event.SHOW, relatedTarget);
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
$(parent).trigger(showEvent);
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
if (showEvent.isDefaultPrevented()) {
2017-05-16 09:59:44 +02:00
return;
2016-10-10 02:26:51 +02:00
}
2015-05-10 22:47:11 +02:00
2017-05-27 05:20:10 +02:00
var element = this._element;
// for dropup with alignment we use the parent as popper container
if ($(parent).hasClass(ClassName.DROPUP)) {
if ($(this._menu).hasClass(ClassName.MENULEFT) || $(this._menu).hasClass(ClassName.MENURIGHT)) {
element = parent;
}
}
this._popper = new Popper(element, this._menu, {
placement: this._getPlacement(),
2017-05-16 09:59:44 +02:00
modifiers: {
offset: {
offset: this._config.offset
},
flip: {
enabled: this._config.flip
}
}
});
2017-04-16 22:54:07 +02:00
// if this is a touch-enabled device we add extra
// 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
2017-03-20 03:03:32 +01:00
if ('ontouchstart' in document.documentElement && !$(parent).closest(Selector.NAVBAR_NAV).length) {
2017-04-22 08:58:09 +02:00
$('body').children().on('mouseover', null, $.noop);
2017-03-20 03:03:32 +01:00
}
2017-05-16 09:59:44 +02:00
this._element.focus();
this._element.setAttribute('aria-expanded', true);
2015-05-10 22:47:11 +02:00
2017-05-16 09:59:44 +02:00
$(this._menu).toggleClass(ClassName.SHOW);
$(parent).toggleClass(ClassName.SHOW).trigger($.Event(Event.SHOWN, relatedTarget));
2016-10-10 02:26:51 +02:00
};
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
Dropdown.prototype.dispose = function dispose() {
$.removeData(this._element, DATA_KEY);
$(this._element).off(EVENT_KEY);
this._element = null;
2017-05-16 09:59:44 +02:00
this._menu = null;
if (this._popper !== null) {
this._popper.destroy();
}
this._popper = null;
};
Dropdown.prototype.update = function update() {
if (this._popper !== null) {
this._popper.scheduleUpdate();
}
2016-10-10 02:26:51 +02:00
};
2015-05-13 21:48:34 +02:00
2016-10-10 02:26:51 +02:00
// private
2015-05-13 21:48:34 +02:00
2016-10-10 02:26:51 +02:00
Dropdown.prototype._addEventListeners = function _addEventListeners() {
2017-05-16 09:59:44 +02:00
var _this = this;
$(this._element).on(Event.CLICK, function (event) {
event.preventDefault();
event.stopPropagation();
_this.toggle();
});
};
Dropdown.prototype._getConfig = function _getConfig(config) {
var elementData = $(this._element).data();
if (elementData.placement !== undefined) {
elementData.placement = AttachmentMap[elementData.placement.toUpperCase()];
}
config = $.extend({}, this.constructor.Default, $(this._element).data(), config);
Util.typeCheckConfig(NAME, config, this.constructor.DefaultType);
return config;
};
Dropdown.prototype._getMenuElement = function _getMenuElement() {
if (!this._menu) {
var parent = Dropdown._getParentFromElement(this._element);
this._menu = $(parent).find(Selector.MENU)[0];
}
return this._menu;
2016-10-10 02:26:51 +02:00
};
2015-05-10 22:47:11 +02:00
2017-05-27 05:20:10 +02:00
Dropdown.prototype._getPlacement = function _getPlacement() {
var $parentDropdown = $(this._element).parent();
var placement = this._config.placement;
// Handle dropup
if ($parentDropdown.hasClass(ClassName.DROPUP) || this._config.placement === AttachmentMap.TOP) {
placement = AttachmentMap.TOP;
if ($(this._menu).hasClass(ClassName.MENURIGHT)) {
placement = AttachmentMap.TOPEND;
}
} else {
if ($(this._menu).hasClass(ClassName.MENURIGHT)) {
placement = AttachmentMap.BOTTOMEND;
}
}
return placement;
};
2016-10-10 02:26:51 +02:00
// static
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
Dropdown._jQueryInterface = function _jQueryInterface(config) {
return this.each(function () {
var data = $(this).data(DATA_KEY);
2017-05-16 09:59:44 +02:00
var _config = (typeof config === 'undefined' ? 'undefined' : _typeof(config)) === 'object' ? config : null;
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
if (!data) {
2017-05-16 09:59:44 +02:00
data = new Dropdown(this, _config);
2016-11-26 00:00:23 +01:00
$(this).data(DATA_KEY, data);
2015-05-10 22:47:11 +02:00
}
2016-10-10 02:26:51 +02:00
if (typeof config === 'string') {
if (data[config] === undefined) {
throw new Error('No method named "' + config + '"');
}
2017-05-16 09:59:44 +02:00
data[config]();
2015-05-10 22:47:11 +02:00
}
2016-10-10 02:26:51 +02:00
});
};
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
Dropdown._clearMenus = function _clearMenus(event) {
2017-04-16 22:54:07 +02:00
if (event && (event.which === RIGHT_MOUSE_BUTTON_WHICH || event.type === 'keyup' && event.which !== TAB_KEYCODE)) {
2016-10-10 02:26:51 +02:00
return;
}
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
var toggles = $.makeArray($(Selector.DATA_TOGGLE));
for (var i = 0; i < toggles.length; i++) {
var parent = Dropdown._getParentFromElement(toggles[i]);
2017-05-16 09:59:44 +02:00
var context = $(toggles[i]).data(DATA_KEY);
2016-11-26 00:00:23 +01:00
var relatedTarget = {
relatedTarget: toggles[i]
};
2015-05-10 22:47:11 +02:00
2017-05-16 09:59:44 +02:00
if (!context) {
continue;
}
var dropdownMenu = context._menu;
2016-12-20 06:48:24 +01:00
if (!$(parent).hasClass(ClassName.SHOW)) {
2016-10-10 02:26:51 +02:00
continue;
}
2015-05-10 22:47:11 +02:00
2017-04-16 22:54:07 +02:00
if (event && (event.type === 'click' && /input|textarea/i.test(event.target.tagName) || event.type === 'keyup' && event.which === TAB_KEYCODE) && $.contains(parent, event.target)) {
2016-10-10 02:26:51 +02:00
continue;
}
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
var hideEvent = $.Event(Event.HIDE, relatedTarget);
$(parent).trigger(hideEvent);
if (hideEvent.isDefaultPrevented()) {
continue;
2015-05-10 22:47:11 +02:00
}
2016-10-10 02:26:51 +02:00
2017-04-16 22:54:07 +02:00
// if this is a touch-enabled device we remove the extra
// empty mouseover listeners we added for iOS support
if ('ontouchstart' in document.documentElement) {
2017-04-22 08:58:09 +02:00
$('body').children().off('mouseover', null, $.noop);
2017-03-20 03:03:32 +01:00
}
2016-10-10 02:26:51 +02:00
toggles[i].setAttribute('aria-expanded', 'false');
2017-05-16 09:59:44 +02:00
$(dropdownMenu).removeClass(ClassName.SHOW);
2016-12-20 06:48:24 +01:00
$(parent).removeClass(ClassName.SHOW).trigger($.Event(Event.HIDDEN, relatedTarget));
2015-05-10 22:47:11 +02:00
}
2016-10-10 02:26:51 +02:00
};
Dropdown._getParentFromElement = function _getParentFromElement(element) {
var parent = void 0;
var selector = Util.getSelectorFromElement(element);
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
if (selector) {
parent = $(selector)[0];
2015-05-10 22:47:11 +02:00
}
2016-10-10 02:26:51 +02:00
return parent || element.parentNode;
};
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
Dropdown._dataApiKeydownHandler = function _dataApiKeydownHandler(event) {
2017-04-16 22:54:07 +02:00
if (!REGEXP_KEYDOWN.test(event.which) || /button/i.test(event.target.tagName) && event.which === SPACE_KEYCODE || /input|textarea/i.test(event.target.tagName)) {
2016-10-10 02:26:51 +02:00
return;
}
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
event.preventDefault();
event.stopPropagation();
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
if (this.disabled || $(this).hasClass(ClassName.DISABLED)) {
return;
}
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
var parent = Dropdown._getParentFromElement(this);
2016-12-20 06:48:24 +01:00
var isActive = $(parent).hasClass(ClassName.SHOW);
2015-05-10 22:47:11 +02:00
2017-04-16 22:54:07 +02:00
if (!isActive && (event.which !== ESCAPE_KEYCODE || event.which !== SPACE_KEYCODE) || isActive && (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE)) {
2016-10-10 02:26:51 +02:00
if (event.which === ESCAPE_KEYCODE) {
var toggle = $(parent).find(Selector.DATA_TOGGLE)[0];
$(toggle).trigger('focus');
2015-05-10 22:47:11 +02:00
}
2016-10-10 02:26:51 +02:00
$(this).trigger('click');
return;
}
2015-05-10 22:47:11 +02:00
2016-11-26 09:40:17 +01:00
var items = $(parent).find(Selector.VISIBLE_ITEMS).get();
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
if (!items.length) {
return;
}
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
var index = items.indexOf(event.target);
2015-08-19 05:28:28 +02:00
2016-10-10 02:26:51 +02:00
if (event.which === ARROW_UP_KEYCODE && index > 0) {
// up
index--;
}
2015-08-19 05:28:28 +02:00
2016-10-10 02:26:51 +02:00
if (event.which === ARROW_DOWN_KEYCODE && index < items.length - 1) {
// down
index++;
}
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
if (index < 0) {
index = 0;
2015-05-10 22:47:11 +02:00
}
2016-10-10 02:26:51 +02:00
items[index].focus();
};
_createClass(Dropdown, null, [{
2015-08-13 06:12:03 +02:00
key: 'VERSION',
get: function get() {
return VERSION;
}
2017-05-16 09:59:44 +02:00
}, {
key: 'Default',
get: function get() {
return Default;
}
}, {
key: 'DefaultType',
get: function get() {
return DefaultType;
}
2015-05-10 22:47:11 +02:00
}]);
return Dropdown;
2016-10-10 02:26:51 +02:00
}();
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-10 22:47:11 +02:00
2017-05-16 09:59:44 +02: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) {
event.preventDefault();
event.stopPropagation();
Dropdown._jQueryInterface.call($(this), 'toggle');
}).on(Event.CLICK_DATA_API, Selector.FORM_CHILD, function (e) {
2015-05-10 22:47:11 +02:00
e.stopPropagation();
});
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
$.fn[NAME] = Dropdown._jQueryInterface;
$.fn[NAME].Constructor = Dropdown;
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return Dropdown._jQueryInterface;
};
return Dropdown;
2017-05-16 09:59:44 +02:00
}(jQuery); /* global Popper */
2017-04-22 08:58:09 +02:00
//# sourceMappingURL=dropdown.js.map