1/* 2 * SmartMenus jQuery Bootstrap Addon - v0.3.0 3 * http://www.smartmenus.org/ 4 * 5 * Copyright Vasil Dinkov, Vadikom Web Ltd. 6 * http://vadikom.com/ 7 * 8 * Released under the MIT license: 9 * http://www.opensource.org/licenses/MIT 10 */ 11 12(function(factory) { 13 if (typeof define === 'function' && define.amd) { 14 // AMD 15 define(['jquery', 'jquery.smartmenus'], factory); 16 } else if (typeof module === 'object' && typeof module.exports === 'object') { 17 // CommonJS 18 module.exports = factory(require('jquery')); 19 } else { 20 // Global jQuery 21 factory(jQuery); 22 } 23} (function($) { 24 25 $.extend($.SmartMenus.Bootstrap = {}, { 26 keydownFix: false, 27 init: function() { 28 // init all navbars that don't have the "data-sm-skip" attribute set 29 var $navbars = $('ul.navbar-nav:not([data-sm-skip])'); 30 $navbars.each(function() { 31 var $this = $(this), 32 obj = $this.data('smartmenus'); 33 // if this navbar is not initialized 34 if (!obj) { 35 $this.smartmenus({ 36 37 // these are some good default options that should work for all 38 // you can, of course, tweak these as you like 39 subMenusSubOffsetX: 2, 40 subMenusSubOffsetY: -6, 41 subIndicators: false, 42 collapsibleShowFunction: null, 43 collapsibleHideFunction: null, 44 rightToLeftSubMenus: $this.hasClass('navbar-right'), 45 bottomToTopSubMenus: $this.closest('.navbar').hasClass('navbar-fixed-bottom') 46 }) 47 .bind({ 48 // set/unset proper Bootstrap classes for some menu elements 49 'show.smapi': function(e, menu) { 50 var $menu = $(menu), 51 $scrollArrows = $menu.dataSM('scroll-arrows'); 52 if ($scrollArrows) { 53 // they inherit border-color from body, so we can use its background-color too 54 $scrollArrows.css('background-color', $(document.body).css('background-color')); 55 } 56 $menu.parent().addClass('open'); 57 }, 58 'hide.smapi': function(e, menu) { 59 $(menu).parent().removeClass('open'); 60 } 61 }); 62 63 function onInit() { 64 // set Bootstrap's "active" class to SmartMenus "current" items (should someone decide to enable markCurrentItem: true) 65 $this.find('a.current').parent().addClass('active'); 66 // remove any Bootstrap required attributes that might cause conflicting issues with the SmartMenus script 67 $this.find('a.has-submenu').each(function() { 68 var $this = $(this); 69 if ($this.is('[data-toggle="dropdown"]')) { 70 $this.dataSM('bs-data-toggle-dropdown', true).removeAttr('data-toggle'); 71 } 72 if ($this.is('[role="button"]')) { 73 $this.dataSM('bs-role-button', true).removeAttr('role'); 74 } 75 }); 76 } 77 78 onInit(); 79 80 function onBeforeDestroy() { 81 $this.find('a.current').parent().removeClass('active'); 82 $this.find('a.has-submenu').each(function() { 83 var $this = $(this); 84 if ($this.dataSM('bs-data-toggle-dropdown')) { 85 $this.attr('data-toggle', 'dropdown').removeDataSM('bs-data-toggle-dropdown'); 86 } 87 if ($this.dataSM('bs-role-button')) { 88 $this.attr('role', 'button').removeDataSM('bs-role-button'); 89 } 90 }); 91 } 92 93 obj = $this.data('smartmenus'); 94 95 // custom "isCollapsible" method for Bootstrap 96 obj.isCollapsible = function() { 97 return !/^(left|right)$/.test(this.$firstLink.parent().css('float')); 98 }; 99 100 // custom "refresh" method for Bootstrap 101 obj.refresh = function() { 102 $.SmartMenus.prototype.refresh.call(this); 103 onInit(); 104 // update collapsible detection 105 detectCollapsible(true); 106 } 107 108 // custom "destroy" method for Bootstrap 109 obj.destroy = function(refresh) { 110 onBeforeDestroy(); 111 $.SmartMenus.prototype.destroy.call(this, refresh); 112 } 113 114 // keep Bootstrap's default behavior for parent items when the "data-sm-skip-collapsible-behavior" attribute is set to the ul.navbar-nav 115 // i.e. use the whole item area just as a sub menu toggle and don't customize the carets 116 if ($this.is('[data-sm-skip-collapsible-behavior]')) { 117 $this.bind({ 118 // click the parent item to toggle the sub menus (and reset deeper levels and other branches on click) 119 'click.smapi': function(e, item) { 120 if (obj.isCollapsible()) { 121 var $item = $(item), 122 $sub = $item.parent().dataSM('sub'); 123 if ($sub && $sub.dataSM('shown-before') && $sub.is(':visible')) { 124 obj.itemActivate($item); 125 obj.menuHide($sub); 126 return false;
127 } 128 } 129 } 130 }); 131 } 132 133 // onresize detect when the navbar becomes collapsible and add it the "sm-collapsible" class 134 var winW; 135 function detectCollapsible(force) { 136 var newW = obj.getViewportWidth(); 137 if (newW != winW || force) { 138 var $carets = $this.find('.caret'); 139 if (obj.isCollapsible()) { 140 $this.addClass('sm-collapsible'); 141 // set "navbar-toggle" class to carets (so they look like a button) if the "data-sm-skip-collapsible-behavior" attribute is not set to the ul.navbar-nav 142 if (!$this.is('[data-sm-skip-collapsible-behavior]')) { 143 $carets.addClass('navbar-toggle sub-arrow'); 144 } 145 } else { 146 $this.removeClass('sm-collapsible'); 147 if (!$this.is('[data-sm-skip-collapsible-behavior]')) { 148 $carets.removeClass('navbar-toggle sub-arrow'); 149 } 150 } 151 winW = newW; 152 } 153 }; 154 detectCollapsible(); 155 $(window).bind('resize.smartmenus' + obj.rootId, detectCollapsible); 156 } 157 }); 158 // keydown fix for Bootstrap 3.3.5+ conflict 159 if ($navbars.length && !$.SmartMenus.Bootstrap.keydownFix) { 160 // unhook BS keydown handler for all dropdowns 161 $(document).off('keydown.bs.dropdown.data-api', '.dropdown-menu'); 162 // restore BS keydown handler for dropdowns that are not inside SmartMenus navbars 163 if ($.fn.dropdown && $.fn.dropdown.Constructor) { 164 $(document).on('keydown.bs.dropdown.data-api', '.dropdown-menu:not([id^="sm-"])', $.fn.dropdown.Constructor.prototype.keydown); 165 } 166 $.SmartMenus.Bootstrap.keydownFix = true; 167 } 168 } 169 }); 170 171 // init ondomready 172 $($.SmartMenus.Bootstrap.init); 173 174 return $; 175}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.