YUI 3.x Home -

YUI Library Examples: Overlay: Stack Support

Overlay: Stack Support

This example shows how you can work with the zIndex attribute which the Overlay supports, to implement a simple bringToTop function. The example code also listens for changes in the zIndex attribute, which it uses to update the content of the overlay, when it is brought to the top of the stack.

Click on an Overlay, to switch it's zIndex with the highest zIndex in the stack, bringing it to the top of the stack:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nunc pretium quam eu mi varius pulvinar. Duis orci arcu, ullamcorper sit amet, luctus ut, interdum ac, quam. Pellentesque euismod. Nam tincidunt, purus in ultrices congue, urna neque posuere arcu, aliquam tristique purus sapien id nulla. Etiam rhoncus nulla at leo. Cras scelerisque nisl in nibh. Sed eget odio. Morbi elit elit, porta a, convallis sit amet, rhoncus non, felis. Mauris nulla pede, pretium eleifend, porttitor at, rutrum id, orci. Quisque non urna. Nulla aliquam rhoncus est. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nunc pretium quam eu mi varius pulvinar. Duis orci arcu, ullamcorper sit amet, luctus ut, interdum ac, quam. Pellentesque euismod. Nam tincidunt, purus in ultrices congue, urna neque posuere arcu, aliquam tristique purus sapien id nulla. Etiam rhoncus nulla at leo. Cras scelerisque nisl in nibh. Sed eget odio. Morbi elit elit, porta a, convallis sit amet, rhoncus non, felis. Mauris nulla pede, pretium eleifend, porttitor at, rutrum id, orci. Quisque non urna. Nulla aliquam rhoncus est.

Basic Overlay Stackability (zIndex and shim support)

Setting Up The YUI Instance

As with the "Basic XY Positioning" example, to create an instance of an Overlay on your page, the only module you need to request is the overlay module. The overlay module will pull in the widget, widget-stack, widget-position, widget-position-ext and widget-stdmod dependencies it has.

  1. YUI({...}).use("overlay", function(Y) {
  2. // We'll write example code here, where we have a Y.Overlay class available.
  3. });
YUI({...}).use("overlay", function(Y) {
    // We'll write example code here, where we have a Y.Overlay class available.
});

Using the overlay module will also pull down the default CSS required for overlay, on top of which we only need to add our required look/feel CSS for the example.

Instantiating The Overlay

For this example we'll instantiate Overlays from script, as we did for the "Extended XY Positioning" example. We'll create 6 Overlay instances and give them increasing zIndex and xy attribute values:

  1. function getOverlayXY(xy, i) {
  2. return [xy[0] + i * 60, xy[1] + i * 40];
  3. }
  4.  
  5. for (var i = 0; i < n; ++i) {
  6.  
  7. ovrXY = getOverlayXY(xy, i);
  8. ovrZIndex = i+1;
  9.  
  10. // Setup n Overlays, with increasing zIndex values and xy positions
  11. overlay = new Y.Overlay({
  12. zIndex:ovrZIndex,
  13. xy:ovrXY,
  14.  
  15. width:"8em",
  16. height:"8em",
  17. headerContent: 'Overlay <span class="yui-ovr-number">' + i + '</span>',
  18. bodyContent: "zIndex = " + ovrZIndex
  19. });
  20.  
  21. overlay.render("#overlay-stack");
  22.  
  23. ...
  24.  
  25. }
function getOverlayXY(xy, i) {
    return [xy[0] + i * 60, xy[1] + i * 40];
}
 
for (var i = 0; i < n; ++i) {
 
    ovrXY = getOverlayXY(xy, i);
    ovrZIndex = i+1;
 
    // Setup n Overlays, with increasing zIndex values and xy positions
    overlay = new Y.Overlay({
        zIndex:ovrZIndex,
        xy:ovrXY,
 
        width:"8em",
        height:"8em",
        headerContent: 'Overlay <span class="yui-ovr-number">' + i + '</span>',
        bodyContent: "zIndex = " + ovrZIndex
    });
 
    overlay.render("#overlay-stack");
 
    ...
 
}

We store the Overlay instances in an overlays array, which we'll later use to sort the Overlays by their zIndex values. We also setup a listener for the zIndex attribute change event, which will update the body section of the Overlay to display its new zIndex value.

  1. overlays.push(overlay);
  2.  
  3. // Update body whenever zIndex changes
  4. overlay.after("zIndexChange", function(e) {
  5. this.set("bodyContent", "zIndex = " + e.newVal);
  6. });
overlays.push(overlay);
 
// Update body whenever zIndex changes
overlay.after("zIndexChange", function(e) {
    this.set("bodyContent", "zIndex = " + e.newVal);
});

Handling MouseDown Using Widget.getByNode

The Widget class has a static getByNode method which can be used to retrieve Widget instances based on a node reference. The method will return the closest Widget which contains the given node.

We'll use this method in a click listener bound to the container of the example ("#overlay-stack"). Target nodes of click events bubbled up to this example container, will be passed to the Widget.getByNode method, to see if an Overlay was clicked on.

  1. function onStackMouseDown(e) {
  2. var widget = Y.Widget.getByNode(e.target);
  3.  
  4. // If user clicked on an Overlay, bring it to the top of the stack
  5. if (widget && widget instanceof Y.Overlay) {
  6. bringToTop(widget);
  7. }
  8. }
  9.  
  10. Y.on("mousedown", onStackMouseDown, "#overlay-stack");
function onStackMouseDown(e) {
    var widget = Y.Widget.getByNode(e.target);
 
    // If user clicked on an Overlay, bring it to the top of the stack
    if (widget && widget instanceof Y.Overlay) {
        bringToTop(widget);
    }
}
 
Y.on("mousedown", onStackMouseDown, "#overlay-stack");

If an Overlay was clicked on, we invoke the simple bringToTop method which will set the zIndex of the clicked Overlay to the highest current Overlay zIndex value.

The bringToTop Implementation

We use a basic comparator function to sort the array of Overlays we have. The comparator makes sure the array element we're dealing with has a WidgetStack implementation (which Overlays do) and if so, sorts them in descending zIndex attribute value order:

  1. // zIndex comparator
  2. function byZIndexDesc(a, b) {
  3. if (!a || !b || !a.hasImpl(Y.WidgetStack) || !b.hasImpl(Y.WidgetStack)) {
  4. return 0;
  5. } else {
  6. var aZ = a.get("zIndex");
  7. var bZ = b.get("zIndex");
  8.  
  9. if (aZ > bZ) {
  10. return -1;
  11. } else if (aZ < bZ) {
  12. return 1;
  13. } else {
  14. return 0;
  15. }
  16. }
  17. }
// zIndex comparator
function byZIndexDesc(a, b) {
    if (!a || !b || !a.hasImpl(Y.WidgetStack) || !b.hasImpl(Y.WidgetStack)) {
        return 0;
    } else {
        var aZ = a.get("zIndex");
        var bZ = b.get("zIndex");
 
        if (aZ > bZ) {
            return -1;
        } else if (aZ < bZ) {
            return 1;
        } else {
            return 0;
        }
    }
}

Once sorted, for the purposes of the example, we simply switch the zIndex of the "highest" Overlay, with the Overlay which was clicked on, giving the selected Overlay the highest zIndex value:

  1. function bringToTop(overlay) {
  2.  
  3. // Sort overlays by their numerical zIndex values
  4. overlays.sort(byZIndexDesc);
  5.  
  6. // Get the highest one
  7. var highest = overlays[0];
  8.  
  9. // If the overlay is not the highest one, switch zIndices
  10. if (highest !== overlay) {
  11. var highestZ = highest.get("zIndex");
  12. var overlayZ = overlay.get("zIndex");
  13.  
  14. overlay.set("zIndex", highestZ);
  15. highest.set("zIndex", overlayZ);
  16. }
  17. }
function bringToTop(overlay) {
 
    // Sort overlays by their numerical zIndex values
    overlays.sort(byZIndexDesc);
 
    // Get the highest one
    var highest = overlays[0];
 
    // If the overlay is not the highest one, switch zIndices
    if (highest !== overlay) {
        var highestZ = highest.get("zIndex");
        var overlayZ = overlay.get("zIndex");
 
        overlay.set("zIndex", highestZ);
        highest.set("zIndex", overlayZ);
    }
}

CSS: Overlay Look/Feel

As mentioned in the "Basic XY Positioning" example, the overlay.css Sam Skin file (build/overlay/assets/skins/sam/overlay.css) provides the default functional CSS for the overlay. Namely the CSS rules to hide the overlay, and position it absolutely. However there's no default out-of-the-box look/feel applied to the Overlay widget.

The example provides it's own look and feel for the Overlay, by defining rules for the content box, header and body sections:

  1. /* Overlay Look/Feel */
  2. .yui-overlay-content {
  3. padding:2px;
  4. border:1px solid #000;
  5. background-color:#aaa;
  6. font-size:93%;
  7. }
  8.  
  9. .yui-overlay-content .yui-widget-hd {
  10. font-weight:bold;
  11. text-align:center;
  12. padding:2px;
  13. border:2px solid #aa0000;
  14. background-color:#fff;
  15. }
  16.  
  17. .yui-overlay-content .yui-widget-bd {
  18. text-align:left;
  19. padding:2px;
  20. border:2px solid #0000aa;
  21. background-color:#fff;
  22. }
  23.  
  24. .yui-overlay-content .yui-widget-hd .yui-ovr-number {
  25. color:#aa0000;
  26. }
/* Overlay Look/Feel */
.yui-overlay-content {
    padding:2px;
    border:1px solid #000;
    background-color:#aaa;
    font-size:93%;
}
 
.yui-overlay-content .yui-widget-hd {
    font-weight:bold;
    text-align:center;
    padding:2px;
    border:2px solid #aa0000;
    background-color:#fff;
}
 
.yui-overlay-content .yui-widget-bd {
    text-align:left;
    padding:2px;
    border:2px solid #0000aa;
    background-color:#fff;
}
 
.yui-overlay-content .yui-widget-hd .yui-ovr-number {
    color:#aa0000;
}

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings