2 * sf-Touchscreen v1.3b - Provides touchscreen 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.sftouchscreen = function(options){
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(){
31 parent = item.closest('li');
32 if (options.disableHover){
33 parent.unbind('mouseenter mouseleave');
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){
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);
48 // No .toggle() here as it's not possible to reset it.
49 item.bind(eventHandler[0], function(event){
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;
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');
66 // Prevent the default action otherwise.
68 event.preventDefault();
69 item.addClass('sf-clicked');
70 parent.showSuperfishUl().siblings('li:has(ul)').hideSuperfishUl().find('.sf-clicked').removeClass('sf-clicked');
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();
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),
87 // The rest is crystal clear, isn't it? :)
88 if (mode == 'always_active'){
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,
95 if ((typeof Modernizr === 'undefined' || typeof Modernizr.mq !== 'function') && windowWidth < breakpoint){
98 else if (typeof Modernizr !== 'undefined' && typeof Modernizr.mq === 'function' && Modernizr.mq('(max-width:' + (breakpoint - 1) + 'px)')) {
101 $(window).resize(function(){
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){
108 else if (typeof Modernizr !== 'undefined' && typeof Modernizr.mq === 'function' && Modernizr.mq('(max-width:' + (breakpoint - 1) + 'px)')) {
114 else if (mode == 'useragent_custom'){
115 if (options.useragent != ''){
116 var ua = RegExp(options.useragent, 'i');
117 if (navigator.userAgent.match(ua)){
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)){