supposition.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Supposition v0.2 - an optional enhancer for Superfish jQuery menu widget - LAST UPDATE: MARCH 23rd, 2011
  3. *
  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.
  6. *
  7. * Dec 28th, 2010 - Modified for the Superfish project for Drupal (http://drupal.org/project/superfish)
  8. *
  9. * jQuery version: 1.3.x or higher.
  10. *
  11. * Dual licensed under the MIT and GPL licenses:
  12. * http://www.opensource.org/licenses/mit-license.php
  13. * http://www.gnu.org/licenses/gpl.html
  14. */
  15. (function($){
  16. $.fn.supposition = function(){
  17. var $w = $(window), /*do this once instead of every onBeforeShow call*/
  18. _offset = function(dir) {
  19. return window[dir == 'y' ? 'pageYOffset' : 'pageXOffset']
  20. || document.documentElement && document.documentElement[dir=='y' ? 'scrollTop' : 'scrollLeft']
  21. || document.body[dir=='y' ? 'scrollTop' : 'scrollLeft'];
  22. },
  23. onHide = function(){
  24. this.css({Top:'',Right:'',Bottom:'',Left:''});
  25. },
  26. onBeforeShow = function(){
  27. this.each(function(){
  28. var $u = $(this);
  29. $u.css('display','block');
  30. var menuWidth = $u.width(),
  31. menuParentWidth = $u.closest('li').outerWidth(true),
  32. menuParentLeft = $u.closest('li').offset().left,
  33. totalRight = $w.width() + _offset('x'),
  34. menuRight = $u.offset().left + menuWidth,
  35. exactMenuWidth = (menuRight > (menuParentWidth + menuParentLeft)) ? menuWidth - (menuRight - (menuParentWidth + menuParentLeft)) : menuWidth;
  36. if ($u.parents('.sf-js-enabled').hasClass('rtl')) {
  37. if (menuParentLeft < exactMenuWidth) {
  38. $u.css('left', menuParentWidth + 'px');
  39. $u.css('right', 'auto');
  40. }
  41. }
  42. else {
  43. if (menuRight > totalRight && menuParentLeft > menuWidth) {
  44. $u.css('right', menuParentWidth + 'px');
  45. $u.css('left', 'auto');
  46. }
  47. }
  48. var windowHeight = $w.height(),
  49. offsetTop = $u.offset().top,
  50. menuParentHeight = $u.parent().outerHeight(true),
  51. menuHeight = $u.height(),
  52. baseline = windowHeight + _offset('y');
  53. var expandUp = ((offsetTop + menuHeight > baseline) && (offsetTop > menuHeight));
  54. if (expandUp) {
  55. $u.css('bottom', menuParentHeight + 'px');
  56. $u.css('top', 'auto');
  57. }
  58. $u.css('display','none');
  59. });
  60. };
  61. return this.each(function() {
  62. var o = $.fn.superfish.o[this.serial]; /* get this menu's options */
  63. /* if callbacks already set, store them */
  64. var _onBeforeShow = o.onBeforeShow,
  65. _onHide = o.onHide;
  66. $.extend($.fn.superfish.o[this.serial],{
  67. onBeforeShow: function() {
  68. onBeforeShow.call(this); /* fire our Supposition callback */
  69. _onBeforeShow.call(this); /* fire stored callbacks */
  70. },
  71. onHide: function() {
  72. onHide.call(this); /* fire our Supposition callback */
  73. _onHide.call(this); /* fire stored callbacks */
  74. }
  75. });
  76. });
  77. };
  78. })(jQuery);