Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / libraries / superfish / sfsmallscreen.js
1 /*
2  * sf-Smallscreen v1.3b - Provides small-screen compatibility for the jQuery Superfish plugin.
3  *
4  * Developer's note:
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
7  *
8  * jQuery version: 1.3.x or higher.
9  *
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
13   */
14
15 (function($){
16   $.fn.sfsmallscreen = function(options){
17     options = $.extend({
18       mode: 'inactive',
19       type: 'accordion',
20       breakpoint: 768,
21       breakpointUnit: 'px',
22       useragent: '',
23       title: '',
24       addSelected: false,
25       menuClasses: false,
26       hyperlinkClasses: false,
27       excludeClass_menu: '',
28       excludeClass_hyperlink: '',
29       includeClass_menu: '',
30       includeClass_hyperlink: '',
31       accordionButton: 1,
32       expandText: 'Expand',
33       collapseText: 'Collapse'
34     }, options);
35
36     // We need to clean up the menu from anything unnecessary.
37     function refine(menu){
38       var
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());
49       }
50       if (options.accordionButton == 2 || options.type == 'select'){
51         for (var b = 0; b < rm.length; b++){
52           rm.eq(b).remove();
53         }
54       }
55       if (mm.length > 0){
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>');
60         }
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());
66           }
67         }
68         refined.find('.sf-multicolumn-column').removeClass('sf-multicolumn-column');
69       }
70       refined.add(refined.find('*')).css({width:''});
71       return refined;
72     }
73
74     // Creating <option> elements out of the menu.
75     function toSelect(menu, level){
76       var
77       items = '',
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++){
82           var
83           item = parent.eq(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')){
91             classes += ' active';
92           }
93           classes = (classes) ? ' class="' + classes + '"' : '';
94           // <option> has to be disabled if the item is not a link.
95           disable = (path == '') || (path == '#') ? ' disabled="disabled"' : '',
96           // Crystal clear.
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);
104           }
105         }
106       }
107       return items;
108     }
109
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'){
118         var
119         toggleID = menuID + '-toggle',
120         accordionID = menuID + '-accordion';
121         // Making sure the accordion does not exist.
122         if ($('#' + accordionID).length == 0){
123           var
124           // Getting the style class.
125           styleClass = menu.attr('class').split(' ').filter(function(item){
126             return item.indexOf('sf-style-') > -1 ? item : '';
127           }),
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');
137           });
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>';
142
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>');
149             }
150           }
151           // Inserting the according and hiding the original menu.
152           menu.before(toggle).before(accordion).hide();
153
154           var
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);
159
160           // Attaching a click event to the toggle switch.
161           $('#' + toggleID).on('click', function(e){
162             // Preventing the click.
163             e.preventDefault();
164             // Adding the sf-expanded class.
165             $(this).toggleClass('sf-expanded');
166
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);
180               }
181             }
182             else {
183               // But if it's collapsed,
184               accordionElement.addClass('sf-expanded').hide().removeClass('sf-hidden').show();
185             }
186           });
187
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){
192               e.preventDefault();
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){
198                 var
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);
207               }
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();
213                 });
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);
217                 }
218               }
219               // Otherwise, expand the sub-menu.
220               else {
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();
230                 })
231                 // Assuming Expand\Collapse buttons do exist, resetting captions, in those hidden sub-menus.
232                 .parent().children('a.sf-accordion-button').text(options.expandText);
233               }
234             }
235           });
236         }
237       }
238       else {
239         var
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 + '"' : '';
244
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.
254           .change(function(){
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();
258             }
259           });
260           // Applying the addSelected option to it.
261           if (options.addSelected){
262             selectList.find('.active').attr('selected', !0);
263           }
264           // Finally inserting the <select> element into the document then hiding the original menu.
265           menu.before(selectList).hide();
266         }
267       }
268     }
269
270     // Turn everything back to normal.
271     function turnBack(menu){
272       var
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();
279       }
280       // Remove inline CSS display property; less clear than simply using .show(), but respects stylesheet
281       $(id).css('display', '');
282     }
283
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++){
287       var
288       menu = $(this).eq(s),
289       mode = options.mode;
290       // The rest is crystal clear, isn't it? :)
291       if (mode == 'always_active'){
292         convert(menu);
293       }
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,
297         timer;
298         if ((typeof Modernizr === 'undefined' || typeof Modernizr.mq !== 'function') && windowWidth < breakpoint){
299           convert(menu);
300         }
301         else if (typeof Modernizr !== 'undefined' && typeof Modernizr.mq === 'function' && Modernizr.mq('(max-width:' + (breakpoint - 1) + 'px)')) {
302           convert(menu);
303         }
304         $(window).resize(function(){
305           clearTimeout(timer);
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){
309               convert(menu);
310             }
311             else if (typeof Modernizr !== 'undefined' && typeof Modernizr.mq === 'function' && Modernizr.mq('(max-width:' + (breakpoint - 1) + 'px)')) {
312               convert(menu);
313             }
314             else {
315               turnBack(menu);
316             }
317           }, 50);
318         });
319       }
320       else if (mode == 'useragent_custom'){
321         if (options.useragent != ''){
322           var ua = RegExp(options.useragent, 'i');
323           if (navigator.userAgent.match(ua)){
324             convert(menu);
325           }
326         }
327       }
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)){
329         convert(menu);
330       }
331     }
332     return this;
333   }
334 })(jQuery);