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