4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.Resizable
14 * @extends Roo.util.Observable
15 * <p>Applies drag handles to an element to make it resizable. The drag handles are inserted into the element
16 * and positioned absolute. Some elements, such as a textarea or image, don't support this. To overcome that, you can wrap
17 * 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
18 * the element will be wrapped for you automatically.</p>
19 * <p>Here is the list of valid resize handles:</p>
22 ------ -------------------
33 * <p>Here's an example showing the creation of a typical Resizable:</p>
35 var resizer = new Roo.Resizable("element-id", {
43 resizer.on("resize", myHandler);
45 * <p>To hide a particular handle, set its display to none in CSS, or through script:<br>
46 * resizer.east.setDisplayed(false);</p>
47 * @cfg {Boolean/String/Element} resizeChild True to resize the first child, or id/element to resize (defaults to false)
48 * @cfg {Array/String} adjustments String "auto" or an array [width, height] with values to be <b>added</b> to the
49 * resize operation's new size (defaults to [0, 0])
50 * @cfg {Number} minWidth The minimum width for the element (defaults to 5)
51 * @cfg {Number} minHeight The minimum height for the element (defaults to 5)
52 * @cfg {Number} maxWidth The maximum width for the element (defaults to 10000)
53 * @cfg {Number} maxHeight The maximum height for the element (defaults to 10000)
54 * @cfg {Boolean} enabled False to disable resizing (defaults to true)
55 * @cfg {Boolean} wrap True to wrap an element with a div if needed (required for textareas and images, defaults to false)
56 * @cfg {Number} width The width of the element in pixels (defaults to null)
57 * @cfg {Number} height The height of the element in pixels (defaults to null)
58 * @cfg {Boolean} animate True to animate the resize (not compatible with dynamic sizing, defaults to false)
59 * @cfg {Number} duration Animation duration if animate = true (defaults to .35)
60 * @cfg {Boolean} dynamic True to resize the element while dragging instead of using a proxy (defaults to false)
61 * @cfg {String} handles String consisting of the resize handles to display (defaults to undefined)
62 * @cfg {Boolean} multiDirectional <b>Deprecated</b>. The old style of adding multi-direction resize handles, deprecated
63 * in favor of the handles config option (defaults to false)
64 * @cfg {Boolean} disableTrackOver True to disable mouse tracking. This is only applied at config time. (defaults to false)
65 * @cfg {String} easing Animation easing if animate = true (defaults to 'easingOutStrong')
66 * @cfg {Number} widthIncrement The increment to snap the width resize in pixels (dynamic must be true, defaults to 0)
67 * @cfg {Number} heightIncrement The increment to snap the height resize in pixels (dynamic must be true, defaults to 0)
68 * @cfg {Boolean} pinned True to ensure that the resize handles are always visible, false to display them only when the
69 * user mouses over the resizable borders. This is only applied at config time. (defaults to false)
70 * @cfg {Boolean} preserveRatio True to preserve the original ratio between height and width during resize (defaults to false)
71 * @cfg {Boolean} transparent True for transparent handles. This is only applied at config time. (defaults to false)
72 * @cfg {Number} minX The minimum allowed page X for the element (only used for west resizing, defaults to 0)
73 * @cfg {Number} minY The minimum allowed page Y for the element (only used for north resizing, defaults to 0)
74 * @cfg {Boolean} draggable Convenience to initialize drag drop (defaults to false)
76 * Create a new resizable component
77 * @param {String/HTMLElement/Roo.Element} el The id or element to resize
78 * @param {Object} config configuration options
80 Roo.Resizable = function(el, config){
81 this.el = Roo.get(el);
83 if(config && config.wrap){
84 config.resizeChild = this.el;
85 this.el = this.el.wrap(typeof config.wrap == "object" ? config.wrap : {cls:"xresizable-wrap"});
86 this.el.id = this.el.dom.id = config.resizeChild.id + "-rzwrap";
87 this.el.setStyle("overflow", "hidden");
88 this.el.setPositioning(config.resizeChild.getPositioning());
89 config.resizeChild.clearPositioning();
90 if(!config.width || !config.height){
91 var csize = config.resizeChild.getSize();
92 this.el.setSize(csize.width, csize.height);
94 if(config.pinned && !config.adjustments){
95 config.adjustments = "auto";
99 this.proxy = this.el.createProxy({tag: "div", cls: "x-resizable-proxy", id: this.el.id + "-rzproxy"});
100 this.proxy.unselectable();
101 this.proxy.enableDisplayMode('block');
103 Roo.apply(this, config);
106 this.disableTrackOver = true;
107 this.el.addClass("x-resizable-pinned");
109 // if the element isn't positioned, make it relative
110 var position = this.el.getStyle("position");
111 if(position != "absolute" && position != "fixed"){
112 this.el.setStyle("position", "relative");
114 if(!this.handles){ // no handles passed, must be legacy style
115 this.handles = 's,e,se';
116 if(this.multiDirectional){
117 this.handles += ',n,w';
120 if(this.handles == "all"){
121 this.handles = "n s e w ne nw se sw";
123 var hs = this.handles.split(/\s*?[,;]\s*?| /);
124 var ps = Roo.Resizable.positions;
125 for(var i = 0, len = hs.length; i < len; i++){
126 if(hs[i] && ps[hs[i]]){
128 this[pos] = new Roo.Resizable.Handle(this, pos, this.disableTrackOver, this.transparent);
132 this.corner = this.southeast;
134 if(this.handles.indexOf("n") != -1 || this.handles.indexOf("w") != -1){
135 this.updateBox = true;
138 this.activeHandle = null;
140 if(this.resizeChild){
141 if(typeof this.resizeChild == "boolean"){
142 this.resizeChild = Roo.get(this.el.dom.firstChild, true);
144 this.resizeChild = Roo.get(this.resizeChild, true);
148 if(this.adjustments == "auto"){
149 var rc = this.resizeChild;
150 var hw = this.west, he = this.east, hn = this.north, hs = this.south;
151 if(rc && (hw || hn)){
152 rc.position("relative");
153 rc.setLeft(hw ? hw.el.getWidth() : 0);
154 rc.setTop(hn ? hn.el.getHeight() : 0);
157 (he ? -he.el.getWidth() : 0) + (hw ? -hw.el.getWidth() : 0),
158 (hn ? -hn.el.getHeight() : 0) + (hs ? -hs.el.getHeight() : 0) -1
163 this.dd = this.dynamic ?
164 this.el.initDD(null) : this.el.initDDProxy(null, {dragElId: this.proxy.id});
165 this.dd.setHandleElId(this.resizeChild ? this.resizeChild.id : this.el.id);
171 * @event beforeresize
172 * Fired before resize is allowed. Set enabled to false to cancel resize.
173 * @param {Roo.Resizable} this
174 * @param {Roo.EventObject} e The mousedown event
176 "beforeresize" : true,
179 * Fired after a resize.
180 * @param {Roo.Resizable} this
181 * @param {Number} width The new width
182 * @param {Number} height The new height
183 * @param {Roo.EventObject} e The mouseup event
188 if(this.width !== null && this.height !== null){
189 this.resizeTo(this.width, this.height);
191 this.updateChildSize();
194 this.el.dom.style.zoom = 1;
196 Roo.Resizable.superclass.constructor.call(this);
199 Roo.extend(Roo.Resizable, Roo.util.Observable, {
201 adjustments : [0, 0],
211 multiDirectional : false,
212 disableTrackOver : false,
213 easing : 'easeOutStrong',
219 preserveRatio : false,
226 * @cfg {String/HTMLElement/Element} constrainTo Constrain the resize to a particular element
228 constrainTo: undefined,
230 * @cfg {Roo.lib.Region} resizeRegion Constrain the resize to a particular region
232 resizeRegion: undefined,
236 * Perform a manual resize
237 * @param {Number} width
238 * @param {Number} height
240 resizeTo : function(width, height){
241 this.el.setSize(width, height);
242 this.updateChildSize();
243 this.fireEvent("resize", this, width, height, null);
247 startSizing : function(e, handle){
248 this.fireEvent("beforeresize", this, e);
249 if(this.enabled){ // 2nd enabled check in case disabled before beforeresize handler
252 this.overlay = this.el.createProxy({tag: "div", cls: "x-resizable-overlay", html: " "});
253 this.overlay.unselectable();
254 this.overlay.enableDisplayMode("block");
255 this.overlay.on("mousemove", this.onMouseMove, this);
256 this.overlay.on("mouseup", this.onMouseUp, this);
258 this.overlay.setStyle("cursor", handle.el.getStyle("cursor"));
260 this.resizing = true;
261 this.startBox = this.el.getBox();
262 this.startPoint = e.getXY();
263 this.offsets = [(this.startBox.x + this.startBox.width) - this.startPoint[0],
264 (this.startBox.y + this.startBox.height) - this.startPoint[1]];
266 this.overlay.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
269 if(this.constrainTo) {
270 var ct = Roo.get(this.constrainTo);
271 this.resizeRegion = ct.getRegion().adjust(
272 ct.getFrameWidth('t'),
273 ct.getFrameWidth('l'),
274 -ct.getFrameWidth('b'),
275 -ct.getFrameWidth('r')
279 this.proxy.setStyle('visibility', 'hidden'); // workaround display none
281 this.proxy.setBox(this.startBox);
283 this.proxy.setStyle('visibility', 'visible');
289 onMouseDown : function(handle, e){
292 this.activeHandle = handle;
293 this.startSizing(e, handle);
298 onMouseUp : function(e){
299 var size = this.resizeElement();
300 this.resizing = false;
304 this.fireEvent("resize", this, size.width, size.height, e);
308 updateChildSize : function(){
309 if(this.resizeChild){
311 var child = this.resizeChild;
312 var adj = this.adjustments;
313 if(el.dom.offsetWidth){
314 var b = el.getSize(true);
315 child.setSize(b.width+adj[0], b.height+adj[1]);
317 // Second call here for IE
318 // The first call enables instant resizing and
319 // the second call corrects scroll bars if they
322 setTimeout(function(){
323 if(el.dom.offsetWidth){
324 var b = el.getSize(true);
325 child.setSize(b.width+adj[0], b.height+adj[1]);
333 snap : function(value, inc, min){
334 if(!inc || !value) return value;
335 var newValue = value;
339 newValue = value + (inc-m);
341 newValue = value - m;
344 return Math.max(min, newValue);
348 resizeElement : function(){
349 var box = this.proxy.getBox();
351 this.el.setBox(box, false, this.animate, this.duration, null, this.easing);
353 this.el.setSize(box.width, box.height, this.animate, this.duration, null, this.easing);
355 this.updateChildSize();
363 constrain : function(v, diff, m, mx){
366 }else if(v - diff > mx){
373 onMouseMove : function(e){
375 try{// try catch so if something goes wrong the user doesn't get hung
377 if(this.resizeRegion && !this.resizeRegion.contains(e.getPoint())) {
381 //var curXY = this.startPoint;
382 var curSize = this.curSize || this.startBox;
383 var x = this.startBox.x, y = this.startBox.y;
385 var w = curSize.width, h = curSize.height;
387 var mw = this.minWidth, mh = this.minHeight;
388 var mxw = this.maxWidth, mxh = this.maxHeight;
389 var wi = this.widthIncrement;
390 var hi = this.heightIncrement;
392 var eventXY = e.getXY();
393 var diffX = -(this.startPoint[0] - Math.max(this.minX, eventXY[0]));
394 var diffY = -(this.startPoint[1] - Math.max(this.minY, eventXY[1]));
396 var pos = this.activeHandle.position;
401 w = Math.min(Math.max(mw, w), mxw);
405 h = Math.min(Math.max(mh, h), mxh);
410 w = Math.min(Math.max(mw, w), mxw);
411 h = Math.min(Math.max(mh, h), mxh);
414 diffY = this.constrain(h, diffY, mh, mxh);
419 diffX = this.constrain(w, diffX, mw, mxw);
425 w = Math.min(Math.max(mw, w), mxw);
426 diffY = this.constrain(h, diffY, mh, mxh);
431 diffX = this.constrain(w, diffX, mw, mxw);
432 diffY = this.constrain(h, diffY, mh, mxh);
439 diffX = this.constrain(w, diffX, mw, mxw);
441 h = Math.min(Math.max(mh, h), mxh);
447 var sw = this.snap(w, wi, mw);
448 var sh = this.snap(h, hi, mh);
449 if(sw != w || sh != h){
472 if(this.preserveRatio){
477 h = Math.min(Math.max(mh, h), mxh);
482 w = Math.min(Math.max(mw, w), mxw);
487 w = Math.min(Math.max(mw, w), mxw);
493 w = Math.min(Math.max(mw, w), mxw);
499 h = Math.min(Math.max(mh, h), mxh);
507 h = Math.min(Math.max(mh, h), mxh);
517 h = Math.min(Math.max(mh, h), mxh);
525 this.proxy.setBounds(x, y, w, h);
527 this.resizeElement();
534 handleOver : function(){
536 this.el.addClass("x-resizable-over");
541 handleOut : function(){
543 this.el.removeClass("x-resizable-over");
548 * Returns the element this component is bound to.
549 * @return {Roo.Element}
556 * Returns the resizeChild element (or null).
557 * @return {Roo.Element}
559 getResizeChild : function(){
560 return this.resizeChild;
564 * Destroys this resizable. If the element was wrapped and
565 * removeEl is not true then the element remains.
566 * @param {Boolean} removeEl (optional) true to remove the element from the DOM
568 destroy : function(removeEl){
571 this.overlay.removeAllListeners();
572 this.overlay.remove();
574 var ps = Roo.Resizable.positions;
576 if(typeof ps[k] != "function" && this[ps[k]]){
578 h.el.removeAllListeners();
590 // hash to map config positions to true positions
591 Roo.Resizable.positions = {
592 n: "north", s: "south", e: "east", w: "west", se: "southeast", sw: "southwest", nw: "northwest", ne: "northeast"
596 Roo.Resizable.Handle = function(rz, pos, disableTrackOver, transparent){
598 // only initialize the template if resizable is used
599 var tpl = Roo.DomHelper.createTemplate(
600 {tag: "div", cls: "x-resizable-handle x-resizable-handle-{0}"}
603 Roo.Resizable.Handle.prototype.tpl = tpl;
607 this.el = this.tpl.append(rz.el.dom, [this.position], true);
608 this.el.unselectable();
610 this.el.setOpacity(0);
612 this.el.on("mousedown", this.onMouseDown, this);
613 if(!disableTrackOver){
614 this.el.on("mouseover", this.onMouseOver, this);
615 this.el.on("mouseout", this.onMouseOut, this);
620 Roo.Resizable.Handle.prototype = {
621 afterResize : function(rz){
625 onMouseDown : function(e){
626 this.rz.onMouseDown(this, e);
629 onMouseOver : function(e){
630 this.rz.handleOver(this, e);
633 onMouseOut : function(e){
634 this.rz.handleOut(this, e);