Bootstrap Submenu
Bootstrap Submenu
/**
* $.inArray: friends with IE8. Use Array.prototype.indexOf in future.
* $.proxy: friends with IE8. Use Function.prototype.bind in future.
*/
'use strict';
(function(factory) {
if (typeof define == 'function' && define.amd) {
// AMD. Register as an anonymous module
define(['jquery'], factory);
}
else if (typeof exports == 'object') {
// Node/CommonJS
module.exports = factory(require('jquery'));
}
else {
// Browser globals
factory(jQuery);
}
})(function($) {
function Item(element) {
this.$element = $(element);
this.$menu = this.$element.closest('.dropdown-menu');
this.$main = this.$menu.parent();
this.$items = this.$menu.children('.dropdown-submenu');
this.init();
}
Item.prototype = {
init: function() {
this.$element.on('keydown', $.proxy(this, 'keydown'));
},
close: function() {
this.$main.removeClass('open');
this.$items.trigger('hide.bs.submenu');
},
keydown: function(event) {
// 27: Esc
if (event.keyCode == 27) {
event.stopPropagation();
this.close();
this.$main.children('a, button').trigger('focus');
}
}
};
function SubmenuItem(element) {
this.$element = $(element);
this.$main = this.$element.parent();
this.$menu = this.$main.children('.dropdown-menu');
this.$subs = this.$main.siblings('.dropdown-submenu');
this.$items = this.$menu.children('.dropdown-submenu');
this.init();
}
$.extend(SubmenuItem.prototype, Item.prototype, {
init: function() {
this.$element.on({
click: $.proxy(this, 'click'),
keydown: $.proxy(this, 'keydown')
});
event.stopPropagation();
this.toggle();
},
hide: function(event) {
// Stop event bubbling
event.stopPropagation();
this.close();
},
open: function() {
this.$main.addClass('open');
this.$subs.trigger('hide.bs.submenu');
},
toggle: function() {
if (this.$main.hasClass('open')) {
this.close();
}
else {
this.open();
}
},
keydown: function(event) {
// 13: Return, 32: Spacebar
if (event.keyCode == 32) {
// Off vertical scrolling
event.preventDefault();
}
function Submenupicker(element) {
this.$element = $(element);
this.$main = this.$element.parent();
this.$menu = this.$main.children('.dropdown-menu');
this.$items = this.$menu.children('.dropdown-submenu');
this.init();
}
Submenupicker.prototype = {
init: function() {
this.$menu.off('keydown.bs.dropdown.data-api');
this.$menu.on('keydown', $.proxy(this, 'itemKeydown'));
event.stopPropagation();
$items.eq(index).trigger('focus');
}
}
};
return $elements.each(function() {
var data = $.data(this, 'bs.submenu');
if (!data) {
data = new Submenupicker(this);
$.fn.submenupicker.Constructor = Submenupicker;
$.fn.submenupicker.noConflict = function() {
$.fn.submenupicker = old;
return this;
};
return $.fn.submenupicker;
});