Changeset 220 for Dev/trunk


Ignore:
Timestamp:
01/10/12 01:41:36 (13 years ago)
Author:
tschipper
Message:

Nieuwe implementatie van dropdown menus. Voorbeeld staat in positiontest.html, maar alle code staat in generalScripts.js, dus kan op alle pagina's gebruikt worden.

Location:
Dev/trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/js/generalScripts.js

    r219 r220  
    156156}
    157157
    158 function createMenuPanel (parentButton, cb1, cb2) {
    159     var outerDiv = ce("div");
    160     outerDiv.className = "ddMenu";
    161     outerDiv.id = "ddAddObjectMenu";
    162     outerDiv.style.position = "absolute";
    163    
    164     var button1 = ce("input");
    165     button1.className = "ddMenuButton";
    166     button1.type = "button";
    167     button1.value = "Add new";
    168     button1.addEventListener("click", cb1, false);
    169    
    170     var button2 = ce("input");
    171     button2.className = "ddMenuButton";
    172     button2.type = "button";
    173     button2.value = "Add existing";
    174     button2.addEventListener("click", cb2, false);
    175    
    176     outerDiv.appendChild(button1);
    177     outerDiv.appendChild(button2);
    178    
    179     var parentPos = findPos(parentButton);
    180     var parentWidth = parentButton.offsetWidth;
    181     var menuPosX = parentPos.X + parentWidth;
    182     var menuPosY = parentPos.Y;
    183     outerDiv.style.left = menuPosX+"px";
    184     outerDiv.style.top = menuPosY+"px";
    185 }
    186 
    187 function findPos(element) {
     158// Function for getting the absolute position of the top left corner of an element, relative to the window
     159function getPos(element) {
    188160    var posX = posY = 0;
    189161    if (element.offsetParent) {
     
    200172    return result;
    201173}
     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        instance.closeTimer = window.setTimeout(instance.Close, instance.timeout);
     203    }
     204   
     205    this.Close = function() {
     206        if (instance.ddMenuItem) {
     207            instance.ddMenuItem.style.visibility = "hidden";
     208        }
     209        instance.Show();
     210       
     211    }
     212   
     213    this.CancelCloseTimer = function() {
     214        if (instance.closeTimer) {
     215            window.clearTimeout(instance.closeTimer);
     216            instance.closeTimer = null;
     217        }
     218    }
     219   
     220    this.Open = function(id) {
     221        instance.CancelCloseTimer();
     222        if (instance.ddMenuItem) {
     223            instance.ddMenuItem.style.visibility = "hidden";
     224        }
     225       
     226        instance.ddMenuItem = ge(id);
     227        instance.ddMenuItem.style.visibility = "visible";
     228        var parentPos = getPos(instance.ddMenuItem.parentNode);
     229        var parentWidth = getWidth(instance.ddMenuItem.parentNode);
     230        instance.ddMenuItem.style.left = (parentPos.X + parentWidth)+"px";
     231        instance.ddMenuItem.style.top = parentPos.Y+"px";
     232       
     233    }
     234   
     235    this.Show = function() {
     236        debugger;
     237        if (instance.visible) {
     238            // Hide the menu
     239            instance.menuElement.style.visibility = "hidden";
     240            instance.parentButton.className = "";
     241            instance.visible = false;
     242        }
     243        else{
     244            //Show the menu
     245            instance.menuElement.style.visibility = "visible";
     246            instance.parentButton.className = "down";
     247            instance.visible = true;
     248        }
     249    }
     250}
Note: See TracChangeset for help on using the changeset viewer.