dropdown.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*!
  2. * Bootstrap dropdown.js v4.6.1 (https://getbootstrap.com/)
  3. * Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery'), require('popper.js'), require('./util.js')) :
  8. typeof define === 'function' && define.amd ? define(['jquery', 'popper.js', './util'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Dropdown = factory(global.jQuery, global.Popper, global.Util));
  10. })(this, (function ($, Popper, Util) { 'use strict';
  11. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  12. var $__default = /*#__PURE__*/_interopDefaultLegacy($);
  13. var Popper__default = /*#__PURE__*/_interopDefaultLegacy(Popper);
  14. var Util__default = /*#__PURE__*/_interopDefaultLegacy(Util);
  15. function _defineProperties(target, props) {
  16. for (var i = 0; i < props.length; i++) {
  17. var descriptor = props[i];
  18. descriptor.enumerable = descriptor.enumerable || false;
  19. descriptor.configurable = true;
  20. if ("value" in descriptor) descriptor.writable = true;
  21. Object.defineProperty(target, descriptor.key, descriptor);
  22. }
  23. }
  24. function _createClass(Constructor, protoProps, staticProps) {
  25. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  26. if (staticProps) _defineProperties(Constructor, staticProps);
  27. return Constructor;
  28. }
  29. function _extends() {
  30. _extends = Object.assign || function (target) {
  31. for (var i = 1; i < arguments.length; i++) {
  32. var source = arguments[i];
  33. for (var key in source) {
  34. if (Object.prototype.hasOwnProperty.call(source, key)) {
  35. target[key] = source[key];
  36. }
  37. }
  38. }
  39. return target;
  40. };
  41. return _extends.apply(this, arguments);
  42. }
  43. /**
  44. * Constants
  45. */
  46. var NAME = 'dropdown';
  47. var VERSION = '4.6.1';
  48. var DATA_KEY = 'bs.dropdown';
  49. var EVENT_KEY = "." + DATA_KEY;
  50. var DATA_API_KEY = '.data-api';
  51. var JQUERY_NO_CONFLICT = $__default["default"].fn[NAME];
  52. var ESCAPE_KEYCODE = 27; // KeyboardEvent.which value for Escape (Esc) key
  53. var SPACE_KEYCODE = 32; // KeyboardEvent.which value for space key
  54. var TAB_KEYCODE = 9; // KeyboardEvent.which value for tab key
  55. var ARROW_UP_KEYCODE = 38; // KeyboardEvent.which value for up arrow key
  56. var ARROW_DOWN_KEYCODE = 40; // KeyboardEvent.which value for down arrow key
  57. var RIGHT_MOUSE_BUTTON_WHICH = 3; // MouseEvent.which value for the right button (assuming a right-handed mouse)
  58. var REGEXP_KEYDOWN = new RegExp(ARROW_UP_KEYCODE + "|" + ARROW_DOWN_KEYCODE + "|" + ESCAPE_KEYCODE);
  59. var CLASS_NAME_DISABLED = 'disabled';
  60. var CLASS_NAME_SHOW = 'show';
  61. var CLASS_NAME_DROPUP = 'dropup';
  62. var CLASS_NAME_DROPRIGHT = 'dropright';
  63. var CLASS_NAME_DROPLEFT = 'dropleft';
  64. var CLASS_NAME_MENURIGHT = 'dropdown-menu-right';
  65. var CLASS_NAME_POSITION_STATIC = 'position-static';
  66. var EVENT_HIDE = "hide" + EVENT_KEY;
  67. var EVENT_HIDDEN = "hidden" + EVENT_KEY;
  68. var EVENT_SHOW = "show" + EVENT_KEY;
  69. var EVENT_SHOWN = "shown" + EVENT_KEY;
  70. var EVENT_CLICK = "click" + EVENT_KEY;
  71. var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY;
  72. var EVENT_KEYDOWN_DATA_API = "keydown" + EVENT_KEY + DATA_API_KEY;
  73. var EVENT_KEYUP_DATA_API = "keyup" + EVENT_KEY + DATA_API_KEY;
  74. var SELECTOR_DATA_TOGGLE = '[data-toggle="dropdown"]';
  75. var SELECTOR_FORM_CHILD = '.dropdown form';
  76. var SELECTOR_MENU = '.dropdown-menu';
  77. var SELECTOR_NAVBAR_NAV = '.navbar-nav';
  78. var SELECTOR_VISIBLE_ITEMS = '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)';
  79. var PLACEMENT_TOP = 'top-start';
  80. var PLACEMENT_TOPEND = 'top-end';
  81. var PLACEMENT_BOTTOM = 'bottom-start';
  82. var PLACEMENT_BOTTOMEND = 'bottom-end';
  83. var PLACEMENT_RIGHT = 'right-start';
  84. var PLACEMENT_LEFT = 'left-start';
  85. var Default = {
  86. offset: 0,
  87. flip: true,
  88. boundary: 'scrollParent',
  89. reference: 'toggle',
  90. display: 'dynamic',
  91. popperConfig: null
  92. };
  93. var DefaultType = {
  94. offset: '(number|string|function)',
  95. flip: 'boolean',
  96. boundary: '(string|element)',
  97. reference: '(string|element)',
  98. display: 'string',
  99. popperConfig: '(null|object)'
  100. };
  101. /**
  102. * Class definition
  103. */
  104. var Dropdown = /*#__PURE__*/function () {
  105. function Dropdown(element, config) {
  106. this._element = element;
  107. this._popper = null;
  108. this._config = this._getConfig(config);
  109. this._menu = this._getMenuElement();
  110. this._inNavbar = this._detectNavbar();
  111. this._addEventListeners();
  112. } // Getters
  113. var _proto = Dropdown.prototype;
  114. // Public
  115. _proto.toggle = function toggle() {
  116. if (this._element.disabled || $__default["default"](this._element).hasClass(CLASS_NAME_DISABLED)) {
  117. return;
  118. }
  119. var isActive = $__default["default"](this._menu).hasClass(CLASS_NAME_SHOW);
  120. Dropdown._clearMenus();
  121. if (isActive) {
  122. return;
  123. }
  124. this.show(true);
  125. };
  126. _proto.show = function show(usePopper) {
  127. if (usePopper === void 0) {
  128. usePopper = false;
  129. }
  130. if (this._element.disabled || $__default["default"](this._element).hasClass(CLASS_NAME_DISABLED) || $__default["default"](this._menu).hasClass(CLASS_NAME_SHOW)) {
  131. return;
  132. }
  133. var relatedTarget = {
  134. relatedTarget: this._element
  135. };
  136. var showEvent = $__default["default"].Event(EVENT_SHOW, relatedTarget);
  137. var parent = Dropdown._getParentFromElement(this._element);
  138. $__default["default"](parent).trigger(showEvent);
  139. if (showEvent.isDefaultPrevented()) {
  140. return;
  141. } // Totally disable Popper for Dropdowns in Navbar
  142. if (!this._inNavbar && usePopper) {
  143. // Check for Popper dependency
  144. if (typeof Popper__default["default"] === 'undefined') {
  145. throw new TypeError('Bootstrap\'s dropdowns require Popper (https://popper.js.org)');
  146. }
  147. var referenceElement = this._element;
  148. if (this._config.reference === 'parent') {
  149. referenceElement = parent;
  150. } else if (Util__default["default"].isElement(this._config.reference)) {
  151. referenceElement = this._config.reference; // Check if it's jQuery element
  152. if (typeof this._config.reference.jquery !== 'undefined') {
  153. referenceElement = this._config.reference[0];
  154. }
  155. } // If boundary is not `scrollParent`, then set position to `static`
  156. // to allow the menu to "escape" the scroll parent's boundaries
  157. // https://github.com/twbs/bootstrap/issues/24251
  158. if (this._config.boundary !== 'scrollParent') {
  159. $__default["default"](parent).addClass(CLASS_NAME_POSITION_STATIC);
  160. }
  161. this._popper = new Popper__default["default"](referenceElement, this._menu, this._getPopperConfig());
  162. } // If this is a touch-enabled device we add extra
  163. // empty mouseover listeners to the body's immediate children;
  164. // only needed because of broken event delegation on iOS
  165. // https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html
  166. if ('ontouchstart' in document.documentElement && $__default["default"](parent).closest(SELECTOR_NAVBAR_NAV).length === 0) {
  167. $__default["default"](document.body).children().on('mouseover', null, $__default["default"].noop);
  168. }
  169. this._element.focus();
  170. this._element.setAttribute('aria-expanded', true);
  171. $__default["default"](this._menu).toggleClass(CLASS_NAME_SHOW);
  172. $__default["default"](parent).toggleClass(CLASS_NAME_SHOW).trigger($__default["default"].Event(EVENT_SHOWN, relatedTarget));
  173. };
  174. _proto.hide = function hide() {
  175. if (this._element.disabled || $__default["default"](this._element).hasClass(CLASS_NAME_DISABLED) || !$__default["default"](this._menu).hasClass(CLASS_NAME_SHOW)) {
  176. return;
  177. }
  178. var relatedTarget = {
  179. relatedTarget: this._element
  180. };
  181. var hideEvent = $__default["default"].Event(EVENT_HIDE, relatedTarget);
  182. var parent = Dropdown._getParentFromElement(this._element);
  183. $__default["default"](parent).trigger(hideEvent);
  184. if (hideEvent.isDefaultPrevented()) {
  185. return;
  186. }
  187. if (this._popper) {
  188. this._popper.destroy();
  189. }
  190. $__default["default"](this._menu).toggleClass(CLASS_NAME_SHOW);
  191. $__default["default"](parent).toggleClass(CLASS_NAME_SHOW).trigger($__default["default"].Event(EVENT_HIDDEN, relatedTarget));
  192. };
  193. _proto.dispose = function dispose() {
  194. $__default["default"].removeData(this._element, DATA_KEY);
  195. $__default["default"](this._element).off(EVENT_KEY);
  196. this._element = null;
  197. this._menu = null;
  198. if (this._popper !== null) {
  199. this._popper.destroy();
  200. this._popper = null;
  201. }
  202. };
  203. _proto.update = function update() {
  204. this._inNavbar = this._detectNavbar();
  205. if (this._popper !== null) {
  206. this._popper.scheduleUpdate();
  207. }
  208. } // Private
  209. ;
  210. _proto._addEventListeners = function _addEventListeners() {
  211. var _this = this;
  212. $__default["default"](this._element).on(EVENT_CLICK, function (event) {
  213. event.preventDefault();
  214. event.stopPropagation();
  215. _this.toggle();
  216. });
  217. };
  218. _proto._getConfig = function _getConfig(config) {
  219. config = _extends({}, this.constructor.Default, $__default["default"](this._element).data(), config);
  220. Util__default["default"].typeCheckConfig(NAME, config, this.constructor.DefaultType);
  221. return config;
  222. };
  223. _proto._getMenuElement = function _getMenuElement() {
  224. if (!this._menu) {
  225. var parent = Dropdown._getParentFromElement(this._element);
  226. if (parent) {
  227. this._menu = parent.querySelector(SELECTOR_MENU);
  228. }
  229. }
  230. return this._menu;
  231. };
  232. _proto._getPlacement = function _getPlacement() {
  233. var $parentDropdown = $__default["default"](this._element.parentNode);
  234. var placement = PLACEMENT_BOTTOM; // Handle dropup
  235. if ($parentDropdown.hasClass(CLASS_NAME_DROPUP)) {
  236. placement = $__default["default"](this._menu).hasClass(CLASS_NAME_MENURIGHT) ? PLACEMENT_TOPEND : PLACEMENT_TOP;
  237. } else if ($parentDropdown.hasClass(CLASS_NAME_DROPRIGHT)) {
  238. placement = PLACEMENT_RIGHT;
  239. } else if ($parentDropdown.hasClass(CLASS_NAME_DROPLEFT)) {
  240. placement = PLACEMENT_LEFT;
  241. } else if ($__default["default"](this._menu).hasClass(CLASS_NAME_MENURIGHT)) {
  242. placement = PLACEMENT_BOTTOMEND;
  243. }
  244. return placement;
  245. };
  246. _proto._detectNavbar = function _detectNavbar() {
  247. return $__default["default"](this._element).closest('.navbar').length > 0;
  248. };
  249. _proto._getOffset = function _getOffset() {
  250. var _this2 = this;
  251. var offset = {};
  252. if (typeof this._config.offset === 'function') {
  253. offset.fn = function (data) {
  254. data.offsets = _extends({}, data.offsets, _this2._config.offset(data.offsets, _this2._element));
  255. return data;
  256. };
  257. } else {
  258. offset.offset = this._config.offset;
  259. }
  260. return offset;
  261. };
  262. _proto._getPopperConfig = function _getPopperConfig() {
  263. var popperConfig = {
  264. placement: this._getPlacement(),
  265. modifiers: {
  266. offset: this._getOffset(),
  267. flip: {
  268. enabled: this._config.flip
  269. },
  270. preventOverflow: {
  271. boundariesElement: this._config.boundary
  272. }
  273. }
  274. }; // Disable Popper if we have a static display
  275. if (this._config.display === 'static') {
  276. popperConfig.modifiers.applyStyle = {
  277. enabled: false
  278. };
  279. }
  280. return _extends({}, popperConfig, this._config.popperConfig);
  281. } // Static
  282. ;
  283. Dropdown._jQueryInterface = function _jQueryInterface(config) {
  284. return this.each(function () {
  285. var data = $__default["default"](this).data(DATA_KEY);
  286. var _config = typeof config === 'object' ? config : null;
  287. if (!data) {
  288. data = new Dropdown(this, _config);
  289. $__default["default"](this).data(DATA_KEY, data);
  290. }
  291. if (typeof config === 'string') {
  292. if (typeof data[config] === 'undefined') {
  293. throw new TypeError("No method named \"" + config + "\"");
  294. }
  295. data[config]();
  296. }
  297. });
  298. };
  299. Dropdown._clearMenus = function _clearMenus(event) {
  300. if (event && (event.which === RIGHT_MOUSE_BUTTON_WHICH || event.type === 'keyup' && event.which !== TAB_KEYCODE)) {
  301. return;
  302. }
  303. var toggles = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLE));
  304. for (var i = 0, len = toggles.length; i < len; i++) {
  305. var parent = Dropdown._getParentFromElement(toggles[i]);
  306. var context = $__default["default"](toggles[i]).data(DATA_KEY);
  307. var relatedTarget = {
  308. relatedTarget: toggles[i]
  309. };
  310. if (event && event.type === 'click') {
  311. relatedTarget.clickEvent = event;
  312. }
  313. if (!context) {
  314. continue;
  315. }
  316. var dropdownMenu = context._menu;
  317. if (!$__default["default"](parent).hasClass(CLASS_NAME_SHOW)) {
  318. continue;
  319. }
  320. if (event && (event.type === 'click' && /input|textarea/i.test(event.target.tagName) || event.type === 'keyup' && event.which === TAB_KEYCODE) && $__default["default"].contains(parent, event.target)) {
  321. continue;
  322. }
  323. var hideEvent = $__default["default"].Event(EVENT_HIDE, relatedTarget);
  324. $__default["default"](parent).trigger(hideEvent);
  325. if (hideEvent.isDefaultPrevented()) {
  326. continue;
  327. } // If this is a touch-enabled device we remove the extra
  328. // empty mouseover listeners we added for iOS support
  329. if ('ontouchstart' in document.documentElement) {
  330. $__default["default"](document.body).children().off('mouseover', null, $__default["default"].noop);
  331. }
  332. toggles[i].setAttribute('aria-expanded', 'false');
  333. if (context._popper) {
  334. context._popper.destroy();
  335. }
  336. $__default["default"](dropdownMenu).removeClass(CLASS_NAME_SHOW);
  337. $__default["default"](parent).removeClass(CLASS_NAME_SHOW).trigger($__default["default"].Event(EVENT_HIDDEN, relatedTarget));
  338. }
  339. };
  340. Dropdown._getParentFromElement = function _getParentFromElement(element) {
  341. var parent;
  342. var selector = Util__default["default"].getSelectorFromElement(element);
  343. if (selector) {
  344. parent = document.querySelector(selector);
  345. }
  346. return parent || element.parentNode;
  347. } // eslint-disable-next-line complexity
  348. ;
  349. Dropdown._dataApiKeydownHandler = function _dataApiKeydownHandler(event) {
  350. // If not input/textarea:
  351. // - And not a key in REGEXP_KEYDOWN => not a dropdown command
  352. // If input/textarea:
  353. // - If space key => not a dropdown command
  354. // - If key is other than escape
  355. // - If key is not up or down => not a dropdown command
  356. // - If trigger inside the menu => not a dropdown command
  357. 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 || $__default["default"](event.target).closest(SELECTOR_MENU).length) : !REGEXP_KEYDOWN.test(event.which)) {
  358. return;
  359. }
  360. if (this.disabled || $__default["default"](this).hasClass(CLASS_NAME_DISABLED)) {
  361. return;
  362. }
  363. var parent = Dropdown._getParentFromElement(this);
  364. var isActive = $__default["default"](parent).hasClass(CLASS_NAME_SHOW);
  365. if (!isActive && event.which === ESCAPE_KEYCODE) {
  366. return;
  367. }
  368. event.preventDefault();
  369. event.stopPropagation();
  370. if (!isActive || event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE) {
  371. if (event.which === ESCAPE_KEYCODE) {
  372. $__default["default"](parent.querySelector(SELECTOR_DATA_TOGGLE)).trigger('focus');
  373. }
  374. $__default["default"](this).trigger('click');
  375. return;
  376. }
  377. var items = [].slice.call(parent.querySelectorAll(SELECTOR_VISIBLE_ITEMS)).filter(function (item) {
  378. return $__default["default"](item).is(':visible');
  379. });
  380. if (items.length === 0) {
  381. return;
  382. }
  383. var index = items.indexOf(event.target);
  384. if (event.which === ARROW_UP_KEYCODE && index > 0) {
  385. // Up
  386. index--;
  387. }
  388. if (event.which === ARROW_DOWN_KEYCODE && index < items.length - 1) {
  389. // Down
  390. index++;
  391. }
  392. if (index < 0) {
  393. index = 0;
  394. }
  395. items[index].focus();
  396. };
  397. _createClass(Dropdown, null, [{
  398. key: "VERSION",
  399. get: function get() {
  400. return VERSION;
  401. }
  402. }, {
  403. key: "Default",
  404. get: function get() {
  405. return Default;
  406. }
  407. }, {
  408. key: "DefaultType",
  409. get: function get() {
  410. return DefaultType;
  411. }
  412. }]);
  413. return Dropdown;
  414. }();
  415. /**
  416. * Data API implementation
  417. */
  418. $__default["default"](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) {
  419. event.preventDefault();
  420. event.stopPropagation();
  421. Dropdown._jQueryInterface.call($__default["default"](this), 'toggle');
  422. }).on(EVENT_CLICK_DATA_API, SELECTOR_FORM_CHILD, function (e) {
  423. e.stopPropagation();
  424. });
  425. /**
  426. * jQuery
  427. */
  428. $__default["default"].fn[NAME] = Dropdown._jQueryInterface;
  429. $__default["default"].fn[NAME].Constructor = Dropdown;
  430. $__default["default"].fn[NAME].noConflict = function () {
  431. $__default["default"].fn[NAME] = JQUERY_NO_CONFLICT;
  432. return Dropdown._jQueryInterface;
  433. };
  434. return Dropdown;
  435. }));
  436. //# sourceMappingURL=dropdown.js.map