Subversion Repositories eFlore/Applications.cel

Rev

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

Rev Author Line No. Line
27 jpm 1
/*
2
 * Ext JS Library 2.0.2
3
 * Copyright(c) 2006-2008, Ext JS, LLC.
4
 * licensing@extjs.com
5
 *
6
 * http://extjs.com/license
7
 */
8
 
9
/**
10
 * @class Ext.Resizable
11
 * @extends Ext.util.Observable
12
 * <p>Applies drag handles to an element to make it resizable. The drag handles are inserted into the element
13
 * and positioned absolute. Some elements, such as a textarea or image, don't support this. To overcome that, you can wrap
14
 * the textarea in a div and set "resizeChild" to true (or to the id of the element), <b>or</b> set wrap:true in your config and
15
 * the element will be wrapped for you automatically.</p>
16
 * <p>Here is the list of valid resize handles:</p>
17
 * <pre>
18
Value   Description
19
------  -------------------
20
 'n'     north
21
 's'     south
22
 'e'     east
23
 'w'     west
24
 'nw'    northwest
25
 'sw'    southwest
26
 'se'    southeast
27
 'ne'    northeast
28
 'all'   all
29
</pre>
30
 * <p>Here's an example showing the creation of a typical Resizable:</p>
31
 * <pre><code>
32
var resizer = new Ext.Resizable("element-id", {
33
    handles: 'all',
34
    minWidth: 200,
35
    minHeight: 100,
36
    maxWidth: 500,
37
    maxHeight: 400,
38
    pinned: true
39
});
40
resizer.on("resize", myHandler);
41
</code></pre>
42
 * <p>To hide a particular handle, set its display to none in CSS, or through script:<br>
43
 * resizer.east.setDisplayed(false);</p>
44
 * @cfg {Boolean/String/Element} resizeChild True to resize the first child, or id/element to resize (defaults to false)
45
 * @cfg {Array/String} adjustments String "auto" or an array [width, height] with values to be <b>added</b> to the
46
 * resize operation's new size (defaults to [0, 0])
47
 * @cfg {Number} minWidth The minimum width for the element (defaults to 5)
48
 * @cfg {Number} minHeight The minimum height for the element (defaults to 5)
49
 * @cfg {Number} maxWidth The maximum width for the element (defaults to 10000)
50
 * @cfg {Number} maxHeight The maximum height for the element (defaults to 10000)
51
 * @cfg {Boolean} enabled False to disable resizing (defaults to true)
52
 * @cfg {Boolean} wrap True to wrap an element with a div if needed (required for textareas and images, defaults to false)
53
 * @cfg {Number} width The width of the element in pixels (defaults to null)
54
 * @cfg {Number} height The height of the element in pixels (defaults to null)
55
 * @cfg {Boolean} animate True to animate the resize (not compatible with dynamic sizing, defaults to false)
56
 * @cfg {Number} duration Animation duration if animate = true (defaults to .35)
57
 * @cfg {Boolean} dynamic True to resize the element while dragging instead of using a proxy (defaults to false)
58
 * @cfg {String} handles String consisting of the resize handles to display (defaults to undefined)
59
 * @cfg {Boolean} multiDirectional <b>Deprecated</b>.  The old style of adding multi-direction resize handles, deprecated
60
 * in favor of the handles config option (defaults to false)
61
 * @cfg {Boolean} disableTrackOver True to disable mouse tracking. This is only applied at config time. (defaults to false)
62
 * @cfg {String} easing Animation easing if animate = true (defaults to 'easingOutStrong')
63
 * @cfg {Number} widthIncrement The increment to snap the width resize in pixels (dynamic must be true, defaults to 0)
64
 * @cfg {Number} heightIncrement The increment to snap the height resize in pixels (dynamic must be true, defaults to 0)
65
 * @cfg {Boolean} pinned True to ensure that the resize handles are always visible, false to display them only when the
66
 * user mouses over the resizable borders. This is only applied at config time. (defaults to false)
67
 * @cfg {Boolean} preserveRatio True to preserve the original ratio between height and width during resize (defaults to false)
68
 * @cfg {Boolean} transparent True for transparent handles. This is only applied at config time. (defaults to false)
69
 * @cfg {Number} minX The minimum allowed page X for the element (only used for west resizing, defaults to 0)
70
 * @cfg {Number} minY The minimum allowed page Y for the element (only used for north resizing, defaults to 0)
71
 * @cfg {Boolean} draggable Convenience to initialize drag drop (defaults to false)
72
 * @constructor
73
 * Create a new resizable component
74
 * @param {Mixed} el The id or element to resize
75
 * @param {Object} config configuration options
76
  */
77
Ext.Resizable = function(el, config){
78
    this.el = Ext.get(el);
79
 
80
    if(config && config.wrap){
81
        config.resizeChild = this.el;
82
        this.el = this.el.wrap(typeof config.wrap == "object" ? config.wrap : {cls:"xresizable-wrap"});
83
        this.el.id = this.el.dom.id = config.resizeChild.id + "-rzwrap";
84
        this.el.setStyle("overflow", "hidden");
85
        this.el.setPositioning(config.resizeChild.getPositioning());
86
        config.resizeChild.clearPositioning();
87
        if(!config.width || !config.height){
88
            var csize = config.resizeChild.getSize();
89
            this.el.setSize(csize.width, csize.height);
90
        }
91
        if(config.pinned && !config.adjustments){
92
            config.adjustments = "auto";
93
        }
94
    }
95
 
96
    this.proxy = this.el.createProxy({tag: "div", cls: "x-resizable-proxy", id: this.el.id + "-rzproxy"});
97
    this.proxy.unselectable();
98
    this.proxy.enableDisplayMode('block');
99
 
100
    Ext.apply(this, config);
101
 
102
    if(this.pinned){
103
        this.disableTrackOver = true;
104
        this.el.addClass("x-resizable-pinned");
105
    }
106
    // if the element isn't positioned, make it relative
107
    var position = this.el.getStyle("position");
108
    if(position != "absolute" && position != "fixed"){
109
        this.el.setStyle("position", "relative");
110
    }
111
    if(!this.handles){ // no handles passed, must be legacy style
112
        this.handles = 's,e,se';
113
        if(this.multiDirectional){
114
            this.handles += ',n,w';
115
        }
116
    }
117
    if(this.handles == "all"){
118
        this.handles = "n s e w ne nw se sw";
119
    }
120
    var hs = this.handles.split(/\s*?[,;]\s*?| /);
121
    var ps = Ext.Resizable.positions;
122
    for(var i = 0, len = hs.length; i < len; i++){
123
        if(hs[i] && ps[hs[i]]){
124
            var pos = ps[hs[i]];
125
            this[pos] = new Ext.Resizable.Handle(this, pos, this.disableTrackOver, this.transparent);
126
        }
127
    }
128
    // legacy
129
    this.corner = this.southeast;
130
 
131
    if(this.handles.indexOf("n") != -1 || this.handles.indexOf("w") != -1){
132
        this.updateBox = true;
133
    }
134
 
135
    this.activeHandle = null;
136
 
137
    if(this.resizeChild){
138
        if(typeof this.resizeChild == "boolean"){
139
            this.resizeChild = Ext.get(this.el.dom.firstChild, true);
140
        }else{
141
            this.resizeChild = Ext.get(this.resizeChild, true);
142
        }
143
    }
144
 
145
    if(this.adjustments == "auto"){
146
        var rc = this.resizeChild;
147
        var hw = this.west, he = this.east, hn = this.north, hs = this.south;
148
        if(rc && (hw || hn)){
149
            rc.position("relative");
150
            rc.setLeft(hw ? hw.el.getWidth() : 0);
151
            rc.setTop(hn ? hn.el.getHeight() : 0);
152
        }
153
        this.adjustments = [
154
            (he ? -he.el.getWidth() : 0) + (hw ? -hw.el.getWidth() : 0),
155
            (hn ? -hn.el.getHeight() : 0) + (hs ? -hs.el.getHeight() : 0) -1
156
        ];
157
    }
158
 
159
    if(this.draggable){
160
        this.dd = this.dynamic ?
161
            this.el.initDD(null) : this.el.initDDProxy(null, {dragElId: this.proxy.id});
162
        this.dd.setHandleElId(this.resizeChild ? this.resizeChild.id : this.el.id);
163
    }
164
 
165
    // public events
166
    this.addEvents(
167
        "beforeresize",
168
        "resize"
169
    );
170
 
171
    if(this.width !== null && this.height !== null){
172
        this.resizeTo(this.width, this.height);
173
    }else{
174
        this.updateChildSize();
175
    }
176
    if(Ext.isIE){
177
        this.el.dom.style.zoom = 1;
178
    }
179
    Ext.Resizable.superclass.constructor.call(this);
180
};
181
 
182
Ext.extend(Ext.Resizable, Ext.util.Observable, {
183
        resizeChild : false,
184
        adjustments : [0, 0],
185
        minWidth : 5,
186
        minHeight : 5,
187
        maxWidth : 10000,
188
        maxHeight : 10000,
189
        enabled : true,
190
        animate : false,
191
        duration : .35,
192
        dynamic : false,
193
        handles : false,
194
        multiDirectional : false,
195
        disableTrackOver : false,
196
        easing : 'easeOutStrong',
197
        widthIncrement : 0,
198
        heightIncrement : 0,
199
        pinned : false,
200
        width : null,
201
        height : null,
202
        preserveRatio : false,
203
        transparent: false,
204
        minX: 0,
205
        minY: 0,
206
        draggable: false,
207
 
208
        /**
209
         * @cfg {Mixed} constrainTo Constrain the resize to a particular element
210
         */
211
        /**
212
         * @cfg {Ext.lib.Region} resizeRegion Constrain the resize to a particular region
213
         */
214
 
215
        /**
216
         * @event beforeresize
217
         * Fired before resize is allowed. Set enabled to false to cancel resize.
218
         * @param {Ext.Resizable} this
219
         * @param {Ext.EventObject} e The mousedown event
220
         */
221
        /**
222
         * @event resize
223
         * Fired after a resize.
224
         * @param {Ext.Resizable} this
225
         * @param {Number} width The new width
226
         * @param {Number} height The new height
227
         * @param {Ext.EventObject} e The mouseup event
228
         */
229
 
230
    /**
231
     * Perform a manual resize
232
     * @param {Number} width
233
     * @param {Number} height
234
     */
235
    resizeTo : function(width, height){
236
        this.el.setSize(width, height);
237
        this.updateChildSize();
238
        this.fireEvent("resize", this, width, height, null);
239
    },
240
 
241
    // private
242
    startSizing : function(e, handle){
243
        this.fireEvent("beforeresize", this, e);
244
        if(this.enabled){ // 2nd enabled check in case disabled before beforeresize handler
245
 
246
            if(!this.overlay){
247
                this.overlay = this.el.createProxy({tag: "div", cls: "x-resizable-overlay", html: "&#160;"}, Ext.getBody());
248
                this.overlay.unselectable();
249
                this.overlay.enableDisplayMode("block");
250
                this.overlay.on("mousemove", this.onMouseMove, this);
251
                this.overlay.on("mouseup", this.onMouseUp, this);
252
            }
253
            this.overlay.setStyle("cursor", handle.el.getStyle("cursor"));
254
 
255
            this.resizing = true;
256
            this.startBox = this.el.getBox();
257
            this.startPoint = e.getXY();
258
            this.offsets = [(this.startBox.x + this.startBox.width) - this.startPoint[0],
259
                            (this.startBox.y + this.startBox.height) - this.startPoint[1]];
260
 
261
            this.overlay.setSize(Ext.lib.Dom.getViewWidth(true), Ext.lib.Dom.getViewHeight(true));
262
            this.overlay.show();
263
 
264
            if(this.constrainTo) {
265
                var ct = Ext.get(this.constrainTo);
266
                this.resizeRegion = ct.getRegion().adjust(
267
                    ct.getFrameWidth('t'),
268
                    ct.getFrameWidth('l'),
269
                    -ct.getFrameWidth('b'),
270
                    -ct.getFrameWidth('r')
271
                );
272
            }
273
 
274
            this.proxy.setStyle('visibility', 'hidden'); // workaround display none
275
            this.proxy.show();
276
            this.proxy.setBox(this.startBox);
277
            if(!this.dynamic){
278
                this.proxy.setStyle('visibility', 'visible');
279
            }
280
        }
281
    },
282
 
283
    // private
284
    onMouseDown : function(handle, e){
285
        if(this.enabled){
286
            e.stopEvent();
287
            this.activeHandle = handle;
288
            this.startSizing(e, handle);
289
        }
290
    },
291
 
292
    // private
293
    onMouseUp : function(e){
294
        var size = this.resizeElement();
295
        this.resizing = false;
296
        this.handleOut();
297
        this.overlay.hide();
298
        this.proxy.hide();
299
        this.fireEvent("resize", this, size.width, size.height, e);
300
    },
301
 
302
    // private
303
    updateChildSize : function(){
304
        if(this.resizeChild){
305
            var el = this.el;
306
            var child = this.resizeChild;
307
            var adj = this.adjustments;
308
            if(el.dom.offsetWidth){
309
                var b = el.getSize(true);
310
                child.setSize(b.width+adj[0], b.height+adj[1]);
311
            }
312
            // Second call here for IE
313
            // The first call enables instant resizing and
314
            // the second call corrects scroll bars if they
315
            // exist
316
            if(Ext.isIE){
317
                setTimeout(function(){
318
                    if(el.dom.offsetWidth){
319
                        var b = el.getSize(true);
320
                        child.setSize(b.width+adj[0], b.height+adj[1]);
321
                    }
322
                }, 10);
323
            }
324
        }
325
    },
326
 
327
    // private
328
    snap : function(value, inc, min){
329
        if(!inc || !value) return value;
330
        var newValue = value;
331
        var m = value % inc;
332
        if(m > 0){
333
            if(m > (inc/2)){
334
                newValue = value + (inc-m);
335
            }else{
336
                newValue = value - m;
337
            }
338
        }
339
        return Math.max(min, newValue);
340
    },
341
 
342
    // private
343
    resizeElement : function(){
344
        var box = this.proxy.getBox();
345
        if(this.updateBox){
346
            this.el.setBox(box, false, this.animate, this.duration, null, this.easing);
347
        }else{
348
            this.el.setSize(box.width, box.height, this.animate, this.duration, null, this.easing);
349
        }
350
        this.updateChildSize();
351
        if(!this.dynamic){
352
            this.proxy.hide();
353
        }
354
        return box;
355
    },
356
 
357
    // private
358
    constrain : function(v, diff, m, mx){
359
        if(v - diff < m){
360
            diff = v - m;
361
        }else if(v - diff > mx){
362
            diff = mx - v;
363
        }
364
        return diff;
365
    },
366
 
367
    // private
368
    onMouseMove : function(e){
369
        if(this.enabled){
370
            try{// try catch so if something goes wrong the user doesn't get hung
371
 
372
            if(this.resizeRegion && !this.resizeRegion.contains(e.getPoint())) {
373
            	return;
374
            }
375
 
376
            //var curXY = this.startPoint;
377
            var curSize = this.curSize || this.startBox;
378
            var x = this.startBox.x, y = this.startBox.y;
379
            var ox = x, oy = y;
380
            var w = curSize.width, h = curSize.height;
381
            var ow = w, oh = h;
382
            var mw = this.minWidth, mh = this.minHeight;
383
            var mxw = this.maxWidth, mxh = this.maxHeight;
384
            var wi = this.widthIncrement;
385
            var hi = this.heightIncrement;
386
 
387
            var eventXY = e.getXY();
388
            var diffX = -(this.startPoint[0] - Math.max(this.minX, eventXY[0]));
389
            var diffY = -(this.startPoint[1] - Math.max(this.minY, eventXY[1]));
390
 
391
            var pos = this.activeHandle.position;
392
 
393
            switch(pos){
394
                case "east":
395
                    w += diffX;
396
                    w = Math.min(Math.max(mw, w), mxw);
397
                    break;
398
                case "south":
399
                    h += diffY;
400
                    h = Math.min(Math.max(mh, h), mxh);
401
                    break;
402
                case "southeast":
403
                    w += diffX;
404
                    h += diffY;
405
                    w = Math.min(Math.max(mw, w), mxw);
406
                    h = Math.min(Math.max(mh, h), mxh);
407
                    break;
408
                case "north":
409
                    diffY = this.constrain(h, diffY, mh, mxh);
410
                    y += diffY;
411
                    h -= diffY;
412
                    break;
413
                case "west":
414
                    diffX = this.constrain(w, diffX, mw, mxw);
415
                    x += diffX;
416
                    w -= diffX;
417
                    break;
418
                case "northeast":
419
                    w += diffX;
420
                    w = Math.min(Math.max(mw, w), mxw);
421
                    diffY = this.constrain(h, diffY, mh, mxh);
422
                    y += diffY;
423
                    h -= diffY;
424
                    break;
425
                case "northwest":
426
                    diffX = this.constrain(w, diffX, mw, mxw);
427
                    diffY = this.constrain(h, diffY, mh, mxh);
428
                    y += diffY;
429
                    h -= diffY;
430
                    x += diffX;
431
                    w -= diffX;
432
                    break;
433
               case "southwest":
434
                    diffX = this.constrain(w, diffX, mw, mxw);
435
                    h += diffY;
436
                    h = Math.min(Math.max(mh, h), mxh);
437
                    x += diffX;
438
                    w -= diffX;
439
                    break;
440
            }
441
 
442
            var sw = this.snap(w, wi, mw);
443
            var sh = this.snap(h, hi, mh);
444
            if(sw != w || sh != h){
445
                switch(pos){
446
                    case "northeast":
447
                        y -= sh - h;
448
                    break;
449
                    case "north":
450
                        y -= sh - h;
451
                        break;
452
                    case "southwest":
453
                        x -= sw - w;
454
                    break;
455
                    case "west":
456
                        x -= sw - w;
457
                        break;
458
                    case "northwest":
459
                        x -= sw - w;
460
                        y -= sh - h;
461
                    break;
462
                }
463
                w = sw;
464
                h = sh;
465
            }
466
 
467
            if(this.preserveRatio){
468
                switch(pos){
469
                    case "southeast":
470
                    case "east":
471
                        h = oh * (w/ow);
472
                        h = Math.min(Math.max(mh, h), mxh);
473
                        w = ow * (h/oh);
474
                       break;
475
                    case "south":
476
                        w = ow * (h/oh);
477
                        w = Math.min(Math.max(mw, w), mxw);
478
                        h = oh * (w/ow);
479
                        break;
480
                    case "northeast":
481
                        w = ow * (h/oh);
482
                        w = Math.min(Math.max(mw, w), mxw);
483
                        h = oh * (w/ow);
484
                    break;
485
                    case "north":
486
                        var tw = w;
487
                        w = ow * (h/oh);
488
                        w = Math.min(Math.max(mw, w), mxw);
489
                        h = oh * (w/ow);
490
                        x += (tw - w) / 2;
491
                        break;
492
                    case "southwest":
493
                        h = oh * (w/ow);
494
                        h = Math.min(Math.max(mh, h), mxh);
495
                        var tw = w;
496
                        w = ow * (h/oh);
497
                        x += tw - w;
498
                        break;
499
                    case "west":
500
                        var th = h;
501
                        h = oh * (w/ow);
502
                        h = Math.min(Math.max(mh, h), mxh);
503
                        y += (th - h) / 2;
504
                        var tw = w;
505
                        w = ow * (h/oh);
506
                        x += tw - w;
507
                       break;
508
                    case "northwest":
509
                        var tw = w;
510
                        var th = h;
511
                        h = oh * (w/ow);
512
                        h = Math.min(Math.max(mh, h), mxh);
513
                        w = ow * (h/oh);
514
                        y += th - h;
515
                         x += tw - w;
516
                       break;
517
 
518
                }
519
            }
520
            this.proxy.setBounds(x, y, w, h);
521
            if(this.dynamic){
522
                this.resizeElement();
523
            }
524
            }catch(e){}
525
        }
526
    },
527
 
528
    // private
529
    handleOver : function(){
530
        if(this.enabled){
531
            this.el.addClass("x-resizable-over");
532
        }
533
    },
534
 
535
    // private
536
    handleOut : function(){
537
        if(!this.resizing){
538
            this.el.removeClass("x-resizable-over");
539
        }
540
    },
541
 
542
    /**
543
     * Returns the element this component is bound to.
544
     * @return {Ext.Element}
545
     */
546
    getEl : function(){
547
        return this.el;
548
    },
549
 
550
    /**
551
     * Returns the resizeChild element (or null).
552
     * @return {Ext.Element}
553
     */
554
    getResizeChild : function(){
555
        return this.resizeChild;
556
    },
557
 
558
    /**
559
     * Destroys this resizable. If the element was wrapped and
560
     * removeEl is not true then the element remains.
561
     * @param {Boolean} removeEl (optional) true to remove the element from the DOM
562
     */
563
    destroy : function(removeEl){
564
        this.proxy.remove();
565
        if(this.overlay){
566
            this.overlay.removeAllListeners();
567
            this.overlay.remove();
568
        }
569
        var ps = Ext.Resizable.positions;
570
        for(var k in ps){
571
            if(typeof ps[k] != "function" && this[ps[k]]){
572
                var h = this[ps[k]];
573
                h.el.removeAllListeners();
574
                h.el.remove();
575
            }
576
        }
577
        if(removeEl){
578
            this.el.update("");
579
            this.el.remove();
580
        }
581
    },
582
 
583
    syncHandleHeight : function(){
584
        var h = this.el.getHeight(true);
585
        if(this.west){
586
            this.west.el.setHeight(h);
587
        }
588
        if(this.east){
589
            this.east.el.setHeight(h);
590
        }
591
    }
592
});
593
 
594
// private
595
// hash to map config positions to true positions
596
Ext.Resizable.positions = {
597
    n: "north", s: "south", e: "east", w: "west", se: "southeast", sw: "southwest", nw: "northwest", ne: "northeast"
598
};
599
 
600
// private
601
Ext.Resizable.Handle = function(rz, pos, disableTrackOver, transparent){
602
    if(!this.tpl){
603
        // only initialize the template if resizable is used
604
        var tpl = Ext.DomHelper.createTemplate(
605
            {tag: "div", cls: "x-resizable-handle x-resizable-handle-{0}"}
606
        );
607
        tpl.compile();
608
        Ext.Resizable.Handle.prototype.tpl = tpl;
609
    }
610
    this.position = pos;
611
    this.rz = rz;
612
    this.el = this.tpl.append(rz.el.dom, [this.position], true);
613
    this.el.unselectable();
614
    if(transparent){
615
        this.el.setOpacity(0);
616
    }
617
    this.el.on("mousedown", this.onMouseDown, this);
618
    if(!disableTrackOver){
619
        this.el.on("mouseover", this.onMouseOver, this);
620
        this.el.on("mouseout", this.onMouseOut, this);
621
    }
622
};
623
 
624
// private
625
Ext.Resizable.Handle.prototype = {
626
    afterResize : function(rz){
627
        // do nothing
628
    },
629
    // private
630
    onMouseDown : function(e){
631
        this.rz.onMouseDown(this, e);
632
    },
633
    // private
634
    onMouseOver : function(e){
635
        this.rz.handleOver(this, e);
636
    },
637
    // private
638
    onMouseOut : function(e){
639
        this.rz.handleOut(this, e);
640
    }
641
};
642
 
643
 
644