2 * sf-Smallscreen v1.3b - Provides small-screen compatibility for the jQuery Superfish plugin.
5 * Built as a part of the Superfish project for Drupal (http://drupal.org/project/superfish)
6 * Found any bug? have any cool ideas? contact me right away! http://drupal.org/user/619294/contact
8 * jQuery version: 1.3.x or higher.
10 * Dual licensed under the MIT and GPL licenses:
11 * http://www.opensource.org/licenses/mit-license.php
12 * http://www.gnu.org/licenses/gpl.html
16 $.fn.sfsmallscreen = function(options){
26 hyperlinkClasses: false,
27 excludeClass_menu: '',
28 excludeClass_hyperlink: '',
29 includeClass_menu: '',
30 includeClass_hyperlink: '',
33 collapseText: 'Collapse'
36 // We need to clean up the menu from anything unnecessary.
37 function refine(menu){
39 refined = menu.clone(),
40 // Things that should not be in the small-screen menus.
41 rm = refined.find('span.sf-sub-indicator'),
42 // This is a helper class for those who need to add extra markup that shouldn't exist
43 // in the small-screen versions.
44 rh = refined.find('.sf-smallscreen-remove'),
45 // Mega-menus has to be removed too.
46 mm = refined.find('ul.sf-multicolumn');
47 for (var a = 0; a < rh.length; a++){
48 rh.eq(a).replaceWith(rh.eq(a).html());
50 if (options.accordionButton == 2 || options.type == 'select'){
51 for (var b = 0; b < rm.length; b++){
56 mm.removeClass('sf-multicolumn');
57 var ol = refined.find('div.sf-multicolumn-column > ol');
58 for (var o = 0; o < ol.length; o++){
59 ol.eq(o).replaceWith('<ul>' + ol.eq(o).html() + '</ul>');
61 var elements = ['div.sf-multicolumn-column','.sf-multicolumn-wrapper > ol','li.sf-multicolumn-wrapper'];
62 for (var i = 0; i < elements.length; i++){
63 obj = refined.find(elements[i]);
64 for (var t = 0; t < obj.length; t++){
65 obj.eq(t).replaceWith(obj.eq(t).html());
68 refined.find('.sf-multicolumn-column').removeClass('sf-multicolumn-column');
70 refined.add(refined.find('*')).css({width:''});
74 // Creating <option> elements out of t
75 function toSelect(menu, level){
78 childLI = $(menu).children('li');
79 for (var a = 0; a < childLI.length; a++){
80 var list = childLI.eq(a), parent = list.children('a, span');
81 for (var b = 0; b < parent.length; b++){
84 path = item.is('a') ? item.attr('href') : '',
85 // Class names modification.
86 itemClone = item.clone(),
87 classes = (options.hyperlinkClasses) ? ((options.excludeClass_hyperlink && itemClone.hasClass(options.excludeClass_hyperlink)) ? itemClone.removeClass(options.excludeClass_hyperlink).attr('class') : itemClone.attr('class')) : '',
88 classes = (options.includeClass_hyperlink && !itemClone.hasClass(options.includeClass_hyperlink)) ? ((options.hyperlinkClasses) ? itemClone.addClass(options.includeClass_hyperlink).attr('class') : options.includeClass_hyperlink) : classes;
89 // Retaining the active class if requested.
90 if (options.addSelected && item.hasClass('active')){
93 // <option> has to be disabled if the item is not a link.
94 disable = item.is('span') || item.attr('href')=='#' ? ' disabled="disabled"' : '',
96 subIndicator = 1 < level ? Array(level).join('-') + ' ' : '';
97 // Preparing the <option> element.
98 items += '<option value="' + path + '" class="' + classes + '"' + disable + '>' + subIndicator + $.trim(item.text()) +'</option>',
99 childUL = list.find('> ul');
100 // Using the function for the sub-menu of this item.
101 for (var u = 0; u < childUL.length; u++){
102 items += toSelect(childUL.eq(u), level + 1);
109 // Create the new version, hide the original.
110 function convert(menu){
111 var menuID = menu.attr('id'),
112 // Creating a refined version of the menu.
113 refinedMenu = refine(menu);
114 // Currently the plugin provides two reactions to small screens.
115 // Converting the menu to a <select> element, and converting to an accordion version of the menu.
116 if (options.type == 'accordion'){
118 toggleID = menuID + '-toggle',
119 accordionID = menuID + '-accordion';
120 // Making sure the accordion does not exist.
121 if ($('#' + accordionID).length == 0){
123 // Getting the style class.
124 styleClass = menu.attr('class').split(' ').filter(function(item){
125 return item.indexOf('sf-style-') > -1 ? item : '';
127 // Creating the accordion.
128 accordion = $(refinedMenu).attr('id', accordionID);
129 // Removing unnecessary classes.
130 accordion.removeClass('sf-horizontal sf-vertical sf-navbar sf-shadow sf-js-enabled');
131 // Adding necessary classes.
132 accordion.addClass('sf-accordion sf-hidden');
133 // Removing style attributes and any unnecessary class.
134 accordion.find('li').each(function(){
135 $(this).removeAttr('style').removeClass('sfHover').attr('id', $(this).attr('id') + '-accordion');
137 // Doing the same and making sure all the sub-menus are off-screen (hidden).
138 accordion.find('ul').removeAttr('style').not('.sf-hidden').addClass('sf-hidden');
139 // Creating the accordion toggle switch.
140 var toggle = '<div class="sf-accordion-toggle ' + styleClass + '"><a href="#" id="' + toggleID + '"><span>' + options.title + '</span></a></div>';
142 // Adding Expand\Collapse buttons if requested.
143 if (options.accordionButton == 2){
144 accordion.addClass('sf-accordion-with-buttons');
145 var parent = accordion.find('li.menuparent');
146 for (var i = 0; i < parent.length; i++){
147 parent.eq(i).prepend('<a href="#" class="sf-accordion-button">' + options.expandText + '</a>');
150 // Inserting the according and hiding the original menu.
151 menu.before(toggle).before(accordion).hide();
154 accordionElement = $('#' + accordionID),
155 // Deciding what should be used as accordion buttons.
156 buttonElement = (options.accordionButton < 2) ? 'a.menuparent,span.nolink.menuparent' : 'a.sf-accordion-button',
157 button = accordionElement.find(buttonElement);
159 // Attaching a click event to the toggle switch.
160 $('#' + toggleID).on('click', function(e){
161 // Preventing the click.
163 // Adding the sf-expanded class.
164 $(this).toggleClass('sf-expanded');
166 if (accordionElement.hasClass('sf-expanded')){
167 // If the accordion is already expanded:
168 // Hiding its expanded sub-menus and then the accordion itself as well.
169 accordionElement.add(accordionElement.find('li.sf-expanded')).removeClass('sf-expanded')
170 .end().find('ul').hide()
171 // This is a bit tricky, it's the same trick that has been in use in the main plugin for some time.
172 // Basically we'll add a class that keeps the sub-menu off-screen and still visible,
173 // and make it invisible and removing the class one moment before showing or hiding it.
174 // This helps screen reader software access all the menu items.
175 .end().hide().addClass('sf-hidden').show();
176 // Changing the caption of any existing accordion buttons to 'Expand'.
177 if (options.accordionButton == 2){
178 accordionElement.find('a.sf-accordion-button').text(options.expandText);
182 // But if it's collapsed,
183 accordionElement.addClass('sf-expanded').hide().removeClass('sf-hidden').show();
187 // Attaching a click event to the buttons.
188 button.on('click', function(e){
189 // Making sure the buttons does not exist already.
190 if ($(this).closest('li').children('ul').length > 0){
192 // Selecting the parent menu items.
193 var parent = $(this).closest('li');
194 // Creating and inserting Expand\Collapse buttons to the parent menu items,
195 // of course only if not already happened.
196 if (options.accordionButton == 1 && parent.children('a.menuparent,span.nolink.menuparent').length > 0 && parent.children('ul').children('li.sf-clone-parent').length == 0){
198 // Cloning the hyperlink of the parent menu item.
199 cloneLink = parent.children('a.menuparent,span.nolink.menuparent').clone();
200 // Removing unnecessary classes and element(s).
201 cloneLink.removeClass('menuparent sf-with-ul').children('.sf-sub-indicator').remove();
202 // Wrapping the hyerplinks in <li>.
203 cloneLink = $('<li class="sf-clone-parent" />').html(cloneLink);
204 // Adding a helper class and attaching them to the sub-menus.
205 parent.children('ul').addClass('sf-has-clone-parent').prepend(cloneLink);
207 // Once the button is clicked, collapse the sub-menu if it's expanded.
208 if (parent.hasClass('sf-expanded')){
209 parent.children('ul').slideUp('fast', function(){
210 // Doing the accessibility trick after hiding the sub-menu.
211 $(this).closest('li').removeClass('sf-expanded').end().addClass('sf-hidden').show();
213 // Changing the caption of the inserted Collapse link to 'Expand', if any is inserted.
214 if (options.accordionButton == 2 && parent.children('.sf-accordion-button').length > 0){
215 parent.children('.sf-accordion-button').text(options.expandText);
218 // Otherwise, expand the sub-menu.
220 // Doing the accessibility trick and then showing the sub-menu.
221 parent.children('ul').hide().removeClass('sf-hidden').slideDown('fast')
222 // Changing the caption of the inserted Expand link to 'Collape', if any is inserted.
223 .end().addClass('sf-expanded').children('a.sf-accordion-button').text(options.collapseText)
224 // Hiding any expanded sub-menu of the same level.
225 .end().siblings('li.sf-expanded').children('ul')
226 .slideUp('fast', function(){
227 // Doing the accessibility trick after hiding it.
228 $(this).closest('li').removeClass('sf-expanded').end().addClass('sf-hidden').show();
230 // Assuming Expand\Collapse buttons do exist, resetting captions, in those hidden sub-menus.
231 .parent().children('a.sf-accordion-button').text(options.expandText);
239 // Class names modification.
240 menuClone = menu.clone(), classes = (options.menuClasses) ? ((options.excludeClass_menu && menuClone.hasClass(options.excludeClass_menu)) ? menuClone.removeClass(options.excludeClass_menu).attr('class') : menuClone.attr('class')) : '',
241 classes = (options.includeClass_menu && !menuClone.hasClass(options.includeClass_menu)) ? ((options.menuClasses) ? menuClone.addClass(options.includeClass_menu).attr('class') : options.includeClass_menu) : classes,
242 classes = (classes) ? ' class="' + classes + '"' : '';
244 // Making sure the <select> element does not exist already.
245 if ($('#' + menuID + '-select').length == 0){
246 // Creating the <option> elements.
247 var newMenu = toSelect(refinedMenu, 1),
248 // Creating the <select> element and assigning an ID and class name.
249 selectList = $('<select class="' + classes + '" id="' + menuID + '-select"/>')
250 // Attaching the title and the items to the <select> element.
251 .html('<option>' + options.title + '</option>' + newMenu)
252 // Attaching an event then.
254 // Except for the first option that is the menu title and not a real menu item.
255 if ($('option:selected', this).index()){
256 window.location = selectList.val();
259 // Applying the addSelected option to it.
260 if (options.addSelected){
261 selectList.find('.active').attr('selected', !0);
263 // Finally inserting the <select> element into the document then hiding the original menu.
264 menu.before(selectList).hide();
269 // Turn everything back to normal.
270 function turnBack(menu){
272 id = '#' + menu.attr('id');
273 // Removing the small screen version.
274 $(id + '-' + options.type).remove();
275 // Removing the accordion toggle switch as well.
276 if (options.type == 'accordion'){
277 $(id + '-toggle').parent('div').remove();
283 // Return original object to support chaining.
284 // Although this is unnecessary because of the way the module uses these plugins.
285 for (var s = 0; s < this.length; s++){
287 menu = $(this).eq(s),
289 // The rest is crystal clear, isn't it? :)
290 if (mode == 'always_active'){
293 else if (mode == 'window_width'){
294 var breakpoint = (options.breakpointUnit == 'em') ? (options.breakpoint * parseFloat($('body').css('font-size'))) : options.breakpoint,
295 windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
297 if ((typeof Modernizr === 'undefined' || typeof Modernizr.mq !== 'function') && windowWidth < breakpoint){
300 else if (typeof Modernizr !== 'undefined' && typeof Modernizr.mq === 'function' && Modernizr.mq('(max-width:' + (breakpoint - 1) + 'px)')) {
303 $(window).resize(function(){
305 timer = setTimeout(function(){
306 var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
307 if ((typeof Modernizr === 'undefined' || typeof Modernizr.mq !== 'function') && windowWidth < breakpoint){
310 else if (typeof Modernizr !== 'undefined' && typeof Modernizr.mq === 'function' && Modernizr.mq('(max-width:' + (breakpoint - 1) + 'px)')) {
319 else if (mode == 'useragent_custom'){
320 if (options.useragent != ''){
321 var ua = RegExp(options.useragent, 'i');
322 if (navigator.userAgent.match(ua)){
327 else if (mode == 'useragent_predefined' && navigator.userAgent.match(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od|ad)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i)){