Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / draw / SpriteDD.js
1 // private - DD implementation for Panels
2 Ext.define('Ext.draw.SpriteDD', {
3     extend: 'Ext.dd.DragSource',
4
5     constructor : function(sprite, cfg){
6         var me = this,
7             el = sprite.el;
8         me.sprite = sprite;
9         me.el = el;
10         me.dragData = {el: el, sprite: sprite};
11         me.callParent([el, cfg]);
12         me.sprite.setStyle('cursor', 'move');
13     },
14
15     showFrame: Ext.emptyFn,
16     createFrame : Ext.emptyFn,
17
18     getDragEl : function(e){
19         return this.el;
20     },
21     
22     getRegion: function() {
23         var me = this,
24             el = me.el,
25             pos, x1, x2, y1, y2, t, r, b, l, bbox, sprite;
26         
27         sprite = me.sprite;
28         bbox = sprite.getBBox();
29         
30         try {
31             pos = Ext.core.Element.getXY(el);
32         } catch (e) { }
33
34         if (!pos) {
35             return null;
36         }
37
38         x1 = pos[0];
39         x2 = x1 + bbox.width;
40         y1 = pos[1];
41         y2 = y1 + bbox.height;
42         
43         return Ext.create('Ext.util.Region', y1, x2, y2, x1);
44     },
45
46     /*
47       TODO(nico): Cumulative translations in VML are handled
48       differently than in SVG. While in SVG we specify the translation
49       relative to the original x, y position attributes, in VML the translation
50       is a delta between the last position of the object (modified by the last
51       translation) and the new one.
52       
53       In VML the translation alters the position
54       of the object, we should change that or alter the SVG impl.
55     */
56      
57     startDrag: function(x, y) {
58         var me = this,
59             attr = me.sprite.attr,
60             trans = attr.translation;
61         if (me.sprite.vml) {
62             me.prevX = x + attr.x;
63             me.prevY = y + attr.y;
64         } else {
65             me.prevX = x - trans.x;
66             me.prevY = y - trans.y;
67         }
68     },
69
70     onDrag: function(e) {
71         var xy = e.getXY(),
72             me = this,
73             sprite = me.sprite,
74             attr = sprite.attr;
75         me.translateX = xy[0] - me.prevX;
76         me.translateY = xy[1] - me.prevY;
77         sprite.setAttributes({
78             translate: {
79                 x: me.translateX,
80                 y: me.translateY
81             }
82         }, true);
83         if (sprite.vml) {
84             me.prevX = xy[0] + attr.x || 0;
85             me.prevY = xy[1] + attr.y || 0;
86         }
87     }
88 });