Subversion Repositories wimsdev

Rev

Rev 11524 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
20 reyssat 1
/*
2
   DynAPI Distribution
3
   DragEvent Class
4
 
5
   The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
6
*/
7
 
8
// DragEvent object
9
function DragEvent(type,src) {
10
        this.MouseEvent = MouseEvent;
11
        this.MouseEvent();
12
        this.DynEvent()
13
        this.isDragging = false;
14
}
15
var p = dynapi.setPrototype('DragEvent','MouseEvent');
16
p.getX=function() {return this.x};
17
p.getY=function() {return this.y};
18
p.getPageX=function() {return this.pageX};
19
p.getPageY=function() {return this.pageY};
20
p.cancelDrag=function() {this.isDragging=false};
21
 
22
//DragEvent.dragPlay=0;
23
 
24
DragEvent.dragevent = new DragEvent();
25
 
26
DragEvent.lyrListener = {
27
        onmousedown : function(e) {
28
                DragEvent.startDrag(e);
29
                //e.preventDefault();
30
        }
31
};
32
 
33
DragEvent.startDrag = function(e,dlyr) {
34
        var origdlyr = dlyr;
35
        if (!dlyr) dlyr = e.getSource();
36
 
37
        if (dynapi.ua.dom) {
38
                dlyr.elm.ondragstart = function() { return false; }
39
                dlyr.elm.onselectstart = function() { return false; }
40
        }
41
 
42
        // Initialize dragEvent object
43
        var de=DragEvent.dragevent;
44
        //de.bubble = true;
45
        de.src = dlyr;
46
        de.origin = (origdlyr)? e.origin : dlyr;
47
        de.x = e.getPageX()-dlyr.getPageX();
48
        de.y = e.getPageY()-dlyr.getPageY();
49
        de.pageX = e.getPageX();
50
        de.pageY = e.getPageY();
51
        de.parentPageX = dlyr.parent.getPageX();
52
        de.parentPageY = dlyr.parent.getPageY();
53
 
54
        de.isDragging = true;
55
 
56
        e.preventDefault();
57
        e.preventBubble();
58
 
59
        //dlyr._dyndoc.addEventListener(DragEvent.docListener);
60
 
61
        dlyr.invokeEvent("dragstart",de);
62
}
63
 
64
DragEvent.docListener = {
65
        onmousemove : function(e) {
66
                //var x = e.getPageX();
67
                //var y = e.getPageY();
68
                //dynapi.debug.status('drag move '+e.x+' '+e.y);
69
 
70
                var de = DragEvent.dragevent;
71
                if (de && de.isDragging) {
72
 
73
 
74
                        var lyr = de.src;
75
                        if (!lyr) return;
76
 
77
                        // DS: what is this?
78
                        // Detect if we should start the drag
79
                        /*if(DragEvent.dragPlay==0 || (Math.abs(de.pageX-e.getPageX())-DragEvent.dragPlay>0) || (Math.abs(de.pageY-e.getPageY())-DragEvent.dragPlay>0)) {
80
                                de.isDragging=true;
81
                                de.src.invokeEvent("dragstart",de);
82
                                e.setBubble(de.bubble);
83
                        }
84
                        */
85
                        /*else if (!de.dragEnabled) {
86
                                // This allows 'cancelDrag' method to fire the mouseUp as if had been released by the user
87
                                lyr.invokeEvent("mouseup");
88
                                return;
89
                        }*/
90
 
91
                        // Properties
92
                        de.type="dragmove";
93
                        de.pageX=e.getPageX();
94
                        de.pageY=e.getPageY();
95
 
96
                        /*if (DragEvent.stopAtDocumentEdge) {
97
                                if (de.pageX<0) de.pageX = 0;
98
                                if (de.pageY<0) de.pageY = 0;
99
                                if (de.pageX>DynAPI.document.w) de.pageX = DynAPI.document.w;
100
                                if (de.pageY>DynAPI.document.h) de.pageY = DynAPI.document.h;
101
                        }*/
102
 
103
                        var x=de.pageX-de.parentPageX-de.x;
104
                        var y=de.pageY-de.parentPageY-de.y;
105
 
106
                        // Respect boundary, if any
107
                        if (lyr._dragBoundary) {
108
                                var dB = lyr._dragBoundary;
109
                                var t = dB.top;
110
                                var r = dB.right;
111
                                var b = dB.bottom;
112
                                var l = dB.left
113
                                if (x<l) x = l;
114
                                else if (x>lyr.parent.w-lyr.w-r) x = lyr.parent.w-lyr.w-r;
115
                                if (y<t) y = t;
116
                                else if (y>lyr.parent.h-lyr.h-b) y = lyr.parent.h-lyr.h-b;
117
                        }
118
                        else if (lyr._dragBoundaryA) {
119
                                var dB = lyr._dragBoundaryA;
120
                                var b=dB[2];
121
                                var r=dB[1];
122
                                var l=dB[3];
123
                                var t=dB[0];
124
                                var w=lyr.w;
125
                                var h=lyr.h;
126
                                if (x<l) x=l;
127
                                else if (x+w>r) x=r-w;
128
                                if (y<t) y=t;
129
                                else if (y+h>b) y=b-h;
130
                        }
131
                        // Move dragged layer
132
                        lyr.setLocation(x,y);
133
                        lyr.invokeEvent("dragmove",de);
134
 
135
                        if (lyr._dragStealth==false && lyr.parent.DragOver) {
136
                                lyr.parent.DragOver(lyr);
137
                        }
138
 
139
                        e.preventDefault();
140
                        e.preventBubble();
141
                }
142
        },
143
        onmouseup : function(e) {
144
                // Get, if any, the currently drag in process and the layer. If none, return
145
                var de=DragEvent.dragevent;
146
                //de.bubble = true;
147
                if (!de) return;
148
                var lyr=de.src;
149
                if (!lyr) return;
150
 
151
                if (!de.isDragging) {
152
                de.type="dragend";
153
                de.src=null;
154
                //e.setBubble(true);
155
                        return;
156
                }
157
                if (dynapi.ua.ie) lyr.doc.body.onselectstart = null;
158
 
159
                // Avoid click for the dragged layer ( with MouseEvent addition )
160
                if (dynapi.ua.def) dynapi.wasDragging=true;
161
                if (lyr.parent.DragDrop) lyr.parent.DragDrop(lyr);
162
                // Properties for the event
163
                de.type="dragend";
164
                de.isDragging=false;
165
                lyr.invokeEvent("dragend",de);
166
 
167
                // Clean drag stuff
168
                de.src=null;
169
                //e.preventDefault();
170
                e.preventBubble();
171
 
172
                //lyr._dyndoc.removeEventListener(DragEvent.docListener);
173
        }
174
};
175
DragEvent.stopAtDocumentEdge = true;
176
DragEvent.setDragBoundary=function(lyr,t,r,b,l) {
177
        if (!lyr) {dynapi.debug.print("Error: no object passed to DragEvent.setDragBoundary()"); return;}
178
        var a=arguments;
179
        if (a.length==0) return;
180
        if (a.length==1) {
181
                lyr._dragBoundary = {left:0,right:0,top:0,bottom:0};
182
        }
183
        if (a.length==2) {
184
                lyr._dragBoundary = arguments[1];
185
        }
186
        else if (a.length==5) lyr._dragBoundaryA = [t,r,b,l];
187
};
188
DragEvent.enableDragEvents=function() {
189
        for (var i=0;i<arguments.length;i++) {
190
                var lyr=arguments[i];
191
                if (!lyr) {dynapi.debug.print("Error: no object passed to DragEvent.enableDragEvents()"); return;}
192
                if (lyr.isClass('DynLayer')) lyr.addEventListener(DragEvent.lyrListener);
193
        }
194
        dynapi.document.addEventListener(DragEvent.docListener);
195
        dynapi.document.captureMouseEvents();
196
};
197
DragEvent.disableDragEvents=function() {
198
        for (var i=0;i<arguments.length;i++) {
199
                var lyr=arguments[i];
200
                lyr.removeEventListener(DragEvent.lyrListener);
201
        }
202
};
203
 
204
DynLayer.setDragMode = function(b,boundry){
205
        if(!self.DragEvent) return false;
206
        if(boundry)DragEvent.setDragBoundary(this,boundry);
207
        if (b) DragEvent.enableDragEvents(this);
208
        else DragEvent.disableDragEvents(this);
209
        return true;   
210
};
211
DynLayer.prototype.setDragOverStealthMode = function(b){
212
        this._dragStealth=(b)? true:false;
213
};
214
// Enable ondrop event
215
DynElement.prototype.DragDrop=function(s){
216
        if (!this.children.length>0) return false;
217
        var ch,chX,sX,sY;
218
        for (var i in this.children) {
219
                ch=this.children[i];
220
                if(ch._hasDragEvents){
221
                        chX=ch.getPageX();
222
                        chY=ch.getPageY();
223
                        sX=s.getPageX();
224
                        sY=s.getPageY();
225
                        if (chX<sX && chX+ch.w>sX+s.w && chY<sY && chY+ch.h>sY+s.h) {
226
                                if (ch.DragDrop(s)) return true;
227
                                ch.invokeEvent("drop");
228
                                return true;
229
                        }
230
                }
231
        }
232
        return false;
233
};
234
 
235
// Enable ondragover event
236
DynElement.prototype.DragOver=function(s){
237
        if (!this.children.length>0) return false;
238
        var ch,chX,sX,sY;
239
        for (var i in this.children) {
240
                ch=this.children[i];
241
                if(ch._hasDragEvents){
242
                        chX=ch.getPageX();
243
                        chY=ch.getPageY();
244
                        sX=s.getPageX();
245
                        sY=s.getPageY();
246
                        if (chX<sX && chX+ch.w>sX+s.w && chY<sY && chY+ch.h>sY+s.h) {
247
                                if (ch.DragDrop(s)) return true;
248
                                ch._isDragOver=true;
249
                                ch.invokeEvent("dragover");                    
250
                                return true;
251
                        }else if (ch._isDragOver) {
252
                                ch._isDragOver=false;
253
                                ch.invokeEvent("dragout");                     
254
                        }
255
                }
256
        }
257
        return false;
258
};