2 * Supposition v0.2 - an optional enhancer for Superfish jQuery menu widget.
4 * Copyright (c) 2008 Joel Birch - based mostly on work by Jesse Klaasse and credit goes largely to him.
5 * Special thanks to Karl Swedberg for valuable input.
7 * Dual licensed under the MIT and GPL licenses:
8 * http://www.opensource.org/licenses/mit-license.php
9 * http://www.gnu.org/licenses/gpl.html
12 * This is not the original jQuery Supposition plugin.
13 * Please refer to the README for more information.
17 $.fn.supposition = function(){
18 var $w = $(window), /*do this once instead of every onBeforeShow call*/
19 _offset = function(dir) {
20 return window[dir == 'y' ? 'pageYOffset' : 'pageXOffset']
21 || document.documentElement && document.documentElement[dir=='y' ? 'scrollTop' : 'scrollLeft']
22 || document.body[dir=='y' ? 'scrollTop' : 'scrollLeft'];
25 this.css({bottom:''});
27 onBeforeShow = function(){
30 $u.css('display','block');
31 var $mul = $u.closest('.sf-menu'),
32 level = $u.parents('ul').length,
33 menuWidth = $u.width(),
34 menuParentWidth = $u.closest('li').outerWidth(true),
35 menuParentLeft = $u.closest('li').offset().left,
36 totalRight = $w.width() + _offset('x'),
37 menuRight = $u.offset().left + menuWidth,
38 exactMenuWidth = (menuRight > (menuParentWidth + menuParentLeft)) ? menuWidth - (menuRight - (menuParentWidth + menuParentLeft)) : menuWidth;
39 if ($u.parents('.sf-js-enabled').hasClass('rtl')) {
40 if (menuParentLeft < exactMenuWidth) {
41 if (($mul.hasClass('sf-horizontal') && level == 1) || ($mul.hasClass('sf-navbar') && level == 2)){
42 $u.css({left:0,right:'auto'});
45 $u.css({left:menuParentWidth + 'px',right:'auto'});
50 if (menuRight > totalRight && menuParentLeft > menuWidth) {
51 if (($mul.hasClass('sf-horizontal') && level == 1) || ($mul.hasClass('sf-navbar') && level == 2)){
52 $u.css({right:0,left:'auto'});
55 $u.css({right:menuParentWidth + 'px',left:'auto'});
59 var windowHeight = $w.height(),
60 offsetTop = $u.offset().top,
61 menuParentShadow = ($mul.hasClass('sf-shadow') && $u.css('padding-bottom').length > 0) ? parseInt($u.css('padding-bottom').slice(0,-2)) : 0,
62 menuParentHeight = ($mul.hasClass('sf-vertical')) ? '-' + menuParentShadow : $u.parent().outerHeight(true) - menuParentShadow,
63 menuHeight = $u.height(),
64 baseline = windowHeight + _offset('y');
65 var expandUp = ((offsetTop + menuHeight > baseline) && (offsetTop > menuHeight));
67 $u.css({bottom:menuParentHeight + 'px',top:'auto'});
69 $u.css('display','none');
73 return this.each(function() {
74 var o = $.fn.superfish.o[this.serial]; /* get this menu's options */
76 /* if callbacks already set, store them */
77 var _onBeforeShow = o.onBeforeShow,
80 $.extend($.fn.superfish.o[this.serial],{
81 onBeforeShow: function() {
82 onBeforeShow.call(this); /* fire our Supposition callback */
83 _onBeforeShow.call(this); /* fire stored callbacks */
86 onHide.call(this); /* fire our Supposition callback */
87 _onHide.call(this); /* fire stored callbacks */