0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-01 13:24:25 +01:00
Bootstrap/js/dropdown.js

323 lines
7.4 KiB
JavaScript
Raw Normal View History

2015-01-03 22:58:44 +01:00
/** =======================================================================
* Bootstrap: dropdown.js v4.0.0
* http://getbootstrap.com/javascript/#dropdown
2013-05-22 04:30:33 +02:00
* ========================================================================
2015-01-01 01:23:48 +01:00
* Copyright 2011-2015 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
2015-01-03 22:58:44 +01:00
* ========================================================================
* @fileoverview - Add dropdown menus to nearly anything with this simple
* plugin, including the navbar, tabs, and pills.
*
* Public Methods & Properties:
*
* + $.dropdown
* + $.dropdown.noConflict
* + $.dropdown.Constructor
* + $.dropdown.Constructor.VERSION
* + $.dropdown.Constructor.prototype.toggle
*
* ========================================================================
*/
'use strict';
/**
* Our dropdown class.
* @param {Element!} element
* @constructor
*/
var Dropdown = function (element) {
$(element).on('click.bs.dropdown', this['toggle'])
}
/**
* @const
* @type {string}
*/
Dropdown['VERSION'] = '4.0.0'
/**
* @const
* @type {string}
* @private
*/
Dropdown._NAME = 'dropdown'
/**
* @const
* @type {string}
* @private
*/
Dropdown._DATA_KEY = 'bs.dropdown'
/**
* @const
* @type {Function}
* @private
*/
Dropdown._JQUERY_NO_CONFLICT = $.fn[Dropdown._NAME]
/**
* @const
* @enum {string}
* @private
*/
Dropdown._Event = {
HIDE : 'hide.bs.dropdown',
HIDDEN : 'hidden.bs.dropdown',
SHOW : 'show.bs.dropdown',
SHOWN : 'shown.bs.dropdown'
}
/**
* @const
* @enum {string}
* @private
*/
Dropdown._ClassName = {
BACKDROP : 'dropdown-backdrop',
DISABLED : 'disabled',
OPEN : 'open'
}
/**
* @const
* @enum {string}
* @private
*/
Dropdown._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(.divider) a, [role="listbox"] li:not(.divider) a'
}
/**
* Provides the jQuery Interface for the alert component.
* @param {string=} opt_config
* @this {jQuery}
* @return {jQuery}
* @private
*/
Dropdown._jQueryInterface = function (opt_config) {
return this.each(function () {
var data = $(this).data(Dropdown._DATA_KEY)
if (!data) {
$(this).data(Dropdown._DATA_KEY, (data = new Dropdown(this)))
}
2011-08-28 06:46:50 +02:00
2015-01-03 22:58:44 +01:00
if (typeof opt_config === 'string') {
data[opt_config].call(this)
}
})
}
2014-06-10 07:18:05 +02:00
2015-01-03 22:58:44 +01:00
/**
* @param {Event=} opt_event
* @private
*/
Dropdown._clearMenus = function (opt_event) {
if (opt_event && opt_event.which == 3) {
return
}
2011-11-21 03:19:50 +01:00
2015-01-03 22:58:44 +01:00
var backdrop = $(Dropdown._Selector.BACKDROP)[0]
if (backdrop) {
backdrop.parentNode.removeChild(backdrop)
}
2014-05-13 06:15:16 +02:00
2015-01-03 22:58:44 +01:00
var toggles = /** @type {Array.<Element>} */ ($.makeArray($(Dropdown._Selector.DATA_TOGGLE)))
2015-01-03 22:58:44 +01:00
for (var i = 0; i < toggles.length; i++) {
var parent = Dropdown._getParentFromElement(toggles[i])
var relatedTarget = { 'relatedTarget': toggles[i] }
2015-01-03 22:58:44 +01:00
if (!$(parent).hasClass(Dropdown._ClassName.OPEN)) {
continue
}
2012-01-28 10:35:13 +01:00
2015-01-03 22:58:44 +01:00
var hideEvent = $.Event(Dropdown._Event.HIDE, relatedTarget)
$(parent).trigger(hideEvent)
if (hideEvent.isDefaultPrevented()) {
continue
}
2012-12-08 00:37:32 +01:00
2015-01-03 22:58:44 +01:00
toggles[i].setAttribute('aria-expanded', 'false')
2015-01-03 22:58:44 +01:00
$(parent)
.removeClass(Dropdown._ClassName.OPEN)
.trigger(Dropdown._Event.HIDDEN, relatedTarget)
}
}
2015-01-03 22:58:44 +01:00
/**
* @param {Element} element
* @return {Element}
* @private
*/
Dropdown._getParentFromElement = function (element) {
var selector = Bootstrap.getSelectorFromElement(element)
2015-01-03 22:58:44 +01:00
if (selector) {
var parent = $(selector)[0]
}
2015-01-03 22:58:44 +01:00
return /** @type {Element} */ (parent || element.parentNode)
}
2015-01-03 22:58:44 +01:00
/**
* @param {Event} event
* @this {Element}
* @private
*/
Dropdown._dataApiKeydownHandler = function (event) {
if (!/(38|40|27|32)/.test(event.which) || /input|textarea/i.test(event.target.tagName)) {
return
}
2012-01-28 10:35:13 +01:00
2015-01-03 22:58:44 +01:00
event.preventDefault()
event.stopPropagation()
2015-01-03 22:58:44 +01:00
if (this.disabled || $(this).hasClass(Dropdown._ClassName.DISABLED)) {
return
}
2015-01-03 22:58:44 +01:00
var parent = Dropdown._getParentFromElement(this)
var isActive = $(parent).hasClass(Dropdown._ClassName.OPEN)
2015-01-03 22:58:44 +01:00
if ((!isActive && event.which != 27) || (isActive && event.which == 27)) {
if (event.which == 27) {
var toggle = $(parent).find(Dropdown._Selector.DATA_TOGGLE)[0]
$(toggle).trigger('focus')
}
2015-01-03 22:58:44 +01:00
$(this).trigger('click')
return
}
2015-01-03 22:58:44 +01:00
var items = $.makeArray($(Dropdown._Selector.VISIBLE_ITEMS))
2015-01-03 22:58:44 +01:00
items = items.filter(function (item) {
return item.offsetWidth || item.offsetHeight
})
2015-01-03 22:58:44 +01:00
if (!items.length) {
return
}
2015-01-03 22:58:44 +01:00
var index = items.indexOf(event.target)
2015-01-03 22:58:44 +01:00
if (event.which == 38 && index > 0) index-- // up
if (event.which == 40 && index < items.length - 1) index++ // down
if (!~index) index = 0
items[index].focus()
}
2015-01-03 22:58:44 +01:00
/**
* Toggles the dropdown
* @this {Element}
* @return {boolean|undefined}
*/
Dropdown.prototype['toggle'] = function () {
if (this.disabled || $(this).hasClass(Dropdown._ClassName.DISABLED)) {
return
}
2015-01-03 22:58:44 +01:00
var parent = Dropdown._getParentFromElement(this)
var isActive = $(parent).hasClass(Dropdown._ClassName.OPEN)
2015-01-03 22:58:44 +01:00
Dropdown._clearMenus()
2015-01-03 22:58:44 +01:00
if (isActive) {
return false
}
2015-01-03 22:58:44 +01:00
if ('ontouchstart' in document.documentElement && !$(parent).closest(Dropdown._Selector.NAVBAR_NAV).length) {
// if mobile we use a backdrop because click events don't delegate
var dropdown = document.createElement('div')
dropdown.className = Dropdown._ClassName.BACKDROP
this.parentNode.insertBefore(this, dropdown)
$(dropdown).on('click', Dropdown._clearMenus)
}
2015-01-03 22:58:44 +01:00
var relatedTarget = { 'relatedTarget': this }
var showEvent = $.Event(Dropdown._Event.SHOW, relatedTarget)
2015-01-03 22:58:44 +01:00
$(parent).trigger(showEvent)
2015-01-03 22:58:44 +01:00
if (showEvent.isDefaultPrevented()) {
return
}
2015-01-03 22:58:44 +01:00
this.focus()
this.setAttribute('aria-expanded', 'true')
2011-11-21 03:19:50 +01:00
2015-01-03 22:58:44 +01:00
$(parent).toggleClass(Dropdown._ClassName.OPEN)
2015-01-03 22:58:44 +01:00
$(parent).trigger(Dropdown._Event.SHOWN, relatedTarget)
2015-01-03 22:58:44 +01:00
return false
}
2011-08-28 06:46:50 +02:00
2015-01-03 22:58:44 +01:00
/**
* ------------------------------------------------------------------------
* jQuery Interface + noConflict implementaiton
* ------------------------------------------------------------------------
*/
2015-01-03 22:58:44 +01:00
/**
* @const
* @type {Function}
*/
$.fn[Dropdown._NAME] = Dropdown._jQueryInterface
2015-01-03 22:58:44 +01:00
/**
* @const
* @type {Function}
*/
$.fn[Dropdown._NAME]['Constructor'] = Dropdown
2015-01-03 22:58:44 +01:00
/**
* @const
* @type {Function}
*/
$.fn[Dropdown._NAME]['noConflict'] = function () {
$.fn[Dropdown._NAME] = Dropdown._JQUERY_NO_CONFLICT
return this
}
2015-01-03 22:58:44 +01:00
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2011-10-05 06:48:53 +02:00
2015-01-03 22:58:44 +01:00
$(document)
.on('click.bs.dropdown.data-api', Dropdown._clearMenus)
.on('click.bs.dropdown.data-api', Dropdown._Selector.FORM_CHILD, function (e) { e.stopPropagation() })
.on('click.bs.dropdown.data-api', Dropdown._Selector.DATA_TOGGLE, Dropdown.prototype['toggle'])
.on('keydown.bs.dropdown.data-api', Dropdown._Selector.DATA_TOGGLE, Dropdown._dataApiKeydownHandler)
.on('keydown.bs.dropdown.data-api', Dropdown._Selector.ROLE_MENU, Dropdown._dataApiKeydownHandler)
.on('keydown.bs.dropdown.data-api', Dropdown._Selector.ROLE_LISTBOX, Dropdown._dataApiKeydownHandler)