2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('widget-position', function (Y, NAME) {
11 * Provides basic XY positioning support for Widgets, though an extension
13 * @module widget-position
20 POSITION = "position",
21 POSITIONED = "positioned",
22 BOUNDING_BOX = "boundingBox",
23 RELATIVE = "relative",
25 RENDERUI = "renderUI",
31 XYChange = "xyChange";
34 * Widget extension, which can be used to add positioning support to the base Widget class,
35 * through the <a href="Base.html#method_build">Base.build</a> method.
37 * @class WidgetPosition
38 * @param {Object} config User configuration object
40 function Position(config) {
44 * Static property used to define the default attribute
45 * configuration introduced by WidgetPosition.
58 * @description Page X co-ordinate for the widget. This attribute acts as a facade for the
59 * xy attribute. Changes in position can be monitored by listening for xyChange events.
62 setter: function(val) {
76 * @description Page Y co-ordinate for the widget. This attribute acts as a facade for the
77 * xy attribute. Changes in position can be monitored by listening for xyChange events.
80 setter: function(val) {
94 * @description Page XY co-ordinate pair for the widget.
98 validator: function(val) {
99 return this._validateXY(val);
105 * Default class used to mark the boundingBox of a positioned widget.
107 * @property POSITIONED_CLASS_NAME
109 * @default "yui-widget-positioned"
112 Position.POSITIONED_CLASS_NAME = Widget.getClassName(POSITIONED);
114 Position.prototype = {
116 initializer : function() {
117 this._posNode = this.get(BOUNDING_BOX);
119 // WIDGET METHOD OVERLAP
120 Y.after(this._renderUIPosition, this, RENDERUI);
121 Y.after(this._syncUIPosition, this, SYNCUI);
122 Y.after(this._bindUIPosition, this, BINDUI);
126 * Creates/Initializes the DOM to support xy page positioning.
128 * This method in invoked after renderUI is invoked for the Widget class
129 * using YUI's aop infrastructure.
131 * @method _renderUIPosition
134 _renderUIPosition : function() {
135 this._posNode.addClass(Position.POSITIONED_CLASS_NAME);
139 * Synchronizes the UI to match the Widgets xy page position state.
141 * This method in invoked after syncUI is invoked for the Widget class
142 * using YUI's aop infrastructure.
144 * @method _syncUIPosition
147 _syncUIPosition : function() {
148 var posNode = this._posNode;
149 if (posNode.getStyle(POSITION) === RELATIVE) {
152 this._uiSetXY(this.get(XY_COORD));
156 * Binds event listeners responsible for updating the UI state in response to
157 * Widget position related state changes.
159 * This method in invoked after bindUI is invoked for the Widget class
160 * using YUI's aop infrastructure.
162 * @method _bindUIPosition
165 _bindUIPosition :function() {
166 this.after(XYChange, this._afterXYChange);
170 * Moves the Widget to the specified page xy co-ordinate position.
174 * @param {Number} x The new x position
175 * @param {Number} y The new y position
177 * @param {Array} x, y values passed as an array ([x, y]), to support
178 * simple pass through of Node.getXY results
181 var args = arguments,
182 coord = (Lang.isArray(args[0])) ? args[0] : [args[0], args[1]];
183 this.set(XY_COORD, coord);
187 * Synchronizes the Panel's "xy", "x", and "y" properties with the
188 * Widget's position in the DOM.
192 syncXY : function () {
193 this.set(XY_COORD, this._posNode.getXY(), {src: UI});
197 * Default validator for the XY attribute
199 * @method _validateXY
201 * @param {Array} val The XY page co-ordinate value which is being set.
202 * @return {boolean} true if valid, false if not.
204 _validateXY : function(val) {
205 return (Lang.isArray(val) && Lang.isNumber(val[0]) && Lang.isNumber(val[1]));
209 * Default setter for the X attribute. The setter passes the X value through
210 * to the XY attribute, which is the sole store for the XY state.
214 * @param {Number} val The X page co-ordinate value
216 _setX : function(val) {
217 this.set(XY_COORD, [val, this.get(XY_COORD)[1]]);
221 * Default setter for the Y attribute. The setter passes the Y value through
222 * to the XY attribute, which is the sole store for the XY state.
226 * @param {Number} val The Y page co-ordinate value
228 _setY : function(val) {
229 this.set(XY_COORD, [this.get(XY_COORD)[0], val]);
233 * Default getter for the X attribute. The value is retrieved from
234 * the XY attribute, which is the sole store for the XY state.
238 * @return {Number} The X page co-ordinate value
241 return this.get(XY_COORD)[0];
245 * Default getter for the Y attribute. The value is retrieved from
246 * the XY attribute, which is the sole store for the XY state.
250 * @return {Number} The Y page co-ordinate value
253 return this.get(XY_COORD)[1];
257 * Default attribute change listener for the xy attribute, responsible
258 * for updating the UI, in response to attribute changes.
260 * @method _afterXYChange
262 * @param {EventFacade} e The event facade for the attribute change
264 _afterXYChange : function(e) {
266 this._uiSetXY(e.newVal);
271 * Updates the UI to reflect the XY page co-ordinates passed in.
275 * @param {String} val The XY page co-ordinates value to be reflected in the UI
277 _uiSetXY : function(val) {
278 this._posNode.setXY(val);
282 Y.WidgetPosition = Position;
285 }, '3.13.0', {"requires": ["base-build", "node-screen", "widget"]});