3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('align-plugin', function(Y) {
10 * Provides advanced positioning support for Node via a Plugin
11 * for centering and alignment.
12 * @module align-plugin
15 var OFFSET_WIDTH = 'offsetWidth',
16 OFFSET_HEIGHT = 'offsetHeight',
17 undefined = undefined;
20 * Node plugin which can be used to align a node with another node,
21 * region, or the viewport.
24 * @param {Object} User configuration object
26 function Align(config) {
28 this._host = config.host;
34 * Aligns node with a point on another node or region.
35 * Possible alignment points are:
42 * <dd>bottom left</dd>
44 * <dd>bottom right</dd>
48 * <dd>bottom center</dd>
50 * <dd>right center</dd>
52 * <dd>left center</dd>
54 * <dd>center center</dd>
57 * @param region {String || Node || HTMLElement || Object} The node or
58 * region to align with. Defaults to the viewport region.
59 * @param regionPoint {String} The point of the region to align with.
60 * @param point {String} The point of the node aligned to the region.
61 * @param resize {Boolean} Whether or not the node should re-align when
62 * the window is resized. Defaults to false.
64 to: function(region, regionPoint, point, syncOnResize) {
65 // cache original args for syncing
66 this._syncArgs = Y.Array(arguments);
68 if (region.top === undefined) {
69 region = Y.one(region).get('region');
73 var xy = [region.left, region.top],
74 offxy = [region.width, region.height],
75 points = Align.points,
78 size = node.getAttrs([OFFSET_HEIGHT, OFFSET_WIDTH]),
79 nodeoff = [0 - size[OFFSET_WIDTH], 0 - size[OFFSET_HEIGHT]], // reverse offsets
80 regionFn0 = regionPoint ? points[regionPoint.charAt(0)]: NULL,
81 regionFn1 = (regionPoint && regionPoint !== 'cc') ? points[regionPoint.charAt(1)] : NULL,
82 nodeFn0 = point ? points[point.charAt(0)] : NULL,
83 nodeFn1 = (point && point !== 'cc') ? points[point.charAt(1)] : NULL;
86 xy = regionFn0(xy, offxy, regionPoint);
89 xy = regionFn1(xy, offxy, regionPoint);
93 xy = nodeFn0(xy, nodeoff, point);
96 xy = nodeFn1(xy, nodeoff, point);
103 this._resize(syncOnResize);
110 this.to.apply(this, this._syncArgs);
114 _resize: function(add) {
115 var handle = this._handle;
116 if (add && !handle) {
117 this._handle = Y.on('resize', this._onresize, window, this);
118 } else if (!add && handle) {
124 _onresize: function() {
126 setTimeout(function() { // for performance
132 * Aligns the center of a node to the center of another node or region.
134 * @param region {Node || HTMLElement || Object} optional The node or
135 * region to align with. Defaults to the viewport region.
136 * the window is resized. If centering to viewport, this defaults
137 * to true, otherwise default is false.
139 center: function(region, resize) {
140 this.to(region, 'cc', 'cc', resize);
145 * Removes the resize handler, if any. This is called automatically
146 * when unplugged from the host node.
149 destroy: function() {
150 var handle = this._handle;
158 't': function(xy, off) {
162 'r': function(xy, off) {
163 return [xy[0] + off[0], xy[1]];
166 'b': function(xy, off) {
167 return [xy[0], xy[1] + off[1]];
170 'l': function(xy, off) {
174 'c': function(xy, off, point) {
175 var axis = (point[0] === 't' || point[0] === 'b') ? 0 : 1,
178 if (point === 'cc') {
179 ret = [xy[0] + off[0] / 2, xy[1] + off[1] / 2];
181 val = xy[axis] + off[axis] / 2;
182 ret = (axis) ? [xy[0], val] : [val, xy[1]];
189 Align.NAME = 'Align';
192 Align.prototype.constructor = Align;
194 Y.namespace('Plugin');
195 Y.Plugin.Align = Align;
199 }, '3.5.1' ,{requires:['node-pluginhost', 'node-screen']});