2 * Bootstrap-submenu v2.0.3 (https://vsn4ik.github.io/bootstrap-submenu/)
3 * Copyright 2015 Vasily A. (https://github.com/vsn4ik)
4 * Licensed under the MIT license
8 * $.inArray: friends with IE8. Use Array.prototype.indexOf in future.
9 * $.proxy: friends with IE8. Use Function.prototype.bind in future.
15 if (typeof define == 'function' && define.amd) {
16 // AMD. Register as an anonymous module
17 define(['jquery'], factory);
19 else if (typeof exports == 'object') {
21 module.exports = factory(require('jquery'));
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');
39 this.$element.on('keydown', $.proxy(this, 'keydown'));
42 this.$main.removeClass('open');
43 this.$items.trigger('hide.bs.submenu');
45 keydown: function(event) {
48 if (event.keyCode == 27) {
49 event.stopPropagation();
52 this.$main.children('a, button').trigger('focus');
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');
67 $.extend(SubmenuItem.prototype, Item.prototype, {
70 click: $.proxy(this, 'click'),
71 keydown: $.proxy(this, 'keydown')
74 this.$main.on('hide.bs.submenu', $.proxy(this, 'hide'));
76 click: function(event) {
77 // Fix a[href="#"]. For community
78 event.preventDefault();
80 event.stopPropagation();
84 hide: function(event) {
85 // Stop event bubbling
86 event.stopPropagation();
91 this.$main.addClass('open');
92 this.$subs.trigger('hide.bs.submenu');
95 if (this.$main.hasClass('open')) {
102 keydown: function(event) {
103 // 13: Return, 32: Spacebar
105 if (event.keyCode == 32) {
106 // Off vertical scrolling
107 event.preventDefault();
110 if ($.inArray(event.keyCode, [13, 32]) != -1) {
116 function Submenupicker(element) {
117 this.$element = $(element);
118 this.$main = this.$element.parent();
119 this.$menu = this.$main.children('.dropdown-menu');
120 this.$items = this.$menu.children('.dropdown-submenu');
125 Submenupicker.prototype = {
127 this.$menu.off('keydown.bs.dropdown.data-api');
128 this.$menu.on('keydown', $.proxy(this, 'itemKeydown'));
130 this.$menu.find('li > a').each(function() {
134 this.$menu.find('.dropdown-submenu > a').each(function() {
135 new SubmenuItem(this);
138 this.$main.on('hidden.bs.dropdown', $.proxy(this, 'hidden'));
141 this.$items.trigger('hide.bs.submenu');
143 itemKeydown: function(event) {
144 // 38: Arrow up, 40: Arrow down
146 if ($.inArray(event.keyCode, [38, 40]) != -1) {
147 // Off vertical scrolling
148 event.preventDefault();
150 event.stopPropagation();
152 var $items = this.$menu.find('li:not(.disabled):visible > a');
153 var index = $items.index(event.target);
155 if (event.keyCode == 38 && index !== 0) {
158 else if (event.keyCode == 40 && index !== $items.length - 1) {
165 $items.eq(index).trigger('focus');
170 // For AMD/Node/CommonJS used elements (optional)
171 // http://learn.jquery.com/jquery-ui/environments/amd/
172 return $.fn.submenupicker = function(elements) {
173 var $elements = this instanceof $ ? this : $(elements);
175 return $elements.each(function() {
176 var data = $.data(this, 'bs.submenu');
179 data = new Submenupicker(this);
181 $.data(this, 'bs.submenu', data);