MDL-35616 import YUI 3.7.2
[moodle.git] / lib / yuilib / 3.7.2 / build / dd-proxy / dd-proxy-debug.js
blobabb761b35c3399db9f1693cb97bf852cab188baa
1 /*
2 YUI 3.7.2 (build 5639)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('dd-proxy', function (Y, NAME) {
10     /**
11      * Plugin for dd-drag for creating a proxy drag node, instead of dragging the original node.
12      * @module dd
13      * @submodule dd-proxy
14      */
15     /**
16      * Plugin for dd-drag for creating a proxy drag node, instead of dragging the original node.
17      * @class DDProxy
18      * @extends Base
19      * @constructor
20      * @namespace Plugin     
21      */
22     var DDM = Y.DD.DDM,
23         NODE = 'node',
24         DRAG_NODE = 'dragNode',
25         HOST = 'host',
26         TRUE = true, proto,
27         P = function(config) {
28             P.superclass.constructor.apply(this, arguments);
29         };
30     
31     P.NAME = 'DDProxy';
32     /**
33     * @property NS
34     * @default con
35     * @readonly
36     * @protected
37     * @static
38     * @description The Proxy instance will be placed on the Drag instance under the proxy namespace.
39     * @type {String}
40     */
41     P.NS = 'proxy';
43     P.ATTRS = {
44         host: {
45         },
46         /**
47         * @attribute moveOnEnd
48         * @description Move the original node at the end of the drag. Default: true
49         * @type Boolean
50         */
51         moveOnEnd: {
52             value: TRUE
53         },
54         /**
55         * @attribute hideOnEnd
56         * @description Hide the drag node at the end of the drag. Default: true
57         * @type Boolean
58         */
59         hideOnEnd: {
60             value: TRUE
61         },
62         /**
63         * @attribute resizeFrame
64         * @description Make the Proxy node assume the size of the original node. Default: true
65         * @type Boolean
66         */
67         resizeFrame: {
68             value: TRUE
69         },
70         /**
71         * @attribute positionProxy
72         * @description Make the Proxy node appear in the same place as the original node. Default: true
73         * @type Boolean
74         */
75         positionProxy: {
76             value: TRUE
77         },
78         /**
79         * @attribute borderStyle
80         * @description The default border style for the border of the proxy. Default: 1px solid #808080
81         * @type Boolean
82         */
83         borderStyle: {
84             value: '1px solid #808080'
85         },
86         /**
87         * @attribute cloneNode
88         * @description Should the node be cloned into the proxy for you. Default: false
89         * @type Boolean
90         */
91         cloneNode: {
92             value: false
93         }
94     };
96     proto = {
97         /**
98         * @private
99         * @property _hands
100         * @description Holds the event handles for setting the proxy
101         */
102         _hands: null,
103         /**
104         * @private
105         * @method _init
106         * @description Handler for the proxy config attribute
107         */
108         _init: function() {
109             if (!DDM._proxy) {
110                 DDM._createFrame();
111                 Y.on('domready', Y.bind(this._init, this));
112                 return;
113             }
114             if (!this._hands) {
115                 this._hands = [];
116             }
117             var h, h1, host = this.get(HOST), dnode = host.get(DRAG_NODE);
118             if (dnode.compareTo(host.get(NODE))) {
119                 if (DDM._proxy) {
120                     host.set(DRAG_NODE, DDM._proxy);
121                 }
122             }
123             Y.each(this._hands, function(v) {
124                 v.detach();
125             });
126             h = DDM.on('ddm:start', Y.bind(function() {
127                 if (DDM.activeDrag === host) {
128                     DDM._setFrame(host);
129                 }
130             }, this));
131             h1 = DDM.on('ddm:end', Y.bind(function() {
132                 if (host.get('dragging')) {
133                     if (this.get('moveOnEnd')) {
134                         host.get(NODE).setXY(host.lastXY);
135                     }
136                     if (this.get('hideOnEnd')) {
137                         host.get(DRAG_NODE).setStyle('display', 'none');
138                     }
139                     if (this.get('cloneNode')) {
140                         host.get(DRAG_NODE).remove();
141                         host.set(DRAG_NODE, DDM._proxy);
142                     }
143                 }
144             }, this));
145             this._hands = [h, h1];
146         },
147         initializer: function() {
148             this._init();
149         },
150         destructor: function() {
151             var host = this.get(HOST);
152             Y.each(this._hands, function(v) {
153                 v.detach();
154             });
155             host.set(DRAG_NODE, host.get(NODE));
156         },
157         clone: function() {
158             var host = this.get(HOST),
159                 n = host.get(NODE),
160                 c = n.cloneNode(true);
162             delete c._yuid;
163             c.setAttribute('id', Y.guid());
164             c.setStyle('position', 'absolute');
165             n.get('parentNode').appendChild(c);
166             host.set(DRAG_NODE, c);
167             return c;
168         }
169     };
170     
171     Y.namespace('Plugin');
172     Y.extend(P, Y.Base, proto);
173     Y.Plugin.DDProxy = P;
175     //Add a couple of methods to the DDM
176     Y.mix(DDM, {
177         /**
178         * @private
179         * @for DDM
180         * @namespace DD
181         * @method _createFrame
182         * @description Create the proxy element if it doesn't already exist and set the DD.DDM._proxy value
183         */
184         _createFrame: function() {
185             if (!DDM._proxy) {
186                 DDM._proxy = TRUE;
188                 var p = Y.Node.create('<div></div>'),
189                 b = Y.one('body');
191                 p.setStyles({
192                     position: 'absolute',
193                     display: 'none',
194                     zIndex: '999',
195                     top: '-999px',
196                     left: '-999px'
197                 });
199                 b.prepend(p);
200                 p.set('id', Y.guid());
201                 p.addClass(DDM.CSS_PREFIX + '-proxy');
202                 DDM._proxy = p;
203             }
204         },
205         /**
206         * @private
207         * @for DDM
208         * @namespace DD
209         * @method _setFrame
210         * @description If resizeProxy is set to true (default) it will resize the proxy element to match the size of the Drag Element.
211         * If positionProxy is set to true (default) it will position the proxy element in the same location as the Drag Element.
212         */
213         _setFrame: function(drag) {
214             var n = drag.get(NODE), d = drag.get(DRAG_NODE), ah, cur = 'auto';
215             
216             ah = DDM.activeDrag.get('activeHandle');
217             if (ah) {
218                 cur = ah.getStyle('cursor');
219             }
220             if (cur == 'auto') {
221                 cur = DDM.get('dragCursor');
222             }
224             d.setStyles({
225                 visibility: 'hidden',
226                 display: 'block',
227                 cursor: cur,
228                 border: drag.proxy.get('borderStyle')
229             });
231             if (drag.proxy.get('cloneNode')) {
232                 d = drag.proxy.clone();
233             }
235             if (drag.proxy.get('resizeFrame')) {
236                 d.setStyles({
237                     height: n.get('offsetHeight') + 'px',
238                     width: n.get('offsetWidth') + 'px'
239                 });
240             }
242             if (drag.proxy.get('positionProxy')) {
243                 d.setXY(drag.nodeXY);
244             }
245             d.setStyle('visibility', 'visible');
246         }
247     });
249     //Create the frame when DOM is ready
250     //Y.on('domready', Y.bind(DDM._createFrame, DDM));
255 }, '3.7.2', {"requires": ["dd-drag"]});