2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
10 * @class Ext.Resizable
\r
11 * @extends Ext.util.Observable
\r
12 * <p>Applies drag handles to an element to make it resizable. The drag handles are inserted into the element
\r
13 * and positioned absolute. Some elements, such as a textarea or image, don't support this. To overcome that, you can wrap
\r
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
\r
15 * the element will be wrapped for you automatically.</p>
\r
16 * <p>Here is the list of valid resize handles:</p>
\r
19 ------ -------------------
\r
30 * <p>Here's an example showing the creation of a typical Resizable:</p>
\r
32 var resizer = new Ext.Resizable("element-id", {
\r
40 resizer.on("resize", myHandler);
\r
42 * <p>To hide a particular handle, set its display to none in CSS, or through script:<br>
\r
43 * resizer.east.setDisplayed(false);</p>
\r
44 * @cfg {Boolean/String/Element} resizeChild True to resize the first child, or id/element to resize (defaults to false)
\r
45 * @cfg {Array/String} adjustments String "auto" or an array [width, height] with values to be <b>added</b> to the
\r
46 * resize operation's new size (defaults to [0, 0])
\r
47 * @cfg {Number} minWidth The minimum width for the element (defaults to 5)
\r
48 * @cfg {Number} minHeight The minimum height for the element (defaults to 5)
\r
49 * @cfg {Number} maxWidth The maximum width for the element (defaults to 10000)
\r
50 * @cfg {Number} maxHeight The maximum height for the element (defaults to 10000)
\r
51 * @cfg {Boolean} enabled False to disable resizing (defaults to true)
\r
52 * @cfg {Boolean} wrap True to wrap an element with a div if needed (required for textareas and images, defaults to false)
\r
53 * @cfg {Number} width The width of the element in pixels (defaults to null)
\r
54 * @cfg {Number} height The height of the element in pixels (defaults to null)
\r
55 * @cfg {Boolean} animate True to animate the resize (not compatible with dynamic sizing, defaults to false)
\r
56 * @cfg {Number} duration Animation duration if animate = true (defaults to .35)
\r
57 * @cfg {Boolean} dynamic True to resize the element while dragging instead of using a proxy (defaults to false)
\r
58 * @cfg {String} handles String consisting of the resize handles to display (defaults to undefined)
\r
59 * @cfg {Boolean} multiDirectional <b>Deprecated</b>. The old style of adding multi-direction resize handles, deprecated
\r
60 * in favor of the handles config option (defaults to false)
\r
61 * @cfg {Boolean} disableTrackOver True to disable mouse tracking. This is only applied at config time. (defaults to false)
\r
62 * @cfg {String} easing Animation easing if animate = true (defaults to 'easingOutStrong')
\r
63 * @cfg {Number} widthIncrement The increment to snap the width resize in pixels (dynamic must be true, defaults to 0)
\r
64 * @cfg {Number} heightIncrement The increment to snap the height resize in pixels (dynamic must be true, defaults to 0)
\r
65 * @cfg {Boolean} pinned True to ensure that the resize handles are always visible, false to display them only when the
\r
66 * user mouses over the resizable borders. This is only applied at config time. (defaults to false)
\r
67 * @cfg {Boolean} preserveRatio True to preserve the original ratio between height and width during resize (defaults to false)
\r
68 * @cfg {Boolean} transparent True for transparent handles. This is only applied at config time. (defaults to false)
\r
69 * @cfg {Number} minX The minimum allowed page X for the element (only used for west resizing, defaults to 0)
\r
70 * @cfg {Number} minY The minimum allowed page Y for the element (only used for north resizing, defaults to 0)
\r
71 * @cfg {Boolean} draggable Convenience to initialize drag drop (defaults to false)
\r
73 * Create a new resizable component
\r
74 * @param {Mixed} el The id or element to resize
\r
75 * @param {Object} config configuration options
\r
77 Ext.Resizable = function(el, config){
\r
78 this.el = Ext.get(el);
\r
80 if(config && config.wrap){
\r
81 config.resizeChild = this.el;
\r
82 this.el = this.el.wrap(typeof config.wrap == "object" ? config.wrap : {cls:"xresizable-wrap"});
\r
83 this.el.id = this.el.dom.id = config.resizeChild.id + "-rzwrap";
\r
84 this.el.setStyle("overflow", "hidden");
\r
85 this.el.setPositioning(config.resizeChild.getPositioning());
\r
86 config.resizeChild.clearPositioning();
\r
87 if(!config.width || !config.height){
\r
88 var csize = config.resizeChild.getSize();
\r
89 this.el.setSize(csize.width, csize.height);
\r
91 if(config.pinned && !config.adjustments){
\r
92 config.adjustments = "auto";
\r
97 * The proxy Element that is resized in place of the real Element during the resize operation.
\r
98 * This may be queried using {@link Ext.Element#getBox} to provide the new area to resize to.
\r
100 * @type Ext.Element.
\r
103 this.proxy = this.el.createProxy({tag: "div", cls: "x-resizable-proxy", id: this.el.id + "-rzproxy"}, Ext.getBody());
\r
104 this.proxy.unselectable();
\r
105 this.proxy.enableDisplayMode('block');
\r
107 Ext.apply(this, config);
\r
110 this.disableTrackOver = true;
\r
111 this.el.addClass("x-resizable-pinned");
\r
113 // if the element isn't positioned, make it relative
\r
114 var position = this.el.getStyle("position");
\r
115 if(position != "absolute" && position != "fixed"){
\r
116 this.el.setStyle("position", "relative");
\r
118 if(!this.handles){ // no handles passed, must be legacy style
\r
119 this.handles = 's,e,se';
\r
120 if(this.multiDirectional){
\r
121 this.handles += ',n,w';
\r
124 if(this.handles == "all"){
\r
125 this.handles = "n s e w ne nw se sw";
\r
127 var hs = this.handles.split(/\s*?[,;]\s*?| /);
\r
128 var ps = Ext.Resizable.positions;
\r
129 for(var i = 0, len = hs.length; i < len; i++){
\r
130 if(hs[i] && ps[hs[i]]){
\r
131 var pos = ps[hs[i]];
\r
132 this[pos] = new Ext.Resizable.Handle(this, pos, this.disableTrackOver, this.transparent);
\r
136 this.corner = this.southeast;
\r
138 if(this.handles.indexOf("n") != -1 || this.handles.indexOf("w") != -1){
\r
139 this.updateBox = true;
\r
142 this.activeHandle = null;
\r
144 if(this.resizeChild){
\r
145 if(typeof this.resizeChild == "boolean"){
\r
146 this.resizeChild = Ext.get(this.el.dom.firstChild, true);
\r
148 this.resizeChild = Ext.get(this.resizeChild, true);
\r
152 if(this.adjustments == "auto"){
\r
153 var rc = this.resizeChild;
\r
154 var hw = this.west, he = this.east, hn = this.north, hs = this.south;
\r
155 if(rc && (hw || hn)){
\r
156 rc.position("relative");
\r
157 rc.setLeft(hw ? hw.el.getWidth() : 0);
\r
158 rc.setTop(hn ? hn.el.getHeight() : 0);
\r
160 this.adjustments = [
\r
161 (he ? -he.el.getWidth() : 0) + (hw ? -hw.el.getWidth() : 0),
\r
162 (hn ? -hn.el.getHeight() : 0) + (hs ? -hs.el.getHeight() : 0) -1
\r
166 if(this.draggable){
\r
167 this.dd = this.dynamic ?
\r
168 this.el.initDD(null) : this.el.initDDProxy(null, {dragElId: this.proxy.id});
\r
169 this.dd.setHandleElId(this.resizeChild ? this.resizeChild.id : this.el.id);
\r
178 if(this.width !== null && this.height !== null){
\r
179 this.resizeTo(this.width, this.height);
\r
181 this.updateChildSize();
\r
184 this.el.dom.style.zoom = 1;
\r
186 Ext.Resizable.superclass.constructor.call(this);
\r
189 Ext.extend(Ext.Resizable, Ext.util.Observable, {
\r
190 resizeChild : false,
\r
191 adjustments : [0, 0],
\r
201 multiDirectional : false,
\r
202 disableTrackOver : false,
\r
203 easing : 'easeOutStrong',
\r
204 widthIncrement : 0,
\r
205 heightIncrement : 0,
\r
209 preserveRatio : false,
\r
210 transparent: false,
\r
216 * @cfg {Mixed} constrainTo Constrain the resize to a particular element
\r
219 * @cfg {Ext.lib.Region} resizeRegion Constrain the resize to a particular region
\r
223 * @event beforeresize
\r
224 * Fired before resize is allowed. Set enabled to false to cancel resize.
\r
225 * @param {Ext.Resizable} this
\r
226 * @param {Ext.EventObject} e The mousedown event
\r
230 * Fired after a resize.
\r
231 * @param {Ext.Resizable} this
\r
232 * @param {Number} width The new width
\r
233 * @param {Number} height The new height
\r
234 * @param {Ext.EventObject} e The mouseup event
\r
238 * Perform a manual resize
\r
239 * @param {Number} width
\r
240 * @param {Number} height
\r
242 resizeTo : function(width, height){
\r
243 this.el.setSize(width, height);
\r
244 this.updateChildSize();
\r
245 this.fireEvent("resize", this, width, height, null);
\r
249 startSizing : function(e, handle){
\r
250 this.fireEvent("beforeresize", this, e);
\r
251 if(this.enabled){ // 2nd enabled check in case disabled before beforeresize handler
\r
254 this.overlay = this.el.createProxy({tag: "div", cls: "x-resizable-overlay", html: " "}, Ext.getBody());
\r
255 this.overlay.unselectable();
\r
256 this.overlay.enableDisplayMode("block");
\r
257 this.overlay.on("mousemove", this.onMouseMove, this);
\r
258 this.overlay.on("mouseup", this.onMouseUp, this);
\r
260 this.overlay.setStyle("cursor", handle.el.getStyle("cursor"));
\r
262 this.resizing = true;
\r
263 this.startBox = this.el.getBox();
\r
264 this.startPoint = e.getXY();
\r
265 this.offsets = [(this.startBox.x + this.startBox.width) - this.startPoint[0],
\r
266 (this.startBox.y + this.startBox.height) - this.startPoint[1]];
\r
268 this.overlay.setSize(Ext.lib.Dom.getViewWidth(true), Ext.lib.Dom.getViewHeight(true));
\r
269 this.overlay.show();
\r
271 if(this.constrainTo) {
\r
272 var ct = Ext.get(this.constrainTo);
\r
273 this.resizeRegion = ct.getRegion().adjust(
\r
274 ct.getFrameWidth('t'),
\r
275 ct.getFrameWidth('l'),
\r
276 -ct.getFrameWidth('b'),
\r
277 -ct.getFrameWidth('r')
\r
281 this.proxy.setStyle('visibility', 'hidden'); // workaround display none
\r
283 this.proxy.setBox(this.startBox);
\r
285 this.proxy.setStyle('visibility', 'visible');
\r
291 onMouseDown : function(handle, e){
\r
294 this.activeHandle = handle;
\r
295 this.startSizing(e, handle);
\r
300 onMouseUp : function(e){
\r
301 var size = this.resizeElement();
\r
302 this.resizing = false;
\r
304 this.overlay.hide();
\r
306 this.fireEvent("resize", this, size.width, size.height, e);
\r
310 updateChildSize : function(){
\r
311 if(this.resizeChild){
\r
313 var child = this.resizeChild;
\r
314 var adj = this.adjustments;
\r
315 if(el.dom.offsetWidth){
\r
316 var b = el.getSize(true);
\r
317 child.setSize(b.width+adj[0], b.height+adj[1]);
\r
319 // Second call here for IE
\r
320 // The first call enables instant resizing and
\r
321 // the second call corrects scroll bars if they
\r
324 setTimeout(function(){
\r
325 if(el.dom.offsetWidth){
\r
326 var b = el.getSize(true);
\r
327 child.setSize(b.width+adj[0], b.height+adj[1]);
\r
335 snap : function(value, inc, min){
\r
336 if(!inc || !value) return value;
\r
337 var newValue = value;
\r
338 var m = value % inc;
\r
341 newValue = value + (inc-m);
\r
343 newValue = value - m;
\r
346 return Math.max(min, newValue);
\r
350 * <p>Performs resizing of the associated Element. This method is called internally by this
\r
351 * class, and should not be called by user code.</p>
\r
352 * <p>If a Resizable is being used to resize an Element which encapsulates a more complex UI
\r
353 * component such as a Panel, this method may be overridden by specifying an implementation
\r
354 * as a config option to provide appropriate behaviour at the end of the resize operation on
\r
355 * mouseup, for example resizing the Panel, and relaying the Panel's content.</p>
\r
356 * <p>The new area to be resized to is available by examining the state of the {@link #proxy}
\r
357 * Element. Example:
\r
360 title: 'Resize me',
\r
363 renderTo: Ext.getBody(),
\r
369 render: function(p) {
\r
370 new Ext.Resizable(p.getEl(), {
\r
374 resizeElement: function() {
\r
375 var box = this.proxy.getBox();
\r
388 resizeElement : function(){
\r
389 var box = this.proxy.getBox();
\r
390 if(this.updateBox){
\r
391 this.el.setBox(box, false, this.animate, this.duration, null, this.easing);
\r
393 this.el.setSize(box.width, box.height, this.animate, this.duration, null, this.easing);
\r
395 this.updateChildSize();
\r
403 constrain : function(v, diff, m, mx){
\r
406 }else if(v - diff > mx){
\r
413 onMouseMove : function(e){
\r
415 try{// try catch so if something goes wrong the user doesn't get hung
\r
417 if(this.resizeRegion && !this.resizeRegion.contains(e.getPoint())) {
\r
421 //var curXY = this.startPoint;
\r
422 var curSize = this.curSize || this.startBox;
\r
423 var x = this.startBox.x, y = this.startBox.y;
\r
424 var ox = x, oy = y;
\r
425 var w = curSize.width, h = curSize.height;
\r
426 var ow = w, oh = h;
\r
427 var mw = this.minWidth, mh = this.minHeight;
\r
428 var mxw = this.maxWidth, mxh = this.maxHeight;
\r
429 var wi = this.widthIncrement;
\r
430 var hi = this.heightIncrement;
\r
432 var eventXY = e.getXY();
\r
433 var diffX = -(this.startPoint[0] - Math.max(this.minX, eventXY[0]));
\r
434 var diffY = -(this.startPoint[1] - Math.max(this.minY, eventXY[1]));
\r
436 var pos = this.activeHandle.position;
\r
441 w = Math.min(Math.max(mw, w), mxw);
\r
445 h = Math.min(Math.max(mh, h), mxh);
\r
450 w = Math.min(Math.max(mw, w), mxw);
\r
451 h = Math.min(Math.max(mh, h), mxh);
\r
454 diffY = this.constrain(h, diffY, mh, mxh);
\r
459 diffX = this.constrain(w, diffX, mw, mxw);
\r
465 w = Math.min(Math.max(mw, w), mxw);
\r
466 diffY = this.constrain(h, diffY, mh, mxh);
\r
471 diffX = this.constrain(w, diffX, mw, mxw);
\r
472 diffY = this.constrain(h, diffY, mh, mxh);
\r
479 diffX = this.constrain(w, diffX, mw, mxw);
\r
481 h = Math.min(Math.max(mh, h), mxh);
\r
487 var sw = this.snap(w, wi, mw);
\r
488 var sh = this.snap(h, hi, mh);
\r
489 if(sw != w || sh != h){
\r
512 if(this.preserveRatio){
\r
517 h = Math.min(Math.max(mh, h), mxh);
\r
522 w = Math.min(Math.max(mw, w), mxw);
\r
527 w = Math.min(Math.max(mw, w), mxw);
\r
533 w = Math.min(Math.max(mw, w), mxw);
\r
539 h = Math.min(Math.max(mh, h), mxh);
\r
547 h = Math.min(Math.max(mh, h), mxh);
\r
557 h = Math.min(Math.max(mh, h), mxh);
\r
565 this.proxy.setBounds(x, y, w, h);
\r
567 this.resizeElement();
\r
574 handleOver : function(){
\r
576 this.el.addClass("x-resizable-over");
\r
581 handleOut : function(){
\r
582 if(!this.resizing){
\r
583 this.el.removeClass("x-resizable-over");
\r
588 * Returns the element this component is bound to.
\r
589 * @return {Ext.Element}
\r
591 getEl : function(){
\r
596 * Returns the resizeChild element (or null).
\r
597 * @return {Ext.Element}
\r
599 getResizeChild : function(){
\r
600 return this.resizeChild;
\r
604 * Destroys this resizable. If the element was wrapped and
\r
605 * removeEl is not true then the element remains.
\r
606 * @param {Boolean} removeEl (optional) true to remove the element from the DOM
\r
608 destroy : function(removeEl){
\r
613 Ext.destroy(this.overlay);
\r
614 this.overlay = null;
\r
616 Ext.destroy(this.proxy);
\r
619 var ps = Ext.Resizable.positions;
\r
621 if(typeof ps[k] != "function" && this[ps[k]]){
\r
622 this[ps[k]].destroy();
\r
626 this.el.update("");
\r
627 Ext.destroy(this.el);
\r
632 syncHandleHeight : function(){
\r
633 var h = this.el.getHeight(true);
\r
635 this.west.el.setHeight(h);
\r
638 this.east.el.setHeight(h);
\r
644 // hash to map config positions to true positions
\r
645 Ext.Resizable.positions = {
\r
646 n: "north", s: "south", e: "east", w: "west", se: "southeast", sw: "southwest", nw: "northwest", ne: "northeast"
\r
650 Ext.Resizable.Handle = function(rz, pos, disableTrackOver, transparent){
\r
652 // only initialize the template if resizable is used
\r
653 var tpl = Ext.DomHelper.createTemplate(
\r
654 {tag: "div", cls: "x-resizable-handle x-resizable-handle-{0}"}
\r
657 Ext.Resizable.Handle.prototype.tpl = tpl;
\r
659 this.position = pos;
\r
661 this.el = this.tpl.append(rz.el.dom, [this.position], true);
\r
662 this.el.unselectable();
\r
664 this.el.setOpacity(0);
\r
666 this.el.on("mousedown", this.onMouseDown, this);
\r
667 if(!disableTrackOver){
\r
668 this.el.on("mouseover", this.onMouseOver, this);
\r
669 this.el.on("mouseout", this.onMouseOut, this);
\r
674 Ext.Resizable.Handle.prototype = {
\r
676 afterResize : function(rz){
\r
680 onMouseDown : function(e){
\r
681 this.rz.onMouseDown(this, e);
\r
684 onMouseOver : function(e){
\r
685 this.rz.handleOver(this, e);
\r
688 onMouseOut : function(e){
\r
689 this.rz.handleOut(this, e);
\r
692 destroy : function(){
\r
693 Ext.destroy(this.el);
\r