Skip to content

Instantly share code, notes, and snippets.

@elson
Created July 14, 2009 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elson/146972 to your computer and use it in GitHub Desktop.
Save elson/146972 to your computer and use it in GitHub Desktop.
--- core/src/widgets/overlay/overlay.js 2008-10-29 14:29:42.000000000 +0000
+++ trunk/src/widgets/overlay/overlay.js 2008-11-19 16:27:48.000000000 +0000
@@ -235,6 +235,9 @@
@param {glow.widgets.Mask} [opts.mask] Mask to use for modal overlays
used to indicate to the user that the overlay is modal. If provided then the modal property is set to true.
@param {Boolean} [opts.closeOnMaskClick="true"] if true then listens for a click event on the mask and hides when it fires
+ @param {selector|DOMElement|NodeList} [opts.context] Context for overlay positioning.
+ The overlay will be positioned so that it is centred over the specified element.
+ If the panel is modal, its Mask will inherit the context and so will only cover the context element.
@param {String|Function} [opts.anim="null"] A transition for showing / hiding the panel
Can be "fade" or "roll", or a function which returns a glow.anim.Animation or glow.anim.Timeline.
The function is passed the overlay as the first parameter, and 'true' if the overlay is showing, 'false' if it's hiding.
@@ -310,9 +313,13 @@
//assume modal if mask provided
if (opts.mask) { opts.modal = true; }
+ // Pass options through to mask
+ var maskOpts = opts.zIndex ? {zIndex: opts.zIndex-1} : {};
+ if(opts.context) maskOpts.context = opts.context;
+
this.opts = opts = glow.lang.apply({
modal: false,
- mask: new glow.widgets.Mask(opts.zIndex ? {zIndex: opts.zIndex-1} : {}),
+ mask: new glow.widgets.Mask( maskOpts ),
closeOnMaskClick: true,
zIndex: 9990,
autoPosition: true,
@@ -325,6 +332,13 @@
hideWindowedFlash: true,
focusOnShow: false
}, opts);
+
+ /*
+ The context for this overlay
+ */
+ if(opts.context) {
+ this.context = opts.context;
+ }
/**
@name glow.widgets.Overlay#content
@@ -440,7 +454,16 @@
@returns this
*/
setPosition: function(x, y) {
+
+ // Set-up context
+ var context, hasContext = false;
+ if(this.context) {
+ context = $(this.context);
+ hasContext = context.length > 0;
+ }
+
var container = this.container;
+
//don't use set position if autoPosition is false
if (this.autoPosition) {
//if values have been provided, set them. Make sure we're not being passed an event object!
@@ -448,6 +471,31 @@
this.opts.x = x;
this.opts.y = y;
}
+
+ // If we've been give a context then
+ // work out the x,y of the overlay
+ // centred over this.
+ if(hasContext) {
+ var ctx, mid;
+
+ var ctxOffset = context.offset();
+ ctx = { x : ctxOffset.left,
+ y : ctxOffset.top,
+ width : context.width(),
+ height : context.height()
+ };
+
+ mid = { x: ctx.x + (ctx.width / 2),
+ y: ctx.y + (ctx.height / 2)
+ };
+
+ x = mid.x - (container.width() / 2);
+ y = mid.y - (container.height() / 2);
+
+ this.opts.x = x + "px";
+ this.opts.y = y + "px";
+ }
+
var win = $(window),
x = this.opts.x,
y = this.opts.y,
@@ -463,6 +511,11 @@
containerHeight;
useFixedThisTime && container.css("position", "fixed");
+
+ // If we have a context then we set the position
+ // of the overlay to absolute, overriding the decision
+ // of usedFixed. This may cause problems?
+ if(hasContext) container.css("position", "absolute");
if (typeof x == "string" && x.indexOf("%") != -1) {
winWidth = win.width();
@@ -481,7 +534,11 @@
container.css("left", Math.max(((winWidth - containerWidth) * (xVal/100)) + extraOffset.x, extraOffset.x) + "px");
}
} else {
- container.css("left", xVal + extraOffset.x + "px");
+ if(hasContext) {
+ container.css("left", xVal + "px");
+ } else {
+ container.css("left", xVal + extraOffset.x + "px");
+ }
}
if (typeof y == "string" && y.indexOf("%") != -1) {
@@ -501,7 +558,11 @@
container.css("top", Math.max(((winHeight - containerHeight) * (yVal/100)) + extraOffset.y, extraOffset.y) + "px");
}
} else {
- container.css("top", yVal + extraOffset.y + "px");
+ if(hasContext) {
+ container.css("top", yVal + "px");
+ } else {
+ container.css("top", yVal + extraOffset.y + "px");
+ }
}
}
@@ -512,6 +573,7 @@
css("width", container[0].offsetWidth + "px").
css("height", container[0].offsetHeight + "px");
}
+
return this;
},
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment