Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('align-plugin', function (Y, NAME) {
2
 
3
    /**
4
     * Provides advanced positioning support for Node via a Plugin
5
     * for centering and alignment.
6
     * @module align-plugin
7
     */
8
 
9
    var OFFSET_WIDTH = 'offsetWidth',
10
        OFFSET_HEIGHT = 'offsetHeight',
11
        undefined = undefined;
12
 
13
    /**
14
     * Node plugin which can be used to align a node with another node,
15
     * region, or the viewport.
16
     *
17
     * @class Plugin.Align
18
     * @param {Object} User configuration object
19
     */
20
    function Align(config) {
21
        if (config.host) {
22
            this._host = config.host;
23
        }
24
    }
25
 
26
    Align.prototype = {
27
        /**
28
         * Aligns node with a point on another node or region.
29
         * Possible alignment points are:
30
         * <dl>
31
         *      <dt>tl</dt>
32
         *      <dd>top left</dd>
33
         *      <dt>tr</dt>
34
         *      <dd>top right</dd>
35
         *      <dt>bl</dt>
36
         *      <dd>bottom left</dd>
37
         *      <dt>br</dt>
38
         *      <dd>bottom right</dd>
39
         *      <dt>tc</dt>
40
         *      <dd>top center</dd>
41
         *      <dt>bc</dt>
42
         *      <dd>bottom center</dd>
43
         *      <dt>rc</dt>
44
         *      <dd>right center</dd>
45
         *      <dt>lc</dt>
46
         *      <dd>left center</dd>
47
         *      <dt>cc</dt>
48
         *      <dd>center center</dd>
49
         * </dl>
50
         * @method to
51
         * @param region {String|Node|HTMLElement|Object} The node or
52
         * region to align with. Defaults to the viewport region.
53
         * @param regionPoint {String} The point of the region to align with.
54
         * @param point {String} The point of the node aligned to the region.
55
         * @param resize {Boolean} Whether or not the node should re-align when
56
         * the window is resized. Defaults to false.
57
         */
58
        to: function(region, regionPoint, point, syncOnResize) {
59
            // cache original args for syncing
60
            this._syncArgs = Y.Array(arguments);
61
 
62
            if (region.top === undefined) {
63
                region = Y.one(region).get('region');
64
            }
65
 
66
            if (region) {
67
                var xy = [region.left, region.top],
68
                    offxy = [region.width, region.height],
69
                    points = Align.points,
70
                    node = this._host,
71
                    NULL = null,
72
                    size = node.getAttrs([OFFSET_HEIGHT, OFFSET_WIDTH]),
73
                    nodeoff = [0 - size[OFFSET_WIDTH], 0 - size[OFFSET_HEIGHT]], // reverse offsets
74
                    regionFn0 = regionPoint ? points[regionPoint.charAt(0)]: NULL,
75
                    regionFn1 = (regionPoint && regionPoint !== 'cc') ? points[regionPoint.charAt(1)] : NULL,
76
                    nodeFn0 = point ? points[point.charAt(0)] : NULL,
77
                    nodeFn1 = (point && point !== 'cc') ? points[point.charAt(1)] : NULL;
78
 
79
                if (regionFn0) {
80
                    xy = regionFn0(xy, offxy, regionPoint);
81
                }
82
                if (regionFn1) {
83
                    xy = regionFn1(xy, offxy, regionPoint);
84
                }
85
 
86
                if (nodeFn0) {
87
                    xy = nodeFn0(xy, nodeoff, point);
88
                }
89
                if (nodeFn1) {
90
                    xy = nodeFn1(xy, nodeoff, point);
91
                }
92
 
93
                if (xy && node) {
94
                    node.setXY(xy);
95
                }
96
 
97
                this._resize(syncOnResize);
98
 
99
            }
100
            return this;
101
        },
102
 
103
        sync: function() {
104
            this.to.apply(this, this._syncArgs);
105
            return this;
106
        },
107
 
108
        _resize: function(add) {
109
            var handle = this._handle;
110
            if (add && !handle) {
111
                this._handle = Y.on('resize', this._onresize, window, this);
112
            } else if (!add && handle) {
113
                handle.detach();
114
            }
115
 
116
        },
117
 
118
        _onresize: function() {
119
            var self = this;
120
            setTimeout(function() { // for performance
121
                self.sync();
122
            });
123
        },
124
 
125
        /**
126
         * Aligns the center of a node to the center of another node or region.
127
         * @method center
128
         * @param region {Node|HTMLElement|Object} optional The node or
129
         * region to align with. Defaults to the viewport region.
130
         * the window is resized. If centering to viewport, this defaults
131
         * to true, otherwise default is false.
132
         */
133
        center: function(region, resize) {
134
            this.to(region, 'cc', 'cc', resize);
135
            return this;
136
        },
137
 
138
        /**
139
         * Removes the resize handler, if any. This is called automatically
140
         * when unplugged from the host node.
141
         * @method destroy
142
         */
143
        destroy: function() {
144
            var handle = this._handle;
145
            if (handle) {
146
                handle.detach();
147
            }
148
        }
149
    };
150
 
151
    Align.points = {
152
        't': function(xy, off) {
153
            return xy;
154
        },
155
 
156
        'r': function(xy, off) {
157
            return [xy[0] + off[0], xy[1]];
158
        },
159
 
160
        'b': function(xy, off) {
161
            return [xy[0], xy[1] + off[1]];
162
        },
163
 
164
        'l': function(xy, off) {
165
            return xy;
166
        },
167
 
168
        'c': function(xy, off, point) {
169
            var axis = (point[0] === 't' || point[0] === 'b') ?  0 : 1,
170
                ret, val;
171
 
172
            if (point === 'cc') {
173
                ret = [xy[0] + off[0] / 2, xy[1] + off[1] / 2];
174
            } else {
175
                val = xy[axis] + off[axis] / 2;
176
                ret = (axis) ? [xy[0], val] : [val, xy[1]];
177
            }
178
 
179
             return ret;
180
        }
181
    };
182
 
183
    Align.NAME = 'Align';
184
    Align.NS = 'align';
185
 
186
    Align.prototype.constructor = Align;
187
 
188
    Y.namespace('Plugin');
189
    Y.Plugin.Align = Align;
190
 
191
 
192
 
193
}, '3.18.1', {"requires": ["node-screen", "node-pluginhost"]});