8ddfede2edadd9c1964831317576137cff958d6b.svn-base 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. (function (global, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. define(['exports', 'module', './util'], factory);
  4. } else if (typeof exports !== 'undefined' && typeof module !== 'undefined') {
  5. factory(exports, module, require('./util'));
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(mod.exports, mod, global.Util);
  11. global.dropdown = mod.exports;
  12. }
  13. })(this, function (exports, module, _util) {
  14. 'use strict';
  15. 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; }; })();
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  17. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  18. var _Util = _interopRequireDefault(_util);
  19. /**
  20. * --------------------------------------------------------------------------
  21. * Bootstrap (v4.0.0): dropdown.js
  22. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  23. * --------------------------------------------------------------------------
  24. */
  25. var Dropdown = (function ($) {
  26. /**
  27. * ------------------------------------------------------------------------
  28. * Constants
  29. * ------------------------------------------------------------------------
  30. */
  31. var NAME = 'dropdown';
  32. var VERSION = '4.0.0';
  33. var DATA_KEY = 'bs.dropdown';
  34. var EVENT_KEY = '.' + DATA_KEY;
  35. var DATA_API_KEY = '.data-api';
  36. var JQUERY_NO_CONFLICT = $.fn[NAME];
  37. var Event = {
  38. HIDE: 'hide' + EVENT_KEY,
  39. HIDDEN: 'hidden' + EVENT_KEY,
  40. SHOW: 'show' + EVENT_KEY,
  41. SHOWN: 'shown' + EVENT_KEY,
  42. CLICK: 'click' + EVENT_KEY,
  43. CLICK_DATA_API: 'click' + EVENT_KEY + DATA_API_KEY,
  44. KEYDOWN_DATA_API: 'keydown' + EVENT_KEY + DATA_API_KEY
  45. };
  46. var ClassName = {
  47. BACKDROP: 'dropdown-backdrop',
  48. DISABLED: 'disabled',
  49. OPEN: 'open'
  50. };
  51. var Selector = {
  52. BACKDROP: '.dropdown-backdrop',
  53. DATA_TOGGLE: '[data-toggle="dropdown"]',
  54. FORM_CHILD: '.dropdown form',
  55. ROLE_MENU: '[role="menu"]',
  56. ROLE_LISTBOX: '[role="listbox"]',
  57. NAVBAR_NAV: '.navbar-nav',
  58. VISIBLE_ITEMS: '[role="menu"] li:not(.disabled) a, ' + '[role="listbox"] li:not(.disabled) a'
  59. };
  60. /**
  61. * ------------------------------------------------------------------------
  62. * Class Definition
  63. * ------------------------------------------------------------------------
  64. */
  65. var Dropdown = (function () {
  66. function Dropdown(element) {
  67. _classCallCheck(this, Dropdown);
  68. this._element = element;
  69. this._addEventListeners();
  70. }
  71. /**
  72. * ------------------------------------------------------------------------
  73. * Data Api implementation
  74. * ------------------------------------------------------------------------
  75. */
  76. // getters
  77. _createClass(Dropdown, [{
  78. key: 'toggle',
  79. // public
  80. value: function toggle() {
  81. if (this.disabled || $(this).hasClass(ClassName.DISABLED)) {
  82. return false;
  83. }
  84. var parent = Dropdown._getParentFromElement(this);
  85. var isActive = $(parent).hasClass(ClassName.OPEN);
  86. Dropdown._clearMenus();
  87. if (isActive) {
  88. return false;
  89. }
  90. if ('ontouchstart' in document.documentElement && !$(parent).closest(Selector.NAVBAR_NAV).length) {
  91. // if mobile we use a backdrop because click events don't delegate
  92. var dropdown = document.createElement('div');
  93. dropdown.className = ClassName.BACKDROP;
  94. $(dropdown).insertBefore(this);
  95. $(dropdown).on('click', Dropdown._clearMenus);
  96. }
  97. var relatedTarget = { relatedTarget: this };
  98. var showEvent = $.Event(Event.SHOW, relatedTarget);
  99. $(parent).trigger(showEvent);
  100. if (showEvent.isDefaultPrevented()) {
  101. return false;
  102. }
  103. this.focus();
  104. this.setAttribute('aria-expanded', 'true');
  105. $(parent).toggleClass(ClassName.OPEN);
  106. $(parent).trigger($.Event(Event.SHOWN, relatedTarget));
  107. return false;
  108. }
  109. }, {
  110. key: 'dispose',
  111. value: function dispose() {
  112. $.removeData(this._element, DATA_KEY);
  113. $(this._element).off(EVENT_KEY);
  114. this._element = null;
  115. }
  116. // private
  117. }, {
  118. key: '_addEventListeners',
  119. value: function _addEventListeners() {
  120. $(this._element).on(Event.CLICK, this.toggle);
  121. }
  122. // static
  123. }], [{
  124. key: '_jQueryInterface',
  125. value: function _jQueryInterface(config) {
  126. return this.each(function () {
  127. var data = $(this).data(DATA_KEY);
  128. if (!data) {
  129. $(this).data(DATA_KEY, data = new Dropdown(this));
  130. }
  131. if (typeof config === 'string') {
  132. data[config].call(this);
  133. }
  134. });
  135. }
  136. }, {
  137. key: '_clearMenus',
  138. value: function _clearMenus(event) {
  139. if (event && event.which === 3) {
  140. return;
  141. }
  142. var backdrop = $(Selector.BACKDROP)[0];
  143. if (backdrop) {
  144. backdrop.parentNode.removeChild(backdrop);
  145. }
  146. var toggles = $.makeArray($(Selector.DATA_TOGGLE));
  147. for (var i = 0; i < toggles.length; i++) {
  148. var _parent = Dropdown._getParentFromElement(toggles[i]);
  149. var relatedTarget = { relatedTarget: toggles[i] };
  150. if (!$(_parent).hasClass(ClassName.OPEN)) {
  151. continue;
  152. }
  153. if (event && event.type === 'click' && /input|textarea/i.test(event.target.tagName) && $.contains(_parent, event.target)) {
  154. continue;
  155. }
  156. var hideEvent = $.Event(Event.HIDE, relatedTarget);
  157. $(_parent).trigger(hideEvent);
  158. if (hideEvent.isDefaultPrevented()) {
  159. continue;
  160. }
  161. toggles[i].setAttribute('aria-expanded', 'false');
  162. $(_parent).removeClass(ClassName.OPEN).trigger($.Event(Event.HIDDEN, relatedTarget));
  163. }
  164. }
  165. }, {
  166. key: '_getParentFromElement',
  167. value: function _getParentFromElement(element) {
  168. var parent = undefined;
  169. var selector = _Util['default'].getSelectorFromElement(element);
  170. if (selector) {
  171. parent = $(selector)[0];
  172. }
  173. return parent || element.parentNode;
  174. }
  175. }, {
  176. key: '_dataApiKeydownHandler',
  177. value: function _dataApiKeydownHandler(event) {
  178. if (!/(38|40|27|32)/.test(event.which) || /input|textarea/i.test(event.target.tagName)) {
  179. return;
  180. }
  181. event.preventDefault();
  182. event.stopPropagation();
  183. if (this.disabled || $(this).hasClass(ClassName.DISABLED)) {
  184. return;
  185. }
  186. var parent = Dropdown._getParentFromElement(this);
  187. var isActive = $(parent).hasClass(ClassName.OPEN);
  188. if (!isActive && event.which !== 27 || isActive && event.which === 27) {
  189. if (event.which === 27) {
  190. var toggle = $(parent).find(Selector.DATA_TOGGLE)[0];
  191. $(toggle).trigger('focus');
  192. }
  193. $(this).trigger('click');
  194. return;
  195. }
  196. var items = $.makeArray($(Selector.VISIBLE_ITEMS));
  197. items = items.filter(function (item) {
  198. return item.offsetWidth || item.offsetHeight;
  199. });
  200. if (!items.length) {
  201. return;
  202. }
  203. var index = items.indexOf(event.target);
  204. if (event.which === 38 && index > 0) {
  205. // up
  206. index--;
  207. }
  208. if (event.which === 40 && index < items.length - 1) {
  209. // down
  210. index++;
  211. }
  212. if (! ~index) {
  213. index = 0;
  214. }
  215. items[index].focus();
  216. }
  217. }, {
  218. key: 'VERSION',
  219. get: function get() {
  220. return VERSION;
  221. }
  222. }]);
  223. return Dropdown;
  224. })();
  225. $(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) {
  226. e.stopPropagation();
  227. });
  228. /**
  229. * ------------------------------------------------------------------------
  230. * jQuery
  231. * ------------------------------------------------------------------------
  232. */
  233. $.fn[NAME] = Dropdown._jQueryInterface;
  234. $.fn[NAME].Constructor = Dropdown;
  235. $.fn[NAME].noConflict = function () {
  236. $.fn[NAME] = JQUERY_NO_CONFLICT;
  237. return Dropdown._jQueryInterface;
  238. };
  239. return Dropdown;
  240. })(jQuery);
  241. module.exports = Dropdown;
  242. });