2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 YUI.add('widget-position-ext', function(Y) {
11 * Provides extended/advanced XY positioning support for Widgets, through an extension.
13 * It builds on top of the widget-position module, to provide alignmentment and centering support.
14 * Future releases aim to add constrained and fixed positioning support.
16 * @module widget-position-ext
24 OFFSET_WIDTH = "offsetWidth",
25 OFFSET_HEIGHT = "offsetHeight",
26 VIEWPORT_REGION = "viewportRegion",
29 AlignChange = "alignChange";
32 * Widget extension, which can be used to add extended XY positioning support to the base Widget class,
33 * through the <a href="Base.html#method_build">Base.build</a> method. This extension requires that
34 * the WidgetPosition extension be added to the Widget (before WidgetPositionExt, if part of the same
35 * extension list passed to Base.build).
37 * @class WidgetPositionExt
38 * @param {Object} User configuration object
40 function PositionExt(config) {
42 Y.error("WidgetPosition needs to be added to the Widget, before WidgetPositionExt is added");
44 Y.after(this._syncUIPosExtras, this, SYNCUI);
45 Y.after(this._bindUIPosExtras, this, BINDUI);
49 * Static property used to define the default attribute
50 * configuration introduced by WidgetPositionExt.
52 * @property WidgetPositionExt.ATTRS
62 * @desciption The align attribute is used to align a reference point on the widget, with the refernce point on another node, or the viewport.
63 * The object which align expects has the following properties:
67 * The node to which the Widget is to be aligned. If set to null, or not provided, the Widget is aligned to the viewport
72 * A two element array, defining the two points on the Widget and node/viewport which are to be aligned. The first element is the point on the Widget, and the second element is the point on the node/viewport.
73 * Supported alignment points are defined as static properties on <code>WidgetPositionExt</code>.
76 * e.g. <code>[WidgetPositionExt.TR, WidgetPositionExt.TL]</code> aligns the Top-Right corner of the Widget with the
77 * Top-Left corner of the node/viewport, and <code>[WidgetPositionExt.CC, WidgetPositionExt.TC]</code> aligns the Center of the
78 * Widget with the Top-Center edge of the node/viewport.
89 * @type {boolean | node}
91 * @description A convenience attribute, which can be used as a shortcut for the align attribute.
92 * If set to true, the Widget is centered in the viewport. If set to a node reference or valid selector string,
93 * the Widget will be centered within the node. If set the false, no center positioning is applied.
96 setter: function(val) {
97 return this._setAlignCenter(val);
105 * Constant used to specify the top-left corner for alignment
107 * @property WidgetPositionExt.TL
112 PositionExt.TL = "tl";
114 * Constant used to specify the top-right corner for alignment
116 * @property WidgetPositionExt.TR
121 PositionExt.TR = "tr";
123 * Constant used to specify the bottom-left corner for alignment
125 * @property WidgetPositionExt.BL
130 PositionExt.BL = "bl";
132 * Constant used to specify the bottom-right corner for alignment
134 * @property WidgetPositionExt.BR
139 PositionExt.BR = "br";
141 * Constant used to specify the top edge-center point for alignment
143 * @property WidgetPositionExt.TC
148 PositionExt.TC = "tc";
150 * Constant used to specify the right edge, center point for alignment
152 * @property WidgetPositionExt.RC
157 PositionExt.RC = "rc";
159 * Constant used to specify the bottom edge, center point for alignment
161 * @property WidgetPositionExt.BC
166 PositionExt.BC = "bc";
168 * Constant used to specify the left edge, center point for alignment
170 * @property WidgetPositionExt.LC
175 PositionExt.LC = "lc";
177 * Constant used to specify the center of widget/node/viewport for alignment
179 * @property WidgetPositionExt.CC
184 PositionExt.CC = "cc";
186 PositionExt.prototype = {
189 * Synchronizes the UI to match the Widgets extended positioning state.
190 * This method in invoked after syncUI is invoked for the Widget class
191 * using YUI's aop infrastructure.
193 * @method _syncUIPosExtras
196 _syncUIPosExtras : function() {
197 var align = this.get(ALIGN);
199 this._uiSetAlign(align.node, align.points);
204 * Binds event listeners responsible for updating the UI state in response to
205 * Widget extended positioning related state changes.
207 * This method is invoked after bindUI is invoked for the Widget class
208 * using YUI's aop infrastructure.
210 * @method _bindUIStack
213 _bindUIPosExtras : function() {
214 this.after(AlignChange, this._afterAlignChange);
218 * Default setter for center attribute changes. Sets up the appropriate value, and passes
219 * it through the to the align attribute.
221 * @method _setAlignCenter
223 * @param {boolean | node} The attribute value being set.
224 * @return {Number} The attribute value being set.
226 _setAlignCenter : function(val) {
229 node: val === true ? null : val,
230 points: [PositionExt.CC, PositionExt.CC]
237 * Default attribute change listener for the align attribute, responsible
238 * for updating the UI, in response to attribute changes.
240 * @method _afterAlignChange
242 * @param {EventFacade} e The event facade for the attribute change
244 _afterAlignChange : function(e) {
246 this._uiSetAlign(e.newVal.node, e.newVal.points);
251 * Updates the UI to reflect the align value passed in (see the align attribute documentation, for the object stucture expected)
252 * @method _uiSetAlign
254 * @param {Node | null} The node to align to, or null to indicate the viewport
256 _uiSetAlign: function (node, points) {
258 if (!L.isArray(points) || points.length != 2) {
259 Y.error("align: Invalid Points Arguments");
263 var nodeRegion, widgetPoint, nodePoint, xy;
266 nodeRegion = this._posNode.get(VIEWPORT_REGION);
268 node = Y.Node.get(node);
270 nodeRegion = node.get(REGION);
276 // TODO: ViewportRegion doesn't have width/height - Workaround until normalized in Node/Dom
277 nodeRegion.width = nodeRegion.width || nodeRegion.right - nodeRegion.left;
278 nodeRegion.height = nodeRegion.height || nodeRegion.bottom - nodeRegion.top;
280 widgetPoint = points[0];
281 nodePoint = points[1];
283 // TODO: Optimize KWeight - Would lookup table help?
286 xy = [nodeRegion.left, nodeRegion.top];
289 xy = [nodeRegion.right, nodeRegion.top];
292 xy = [nodeRegion.left, nodeRegion.bottom];
295 xy = [nodeRegion.right, nodeRegion.bottom];
298 xy = [nodeRegion.left + Math.floor(nodeRegion.width/2), nodeRegion.top];
301 xy = [nodeRegion.left + Math.floor(nodeRegion.width/2), nodeRegion.bottom];
304 xy = [nodeRegion.left, nodeRegion.top + Math.floor(nodeRegion.height/2)];
307 xy = [nodeRegion.right, nodeRegion.top + Math.floor(nodeRegion.height/2), widgetPoint];
310 xy = [nodeRegion.left + Math.floor(nodeRegion.width/2), nodeRegion.top + Math.floor(nodeRegion.height/2), widgetPoint];
317 this._doAlign(widgetPoint, xy[0], xy[1]);
323 * Helper method, used to align the given point on the widget, with the XY page co-ordinates provided.
327 * @param {String} widgetPoint Supported point constant (e.g. WidgetPositionExt.TL)
328 * @param {Number} x X page co-ordinate to align to
329 * @param {Number} y Y page co-ordinate to align to
331 _doAlign : function(widgetPoint, x, y) {
332 var widgetNode = this._posNode,
335 switch (widgetPoint) {
340 xy = [x - widgetNode.get(OFFSET_WIDTH), y];
343 xy = [x, y - widgetNode.get(OFFSET_HEIGHT)];
346 xy = [x - widgetNode.get(OFFSET_WIDTH), y - widgetNode.get(OFFSET_HEIGHT)];
349 xy = [x - (widgetNode.get(OFFSET_WIDTH)/2), y];
352 xy = [x - (widgetNode.get(OFFSET_WIDTH)/2), y - widgetNode.get(OFFSET_HEIGHT)];
355 xy = [x, y - (widgetNode.get(OFFSET_HEIGHT)/2)];
358 xy = [(x - widgetNode.get(OFFSET_WIDTH)), y - (widgetNode.get(OFFSET_HEIGHT)/2)];
361 xy = [x - (widgetNode.get(OFFSET_WIDTH)/2), y - (widgetNode.get(OFFSET_HEIGHT)/2)];
373 * Aligns the Widget to the provided node (or viewport) using the provided
374 * points. The method can be invoked directly, however it will result in
375 * the align attribute being out of sync with current position of the of Widget.
378 * @param {Node | String | null} node A reference (or selector string) for the Node which with the Widget is to be aligned.
379 * If null is passed in, the Widget will be aligned with the viewport.
380 * @param {Array[2]} points A two element array, specifying the points on the Widget and node/viewport which need to be aligned.
381 * The first entry is the point on the Widget, and the second entry is the point on the node/viewport which need to align.
382 * Valid point references are defined as static constants on the WidgetPositionExt class.
384 * e.g. [WidgetPositionExt.TL, WidgetPositionExt.TR] will align the top-left corner of the Widget with the top-right corner of the node/viewport.
386 align: function (node, points) {
387 this.set(ALIGN, {node: node, points:points});
391 * Centers the container in the viewport, or if a node is passed in,
395 * @param {Node | String} node Optional. A node reference or selector string defining the node
396 * inside which the Widget is to be centered. If not passed in, the Widget will be centered in the
399 centered: function (node) {
400 this.align(node, [PositionExt.CC, PositionExt.CC]);
404 Y.WidgetPositionExt = PositionExt;
407 }, '3.0.0' ,{requires:['widget', 'widget-position']});