3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.Resizable
\r
9 * @extends Ext.util.Observable
\r
10 * <p>Applies drag handles to an element to make it resizable. The drag handles are inserted into the element
\r
11 * and positioned absolute. Some elements, such as a textarea or image, don't support this. To overcome that, you can wrap
\r
12 * 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
13 * the element will be wrapped for you automatically.</p>
\r
14 * <p>Here is the list of valid resize handles:</p>
\r
17 ------ -------------------
\r
28 * <p>Here's an example showing the creation of a typical Resizable:</p>
\r
30 var resizer = new Ext.Resizable('element-id', {
\r
38 resizer.on('resize', myHandler);
\r
40 * <p>To hide a particular handle, set its display to none in CSS, or through script:<br>
\r
41 * resizer.east.setDisplayed(false);</p>
\r
43 * Create a new resizable component
\r
44 * @param {Mixed} el The id or element to resize
\r
45 * @param {Object} config configuration options
\r
47 Ext.Resizable = function(el, config){
\r
48 this.el = Ext.get(el);
\r
50 if(config && config.wrap){
\r
51 config.resizeChild = this.el;
\r
52 this.el = this.el.wrap(typeof config.wrap == 'object' ? config.wrap : {cls:'xresizable-wrap'});
\r
53 this.el.id = this.el.dom.id = config.resizeChild.id + '-rzwrap';
\r
54 this.el.setStyle('overflow', 'hidden');
\r
55 this.el.setPositioning(config.resizeChild.getPositioning());
\r
56 config.resizeChild.clearPositioning();
\r
57 if(!config.width || !config.height){
\r
58 var csize = config.resizeChild.getSize();
\r
59 this.el.setSize(csize.width, csize.height);
\r
61 if(config.pinned && !config.adjustments){
\r
62 config.adjustments = 'auto';
\r
67 * The proxy Element that is resized in place of the real Element during the resize operation.
\r
68 * This may be queried using {@link Ext.Element#getBox} to provide the new area to resize to.
\r
70 * @type Ext.Element.
\r
73 this.proxy = this.el.createProxy({tag: 'div', cls: 'x-resizable-proxy', id: this.el.id + '-rzproxy'}, Ext.getBody());
\r
74 this.proxy.unselectable();
\r
75 this.proxy.enableDisplayMode('block');
\r
77 Ext.apply(this, config);
\r
80 this.disableTrackOver = true;
\r
81 this.el.addClass('x-resizable-pinned');
\r
83 // if the element isn't positioned, make it relative
\r
84 var position = this.el.getStyle('position');
\r
85 if(position != 'absolute' && position != 'fixed'){
\r
86 this.el.setStyle('position', 'relative');
\r
88 if(!this.handles){ // no handles passed, must be legacy style
\r
89 this.handles = 's,e,se';
\r
90 if(this.multiDirectional){
\r
91 this.handles += ',n,w';
\r
94 if(this.handles == 'all'){
\r
95 this.handles = 'n s e w ne nw se sw';
\r
97 var hs = this.handles.split(/\s*?[,;]\s*?| /);
\r
98 var ps = Ext.Resizable.positions;
\r
99 for(var i = 0, len = hs.length; i < len; i++){
\r
100 if(hs[i] && ps[hs[i]]){
\r
101 var pos = ps[hs[i]];
\r
102 this[pos] = new Ext.Resizable.Handle(this, pos, this.disableTrackOver, this.transparent);
\r
106 this.corner = this.southeast;
\r
108 if(this.handles.indexOf('n') != -1 || this.handles.indexOf('w') != -1){
\r
109 this.updateBox = true;
\r
112 this.activeHandle = null;
\r
114 if(this.resizeChild){
\r
115 if(typeof this.resizeChild == 'boolean'){
\r
116 this.resizeChild = Ext.get(this.el.dom.firstChild, true);
\r
118 this.resizeChild = Ext.get(this.resizeChild, true);
\r
122 if(this.adjustments == 'auto'){
\r
123 var rc = this.resizeChild;
\r
124 var hw = this.west, he = this.east, hn = this.north, hs = this.south;
\r
125 if(rc && (hw || hn)){
\r
126 rc.position('relative');
\r
127 rc.setLeft(hw ? hw.el.getWidth() : 0);
\r
128 rc.setTop(hn ? hn.el.getHeight() : 0);
\r
130 this.adjustments = [
\r
131 (he ? -he.el.getWidth() : 0) + (hw ? -hw.el.getWidth() : 0),
\r
132 (hn ? -hn.el.getHeight() : 0) + (hs ? -hs.el.getHeight() : 0) -1
\r
136 if(this.draggable){
\r
137 this.dd = this.dynamic ?
\r
138 this.el.initDD(null) : this.el.initDDProxy(null, {dragElId: this.proxy.id});
\r
139 this.dd.setHandleElId(this.resizeChild ? this.resizeChild.id : this.el.id);
\r
144 * @event beforeresize
\r
145 * Fired before resize is allowed. Set {@link #enabled} to false to cancel resize.
\r
146 * @param {Ext.Resizable} this
\r
147 * @param {Ext.EventObject} e The mousedown event
\r
152 * Fired after a resize.
\r
153 * @param {Ext.Resizable} this
\r
154 * @param {Number} width The new width
\r
155 * @param {Number} height The new height
\r
156 * @param {Ext.EventObject} e The mouseup event
\r
161 if(this.width !== null && this.height !== null){
\r
162 this.resizeTo(this.width, this.height);
\r
164 this.updateChildSize();
\r
167 this.el.dom.style.zoom = 1;
\r
169 Ext.Resizable.superclass.constructor.call(this);
\r
172 Ext.extend(Ext.Resizable, Ext.util.Observable, {
\r
175 * @cfg {Array/String} adjustments String 'auto' or an array [width, height] with values to be <b>added</b> to the
\r
176 * resize operation's new size (defaults to <tt>[0, 0]</tt>)
\r
178 adjustments : [0, 0],
\r
180 * @cfg {Boolean} animate True to animate the resize (not compatible with dynamic sizing, defaults to false)
\r
184 * @cfg {Mixed} constrainTo Constrain the resize to a particular element
\r
187 * @cfg {Boolean} disableTrackOver True to disable mouse tracking. This is only applied at config time. (defaults to false)
\r
189 disableTrackOver : false,
\r
191 * @cfg {Boolean} draggable Convenience to initialize drag drop (defaults to false)
\r
195 * @cfg {Number} duration Animation duration if animate = true (defaults to 0.35)
\r
199 * @cfg {Boolean} dynamic True to resize the element while dragging instead of using a proxy (defaults to false)
\r
203 * @cfg {String} easing Animation easing if animate = true (defaults to <tt>'easingOutStrong'</tt>)
\r
205 easing : 'easeOutStrong',
\r
207 * @cfg {Boolean} enabled False to disable resizing (defaults to true)
\r
211 * @property enabled Writable. False if resizing is disabled.
\r
215 * @cfg {String} handles String consisting of the resize handles to display (defaults to undefined).
\r
216 * Specify either <tt>'all'</tt> or any of <tt>'n s e w ne nw se sw'</tt>.
\r
220 * @cfg {Boolean} multiDirectional <b>Deprecated</b>. Deprecated style of adding multi-direction resize handles.
\r
222 multiDirectional : false,
\r
224 * @cfg {Number} height The height of the element in pixels (defaults to null)
\r
228 * @cfg {Number} width The width of the element in pixels (defaults to null)
\r
232 * @cfg {Number} heightIncrement The increment to snap the height resize in pixels
\r
233 * (only applies if <code>{@link #dynamic}==true</code>). Defaults to <tt>0</tt>.
\r
235 heightIncrement : 0,
\r
237 * @cfg {Number} widthIncrement The increment to snap the width resize in pixels
\r
238 * (only applies if <code>{@link #dynamic}==true</code>). Defaults to <tt>0</tt>.
\r
240 widthIncrement : 0,
\r
242 * @cfg {Number} minHeight The minimum height for the element (defaults to 5)
\r
246 * @cfg {Number} minWidth The minimum width for the element (defaults to 5)
\r
250 * @cfg {Number} maxHeight The maximum height for the element (defaults to 10000)
\r
254 * @cfg {Number} maxWidth The maximum width for the element (defaults to 10000)
\r
258 * @cfg {Number} minX The minimum x for the element (defaults to 0)
\r
262 * @cfg {Number} minY The minimum x for the element (defaults to 0)
\r
266 * @cfg {Boolean} pinned True to ensure that the resize handles are always visible, false to display them only when the
\r
267 * user mouses over the resizable borders. This is only applied at config time. (defaults to false)
\r
271 * @cfg {Boolean} preserveRatio True to preserve the original ratio between height
\r
272 * and width during resize (defaults to false)
\r
274 preserveRatio : false,
\r
276 * @cfg {Boolean/String/Element} resizeChild True to resize the first child, or id/element to resize (defaults to false)
\r
278 resizeChild : false,
\r
280 * @cfg {Boolean} transparent True for transparent handles. This is only applied at config time. (defaults to false)
\r
282 transparent: false,
\r
284 * @cfg {Ext.lib.Region} resizeRegion Constrain the resize to a particular region
\r
287 * @cfg {Boolean} wrap True to wrap an element with a div if needed (required for textareas and images, defaults to false)
\r
288 * in favor of the handles config option (defaults to false)
\r
293 * Perform a manual resize and fires the 'resize' event.
\r
294 * @param {Number} width
\r
295 * @param {Number} height
\r
297 resizeTo : function(width, height){
\r
298 this.el.setSize(width, height);
\r
299 this.updateChildSize();
\r
300 this.fireEvent('resize', this, width, height, null);
\r
304 startSizing : function(e, handle){
\r
305 this.fireEvent('beforeresize', this, e);
\r
306 if(this.enabled){ // 2nd enabled check in case disabled before beforeresize handler
\r
309 this.overlay = this.el.createProxy({tag: 'div', cls: 'x-resizable-overlay', html: ' '}, Ext.getBody());
\r
310 this.overlay.unselectable();
\r
311 this.overlay.enableDisplayMode('block');
\r
314 mousemove: this.onMouseMove,
\r
315 mouseup: this.onMouseUp
\r
318 this.overlay.setStyle('cursor', handle.el.getStyle('cursor'));
\r
320 this.resizing = true;
\r
321 this.startBox = this.el.getBox();
\r
322 this.startPoint = e.getXY();
\r
323 this.offsets = [(this.startBox.x + this.startBox.width) - this.startPoint[0],
\r
324 (this.startBox.y + this.startBox.height) - this.startPoint[1]];
\r
326 this.overlay.setSize(Ext.lib.Dom.getViewWidth(true), Ext.lib.Dom.getViewHeight(true));
\r
327 this.overlay.show();
\r
329 if(this.constrainTo) {
\r
330 var ct = Ext.get(this.constrainTo);
\r
331 this.resizeRegion = ct.getRegion().adjust(
\r
332 ct.getFrameWidth('t'),
\r
333 ct.getFrameWidth('l'),
\r
334 -ct.getFrameWidth('b'),
\r
335 -ct.getFrameWidth('r')
\r
339 this.proxy.setStyle('visibility', 'hidden'); // workaround display none
\r
341 this.proxy.setBox(this.startBox);
\r
343 this.proxy.setStyle('visibility', 'visible');
\r
349 onMouseDown : function(handle, e){
\r
352 this.activeHandle = handle;
\r
353 this.startSizing(e, handle);
\r
358 onMouseUp : function(e){
\r
359 this.activeHandle = null;
\r
360 var size = this.resizeElement();
\r
361 this.resizing = false;
\r
363 this.overlay.hide();
\r
365 this.fireEvent('resize', this, size.width, size.height, e);
\r
369 updateChildSize : function(){
\r
370 if(this.resizeChild){
\r
372 var child = this.resizeChild;
\r
373 var adj = this.adjustments;
\r
374 if(el.dom.offsetWidth){
\r
375 var b = el.getSize(true);
\r
376 child.setSize(b.width+adj[0], b.height+adj[1]);
\r
378 // Second call here for IE
\r
379 // The first call enables instant resizing and
\r
380 // the second call corrects scroll bars if they
\r
383 setTimeout(function(){
\r
384 if(el.dom.offsetWidth){
\r
385 var b = el.getSize(true);
\r
386 child.setSize(b.width+adj[0], b.height+adj[1]);
\r
394 snap : function(value, inc, min){
\r
395 if(!inc || !value){
\r
398 var newValue = value;
\r
399 var m = value % inc;
\r
402 newValue = value + (inc-m);
\r
404 newValue = value - m;
\r
407 return Math.max(min, newValue);
\r
411 * <p>Performs resizing of the associated Element. This method is called internally by this
\r
412 * class, and should not be called by user code.</p>
\r
413 * <p>If a Resizable is being used to resize an Element which encapsulates a more complex UI
\r
414 * component such as a Panel, this method may be overridden by specifying an implementation
\r
415 * as a config option to provide appropriate behaviour at the end of the resize operation on
\r
416 * mouseup, for example resizing the Panel, and relaying the Panel's content.</p>
\r
417 * <p>The new area to be resized to is available by examining the state of the {@link #proxy}
\r
418 * Element. Example:
\r
421 title: 'Resize me',
\r
424 renderTo: Ext.getBody(),
\r
430 render: function(p) {
\r
431 new Ext.Resizable(p.getEl(), {
\r
435 resizeElement: function() {
\r
436 var box = this.proxy.getBox();
\r
449 resizeElement : function(){
\r
450 var box = this.proxy.getBox();
\r
451 if(this.updateBox){
\r
452 this.el.setBox(box, false, this.animate, this.duration, null, this.easing);
\r
454 this.el.setSize(box.width, box.height, this.animate, this.duration, null, this.easing);
\r
456 this.updateChildSize();
\r
464 constrain : function(v, diff, m, mx){
\r
467 }else if(v - diff > mx){
\r
474 onMouseMove : function(e){
\r
475 if(this.enabled && this.activeHandle){
\r
476 try{// try catch so if something goes wrong the user doesn't get hung
\r
478 if(this.resizeRegion && !this.resizeRegion.contains(e.getPoint())) {
\r
482 //var curXY = this.startPoint;
\r
483 var curSize = this.curSize || this.startBox,
\r
484 x = this.startBox.x, y = this.startBox.y,
\r
487 w = curSize.width,
\r
488 h = curSize.height,
\r
491 mw = this.minWidth,
\r
492 mh = this.minHeight,
\r
493 mxw = this.maxWidth,
\r
494 mxh = this.maxHeight,
\r
495 wi = this.widthIncrement,
\r
496 hi = this.heightIncrement,
\r
497 eventXY = e.getXY(),
\r
498 diffX = -(this.startPoint[0] - Math.max(this.minX, eventXY[0])),
\r
499 diffY = -(this.startPoint[1] - Math.max(this.minY, eventXY[1])),
\r
500 pos = this.activeHandle.position,
\r
507 w = Math.min(Math.max(mw, w), mxw);
\r
511 h = Math.min(Math.max(mh, h), mxh);
\r
516 w = Math.min(Math.max(mw, w), mxw);
\r
517 h = Math.min(Math.max(mh, h), mxh);
\r
520 diffY = this.constrain(h, diffY, mh, mxh);
\r
525 diffX = this.constrain(w, diffX, mw, mxw);
\r
531 w = Math.min(Math.max(mw, w), mxw);
\r
532 diffY = this.constrain(h, diffY, mh, mxh);
\r
537 diffX = this.constrain(w, diffX, mw, mxw);
\r
538 diffY = this.constrain(h, diffY, mh, mxh);
\r
545 diffX = this.constrain(w, diffX, mw, mxw);
\r
547 h = Math.min(Math.max(mh, h), mxh);
\r
553 var sw = this.snap(w, wi, mw);
\r
554 var sh = this.snap(h, hi, mh);
\r
555 if(sw != w || sh != h){
\r
578 if(this.preserveRatio){
\r
583 h = Math.min(Math.max(mh, h), mxh);
\r
588 w = Math.min(Math.max(mw, w), mxw);
\r
593 w = Math.min(Math.max(mw, w), mxw);
\r
599 w = Math.min(Math.max(mw, w), mxw);
\r
605 h = Math.min(Math.max(mh, h), mxh);
\r
613 h = Math.min(Math.max(mh, h), mxh);
\r
623 h = Math.min(Math.max(mh, h), mxh);
\r
631 this.proxy.setBounds(x, y, w, h);
\r
633 this.resizeElement();
\r
640 handleOver : function(){
\r
642 this.el.addClass('x-resizable-over');
\r
647 handleOut : function(){
\r
648 if(!this.resizing){
\r
649 this.el.removeClass('x-resizable-over');
\r
654 * Returns the element this component is bound to.
\r
655 * @return {Ext.Element}
\r
657 getEl : function(){
\r
662 * Returns the resizeChild element (or null).
\r
663 * @return {Ext.Element}
\r
665 getResizeChild : function(){
\r
666 return this.resizeChild;
\r
670 * Destroys this resizable. If the element was wrapped and
\r
671 * removeEl is not true then the element remains.
\r
672 * @param {Boolean} removeEl (optional) true to remove the element from the DOM
\r
674 destroy : function(removeEl){
\r
675 Ext.destroy(this.dd, this.overlay, this.proxy);
\r
676 this.overlay = null;
\r
679 var ps = Ext.Resizable.positions;
\r
681 if(typeof ps[k] != 'function' && this[ps[k]]){
\r
682 this[ps[k]].destroy();
\r
686 this.el.update('');
\r
687 Ext.destroy(this.el);
\r
690 this.purgeListeners();
\r
693 syncHandleHeight : function(){
\r
694 var h = this.el.getHeight(true);
\r
696 this.west.el.setHeight(h);
\r
699 this.east.el.setHeight(h);
\r
705 // hash to map config positions to true positions
\r
706 Ext.Resizable.positions = {
\r
707 n: 'north', s: 'south', e: 'east', w: 'west', se: 'southeast', sw: 'southwest', nw: 'northwest', ne: 'northeast'
\r
711 Ext.Resizable.Handle = function(rz, pos, disableTrackOver, transparent){
\r
713 // only initialize the template if resizable is used
\r
714 var tpl = Ext.DomHelper.createTemplate(
\r
715 {tag: 'div', cls: 'x-resizable-handle x-resizable-handle-{0}'}
\r
718 Ext.Resizable.Handle.prototype.tpl = tpl;
\r
720 this.position = pos;
\r
722 this.el = this.tpl.append(rz.el.dom, [this.position], true);
\r
723 this.el.unselectable();
\r
725 this.el.setOpacity(0);
\r
727 this.el.on('mousedown', this.onMouseDown, this);
\r
728 if(!disableTrackOver){
\r
731 mouseover: this.onMouseOver,
\r
732 mouseout: this.onMouseOut
\r
738 Ext.Resizable.Handle.prototype = {
\r
740 afterResize : function(rz){
\r
744 onMouseDown : function(e){
\r
745 this.rz.onMouseDown(this, e);
\r
748 onMouseOver : function(e){
\r
749 this.rz.handleOver(this, e);
\r
752 onMouseOut : function(e){
\r
753 this.rz.handleOut(this, e);
\r
756 destroy : function(){
\r
757 Ext.destroy(this.el);
\r