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)
82 this.el = Roo.get(el);
84 if(config && config.wrap){
85 config.resizeChild = this.el;
86 this.el = this.el.wrap(typeof config.wrap == "object" ? config.wrap : {cls:"xresizable-wrap"});
87 this.el.id = this.el.dom.id = config.resizeChild.id + "-rzwrap";
88 this.el.setStyle("overflow", "hidden");
89 this.el.setPositioning(config.resizeChild.getPositioning());
90 config.resizeChild.clearPositioning();
91 if(!config.width || !config.height){
92 var csize = config.resizeChild.getSize();
93 this.el.setSize(csize.width, csize.height);
95 if(config.pinned && !config.adjustments){
96 config.adjustments = "auto";
100 this.proxy = this.el.createProxy({tag: "div", cls: "x-resizable-proxy", id: this.el.id + "-rzproxy"});
101 this.proxy.unselectable();
102 this.proxy.enableDisplayMode('block');
104 Roo.apply(this, config);
107 this.disableTrackOver = true;
108 this.el.addClass("x-resizable-pinned");
110 // if the element isn't positioned, make it relative
111 var position = this.el.getStyle("position");
112 if(position != "absolute" && position != "fixed"){
113 this.el.setStyle("position", "relative");
115 if(!this.handles){ // no handles passed, must be legacy style
116 this.handles = 's,e,se';
117 if(this.multiDirectional){
118 this.handles += ',n,w';
121 if(this.handles == "all"){
122 this.handles = "n s e w ne nw se sw";
124 var hs = this.handles.split(/\s*?[,;]\s*?| /);
125 var ps = Roo.Resizable.positions;
126 for(var i = 0, len = hs.length; i < len; i++){
127 if(hs[i] && ps[hs[i]]){
129 this[pos] = new Roo.Resizable.Handle(this, pos, this.disableTrackOver, this.transparent);
133 this.corner = this.southeast;
135 // updateBox = the box can move..
136 if(this.handles.indexOf("n") != -1 || this.handles.indexOf("w") != -1 || this.handles.indexOf("hd") != -1) {
137 this.updateBox = true;
140 this.activeHandle = null;
142 if(this.resizeChild){
143 if(typeof this.resizeChild == "boolean"){
144 this.resizeChild = Roo.get(this.el.dom.firstChild, true);
146 this.resizeChild = Roo.get(this.resizeChild, true);
150 if(this.adjustments == "auto"){
151 var rc = this.resizeChild;
152 var hw = this.west, he = this.east, hn = this.north, hs = this.south;
153 if(rc && (hw || hn)){
154 rc.position("relative");
155 rc.setLeft(hw ? hw.el.getWidth() : 0);
156 rc.setTop(hn ? hn.el.getHeight() : 0);
159 (he ? -he.el.getWidth() : 0) + (hw ? -hw.el.getWidth() : 0),
160 (hn ? -hn.el.getHeight() : 0) + (hs ? -hs.el.getHeight() : 0) -1
165 this.dd = this.dynamic ?
166 this.el.initDD(null) : this.el.initDDProxy(null, {dragElId: this.proxy.id});
167 this.dd.setHandleElId(this.resizeChild ? this.resizeChild.id : this.el.id);
173 * @event beforeresize
174 * Fired before resize is allowed. Set enabled to false to cancel resize.
175 * @param {Roo.Resizable} this
176 * @param {Roo.EventObject} e The mousedown event
178 "beforeresize" : true,
181 * Fired after a resize.
182 * @param {Roo.Resizable} this
183 * @param {Number} width The new width
184 * @param {Number} height The new height
185 * @param {Roo.EventObject} e The mouseup event
190 if(this.width !== null && this.height !== null){
191 this.resizeTo(this.width, this.height);
193 this.updateChildSize();
196 this.el.dom.style.zoom = 1;
198 Roo.Resizable.superclass.constructor.call(this);
201 Roo.extend(Roo.Resizable, Roo.util.Observable, {
203 adjustments : [0, 0],
213 multiDirectional : false,
214 disableTrackOver : false,
215 easing : 'easeOutStrong',
221 preserveRatio : false,
228 * @cfg {String/HTMLElement/Element} constrainTo Constrain the resize to a particular element
230 constrainTo: undefined,
232 * @cfg {Roo.lib.Region} resizeRegion Constrain the resize to a particular region
234 resizeRegion: undefined,
238 * Perform a manual resize
239 * @param {Number} width
240 * @param {Number} height
242 resizeTo : function(width, height){
243 this.el.setSize(width, height);
244 this.updateChildSize();
245 this.fireEvent("resize", this, width, height, null);
249 startSizing : function(e, handle){
250 this.fireEvent("beforeresize", this, e);
251 if(this.enabled){ // 2nd enabled check in case disabled before beforeresize handler
254 this.overlay = this.el.createProxy({tag: "div", cls: "x-resizable-overlay", html: " "});
255 this.overlay.unselectable();
256 this.overlay.enableDisplayMode("block");
257 this.overlay.on("mousemove", this.onMouseMove, this);
258 this.overlay.on("mouseup", this.onMouseUp, this);
260 this.overlay.setStyle("cursor", handle.el.getStyle("cursor"));
262 this.resizing = true;
263 this.startBox = this.el.getBox();
264 this.startPoint = e.getXY();
265 this.offsets = [(this.startBox.x + this.startBox.width) - this.startPoint[0],
266 (this.startBox.y + this.startBox.height) - this.startPoint[1]];
268 this.overlay.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
271 if(this.constrainTo) {
272 var ct = Roo.get(this.constrainTo);
273 this.resizeRegion = ct.getRegion().adjust(
274 ct.getFrameWidth('t'),
275 ct.getFrameWidth('l'),
276 -ct.getFrameWidth('b'),
277 -ct.getFrameWidth('r')
281 this.proxy.setStyle('visibility', 'hidden'); // workaround display none
283 this.proxy.setBox(this.startBox);
285 this.proxy.setStyle('visibility', 'visible');
291 onMouseDown : function(handle, e){
294 this.activeHandle = handle;
295 this.startSizing(e, handle);
300 onMouseUp : function(e){
301 var size = this.resizeElement();
302 this.resizing = false;
306 this.fireEvent("resize", this, size.width, size.height, e);
310 updateChildSize : function(){
311 if(this.resizeChild){
313 var child = this.resizeChild;
314 var adj = this.adjustments;
315 if(el.dom.offsetWidth){
316 var b = el.getSize(true);
317 child.setSize(b.width+adj[0], b.height+adj[1]);
319 // Second call here for IE
320 // The first call enables instant resizing and
321 // the second call corrects scroll bars if they
324 setTimeout(function(){
325 if(el.dom.offsetWidth){
326 var b = el.getSize(true);
327 child.setSize(b.width+adj[0], b.height+adj[1]);
335 snap : function(value, inc, min){
336 if(!inc || !value) return value;
337 var newValue = value;
341 newValue = value + (inc-m);
343 newValue = value - m;
346 return Math.max(min, newValue);
350 resizeElement : function(){
351 var box = this.proxy.getBox();
353 this.el.setBox(box, false, this.animate, this.duration, null, this.easing);
355 this.el.setSize(box.width, box.height, this.animate, this.duration, null, this.easing);
357 this.updateChildSize();
365 constrain : function(v, diff, m, mx){
368 }else if(v - diff > mx){
375 onMouseMove : function(e){
377 try{// try catch so if something goes wrong the user doesn't get hung
379 if(this.resizeRegion && !this.resizeRegion.contains(e.getPoint())) {
383 //var curXY = this.startPoint;
384 var curSize = this.curSize || this.startBox;
385 var x = this.startBox.x, y = this.startBox.y;
387 var w = curSize.width, h = curSize.height;
389 var mw = this.minWidth, mh = this.minHeight;
390 var mxw = this.maxWidth, mxh = this.maxHeight;
391 var wi = this.widthIncrement;
392 var hi = this.heightIncrement;
394 var eventXY = e.getXY();
395 var diffX = -(this.startPoint[0] - Math.max(this.minX, eventXY[0]));
396 var diffY = -(this.startPoint[1] - Math.max(this.minY, eventXY[1]));
398 var pos = this.activeHandle.position;
403 w = Math.min(Math.max(mw, w), mxw);
408 h = Math.min(Math.max(mh, h), mxh);
413 w = Math.min(Math.max(mw, w), mxw);
414 h = Math.min(Math.max(mh, h), mxh);
417 diffY = this.constrain(h, diffY, mh, mxh);
422 diffX = this.constrain(w, diffX, mw-diffX, mxw);
427 diffX = this.constrain(w, diffX, mw, mxw);
433 w = Math.min(Math.max(mw, w), mxw);
434 diffY = this.constrain(h, diffY, mh, mxh);
439 diffX = this.constrain(w, diffX, mw, mxw);
440 diffY = this.constrain(h, diffY, mh, mxh);
447 diffX = this.constrain(w, diffX, mw, mxw);
449 h = Math.min(Math.max(mh, h), mxh);
455 var sw = this.snap(w, wi, mw);
456 var sh = this.snap(h, hi, mh);
457 if(sw != w || sh != h){
481 if(this.preserveRatio){
486 h = Math.min(Math.max(mh, h), mxh);
491 w = Math.min(Math.max(mw, w), mxw);
496 w = Math.min(Math.max(mw, w), mxw);
502 w = Math.min(Math.max(mw, w), mxw);
508 h = Math.min(Math.max(mh, h), mxh);
516 h = Math.min(Math.max(mh, h), mxh);
526 h = Math.min(Math.max(mh, h), mxh);
534 if (pos == 'hdrag') {
537 this.proxy.setBounds(x, y, w, h);
539 this.resizeElement();
546 handleOver : function(){
548 this.el.addClass("x-resizable-over");
553 handleOut : function(){
555 this.el.removeClass("x-resizable-over");
560 * Returns the element this component is bound to.
561 * @return {Roo.Element}
568 * Returns the resizeChild element (or null).
569 * @return {Roo.Element}
571 getResizeChild : function(){
572 return this.resizeChild;
576 * Destroys this resizable. If the element was wrapped and
577 * removeEl is not true then the element remains.
578 * @param {Boolean} removeEl (optional) true to remove the element from the DOM
580 destroy : function(removeEl){
583 this.overlay.removeAllListeners();
584 this.overlay.remove();
586 var ps = Roo.Resizable.positions;
588 if(typeof ps[k] != "function" && this[ps[k]]){
590 h.el.removeAllListeners();
602 // hash to map config positions to true positions
603 Roo.Resizable.positions = {
604 n: "north", s: "south", e: "east", w: "west", se: "southeast", sw: "southwest", nw: "northwest", ne: "northeast",
609 Roo.Resizable.Handle = function(rz, pos, disableTrackOver, transparent){
611 // only initialize the template if resizable is used
612 var tpl = Roo.DomHelper.createTemplate(
613 {tag: "div", cls: "x-resizable-handle x-resizable-handle-{0}"}
616 Roo.Resizable.Handle.prototype.tpl = tpl;
620 // show north drag fro topdra
621 var handlepos = pos == 'hdrag' ? 'north' : pos;
623 this.el = this.tpl.append(rz.el.dom, [handlepos], true);
624 if (pos == 'hdrag') {
625 this.el.setStyle('cursor', 'pointer');
627 this.el.unselectable();
629 this.el.setOpacity(0);
631 this.el.on("mousedown", this.onMouseDown, this);
632 if(!disableTrackOver){
633 this.el.on("mouseover", this.onMouseOver, this);
634 this.el.on("mouseout", this.onMouseOut, this);
639 Roo.Resizable.Handle.prototype = {
640 afterResize : function(rz){
644 onMouseDown : function(e){
645 this.rz.onMouseDown(this, e);
648 onMouseOver : function(e){
649 this.rz.handleOver(this, e);
652 onMouseOut : function(e){
653 this.rz.handleOut(this, e);