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

286 lines
8.7 KiB
JavaScript
Raw Normal View History

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
/**
* --------------------------------------------------------------------------
2016-10-19 17:27:41 +02:00
* Bootstrap (v4.0.0-alpha.5): 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
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'dropdown';
2016-10-19 17:27:41 +02:00
var VERSION = '4.0.0-alpha.5';
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
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)
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,
KEYDOWN_DATA_API: 'keydown' + EVENT_KEY + DATA_API_KEY
2015-05-10 22:47:11 +02:00
};
var ClassName = {
BACKDROP: 'dropdown-backdrop',
DISABLED: 'disabled',
OPEN: 'open'
};
var Selector = {
BACKDROP: '.dropdown-backdrop',
DATA_TOGGLE: '[data-toggle="dropdown"]',
FORM_CHILD: '.dropdown form',
ROLE_MENU: '[role="menu"]',
ROLE_LISTBOX: '[role="listbox"]',
NAVBAR_NAV: '.navbar-nav',
VISIBLE_ITEMS: '[role="menu"] li:not(.disabled) a, ' + '[role="listbox"] li:not(.disabled) a'
};
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2016-10-10 02:26:51 +02:00
var Dropdown = function () {
2015-05-10 22:47:11 +02:00
function Dropdown(element) {
_classCallCheck(this, Dropdown);
2015-05-13 21:48:34 +02:00
this._element = element;
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() {
if (this.disabled || $(this).hasClass(ClassName.DISABLED)) {
return false;
}
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
var parent = Dropdown._getParentFromElement(this);
var isActive = $(parent).hasClass(ClassName.OPEN);
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) {
return false;
}
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
if ('ontouchstart' in document.documentElement && !$(parent).closest(Selector.NAVBAR_NAV).length) {
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
// if mobile we use a backdrop because click events don't delegate
var dropdown = document.createElement('div');
dropdown.className = ClassName.BACKDROP;
$(dropdown).insertBefore(this);
$(dropdown).on('click', Dropdown._clearMenus);
}
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
var relatedTarget = { relatedTarget: this };
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()) {
return false;
}
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
this.focus();
this.setAttribute('aria-expanded', 'true');
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
$(parent).toggleClass(ClassName.OPEN);
$(parent).trigger($.Event(Event.SHOWN, relatedTarget));
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
return false;
};
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;
};
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() {
$(this._element).on(Event.CLICK, this.toggle);
};
2015-05-10 22:47:11 +02:00
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);
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
if (!data) {
$(this).data(DATA_KEY, data = new Dropdown(this));
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 + '"');
}
data[config].call(this);
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) {
if (event && event.which === RIGHT_MOUSE_BUTTON_WHICH) {
return;
}
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
var backdrop = $(Selector.BACKDROP)[0];
if (backdrop) {
backdrop.parentNode.removeChild(backdrop);
}
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
var toggles = $.makeArray($(Selector.DATA_TOGGLE));
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
for (var i = 0; i < toggles.length; i++) {
var parent = Dropdown._getParentFromElement(toggles[i]);
var relatedTarget = { relatedTarget: toggles[i] };
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
if (!$(parent).hasClass(ClassName.OPEN)) {
continue;
}
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
if (event && event.type === 'click' && /input|textarea/i.test(event.target.tagName) && $.contains(parent, event.target)) {
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
toggles[i].setAttribute('aria-expanded', 'false');
$(parent).removeClass(ClassName.OPEN).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) {
if (!/(38|40|27|32)/.test(event.which) || /input|textarea/i.test(event.target.tagName)) {
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);
var isActive = $(parent).hasClass(ClassName.OPEN);
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
if (!isActive && event.which !== ESCAPE_KEYCODE || isActive && event.which === ESCAPE_KEYCODE) {
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-10-10 02:26:51 +02:00
var items = $.makeArray($(Selector.VISIBLE_ITEMS));
2015-05-10 22:47:11 +02:00
2016-10-10 02:26:51 +02:00
items = items.filter(function (item) {
return item.offsetWidth || item.offsetHeight;
});
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;
}
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
2015-05-13 21:48:34 +02:00
$(document).on(Event.KEYDOWN_DATA_API, Selector.DATA_TOGGLE, Dropdown._dataApiKeydownHandler).on(Event.KEYDOWN_DATA_API, Selector.ROLE_MENU, Dropdown._dataApiKeydownHandler).on(Event.KEYDOWN_DATA_API, Selector.ROLE_LISTBOX, Dropdown._dataApiKeydownHandler).on(Event.CLICK_DATA_API, Dropdown._clearMenus).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, Dropdown.prototype.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;
2016-10-10 02:26:51 +02:00
}(jQuery);
2015-08-13 06:12:03 +02:00
//# sourceMappingURL=dropdown.js.map