[ Index ]
 

Code source de e107 0.7.8

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/e107_files/ -> nav_menu.js (source)

   1  //<![CDATA[
   2  
   3  //*****************************************************************************
   4  // Do not remove this notice.
   5  //
   6  // Copyright 2000-2004 by Mike Hall.
   7  // See http://www.brainjar.com for terms of use.
   8  //*****************************************************************************
   9  
  10  //----------------------------------------------------------------------------
  11  // Code to determine the browser and version.
  12  //----------------------------------------------------------------------------
  13  
  14  function Browser() {
  15  
  16    var ua, s, i;
  17  
  18    this.isIE    = false;  // Internet Explorer
  19    this.isOP    = false;  // Opera
  20    this.isNS    = false;  // Netscape
  21    this.version = null;
  22  
  23    ua = navigator.userAgent;
  24  
  25    s = "Opera";
  26    if ((i = ua.indexOf(s)) >= 0) {
  27      this.isOP = true;
  28      this.version = parseFloat(ua.substr(i + s.length));
  29      return;
  30    }
  31  
  32    s = "Netscape6/";
  33    if ((i = ua.indexOf(s)) >= 0) {
  34      this.isNS = true;
  35      this.version = parseFloat(ua.substr(i + s.length));
  36      return;
  37    }
  38  
  39    // Treat any other "Gecko" browser as Netscape 6.1.
  40  
  41    s = "Gecko";
  42    if ((i = ua.indexOf(s)) >= 0) {
  43      this.isNS = true;
  44      this.version = 6.1;
  45      return;
  46    }
  47  
  48    s = "MSIE";
  49    if ((i = ua.indexOf(s))) {
  50      this.isIE = true;
  51      this.version = parseFloat(ua.substr(i + s.length));
  52      return;
  53    }
  54  }
  55  
  56  var browser = new Browser();
  57  
  58  //----------------------------------------------------------------------------
  59  // Code for handling the menu bar and active button.
  60  //----------------------------------------------------------------------------
  61  
  62  var activeButton = null;
  63  
  64  // Capture mouse clicks on the page so any active button can be
  65  // deactivated.
  66  
  67  if (browser.isIE)
  68    document.onmousedown = pageMousedown;
  69  else
  70    document.addEventListener("mousedown", pageMousedown, true);
  71  
  72  function pageMousedown(event) {
  73  
  74    var el;
  75  
  76    // If there is no active button, exit.
  77  
  78    if (activeButton == null)
  79      return;
  80  
  81    // Find the element that was clicked on.
  82  
  83    if (browser.isIE)
  84      el = window.event.srcElement;
  85    else
  86      el = (event.target.tagName ? event.target : event.target.parentNode);
  87  
  88    // If the active button was clicked on, exit.
  89  
  90    if (el == activeButton)
  91      return;
  92  
  93    // If the element is not part of a menu, reset and clear the active
  94    // button.
  95  
  96    if (getContainerWith(el, "DIV", "menu") == null) {
  97      resetButton(activeButton);
  98      activeButton = null;
  99    }
 100  }
 101  
 102  function buttonClick(event, menuId) {
 103  
 104    var button;
 105  
 106    // Get the target button element.
 107  
 108    if (browser.isIE)
 109      button = window.event.srcElement;
 110    else
 111      button = event.currentTarget;
 112  
 113    // Blur focus from the link to remove that annoying outline.
 114  
 115    button.blur();
 116  
 117    // Associate the named menu to this button if not already done.
 118    // Additionally, initialize menu display.
 119  
 120    if (button.menu == null) {
 121      button.menu = document.getElementById(menuId);
 122      if (button.menu.isInitialized == null)
 123        menuInit(button.menu);
 124    }
 125  
 126    // Reset the currently active button, if any.
 127  
 128    if (activeButton != null)
 129      resetButton(activeButton);
 130  
 131    // Activate this button, unless it was the currently active one.
 132  
 133    if (button != activeButton) {
 134      depressButton(button);
 135      activeButton = button;
 136    }
 137    else
 138      activeButton = null;
 139  
 140    return false;
 141  }
 142  
 143  function buttonMouseover(event, menuId) {
 144  
 145    var button;
 146  
 147    // Find the target button element.
 148  
 149    if (browser.isIE)
 150      button = window.event.srcElement;
 151    else
 152      button = event.currentTarget;
 153  
 154    // If any other button menu is active, make this one active instead.
 155  
 156    if (activeButton != null && activeButton != button)
 157      buttonClick(event, menuId);
 158  }
 159  
 160  function depressButton(button) {
 161  
 162    var x, y;
 163  
 164    // Update the button's style class to make it look like it's
 165    // depressed.
 166  
 167    button.className += " menuButtonActive";
 168  
 169    // Position the associated drop down menu under the button and
 170    // show it.
 171  
 172    x = getPageOffsetLeft(button);
 173    y = getPageOffsetTop(button) + button.offsetHeight;
 174  
 175    // For IE, adjust position.
 176  
 177    if (browser.isIE) {
 178      x += button.offsetParent.clientLeft;
 179      y += button.offsetParent.clientTop - 1;
 180    }
 181  
 182    button.menu.style.left = x + "px";
 183    button.menu.style.top  = y + "px";
 184    button.menu.style.visibility = "visible";
 185  
 186    // For IE; size, position and show the menu's IFRAME as well.
 187  
 188    if (button.menu.iframeEl != null)
 189    {
 190      button.menu.iframeEl.style.left = button.menu.style.left;
 191      button.menu.iframeEl.style.top  = button.menu.style.top;
 192      button.menu.iframeEl.style.width  = button.menu.offsetWidth + "px";
 193      button.menu.iframeEl.style.height = button.menu.offsetHeight + "px";
 194      button.menu.iframeEl.style.display = "";
 195    }
 196  }
 197  
 198  function resetButton(button) {
 199  
 200    // Restore the button's style class.
 201  
 202    removeClassName(button, "menuButtonActive");
 203  
 204    // Hide the button's menu, first closing any sub menus.
 205  
 206    if (button.menu != null) {
 207      closeSubMenu(button.menu);
 208      button.menu.style.visibility = "hidden";
 209  
 210      // For IE, hide menu's IFRAME as well.
 211  
 212      if (button.menu.iframeEl != null)
 213        button.menu.iframeEl.style.display = "none";
 214    }
 215  }
 216  
 217  //----------------------------------------------------------------------------
 218  // Code to handle the menus and sub menus.
 219  //----------------------------------------------------------------------------
 220  
 221  function menuMouseover(event) {
 222  
 223    var menu;
 224  
 225    // Find the target menu element.
 226  
 227    if (browser.isIE)
 228      menu = getContainerWith(window.event.srcElement, "DIV", "menu");
 229    else
 230      menu = event.currentTarget;
 231  
 232    // Close any active sub menu.
 233  
 234    if (menu.activeItem != null)
 235      closeSubMenu(menu);
 236  }
 237  
 238  function menuItemMouseover(event, menuId) {
 239  
 240    var item, menu, x, y;
 241  
 242    // Find the target item element and its parent menu element.
 243  
 244    if (browser.isIE)
 245      item = getContainerWith(window.event.srcElement, "A", "menuItem");
 246    else
 247      item = event.currentTarget;
 248    menu = getContainerWith(item, "DIV", "menu");
 249  
 250    // Close any active sub menu and mark this one as active.
 251  
 252    if (menu.activeItem != null)
 253      closeSubMenu(menu);
 254    menu.activeItem = item;
 255  
 256    // Highlight the item element.
 257  
 258    item.className += " menuItemHighlight";
 259  
 260    // Initialize the sub menu, if not already done.
 261  
 262    if (item.subMenu == null) {
 263      item.subMenu = document.getElementById(menuId);
 264      if (item.subMenu.isInitialized == null)
 265        menuInit(item.subMenu);
 266    }
 267  
 268    // Get position for submenu based on the menu item.
 269  
 270    x = getPageOffsetLeft(item) + item.offsetWidth;
 271    y = getPageOffsetTop(item);
 272  
 273    // Adjust position to fit in view.
 274  
 275    var maxX, maxY;
 276  
 277    if (browser.isIE) {
 278      maxX = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) +
 279        (document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth);
 280      maxY = Math.max(document.documentElement.scrollTop, document.body.scrollTop) +
 281        (document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
 282    }
 283    if (browser.isOP) {
 284      maxX = document.documentElement.scrollLeft + window.innerWidth;
 285      maxY = document.documentElement.scrollTop  + window.innerHeight;
 286    }
 287    if (browser.isNS) {
 288      maxX = window.scrollX + window.innerWidth;
 289      maxY = window.scrollY + window.innerHeight;
 290    }
 291    maxX -= item.subMenu.offsetWidth;
 292    maxY -= item.subMenu.offsetHeight;
 293  
 294    if (x > maxX)
 295      x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth
 296        + (menu.offsetWidth - item.offsetWidth));
 297    y = Math.max(0, Math.min(y, maxY));
 298  
 299    // Position and show it.
 300  
 301    item.subMenu.style.left       = x + "px";
 302    item.subMenu.style.top        = y + "px";
 303    item.subMenu.style.visibility = "visible";
 304  
 305    // For IE; size, position and show the menu's IFRAME as well.
 306  
 307    if (item.subMenu.iframeEl != null)
 308    {
 309      item.subMenu.iframeEl.style.left    = item.subMenu.style.left;
 310      item.subMenu.iframeEl.style.top     = item.subMenu.style.top;
 311      item.subMenu.iframeEl.style.width   = item.subMenu.offsetWidth + "px";
 312      item.subMenu.iframeEl.style.height  = item.subMenu.offsetHeight + "px";
 313      item.subMenu.iframeEl.style.display = "";
 314    }
 315  
 316    // Stop the event from bubbling.
 317  
 318    if (browser.isIE)
 319      window.event.cancelBubble = true;
 320    else
 321      event.stopPropagation();
 322  }
 323  
 324  function closeSubMenu(menu) {
 325  
 326    if (menu == null || menu.activeItem == null)
 327      return;
 328  
 329    // Recursively close any sub menus.
 330  
 331    if (menu.activeItem.subMenu != null) {
 332      closeSubMenu(menu.activeItem.subMenu);
 333  
 334  
 335      // Hide the sub menu.
 336      menu.activeItem.subMenu.style.visibility = "hidden";
 337  
 338      // For IE, hide the sub menu's IFRAME as well.
 339  
 340      if (menu.activeItem.subMenu.iframeEl != null)
 341        menu.activeItem.subMenu.iframeEl.style.display = "none";
 342  
 343      menu.activeItem.subMenu = null;
 344    }
 345  
 346    // Deactivate the active menu item.
 347  
 348    removeClassName(menu.activeItem, "menuItemHighlight");
 349    menu.activeItem = null;
 350  }
 351  
 352  //----------------------------------------------------------------------------
 353  // Code to initialize menus.
 354  //----------------------------------------------------------------------------
 355  
 356  function menuInit(menu) {
 357  
 358    var itemList, spanList;
 359    var textEl, arrowEl;
 360    var itemWidth;
 361    var w, dw;
 362    var i, j;
 363  
 364    // For IE, replace arrow characters.
 365  
 366    if (browser.isIE) {
 367      menu.style.lineHeight = "2.5ex";
 368      spanList = menu.getElementsByTagName("SPAN");
 369      for (i = 0; i < spanList.length; i++)
 370        if (hasClassName(spanList[i], "menuItemArrow")) {
 371          spanList[i].style.fontFamily = "Webdings";
 372          spanList[i].firstChild.nodeValue = "4";
 373        }
 374    }
 375  
 376    // Find the width of a menu item.
 377  
 378    itemList = menu.getElementsByTagName("A");
 379    if (itemList.length > 0)
 380      itemWidth = itemList[0].offsetWidth;
 381    else
 382      return;
 383  
 384    // For items with arrows, add padding to item text to make the
 385    // arrows flush right.
 386  
 387    for (i = 0; i < itemList.length; i++) {
 388      spanList = itemList[i].getElementsByTagName("SPAN");
 389      textEl  = null;
 390      arrowEl = null;
 391      for (j = 0; j < spanList.length; j++) {
 392        if (hasClassName(spanList[j], "menuItemText"))
 393          textEl = spanList[j];
 394        if (hasClassName(spanList[j], "menuItemArrow")) {
 395          arrowEl = spanList[j];
 396        }
 397      }
 398      if (textEl != null && arrowEl != null) {
 399        textEl.style.paddingRight = (itemWidth 
 400          - (textEl.offsetWidth + arrowEl.offsetWidth)) + "px";
 401  
 402        // For Opera, remove the negative right margin to fix a display bug.
 403  
 404        if (browser.isOP)
 405          arrowEl.style.marginRight = "0px";
 406      }
 407    }
 408  
 409    // Fix IE hover problem by setting an explicit width on first item of
 410    // the menu.
 411  
 412    if (browser.isIE) {
 413      w = itemList[0].offsetWidth;
 414      itemList[0].style.width = w + "px";
 415      dw = itemList[0].offsetWidth - w;
 416      w -= dw;
 417      itemList[0].style.width = w + "px";
 418    }
 419  
 420    // Fix the IE display problem (SELECT elements and other windowed controls
 421    // overlaying the menu) by adding an IFRAME under the menu.
 422  
 423    if (browser.isIE) {
 424      menu.iframeEl = menu.parentNode.insertBefore(document.createElement("IFRAME"), menu);
 425      menu.iframeEl.style.display = "none";
 426      menu.iframeEl.style.position = "absolute";
 427    }
 428  
 429    // Mark menu as initialized.
 430  
 431    menu.isInitialized = true;
 432  }
 433  
 434  //----------------------------------------------------------------------------
 435  // General utility functions.
 436  //----------------------------------------------------------------------------
 437  
 438  function getContainerWith(node, tagName, className) {
 439  
 440    // Starting with the given node, find the nearest containing element
 441    // with the specified tag name and style class.
 442  
 443    while (node != null) {
 444      if (node.tagName != null && node.tagName == tagName &&
 445          hasClassName(node, className))
 446        return node;
 447      node = node.parentNode;
 448    }
 449  
 450    return node;
 451  }
 452  
 453  function hasClassName(el, name) {
 454  
 455    var i, list;
 456  
 457    // Return true if the given element currently has the given class
 458    // name.
 459  
 460    list = el.className.split(" ");
 461    for (i = 0; i < list.length; i++)
 462      if (list[i] == name)
 463        return true;
 464  
 465    return false;
 466  }
 467  
 468  function removeClassName(el, name) {
 469  
 470    var i, curList, newList;
 471  
 472    if (el.className == null)
 473      return;
 474  
 475    // Remove the given class name from the element's className property.
 476  
 477    newList = new Array();
 478    curList = el.className.split(" ");
 479    for (i = 0; i < curList.length; i++)
 480      if (curList[i] != name)
 481        newList.push(curList[i]);
 482    el.className = newList.join(" ");
 483  }
 484  
 485  function getPageOffsetLeft(el) {
 486  
 487    var x;
 488  
 489    // Return the x coordinate of an element relative to the page.
 490  
 491    x = el.offsetLeft;
 492    if (el.offsetParent != null)
 493      x += getPageOffsetLeft(el.offsetParent);
 494  
 495    return x;
 496  }
 497  
 498  function getPageOffsetTop(el) {
 499  
 500    var y;
 501  
 502    // Return the x coordinate of an element relative to the page.
 503  
 504    y = el.offsetTop;
 505    if (el.offsetParent != null)
 506      y += getPageOffsetTop(el.offsetParent);
 507  
 508    return y;
 509  }
 510  
 511  //]]>


Généré le : Sun Apr 1 01:23:32 2007 par Balluche grâce à PHPXref 0.7