Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / draw / SpriteDD.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 // private - DD implementation for Panels
16 Ext.define('Ext.draw.SpriteDD', {
17     extend: 'Ext.dd.DragSource',
18
19     constructor : function(sprite, cfg){
20         var me = this,
21             el = sprite.el;
22         me.sprite = sprite;
23         me.el = el;
24         me.dragData = {el: el, sprite: sprite};
25         me.callParent([el, cfg]);
26         me.sprite.setStyle('cursor', 'move');
27     },
28
29     showFrame: Ext.emptyFn,
30     createFrame : Ext.emptyFn,
31
32     getDragEl : function(e){
33         return this.el;
34     },
35     
36     getRegion: function() {
37         var me = this,
38             el = me.el,
39             pos, x1, x2, y1, y2, t, r, b, l, bbox, sprite;
40         
41         sprite = me.sprite;
42         bbox = sprite.getBBox();
43         
44         try {
45             pos = Ext.Element.getXY(el);
46         } catch (e) { }
47
48         if (!pos) {
49             return null;
50         }
51
52         x1 = pos[0];
53         x2 = x1 + bbox.width;
54         y1 = pos[1];
55         y2 = y1 + bbox.height;
56         
57         return Ext.create('Ext.util.Region', y1, x2, y2, x1);
58     },
59
60     /*
61       TODO(nico): Cumulative translations in VML are handled
62       differently than in SVG. While in SVG we specify the translation
63       relative to the original x, y position attributes, in VML the translation
64       is a delta between the last position of the object (modified by the last
65       translation) and the new one.
66       
67       In VML the translation alters the position
68       of the object, we should change that or alter the SVG impl.
69     */
70      
71     startDrag: function(x, y) {
72         var me = this,
73             attr = me.sprite.attr;
74         me.prev = me.sprite.surface.transformToViewBox(x, y);
75     },
76
77     onDrag: function(e) {
78         var xy = e.getXY(),
79             me = this,
80             sprite = me.sprite,
81             attr = sprite.attr, dx, dy;
82         xy = me.sprite.surface.transformToViewBox(xy[0], xy[1]);
83         dx = xy[0] - me.prev[0];
84         dy = xy[1] - me.prev[1];
85         sprite.setAttributes({
86             translate: {
87                 x: attr.translation.x + dx,
88                 y: attr.translation.y + dy
89             }
90         }, true);
91         me.prev = xy;
92     },
93
94     setDragElPos: function () {
95         // Disable automatic DOM move in DD that spoils layout of VML engine.
96         return false;
97     }
98 });