Version 1
[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               window.location = item.attr('href');
55             }
56             // or collapse the sub-menu.
57             else if (options.behaviour == 1 || options.behaviour == 2){
58               event.preventDefault();
59               item.removeClass('sf-clicked');
60               parent.hideSuperfishUl().find('a,span.nolink').removeClass('sf-clicked');
61             }
62           }
63           // Prevent the default action otherwise.
64           else {
65             event.preventDefault();
66             item.addClass('sf-clicked');
67             parent.showSuperfishUl().siblings('li:has(ul)').hideSuperfishUl().find('.sf-clicked').removeClass('sf-clicked');
68           }
69         });
70       });
71
72       $(document).bind(eventHandler[1], function(event){
73         if (menu.not(event.target) && menu.has(event.target).length === 0){
74           menu.find('.sf-clicked').removeClass('sf-clicked');
75           menu.find('li:has(ul)').hideSuperfishUl();
76         }
77       });
78     }
79     // Return original object to support chaining.
80     // This is not necessary actually because of the way the module uses these plugins.
81     for (var b = 0; b < this.length; b++) {
82       var menu = $(this).eq(b),
83       mode = options.mode;
84       // The rest is crystal clear, isn't it? :)
85       if (mode == 'always_active'){
86         activate(menu);
87       }
88       else if (mode == 'window_width'){
89         var breakpoint = (options.breakpointUnit == 'em') ? (options.breakpoint * parseFloat($('body').css('font-size'))) : options.breakpoint,
90         windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
91         timer;
92         if ((typeof Modernizr === 'undefined' || typeof Modernizr.mq !== 'function') && windowWidth < breakpoint){
93           activate(menu);
94         }
95         else if (typeof Modernizr !== 'undefined' && typeof Modernizr.mq === 'function' && Modernizr.mq('(max-width:' + (breakpoint - 1) + 'px)')) {
96           activate(menu);
97         }
98         $(window).resize(function(){
99           clearTimeout(timer);
100           timer = setTimeout(function(){
101             var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
102             if ((typeof Modernizr === 'undefined' || typeof Modernizr.mq !== 'function') && windowWidth < breakpoint){
103               activate(menu);
104             }
105             else if (typeof Modernizr !== 'undefined' && typeof Modernizr.mq === 'function' && Modernizr.mq('(max-width:' + (breakpoint - 1) + 'px)')) {
106               activate(menu);
107             }
108           }, 50);
109         });
110       }
111       else if (mode == 'useragent_custom'){
112         if (options.useragent != ''){
113           var ua = RegExp(options.useragent, 'i');
114           if (navigator.userAgent.match(ua)){
115             activate(menu);
116           }
117         }
118       }
119       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)){
120         activate(menu);
121       }
122     }
123     return this;
124   }
125 })(jQuery);