Minor dependency updates
[yaffs-website] / vendor / mehrpadin / superfish / sftouchscreen.js
1 /*
2  * sf-Touchscreen v1.3b - Provides touchscreen 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.sftouchscreen = function(options){
17     options = $.extend({
18       mode: 'inactive',
19       breakpoint: 768,
20       breakpointUnit: 'px',
21       useragent: '',
22       behaviour: 2,
23       disableHover: false
24     }, options);
25
26     function activate(menu){
27       var eventHandler = (('ontouchstart' in window) || (window.DocumentTouch && document instanceof DocumentTouch)) ? ['click touchstart','mouseup touchend'] : ['click','mouseup'];
28       // Select hyperlinks from parent menu items.
29       menu.find('li:has(ul)').children('a,span.nolink').each(function(){
30         var item = $(this),
31         parent = item.closest('li');
32         if (options.disableHover){
33           parent.unbind('mouseenter mouseleave');
34         }
35         if (options.behaviour == 2){
36           if (parent.children('a.menuparent,span.nolink.menuparent').length > 0 && parent.children('ul').children('.sf-clone-parent').length == 0){
37             var
38             // Cloning the hyperlink of the parent menu item.
39             cloneLink = parent.children('a.menuparent,span.nolink.menuparent').clone(),
40             // Wrapping the hyerplinks in <li>.
41             cloneLink = $('<li class="sf-clone-parent" />').html(cloneLink);
42             // Removing unnecessary stuff.
43             cloneLink.find('.sf-sub-indicator').remove(),
44             // Adding a helper class and attaching them to the sub-menus.
45             parent.children('ul').addClass('sf-has-clone-parent').prepend(cloneLink);
46           }
47         }
48         // No .toggle() here as it's not possible to reset it.
49         item.bind(eventHandler[0], function(event){
50           // Already clicked?
51           if (item.hasClass('sf-clicked')){
52             // Depending on the preferred behaviour, either proceed to the URL.
53             if (options.behaviour == 0){
54               url = item.attr('href');
55               if (typeof(url) != 'undefined'){
56                 window.location = url;
57               }
58             }
59             // or collapse the sub-menu.
60             else if (options.behaviour == 1 || options.behaviour == 2){
61               event.preventDefault();
62               item.removeClass('sf-clicked');
63               parent.hideSuperfishUl().find('a,span.nolink').removeClass('sf-clicked');
64             }
65           }
66           // Prevent the default action otherwise.
67           else {
68             event.preventDefault();
69             item.addClass('sf-clicked');
70             parent.showSuperfishUl().siblings('li:has(ul)').hideSuperfishUl().find('.sf-clicked').removeClass('sf-clicked');
71           }
72         });
73       });
74
75       $(document).bind(eventHandler[1], function(event){
76         if (menu.not(event.target) && menu.has(event.target).length === 0){
77           menu.find('.sf-clicked').removeClass('sf-clicked');
78           menu.find('li:has(ul)').hideSuperfishUl();
79         }
80       });
81     }
82     // Return original object to support chaining.
83     // This is not necessary actually because of the way the module uses these plugins.
84     for (var b = 0; b < this.length; b++) {
85       var menu = $(this).eq(b),
86       mode = options.mode;
87       // The rest is crystal clear, isn't it? :)
88       if (mode == 'always_active'){
89         activate(menu);
90       }
91       else if (mode == 'window_width'){
92         var breakpoint = (options.breakpointUnit == 'em') ? (options.breakpoint * parseFloat($('body').css('font-size'))) : options.breakpoint,
93         windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
94         timer;
95         if ((typeof Modernizr === 'undefined' || typeof Modernizr.mq !== 'function') && windowWidth < breakpoint){
96           activate(menu);
97         }
98         else if (typeof Modernizr !== 'undefined' && typeof Modernizr.mq === 'function' && Modernizr.mq('(max-width:' + (breakpoint - 1) + 'px)')) {
99           activate(menu);
100         }
101         $(window).resize(function(){
102           clearTimeout(timer);
103           timer = setTimeout(function(){
104             var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
105             if ((typeof Modernizr === 'undefined' || typeof Modernizr.mq !== 'function') && windowWidth < breakpoint){
106               activate(menu);
107             }
108             else if (typeof Modernizr !== 'undefined' && typeof Modernizr.mq === 'function' && Modernizr.mq('(max-width:' + (breakpoint - 1) + 'px)')) {
109               activate(menu);
110             }
111           }, 50);
112         });
113       }
114       else if (mode == 'useragent_custom'){
115         if (options.useragent != ''){
116           var ua = RegExp(options.useragent, 'i');
117           if (navigator.userAgent.match(ua)){
118             activate(menu);
119           }
120         }
121       }
122       else if (mode == 'useragent_predefined' && navigator.userAgent.match(/(android|bb\d+|meego)|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|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)){
123         activate(menu);
124       }
125     }
126     return this;
127   }
128 })(jQuery);