Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. /*!
  2. * Bootstrap-submenu v2.0.4 (https://vsn4ik.github.io/bootstrap-submenu/)
  3. * Copyright 2014-2016 Vasily A. (https://github.com/vsn4ik)
  4. * Licensed under the MIT license
  5. */
  6.  
  7. /**
  8. * $.inArray: friends with IE8. Use Array.prototype.indexOf in future.
  9. * $.proxy: friends with IE8. Use Function.prototype.bind in future.
  10. */
  11.  
  12. 'use strict';
  13.  
  14. (function(factory) {
  15. if (typeof define == 'function' && define.amd) {
  16. // AMD. Register as an anonymous module
  17. define(['jquery'], factory);
  18. }
  19. else if (typeof exports == 'object') {
  20. // Node/CommonJS
  21. module.exports = factory(require('jquery'));
  22. }
  23. else {
  24. // Browser globals
  25. factory(jQuery);
  26. }
  27. })(function($) {
  28. function Item(element) {
  29. this.$element = $(element);
  30. this.$menu = this.$element.closest('.dropdown-menu');
  31. this.$main = this.$menu.parent();
  32. this.$items = this.$menu.children('.dropdown-submenu');
  33.  
  34. this.init();
  35. }
  36.  
  37. Item.prototype = {
  38. init: function() {
  39. this.$element.on('keydown', $.proxy(this, 'keydown'));
  40. },
  41. close: function() {
  42. this.$main.removeClass('open');
  43. this.$items.trigger('hide.bs.submenu');
  44. },
  45. keydown: function(event) {
  46. // 27: Esc
  47.  
  48. if (event.keyCode == 27) {
  49. event.stopPropagation();
  50.  
  51. this.close();
  52. this.$main.children('a, button').trigger('focus');
  53. }
  54. }
  55. };
  56.  
  57. function SubmenuItem(element) {
  58. this.$element = $(element);
  59. this.$main = this.$element.parent();
  60. this.$menu = this.$main.children('.dropdown-menu');
  61. this.$subs = this.$main.siblings('.dropdown-submenu');
  62. this.$items = this.$menu.children('.dropdown-submenu');
  63.  
  64. this.init();
  65. }
  66.  
  67. $.extend(SubmenuItem.prototype, Item.prototype, {
  68. init: function() {
  69. this.$element.on({
  70. click: $.proxy(this, 'click'),
  71. keydown: $.proxy(this, 'keydown'),
  72. hover: $.proxy(this, 'hover'),
  73. });
  74.  
  75. this.$main.on('hide.bs.submenu', $.proxy(this, 'hide'));
  76. },
  77. click: function(event) {
  78. // Fix a[href="#"]. For community
  79. event.preventDefault();
  80.  
  81. event.stopPropagation();
  82.  
  83. this.toggle();
  84. },
  85. hover: function(event) {
  86. console.log('test hover');
  87. },
  88. hide: function(event) {
  89. // Stop event bubbling
  90. event.stopPropagation();
  91.  
  92. this.close();
  93. },
  94. open: function() {
  95. this.$main.addClass('open');
  96. this.$subs.trigger('hide.bs.submenu');
  97. },
  98. toggle: function() {
  99. if (this.$main.hasClass('open')) {
  100. this.close();
  101. }
  102. else {
  103. this.open();
  104. }
  105. },
  106. keydown: function(event) {
  107. // 13: Return, 32: Spacebar
  108.  
  109. if (event.keyCode == 32) {
  110. // Off vertical scrolling
  111. event.preventDefault();
  112. }
  113.  
  114. if ($.inArray(event.keyCode, [13, 32]) != -1) {
  115. this.toggle();
  116. }
  117. }
  118. });
  119.  
  120. function Submenupicker(element) {
  121. this.$element = $(element);
  122. this.$main = this.$element.parent();
  123. this.$menu = this.$main.children('.dropdown-menu');
  124. this.$items = this.$menu.children('.dropdown-submenu');
  125.  
  126. this.init();
  127. }
  128.  
  129. Submenupicker.prototype = {
  130. init: function() {
  131. this.$menu.off('keydown.bs.dropdown.data-api');
  132. this.$menu.on('keydown', $.proxy(this, 'itemKeydown'));
  133.  
  134. this.$menu.find('li > a').each(function() {
  135. new Item(this);
  136. });
  137.  
  138. this.$menu.find('.dropdown-submenu > a').each(function() {
  139. new SubmenuItem(this);
  140. });
  141.  
  142. this.$main.on('hidden.bs.dropdown', $.proxy(this, 'hidden'));
  143. },
  144. hidden: function() {
  145. this.$items.trigger('hide.bs.submenu');
  146. },
  147. itemKeydown: function(event) {
  148. // 38: Arrow up, 40: Arrow down
  149.  
  150. if ($.inArray(event.keyCode, [38, 40]) != -1) {
  151. // Off vertical scrolling
  152. event.preventDefault();
  153.  
  154. event.stopPropagation();
  155.  
  156. var $items = this.$menu.find('li:not(.disabled):visible > a');
  157. var index = $items.index(event.target);
  158.  
  159. if (event.keyCode == 38 && index !== 0) {
  160. index--;
  161. }
  162. else if (event.keyCode == 40 && index !== $items.length - 1) {
  163. index++;
  164. }
  165. else {
  166. return;
  167. }
  168.  
  169. $items.eq(index).trigger('focus');
  170. }
  171. }
  172. };
  173.  
  174. var old = $.fn.submenupicker;
  175.  
  176. // For AMD/Node/CommonJS used elements (optional)
  177. // http://learn.jquery.com/jquery-ui/environments/amd/
  178. $.fn.submenupicker = function(elements) {
  179. var $elements = this instanceof $ ? this : $(elements);
  180.  
  181. return $elements.each(function() {
  182. var data = $.data(this, 'bs.submenu');
  183.  
  184. if (!data) {
  185. data = new Submenupicker(this);
  186.  
  187. $.data(this, 'bs.submenu', data);
  188. }
  189. });
  190. };
  191.  
  192. $.fn.submenupicker.Constructor = Submenupicker;
  193. $.fn.submenupicker.noConflict = function() {
  194. $.fn.submenupicker = old;
  195. return this;
  196. };
  197.  
  198. return $.fn.submenupicker;
  199. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement