2
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.com/yui/license.html
8
YUI.add('align-plugin', function(Y) {
11
* Provides advanced positioning support for Node via a Plugin
12
* for centering and alignment.
13
* @module align-plugin
16
var OFFSET_WIDTH = 'offsetWidth',
17
OFFSET_HEIGHT = 'offsetHeight',
18
undefined = undefined;
21
* Node plugin which can be used to align a node with another node,
22
* region, or the viewport.
25
* @param {Object} User configuration object
27
function Align(config) {
29
this._host = config.host;
35
* Aligns node with a point on another node or region.
36
* Possible alignment points are:
43
* <dd>bottom left</dd>
45
* <dd>bottom right</dd>
49
* <dd>bottom center</dd>
51
* <dd>right center</dd>
53
* <dd>left center</dd>
55
* <dd>center center</dd>
58
* @parm region {String || Node || HTMLElement || Object} The node or
59
* region to align with. Defaults to the viewport region.
60
* @parm regionPoint {String} The point of the region to align with.
61
* @parm point {String} The point of the node aligned to the region.
62
* @parm resize {Boolean} Whether or not the node should re-align when
63
* the window is resized. Defaults to false.
65
to: function(region, regionPoint, point, syncOnResize) {
66
// cache original args for syncing
67
this._syncArgs = Y.Array(arguments);
69
if (region.top === undefined) {
70
region = Y.one(region).get('region');
74
var xy = [region.left, region.top],
75
offxy = [region.width, region.height],
76
points = Align.points,
79
size = node.getAttrs([OFFSET_HEIGHT, OFFSET_WIDTH]),
80
nodeoff = [0 - size[OFFSET_WIDTH], 0 - size[OFFSET_HEIGHT]], // reverse offsets
81
regionFn0 = regionPoint ? points[regionPoint.charAt(0)]: NULL,
82
regionFn1 = (regionPoint && regionPoint !== 'cc') ? points[regionPoint.charAt(1)] : NULL,
83
nodeFn0 = point ? points[point.charAt(0)] : NULL,
84
nodeFn1 = (point && point !== 'cc') ? points[point.charAt(1)] : NULL;
87
xy = regionFn0(xy, offxy, regionPoint);
90
xy = regionFn1(xy, offxy, regionPoint);
94
xy = nodeFn0(xy, nodeoff, point);
97
xy = nodeFn1(xy, nodeoff, point);
104
this._resize(syncOnResize);
111
this.to.apply(this, this._syncArgs);
115
_resize: function(add) {
116
var handle = this._handle;
117
if (add && !handle) {
118
this._handle = Y.on('resize', this._onresize, window, this);
119
} else if (!add && handle) {
125
_onresize: function() {
127
setTimeout(function() { // for performance
133
* Aligns the center of a node to the center of another node or region.
135
* @parm region {Node || HTMLElement || Object} optional The node or
136
* region to align with. Defaults to the viewport region.
137
* the window is resized. If centering to viewport, this defaults
138
* to true, otherwise default is false.
140
center: function(region, resize) {
141
this.to(region, 'cc', 'cc', resize);
146
* Removes the resize handler, if any. This is called automatically
147
* when unplugged from the host node.
150
destroy: function() {
151
var handle = this._handle;
159
't': function(xy, off) {
163
'r': function(xy, off) {
164
return [xy[0] + off[0], xy[1]];
167
'b': function(xy, off) {
168
return [xy[0], xy[1] + off[1]];
171
'l': function(xy, off) {
175
'c': function(xy, off, point) {
176
var axis = (point[0] === 't' || point[0] === 'b') ? 0 : 1,
179
if (point === 'cc') {
180
ret = [xy[0] + off[0] / 2, xy[1] + off[1] / 2];
182
val = xy[axis] + off[axis] / 2;
183
ret = (axis) ? [xy[0], val] : [val, xy[1]];
190
Align.NAME = 'Align';
193
Align.prototype.constructor = Align;
195
Y.namespace('Plugin');
196
Y.Plugin.Align = Align;
200
}, '3.2.0' ,{requires:['node-region']});