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 the menu.
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')) ? 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 classes = (classes) ? ' class="' + classes + '"' : '';
94 // <option> has to be disabled if the item is not a link.
95 disable = (path == '') || (path == '#') ? ' disabled="disabled"' : '',
97 subIndicator = 1 < level ? Array(level).join('-') + ' ' : '';
98 // Preparing the <option> element.
99 items += '<option value="' + path + '"' + classes + disable + '>' + subIndicator + $.trim(item.text()) +'</option>',
100 childUL = list.find('> ul');
101 // Using the function for the sub-menu of this item.
102 for (var u = 0; u < childUL.length; u++){
103 items += toSelect(childUL.eq(u), level + 1);
110 // Create the new version, hide the original.
111 function convert(menu){
112 var menuID = menu.attr('id'),
113 // Creating a refined version of the menu.
114 refinedMenu = refine(menu);
115 // Currently the plugin provides two reactions to small screens.
116 // Converting the menu to a <select> element, and converting to an accordion version of the menu.
117 if (options.type == 'accordion'){
119 toggleID = menuID + '-toggle',
120 accordionID = menuID + '-accordion';
121 // Making sure the accordion does not exist.
122 if ($('#' + accordionID).length == 0){
124 // Getting the style class.
125 styleClass = menu.attr('class').split(' ').filter(function(item){
126 return item.indexOf('sf-style-') > -1 ? item : '';
128 // Creating the accordion.
129 accordion = $(refinedMenu).attr('id', accordionID);
130 // Removing unnecessary classes.
131 accordion.removeClass('sf-horizontal sf-vertical sf-navbar sf-shadow sf-js-enabled');
132 // Adding necessary classes.
133 accordion.addClass('sf-accordion sf-hidden');
134 // Removing style attributes and any unnecessary class.
135 accordion.find('li').each(function(){
136 $(this).removeAttr('style').removeClass('sfHover').attr('id', $(this).attr('id') + '-accordion');
138 // Doing the same and making sure all the sub-menus are off-screen (hidden).
139 accordion.children('ul').removeAttr('style').not('.sf-hidden').addClass('sf-hidden');
140 // Creating the accordion toggle switch.
141 var toggle = '<div class="sf-accordion-toggle ' + styleClass + '"><a href="#" id="' + toggleID + '"><span>' + options.title + '</span></a></div>';
143 // Adding Expand\Collapse buttons if requested.
144 if (options.accordionButton == 2){
145 accordion.addClass('sf-accordion-with-buttons');
146 var parent = accordion.find('li.menuparent');
147 for (var i = 0; i < parent.length; i++){
148 parent.eq(i).prepend('<a href="#" class="sf-accordion-button">' + options.expandText + '</a>');
151 // Inserting the according and hiding the original menu.
152 menu.before(toggle).before(accordion).hide();
155 accordionElement = $('#' + accordionID),
156 // Deciding what should be used as accordion buttons.
157 buttonElement = (options.accordionButton < 2) ? 'a.menuparent,span.nolink.menuparent' : 'a.sf-accordion-button',
158 button = accordionElement.find(buttonElement);
160 // Attaching a click event to the toggle switch.
161 $('#' + toggleID).on('click', function(e){
162 // Preventing the click.
164 // Adding the sf-expanded class.
165 $(this).toggleClass('sf-expanded');
167 if (accordionElement.hasClass('sf-expanded')){
168 // If the accordion is already expanded:
169 // Hiding its expanded sub-menus and then the accordion itself as well.
170 accordionElement.add(accordionElement.find('li.sf-expanded')).removeClass('sf-expanded')
171 .end().children('ul').hide()
172 // This is a bit tricky, it's the same trick that has been in use in the main plugin for some time.
173 // Basically we'll add a class that keeps the sub-menu off-screen and still visible,
174 // and make it invisible and removing the class one moment before showing or hiding it.
175 // This helps screen reader software access all the menu items.
176 .end().hide().addClass('sf-hidden').show();
177 // Changing the caption of any existing accordion buttons to 'Expand'.
178 if (options.accordionButton == 2){
179 accordionElement.find('a.sf-accordion-button').text(options.expandText);
183 // But if it's collapsed,
184 accordionElement.addClass('sf-expanded').hide().removeClass('sf-hidden').show();
188 // Attaching a click event to the buttons.
189 button.on('click', function(e){
190 // Making sure the buttons does not exist already.
191 if ($(this).closest('li').children('ul').length > 0){
193 // Selecting the parent menu items.
194 var parent = $(this).closest('li');
195 // Creating and inserting Expand\Collapse buttons to the parent menu items,
196 // of course only if not already happened.
197 if (options.accordionButton == 1 && parent.children('a.menuparent,span.nolink.menuparent').length > 0 && parent.children('ul').children('li.sf-clone-parent').length == 0){
199 // Cloning the hyperlink of the parent menu item.
200 cloneLink = parent.children('a.menuparent,span.nolink.menuparent').clone();
201 // Removing unnecessary classes and element(s).
202 cloneLink.removeClass('menuparent sf-with-ul').children('.sf-sub-indicator').remove();
203 // Wrapping the hyerplinks in <li>.
204 cloneLink = $('<li class="sf-clone-parent" />').html(cloneLink);
205 // Adding a helper class and attaching them to the sub-menus.
206 parent.children('ul').addClass('sf-has-clone-parent').prepend(cloneLink);
208 // Once the button is clicked, collapse the sub-menu if it's expanded.
209 if (parent.hasClass('sf-expanded')){
210 parent.children('ul').slideUp('fast', function(){
211 // Doing the accessibility trick after hiding the sub-menu.
212 $(this).closest('li').removeClass('sf-expanded').end().addClass('sf-hidden').show();
214 // Changing the caption of the inserted Collapse link to 'Expand', if any is inserted.
215 if (options.accordionButton == 2 && parent.children('.sf-accordion-button').length > 0){
216 parent.children('.sf-accordion-button').text(options.expandText);
219 // Otherwise, expand the sub-menu.
221 // Doing the accessibility trick and then showing the sub-menu.
222 parent.children('ul').hide().removeClass('sf-hidden').slideDown('fast')
223 // Changing the caption of the inserted Expand link to 'Collape', if any is inserted.
224 .end().addClass('sf-expanded').children('a.sf-accordion-button').text(options.collapseText)
225 // Hiding any expanded sub-menu of the same level.
226 .end().siblings('li.sf-expanded').children('ul')
227 .slideUp('fast', function(){
228 // Doing the accessibility trick after hiding it.
229 $(this).closest('li').removeClass('sf-expanded').end().addClass('sf-hidden').show();
231 // Assuming Expand\Collapse buttons do exist, resetting captions, in those hidden sub-menus.
232 .parent().children('a.sf-accordion-button').text(options.expandText);
240 // Class names modification.
241 menuClone = menu.clone(), classes = (options.menuClasses) ? ((options.excludeClass_menu && menuClone.hasClass(options.excludeClass_menu)) ? menuClone.removeClass(options.excludeClass_menu).attr('class') : menuClone.attr('class')) : '',
242 classes = (options.includeClass_menu && !menuClone.hasClass(options.includeClass_menu)) ? ((options.menuClasses) ? menuClone.addClass(options.includeClass_menu).attr('class') : options.includeClass_menu) : classes,
243 classes = (classes) ? ' class="' + classes + '"' : '';
245 // Making sure the <select> element does not exist already.
246 if ($('#' + menuID + '-select').length == 0){
247 // Creating the <option> elements.
248 var newMenu = toSelect(refinedMenu, 1),
249 // Creating the <select> element and assigning an ID and class name.
250 selectList = $('<select' + classes + ' id="' + menuID + '-select"/>')
251 // Attaching the title and the items to the <select> element.
252 .html('<option>' + options.title + '</option>' + newMenu)
253 // Attaching an event then.
255 // Except for the first option that is the menu title and not a real menu item.
256 if ($('option:selected', this).index()){
257 window.location = selectList.val();
260 // Applying the addSelected option to it.
261 if (options.addSelected){
262 selectList.find('.active').attr('selected', !0);
264 // Finally inserting the <select> element into the document then hiding the original menu.
265 menu.before(selectList).hide();
270 // Turn everything back to normal.
271 function turnBack(menu){
273 id = '#' + menu.attr('id');
274 // Removing the small screen version.
275 $(id + '-' + options.type).remove();
276 // Removing the accordion toggle switch as well.
277 if (options.type == 'accordion'){
278 $(id + '-toggle').parent('div').remove();
280 // Remove inline CSS display property; less clear than simply using .show(), but respects stylesheet
281 $(id).css('display', '');
284 // Return original object to support chaining.
285 // Although this is unnecessary because of the way the module uses these plugins.
286 for (var s = 0; s < this.length; s++){
288 menu = $(this).eq(s),
290 // The rest is crystal clear, isn't it? :)
291 if (mode == 'always_active'){
294 else if (mode == 'window_width'){
295 var breakpoint = (options.breakpointUnit == 'em') ? (options.breakpoint * parseFloat($('body').css('font-size'))) : options.breakpoint,
296 windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
298 if ((typeof Modernizr === 'undefined' || typeof Modernizr.mq !== 'function') && windowWidth < breakpoint){
301 else if (typeof Modernizr !== 'undefined' && typeof Modernizr.mq === 'function' && Modernizr.mq('(max-width:' + (breakpoint - 1) + 'px)')) {
304 $(window).resize(function(){
306 timer = setTimeout(function(){
307 var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
308 if ((typeof Modernizr === 'undefined' || typeof Modernizr.mq !== 'function') && windowWidth < breakpoint){
311 else if (typeof Modernizr !== 'undefined' && typeof Modernizr.mq === 'function' && Modernizr.mq('(max-width:' + (breakpoint - 1) + 'px)')) {
320 else if (mode == 'useragent_custom'){
321 if (options.useragent != ''){
322 var ua = RegExp(options.useragent, 'i');
323 if (navigator.userAgent.match(ua)){
328 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)){