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', function(Y) {
11 * Provides basic XY positioning support for Widgets, though an extension
13 * @module widget-position
20 POSITIONED = "positioned",
21 BOUNDING_BOX = "boundingBox",
23 RENDERUI = "renderUI",
29 XYChange = "xyChange";
32 * Widget extension, which can be used to add positioning support to the base Widget class,
33 * through the <a href="Base.html#method_build">Base.build</a> method.
35 * @class WidgetPosition
36 * @param {Object} config User configuration object
38 function Position(config) {
39 this._posNode = this.get(BOUNDING_BOX);
41 // WIDGET METHOD OVERLAP
42 Y.after(this._renderUIPosition, this, RENDERUI);
43 Y.after(this._syncUIPosition, this, SYNCUI);
44 Y.after(this._bindUIPosition, this, BINDUI);
48 * Static property used to define the default attribute
49 * configuration introduced by WidgetPosition.
51 * @property WidgetPosition.ATTRS
62 * @description Page X co-ordinate for the widget. This attribute acts as a facade for the
63 * xy attribute. Changes in position can be monitored by listening for xyChange events.
66 setter: function(val) {
80 * @description Page Y co-ordinate for the widget. This attribute acts as a facade for the
81 * xy attribute. Changes in position can be monitored by listening for xyChange events.
84 setter: function(val) {
98 * @description Page XY co-ordinate pair for the widget.
103 validator: function(val) {
104 return this._validateXY(val);
110 * Default class used to mark the boundingBox of a positioned widget.
112 * @property WidgetPosition.POSITIONED_CLASS_NAME
114 * @default "yui-widget-positioned"
117 Position.POSITIONED_CLASS_NAME = Widget.getClassName(POSITIONED);
119 Position.prototype = {
122 * Creates/Initializes the DOM to support xy page positioning.
124 * This method in invoked after renderUI is invoked for the Widget class
125 * using YUI's aop infrastructure.
127 * @method _renderUIPosition
130 _renderUIPosition : function() {
131 this._posNode.addClass(Position.POSITIONED_CLASS_NAME);
135 * Synchronizes the UI to match the Widgets xy page position state.
137 * This method in invoked after syncUI is invoked for the Widget class
138 * using YUI's aop infrastructure.
140 * @method _syncUIPosition
143 _syncUIPosition : function() {
144 this._uiSetXY(this.get(XY_COORD));
148 * Binds event listeners responsible for updating the UI state in response to
149 * Widget position related state changes.
151 * This method in invoked after bindUI is invoked for the Widget class
152 * using YUI's aop infrastructure.
154 * @method _bindUIPosition
157 _bindUIPosition :function() {
158 this.after(XYChange, this._afterXYChange);
162 * Moves the Widget to the specified page xy co-ordinate position.
166 * @param {Number} x The new x position
167 * @param {Number} y The new y position
169 * @param {Array} x, y values passed as an array ([x, y]), to support
170 * simple pass through of Node.getXY results
173 var args = arguments,
174 coord = (Lang.isArray(args[0])) ? args[0] : [args[0], args[1]];
175 this.set(XY_COORD, coord);
179 * Synchronizes the Panel's "xy", "x", and "y" properties with the
180 * Widget's position in the DOM.
184 syncXY : function () {
185 this.set(XY_COORD, this._posNode.getXY(), {src: UI});
189 * Default validator for the XY attribute
191 * @method _validateXY
192 * @param {Array} val The XY page co-ordinate value which is being set.
193 * @return {boolean} true if valid, false if not.
195 _validateXY : function(val) {
196 return (Lang.isArray(val) && Lang.isNumber(val[0]) && Lang.isNumber(val[1]));
200 * Default setter for the X attribute. The setter passes the X value through
201 * to the XY attribute, which is the sole store for the XY state.
204 * @param {Number} val The X page co-ordinate value
206 _setX : function(val) {
207 this.set(XY_COORD, [val, this.get(XY_COORD)[1]]);
211 * Default setter for the Y attribute. The setter passes the Y value through
212 * to the XY attribute, which is the sole store for the XY state.
215 * @param {Number} val The Y page co-ordinate value
217 _setY : function(val) {
218 this.set(XY_COORD, [this.get(XY_COORD)[0], val]);
222 * Default getter for the X attribute. The value is retrieved from
223 * the XY attribute, which is the sole store for the XY state.
226 * @return {Number} The X page co-ordinate value
229 return this.get(XY_COORD)[0];
233 * Default getter for the Y attribute. The value is retrieved from
234 * the XY attribute, which is the sole store for the XY state.
237 * @return {Number} The Y page co-ordinate value
240 return this.get(XY_COORD)[1];
244 * Default attribute change listener for the xy attribute, responsible
245 * for updating the UI, in response to attribute changes.
247 * @method _afterXYChange
249 * @param {EventFacade} e The event facade for the attribute change
251 _afterXYChange : function(e) {
253 this._uiSetXY(e.newVal);
258 * Updates the UI to reflect the XY page co-ordinates passed in.
262 * @param {String} val The XY page co-ordinates value to be reflected in the UI
264 _uiSetXY : function(val) {
265 this._posNode.setXY(val);
269 Y.WidgetPosition = Position;
272 }, '3.0.0' ,{requires:['widget']});