Subversion Repositories wimsdev

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
20 reyssat 1
/*
2
    DynAPI Distribution
3
    SnapX Class by Leif Westerlind <warp-9.9 (at) usa (dot) net>
4
 
5
    The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
6
 
7
    requires: Dynlayer
8
*/
9
 
10
var p = DynLayer.prototype;
11
p._SnapSetLocation = p.setLocation;
12
p.setLocation  = function(x,y){
13
        if(Math.abs(x-this._snapLockX)>20) this._snapLockX=null;
14
        if(Math.abs(y-this._snapLockY)>20) this._snapLockY=null;
15
        this._SnapSetLocation(this._snapLockX||x,this._snapLockY||y);
16
        status=this.lockX
17
        if (this.isSnapEnabled) {
18
                var dirX = '', dirY = '';
19
 
20
                // get direction
21
                if (this._snapX != this.x){
22
                        if (this._snapX < this.x){
23
                                dirX="E";
24
                        }
25
                        else {
26
                                dirX="W";
27
                        }
28
                }
29
                if (this._snapY != this.y){
30
                        if (this._snapY < this.y){
31
                                dirY="S";
32
                        }
33
                        else {
34
                                dirY="N";
35
                        }
36
                }
37
 
38
                this._snapX = this.x;
39
                this._snapY = this.y;
40
                this._snapDirection = dirY + dirX;
41
                this._checkForSnap();  
42
        }
43
};
44
p.enableSnap = function (){
45
    this.isSnapEnabled = true;
46
    this.setSnapBoundary();
47
    this.setSnapBoundaryType();
48
 
49
    this._snapX = this.x;
50
    this._snapY = this.y;
51
 
52
/*    this.snapEvent ={
53
        onlocationchange : function(e) {
54
            var dirX = '', dirY = '', me = e.getSource();
55
 
56
            // get direction
57
            if (me._snapX != me.x){
58
                if (me._snapX < me.x){
59
                    dirX="E";
60
                }
61
                else {
62
                    dirX="W";
63
                }
64
            }
65
            if (me._snapY != me.y){
66
                if (me._snapY < me.y){
67
                    dirY="S";
68
                }
69
                else {
70
                    dirY="N";
71
                }
72
            }
73
 
74
            me._snapX = me.x;
75
            me._snapY = me.y;
76
            me._snapDirection = dirY + dirX;
77
            me._checkForSnap();
78
        }
79
    }
80
    this.addEventListener(this.snapEvent);
81
*/
82
};
83
p.disableSnap = function(){
84
    this.isSnapEnabled = false;
85
};
86
p.setSnapBoundaryType = function(snapBoundaryType){
87
    var def = 'outer';
88
    if (arguments.length == 0){
89
        this._snapBoundaryType = def;
90
    }
91
    else {
92
        this._snapBoundaryType = snapBoundaryType;
93
    }
94
};
95
p.getSnapBoundaryType = function(){
96
    return(this._snapBoundaryType);
97
};
98
 
99
DynLayer._snapBoundaryDefault = 25;
100
p.setSnapBoundaryDefault = function(b){
101
    DynLayer._snapBoundaryDefault = b;
102
}
103
 
104
p.getSnapBoundaryDefault = function(){
105
    return(DynLayer._snapBoundaryDefault);
106
}
107
 
108
/*
109
INPUT:
110
 
111
        1 arg: if snapBoundaryType set boundary to default,
112
                if boundary set for all sides and snapBoundaryType inner & outer
113
        2 args: snapBoundaryType, boundary for all sides
114
        5 args: snapBoundaryType, t, r, b, l
115
*/
116
p.setSnapBoundary = function(){
117
        var a=arguments;
118
        var snapDefault = DynLayer._snapBoundaryDefault;
119
    if (a.length == 0){
120
        this.setSnapBoundaryType('both');
121
        this._snapBndTi = snapDefault;
122
        this._snapBndRi = snapDefault;
123
        this._snapBndBi = snapDefault;
124
        this._snapBndLi = snapDefault;
125
        this._snapBndTo = snapDefault;
126
        this._snapBndRo = snapDefault;
127
        this._snapBndBo = snapDefault;
128
        this._snapBndLo = snapDefault;
129
    }
130
    if (a.length == 1){
131
        if(a[0] == 'inner' || a[0] == 'outer' || a[0] == 'both'){
132
            this.setSnapBoundaryType(a[0]);
133
            this._snapBndTi = snapDefault;
134
            this._snapBndRi = snapDefault;
135
            this._snapBndBi = snapDefault;
136
            this._snapBndLi = snapDefault;
137
            this._snapBndTo = snapDefault;
138
            this._snapBndRo = snapDefault;
139
            this._snapBndBo = snapDefault;
140
            this._snapBndLo = snapDefault;
141
        }
142
        else {
143
            this.setSnapBoundaryType('both');
144
            this._snapBndTi = a[0];
145
            this._snapBndRi = a[0];
146
            this._snapBndBi = a[0];
147
            this._snapBndLi = a[0];
148
            this._snapBndTo = a[0];
149
            this._snapBndRo = a[0];
150
            this._snapBndBo = a[0];
151
            this._snapBndLo = a[0];
152
        }
153
    }
154
    else if (a.length == 2){
155
        var snapBoundaryType = arguments[0];
156
 
157
        if (snapBoundaryType == 'inner'){
158
            this.setSnapBoundaryType('inner');
159
            this._snapBndTi = a[1];
160
            this._snapBndRi = a[1];
161
            this._snapBndBi = a[1];
162
            this._snapBndLi = a[1];
163
        }
164
        else if (snapBoundaryType == 'outer'){
165
            this.setSnapBoundaryType('outer');
166
            this._snapBndTo = a[1];
167
            this._snapBndRo = a[1];
168
            this._snapBndBo = a[1];
169
            this._snapBndLo = a[1];
170
        }
171
        else if (snapBoundaryType == 'both' || snapBoundaryType == null){
172
            this.setSnapBoundaryType('both');
173
            this._snapBndTi = a[1];
174
            this._snapBndRi = a[1];
175
            this._snapBndBi = a[1];
176
            this._snapBndLi = a[1];
177
            this._snapBndTo = a[1];
178
            this._snapBndRo = a[1];
179
            this._snapBndBo = a[1];
180
            this._snapBndLo = a[1];
181
        }
182
    }
183
    else if (a.length == 5){
184
        if(a[0] == 'inner' || a[0] == 'outer' || a[0] == 'both'){
185
            this.setSnapBoundaryType(a[0]);
186
        }
187
 
188
        if (this._snapBoundaryType == 'inner'){
189
            this._snapBndTi = a[1];
190
            this._snapBndRi = a[2];
191
            this._snapBndBi = a[3];
192
            this._snapBndLi = a[4];
193
        }
194
        else if (this._snapBoundaryType == 'outer'){
195
            this._snapBndTo = a[1];
196
            this._snapBndRo = a[2];
197
            this._snapBndBo = a[3];
198
            this._snapBndLo = a[4];
199
        }
200
        else if (this._snapBoundaryType == 'both'){
201
            this._snapBndTi = a[1];
202
            this._snapBndRi = a[2];
203
            this._snapBndBi = a[3];
204
            this._snapBndLi = a[4];
205
            this._snapBndTo = a[1];
206
            this._snapBndRo = a[2];
207
            this._snapBndBo = a[3];
208
            this._snapBndLo = a[4];
209
        }
210
    }
211
    else {
212
        this.setSnapBoundaryType('both');
213
        this._snapBndTo = snapDefault;
214
        this._snapBndRo = snapDefault;
215
        this._snapBndBo = snapDefault;
216
        this._snapBndLo = snapDefault;
217
        this._snapBndTi = snapDefault;
218
        this._snapBndRi = snapDefault;
219
        this._snapBndBi = snapDefault;
220
        this._snapBndLi = snapDefault;
221
    }
222
};
223
 
224
p.getSnapBoundary = function(t){
225
    var To,Ro,Bo,Lo,Ti,Ri,Bi,Li,bndAry,X,Y,W,H;
226
 
227
    X = this.x;
228
    Y = this.y;
229
    W = this.w;
230
    H = this.h;
231
 
232
    Ti = Y + this._snapBndTi;
233
    Ri = X + W - this._snapBndRi;
234
    Bi = Y + H - this._snapBndBi;
235
    Li = X + this._snapBndLi;
236
 
237
    To = Y - this._snapBndTo;
238
    Ro = X + W + this._snapBndRo;
239
    Bo = Y + H + this._snapBndBo;
240
    Lo = X - this._snapBndLo;
241
 
242
    if (t==null) bndAry = [Ti,Ri,Bi,Li,To,Ro,Bo,Lo];
243
    else {
244
        if (t=='inner') bndAry = [Ti,Ri,Bi,Li];
245
        else if (t=='outer') bndAry = [To,Ro,Bo,Lo];
246
        else if (t=='both') bndAry = [Ti,Ri,Bi,Li,To,Ro,Bo,Lo];
247
    }
248
    return(bndAry);
249
};
250
 
251
p._checkForSnap = function(){
252
    switch (this._snapBoundaryType) {
253
        case 'outer' :
254
            this._checkForSnapOuter();
255
            break;
256
        case 'inner' :
257
            this._checkForSnapInner();
258
            break;
259
        case 'both' :
260
            this._checkForSnapInner();
261
            this._checkForSnapOuter();
262
            break;
263
        default:
264
            return(false);
265
    }
266
};
267
 
268
p.getSnapDirection = function (){
269
    return(this._snapDirection);
270
};
271
 
272
p._checkForSnapInner = function(){
273
    if (!this.parent.children.length>0) return(false);
274
    if (!this.isSnapEnabled==true) return(false);
275
 
276
    var ch,chX1,chY1,chX2,chY2,chBX1,chBY1,chBX2,chBY2
277
    var sX1,sY1,sX2,sY2,chBndAry;
278
    var C1,C2a,C2b,C3,C4a,C4b,C5,C6a,C6b,C7,C8a,C8b,sDir;
279
 
280
    sX1 = this.x;
281
    sY1 = this.y;
282
    sW  = this.w;
283
    sH  = this.h;
284
    sX2 = sX1 + sW;
285
    sY2 = sY1 + sH;
286
    sDir = this.getSnapDirection();
287
 
288
    for (var i in this.parent.children){
289
        ch = this.parent.children[i];
290
        if (ch != this && ch.isSnapEnabled == true){
291
            chX1 = ch.x;
292
            chY1 = ch.y;
293
            chX2 = chX1 + ch.w;
294
            chY2 = chY1 + ch.h;
295
            chBndAry = ch.getSnapBoundary('inner');
296
            chBiX1 = chBndAry[3];
297
            chBiY1 = chBndAry[0];
298
            chBiX2 = chBndAry[1];
299
            chBiY2 = chBndAry[2];
300
 
301
            // inner top-left corner, source top-left, move N, NW, W
302
            C1  = (sX1 <= chBiX1 && sX1 >  chX1   && sY1 <= chBiY1 && sY1 > chY1   && (sDir == 'N' || sDir == 'NW' || sDir == 'W'));
303
 
304
            // inner top-middle side, source top-left, move NE, N, NW
305
            C2a = (sX1 >  chBiX1 && sX1 <  chBiX2 && sY1 <= chBiY1 && sY1 > chY1   && (sDir == 'NE' || sDir == 'N' || sDir == 'NW'));
306
 
307
            // inner top-middle side, source top-right, move NE, N, NW
308
            C2b = (sX2 >  chBiX1 && sX2 <  chBiX2 && sY1 <= chBiY1 && sY1 > chY1   && (sDir == 'NE' || sDir == 'N' || sDir == 'NW'));
309
 
310
            // inner top-right corner, source top-right, move E, NE, N
311
            C3  = (sX2 >= chBiX2 && sX2 <  chX2   && sY1 <= chBiY1 && sY1 > chY1   && (sDir == 'E' || sDir == 'NE' || sDir == 'N'));
312
 
313
            // inner right-middle side, source top-right, move SE, E, NE
314
            C4a = (sX2 >= chBiX2 && sX2 <  chX2   && sY1 >  chBiY1 && sY1 < chBiY2 && (sDir == 'SE' || sDir == 'E' || sDir == 'NE'));
315
 
316
            // inner right-middle side, source bottom-right, move SE, E, NE
317
            C4b = (sX2 >= chBiX2 && sX2 <  chX2   && sY2 >  chBiY1 && sY2 < chBiY2 && (sDir == 'SE' || sDir == 'E' || sDir == 'NE'));
318
 
319
            // inner bottom-right corner, source lower-right, move dir E, SE, S
320
            C5  = (sX2 >= chBiX2 && sX2 <  chX2   && sY2 >= chBiY2 && sY2 < chY2   && (sDir == 'E' || sDir == 'SE' || sDir == 'S'));
321
 
322
            // inner bottom-middle side, source lower-left, move SW, S, SE
323
            C6a = (sX1 >  chBiX1 && sX1 <  chBiX2 && sY2 >= chBiY2 && sY2 < chY2   && (sDir == 'SW' || sDir == 'S' || sDir == 'SE'));
324
 
325
            // inner bottom-middle side, source lower-right, move SW, S, SE
326
            C6b = (sX2 >  chBiX1 && sX2 <  chBiX2 && sY2 >= chBiY2 && sY2 < chY2   && (sDir == 'SW' || sDir == 'S' || sDir == 'SE'));
327
 
328
            // inner bottom-left corner, source lower-left, move W, SW, S
329
            C7  = (sX1 <= chBiX1 && sX1 >  chX1   && sY2 >= chBiY2 && sY2 < chY2   && (sDir == 'W' || sDir == 'SW' || sDir == 'S'));
330
 
331
            // inner left-middle side, source top-left, move NW, W, SW
332
            C8a = (sX1 <= chBiX1 && sX1 >  chX1   && sY1 >  chBiY1 && sY1 < chBiY2 && (sDir == 'NW' || sDir == 'W' || sDir == 'SW'));
333
 
334
            // inner left-middle side, source bottom-left, move NW, W, SW
335
            C8b = (sX1 <= chBiX1 && sX1 >  chX1   && sY2 >  chBiY1 && sY2 < chBiY2 && (sDir == 'NW' || sDir == 'W' || sDir == 'SW'));
336
 
337
            if (C1){
338
                this._SnapSetLocation(chX1, chY1);
339
            }
340
            else if (C3){
341
                this._SnapSetLocation(chX2-sW, chY1);
342
            }
343
            else if (C5){
344
                this._SnapSetLocation(chX2-sW, chY2-sH);
345
            }
346
            else if (C7){
347
                this._SnapSetLocation(chX1, chY2-sH);
348
            }
349
            else if (C2a || C2b){
350
                this._SnapSetLocation(sX1, chY1);
351
            }
352
            else if (C4a || C4b){
353
                this._SnapSetLocation(chX2-sW, sY1);
354
            }
355
            else if (C6a || C6b){
356
                this._SnapSetLocation(sX1, chY2-sH);
357
            }
358
            else if (C8a || C8b){
359
                this._SnapSetLocation(chX1 , sY1);
360
            }
361
 
362
            if (C1 || C2a || C2b || C3 || C4a || C4b ||
363
                 C5 || C6a || C6b || C7 || C8a || C8b){
364
                this._snapObject=ch
365
                this.invokeEvent("snap");
366
                ch._snapObject=this
367
                ch.invokeEvent("snap");
368
            }
369
        }
370
    }
371
    return(false);
372
};
373
 
374
p._checkForSnapOuter = function(){
375
    if (! this.parent.children.length > 0) return(false);
376
    if (! this.isSnapEnabled == true) return(false);
377
 
378
    var ch,chX1,chY1,chX2,chY2,chBX1,chBY1,chBX2,chBY2,sX1,sY1,sX2,sY2,chBndAry,C1,C2a,C2b,C3,C4a,C4b,C5,C6a,C6b,C7,C8a,C8b,sDir;
379
 
380
    sX1 = this.x;
381
    sY1 = this.y;
382
    sW  = this.w;
383
    sH  = this.h;
384
    sX2 = sX1 + sW;
385
    sY2 = sY1 + sH;
386
    sDir = this.getSnapDirection();
387
 
388
    for (var i in this.parent.children){
389
        ch = this.parent.children[i];
390
        if (ch != this && ch.isSnapEnabled == true){
391
            chX1 = ch.x;
392
            chY1 = ch.y;
393
            chX2 = chX1 + ch.w;
394
            chY2 = chY1 + ch.h;
395
            chBndAry = ch.getSnapBoundary('outer');
396
            chBoX1 = chBndAry[3];
397
            chBoY1 = chBndAry[0];
398
            chBoX2 = chBndAry[1];
399
            chBoY2 = chBndAry[2];
400
 
401
            // outer top-left corner, source lower-right, move dir E, SE, S
402
            C1  = (sX2 >= chBoX1 && sX2 <  chX1 && sY2 >= chBoY1 && sY2 <  chY1 && (sDir == 'E' || sDir == 'SE' || sDir == 'S'));
403
 
404
            // outer top-middle side, source lower-left, move SW, S, SE
405
            C2a = (sX1 >= chX1   && sX1 <= chX2 && sY2 >= chBoY1 && sY2 <  chY1 && (sDir == 'SW' || sDir == 'S' || sDir == 'SE'));
406
 
407
            // outer top-middle side, source lower-right, move SW, S, SE
408
            C2b = (sX2 >= chX1   && sX2 <= chX2 && sY2 >= chBoY1 && sY2 <  chY1 && (sDir == 'SW' || sDir == 'S' || sDir == 'SE'));
409
 
410
            // outer top-right corner, source lower-left, move W, SW, S
411
            C3  = (sX1 <= chBoX2 && sX1 >  chX2 && sY2 >= chBoY1 && sY2 <  chY1 && (sDir == 'W' || sDir == 'SW' || sDir == 'S'));
412
 
413
            // outer right-middle side, source top-left, move NW, W, SW
414
            C4a = (sX1 <= chBoX2 && sX1 >  chX2 && sY1 >= chY1   && sY1 <= chY2 && (sDir == 'NW' || sDir == 'W' || sDir == 'SW'));
415
 
416
            // outer right-middle side, source bottom-left, move NW, W, SW
417
            C4b = (sX1 <= chBoX2 && sX1 >  chX2 && sY2 >= chY1   && sY2 <= chY2 && (sDir == 'NW' || sDir == 'W' || sDir == 'SW'));
418
 
419
            // outer bottom-right corner, source top-left, move N, NW, W
420
            C5  = (sX1 <= chBoX2 && sX1 >  chX2 && sY1 <= chBoY2 && sY1 >  chY2 && (sDir == 'N' || sDir == 'NW' || sDir == 'W'));
421
 
422
            // outer bottom-middle side, source top-left, move NE, N, NW
423
            C6a = (sX1 >= chX1   && sX1 <= chX2 && sY1 <= chBoY2 && sY1 >  chY2 && (sDir == 'NE' || sDir == 'N' || sDir == 'NW'));
424
 
425
            // outer bottom-middle side, source top-right, move NE, N, NW
426
            C6b = (sX2 >= chX1   && sX2 <= chX2 && sY1 <= chBoY2 && sY1 >  chY2 && (sDir == 'NE' || sDir == 'N' || sDir == 'NW'));
427
 
428
            // outer bottom-left corner, source top-right, move E, NE, N
429
            C7  = (sX2 >= chBoX1 && sX2 <  chX1 && sY1 <= chBoY2 && sY1 >  chY2 && (sDir == 'E' || sDir == 'NE' || sDir == 'N'));
430
 
431
            // outer left-middle side, source top-right, move SE, E, NE
432
            C8a = (sX2 >= chBoX1 && sX2 <  chX1 && sY1 >= chY1  && sY1  <= chY2 && (sDir == 'SE' || sDir == 'E' || sDir == 'NE'));
433
 
434
            // outer left-middle side, source bottom-right, move SE, E, NE
435
            C8b = (sX2 >= chBoX1 && sX2 <  chX1 && sY2 >= chY1  && sY2  <= chY2 && (sDir == 'SE' || sDir == 'E' || sDir == 'NE'));
436
 
437
            if (C1){
438
                this._SnapSetLocation(chX1-sW, chY1-sH);
439
            }
440
            else if (C3){
441
                this._SnapSetLocation(chX2, chY1-sH);
442
            }
443
            else if (C5){
444
                this._SnapSetLocation(chX2, chY2);
445
            }
446
            else if (C7){
447
                this._SnapSetLocation(chX1-sW, chY2);
448
            }
449
            else if (C2a || C2b){
450
                if(!this._snapLockY) this._snapLockY=chY1-sH;
451
                this._SnapSetLocation(sX1 , this._snapLockY);
452
            }
453
            else if (C4a || C4b){
454
                if(!this._snapLockX) this._snapLockX=chX2;
455
                this._SnapSetLocation(this._snapLockX, sY1);
456
            }
457
            else if (C6a || C6b){
458
                if(!this._snapLockY) this._snapLockY=chY2;
459
                this._SnapSetLocation(sX1 , this._snapLockY);
460
            }
461
            else if (C8a || C8b){
462
                if(!this._snapLockX) this._snapLockX=chX1-sW;
463
                this._SnapSetLocation(chX1-sW , sY1);
464
            }
465
 
466
            if (C1 || C2a || C2b || C3 || C4a || C4b ||
467
                 C5 || C6a || C6b || C7 || C8a || C8b){
468
                this._snapObject=ch
469
                this.invokeEvent("snap");
470
                ch._snapObject=this
471
                ch.invokeEvent("snap");
472
            }
473
        }
474
    }
475
 
476
    return false;
477
};
478
p.enableStickySnap = function(){
479
    this._stickySnap = true;
480
};
481
p.disableStickySnap = function(){
482
    this._stickySnap = false;
483
};
484
p.setStickySnap = function(){
485
    if (arguments.length == 0){
486
        this._stickySnapBorder == this._snapBoundaryDefault;
487
    }
488
    else if (arguments[0] === true){
489
        this._stickySnap = true;
490
    }
491
    else if (arguments[0] === false){
492
        this._stickySnap = false;
493
    } else {
494
 
495
    }
496
}
497
 
498