Ignore:
Timestamp:
01/12/12 19:52:32 (13 years ago)
Author:
fpvanagthoven
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/branches/Demo/js/generalScripts.js

    r191 r228  
    155155    return document.getElementById(s);
    156156}
     157
     158// Function for getting the absolute position of the top left corner of an element, relative to the window
     159function getPos(element) {
     160    var posX = posY = 0;
     161    if (element.offsetParent) {
     162        do {
     163            posX += element.offsetLeft;
     164            posY += element.offsetTop;
     165        } while (element = element.offSetParent);
     166    }
     167   
     168    var result = {
     169        X: posX,
     170        Y: posY
     171    }
     172    return result;
     173}
     174
     175// Returns the computed width of an element
     176function getWidth(element) {
     177    var width = 0;
     178    if (element.offsetWidth) {
     179        width = element.offsetWidth;
     180    }
     181   
     182    return width;
     183}
     184
     185// Drop down menu implementation. Supports three levels: Base button, 1st level categories, and 2nd level links
     186function DDMenu() {
     187    // Initialize function, setting all needed variables.
     188    var instance = this;
     189    this.closeTimer = 0;
     190    this.ddMenuItem = null;
     191    this.timeout = 350;
     192    this.visible = false;
     193    this.menuElement = null;
     194    this.parentButton = null
     195   
     196    this.Init = function(id1, id2) {   
     197        instance.menuElement = ge(id1);
     198        instance.parentButton = ge(id2);
     199    }
     200   
     201    this.SetCloseTimer = function() {
     202        debugger;
     203        instance.closeTimer = window.setTimeout(instance.Close, instance.timeout);
     204    }
     205   
     206    this.Close = function() {
     207        if (instance.ddMenuItem) {
     208            instance.ddMenuItem.style.visibility = "hidden";
     209        }
     210        instance.Toggle();
     211       
     212    }
     213   
     214    this.CancelCloseTimer = function() {
     215        if (instance.closeTimer) {
     216            window.clearTimeout(instance.closeTimer);
     217            instance.closeTimer = null;
     218        }
     219    }
     220   
     221    this.Open = function(id) {
     222        instance.CancelCloseTimer();
     223        if (instance.ddMenuItem) {
     224            instance.ddMenuItem.style.visibility = "hidden";
     225        }
     226       
     227        instance.ddMenuItem = ge(id);
     228        instance.ddMenuItem.style.visibility = "visible";
     229        var parentPos = getPos(instance.ddMenuItem.parentNode);
     230        var parentWidth = getWidth(instance.ddMenuItem.parentNode);
     231        instance.ddMenuItem.style.left = (parentPos.X + parentWidth)+"px";
     232        instance.ddMenuItem.style.top = parentPos.Y+"px";
     233       
     234    }
     235   
     236    this.Toggle = function() {
     237        debugger;
     238        if (instance.visible) {
     239            // Hide the menu
     240            instance.menuElement.style.visibility = "hidden";
     241            instance.parentButton.className = "";
     242            instance.visible = false;
     243        }
     244        else{
     245            //Show the menu
     246            if (instance.menuElement) {
     247                instance.menuElement.style.visibility = "visible";
     248                instance.parentButton.className = "down";
     249            }
     250            instance.visible = true;
     251        }
     252    }
     253}
Note: See TracChangeset for help on using the changeset viewer.