3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.Resizable"></div>/**
\r
9 * @class Ext.Resizable
\r
10 * @extends Ext.util.Observable
\r
11 * <p>Applies drag handles to an element to make it resizable. The drag handles are inserted into the element
\r
12 * and positioned absolute. Some elements, such as a textarea or image, don't support this. To overcome that, you can wrap
\r
13 * 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
14 * the element will be wrapped for you automatically.</p>
\r
15 * <p>Here is the list of valid resize handles:</p>
\r
18 ------ -------------------
\r
29 * <p>Here's an example showing the creation of a typical Resizable:</p>
\r
31 var resizer = new Ext.Resizable('element-id', {
\r
39 resizer.on('resize', myHandler);
\r
41 * <p>To hide a particular handle, set its display to none in CSS, or through script:<br>
\r
42 * resizer.east.setDisplayed(false);</p>
\r
44 * Create a new resizable component
\r
45 * @param {Mixed} el The id or element to resize
\r
46 * @param {Object} config configuration options
\r
48 Ext.Resizable = function(el, config){
\r
49 this.el = Ext.get(el);
\r
51 if(config && config.wrap){
\r
52 config.resizeChild = this.el;
\r
53 this.el = this.el.wrap(typeof config.wrap == 'object' ? config.wrap : {cls:'xresizable-wrap'});
\r
54 this.el.id = this.el.dom.id = config.resizeChild.id + '-rzwrap';
\r
55 this.el.setStyle('overflow', 'hidden');
\r
56 this.el.setPositioning(config.resizeChild.getPositioning());
\r
57 config.resizeChild.clearPositioning();
\r
58 if(!config.width || !config.height){
\r
59 var csize = config.resizeChild.getSize();
\r
60 this.el.setSize(csize.width, csize.height);
\r
62 if(config.pinned && !config.adjustments){
\r
63 config.adjustments = 'auto';
\r
67 <div id="prop-Ext.Resizable-proxy"></div>/**
\r
68 * The proxy Element that is resized in place of the real Element during the resize operation.
\r
69 * This may be queried using {@link Ext.Element#getBox} to provide the new area to resize to.
\r
71 * @type Ext.Element.
\r
74 this.proxy = this.el.createProxy({tag: 'div', cls: 'x-resizable-proxy', id: this.el.id + '-rzproxy'}, Ext.getBody());
\r
75 this.proxy.unselectable();
\r
76 this.proxy.enableDisplayMode('block');
\r
78 Ext.apply(this, config);
\r
81 this.disableTrackOver = true;
\r
82 this.el.addClass('x-resizable-pinned');
\r
84 // if the element isn't positioned, make it relative
\r
85 var position = this.el.getStyle('position');
\r
86 if(position != 'absolute' && position != 'fixed'){
\r
87 this.el.setStyle('position', 'relative');
\r
89 if(!this.handles){ // no handles passed, must be legacy style
\r
90 this.handles = 's,e,se';
\r
91 if(this.multiDirectional){
\r
92 this.handles += ',n,w';
\r
95 if(this.handles == 'all'){
\r
96 this.handles = 'n s e w ne nw se sw';
\r
98 var hs = this.handles.split(/\s*?[,;]\s*?| /);
\r
99 var ps = Ext.Resizable.positions;
\r
100 for(var i = 0, len = hs.length; i < len; i++){
\r
101 if(hs[i] && ps[hs[i]]){
\r
102 var pos = ps[hs[i]];
\r
103 this[pos] = new Ext.Resizable.Handle(this, pos, this.disableTrackOver, this.transparent);
\r
107 this.corner = this.southeast;
\r
109 if(this.handles.indexOf('n') != -1 || this.handles.indexOf('w') != -1){
\r
110 this.updateBox = true;
\r
113 this.activeHandle = null;
\r
115 if(this.resizeChild){
\r
116 if(typeof this.resizeChild == 'boolean'){
\r
117 this.resizeChild = Ext.get(this.el.dom.firstChild, true);
\r
119 this.resizeChild = Ext.get(this.resizeChild, true);
\r
123 if(this.adjustments == 'auto'){
\r
124 var rc = this.resizeChild;
\r
125 var hw = this.west, he = this.east, hn = this.north, hs = this.south;
\r
126 if(rc && (hw || hn)){
\r
127 rc.position('relative');
\r
128 rc.setLeft(hw ? hw.el.getWidth() : 0);
\r
129 rc.setTop(hn ? hn.el.getHeight() : 0);
\r
131 this.adjustments = [
\r
132 (he ? -he.el.getWidth() : 0) + (hw ? -hw.el.getWidth() : 0),
\r
133 (hn ? -hn.el.getHeight() : 0) + (hs ? -hs.el.getHeight() : 0) -1
\r
137 if(this.draggable){
\r
138 this.dd = this.dynamic ?
\r
139 this.el.initDD(null) : this.el.initDDProxy(null, {dragElId: this.proxy.id});
\r
140 this.dd.setHandleElId(this.resizeChild ? this.resizeChild.id : this.el.id);
\r
144 <div id="event-Ext.Resizable-beforeresize"></div>/**
\r
145 * @event beforeresize
\r
146 * Fired before resize is allowed. Set {@link #enabled} to false to cancel resize.
\r
147 * @param {Ext.Resizable} this
\r
148 * @param {Ext.EventObject} e The mousedown event
\r
151 <div id="event-Ext.Resizable-resize"></div>/**
\r
153 * Fired after a resize.
\r
154 * @param {Ext.Resizable} this
\r
155 * @param {Number} width The new width
\r
156 * @param {Number} height The new height
\r
157 * @param {Ext.EventObject} e The mouseup event
\r
162 if(this.width !== null && this.height !== null){
\r
163 this.resizeTo(this.width, this.height);
\r
165 this.updateChildSize();
\r
168 this.el.dom.style.zoom = 1;
\r
170 Ext.Resizable.superclass.constructor.call(this);
\r
173 Ext.extend(Ext.Resizable, Ext.util.Observable, {
\r
175 <div id="cfg-Ext.Resizable-adjustments"></div>/**
\r
176 * @cfg {Array/String} adjustments String 'auto' or an array [width, height] with values to be <b>added</b> to the
\r
177 * resize operation's new size (defaults to <tt>[0, 0]</tt>)
\r
179 adjustments : [0, 0],
\r
180 <div id="cfg-Ext.Resizable-animate"></div>/**
\r
181 * @cfg {Boolean} animate True to animate the resize (not compatible with dynamic sizing, defaults to false)
\r
184 <div id="cfg-Ext.Resizable-constrainTo"></div>/**
\r
185 * @cfg {Mixed} constrainTo Constrain the resize to a particular element
\r
187 <div id="cfg-Ext.Resizable-disableTrackOver"></div>/**
\r
188 * @cfg {Boolean} disableTrackOver True to disable mouse tracking. This is only applied at config time. (defaults to false)
\r
190 disableTrackOver : false,
\r
191 <div id="cfg-Ext.Resizable-draggable"></div>/**
\r
192 * @cfg {Boolean} draggable Convenience to initialize drag drop (defaults to false)
\r
195 <div id="cfg-Ext.Resizable-duration"></div>/**
\r
196 * @cfg {Number} duration Animation duration if animate = true (defaults to 0.35)
\r
199 <div id="cfg-Ext.Resizable-dynamic"></div>/**
\r
200 * @cfg {Boolean} dynamic True to resize the element while dragging instead of using a proxy (defaults to false)
\r
203 <div id="cfg-Ext.Resizable-easing"></div>/**
\r
204 * @cfg {String} easing Animation easing if animate = true (defaults to <tt>'easingOutStrong'</tt>)
\r
206 easing : 'easeOutStrong',
\r
207 <div id="cfg-Ext.Resizable-enabled"></div>/**
\r
208 * @cfg {Boolean} enabled False to disable resizing (defaults to true)
\r
211 <div id="prop-Ext.Resizable-enabled"></div>/**
\r
212 * @property enabled Writable. False if resizing is disabled.
\r
215 <div id="cfg-Ext.Resizable-handles"></div>/**
\r
216 * @cfg {String} handles String consisting of the resize handles to display (defaults to undefined).
\r
217 * Specify either <tt>'all'</tt> or any of <tt>'n s e w ne nw se sw'</tt>.
\r
220 <div id="cfg-Ext.Resizable-multiDirectional"></div>/**
\r
221 * @cfg {Boolean} multiDirectional <b>Deprecated</b>. Deprecated style of adding multi-direction resize handles.
\r
223 multiDirectional : false,
\r
224 <div id="cfg-Ext.Resizable-height"></div>/**
\r
225 * @cfg {Number} height The height of the element in pixels (defaults to null)
\r
228 <div id="cfg-Ext.Resizable-width"></div>/**
\r
229 * @cfg {Number} width The width of the element in pixels (defaults to null)
\r
232 <div id="cfg-Ext.Resizable-heightIncrement"></div>/**
\r
233 * @cfg {Number} heightIncrement The increment to snap the height resize in pixels
\r
234 * (only applies if <code>{@link #dynamic}==true</code>). Defaults to <tt>0</tt>.
\r
236 heightIncrement : 0,
\r
237 <div id="cfg-Ext.Resizable-widthIncrement"></div>/**
\r
238 * @cfg {Number} widthIncrement The increment to snap the width resize in pixels
\r
239 * (only applies if <code>{@link #dynamic}==true</code>). Defaults to <tt>0</tt>.
\r
241 widthIncrement : 0,
\r
242 <div id="cfg-Ext.Resizable-minHeight"></div>/**
\r
243 * @cfg {Number} minHeight The minimum height for the element (defaults to 5)
\r
246 <div id="cfg-Ext.Resizable-minWidth"></div>/**
\r
247 * @cfg {Number} minWidth The minimum width for the element (defaults to 5)
\r
250 <div id="cfg-Ext.Resizable-maxHeight"></div>/**
\r
251 * @cfg {Number} maxHeight The maximum height for the element (defaults to 10000)
\r
254 <div id="cfg-Ext.Resizable-maxWidth"></div>/**
\r
255 * @cfg {Number} maxWidth The maximum width for the element (defaults to 10000)
\r
258 <div id="cfg-Ext.Resizable-minX"></div>/**
\r
259 * @cfg {Number} minX The minimum x for the element (defaults to 0)
\r
262 <div id="cfg-Ext.Resizable-minY"></div>/**
\r
263 * @cfg {Number} minY The minimum x for the element (defaults to 0)
\r
266 <div id="cfg-Ext.Resizable-pinned"></div>/**
\r
267 * @cfg {Boolean} pinned True to ensure that the resize handles are always visible, false to display them only when the
\r
268 * user mouses over the resizable borders. This is only applied at config time. (defaults to false)
\r
271 <div id="cfg-Ext.Resizable-preserveRatio"></div>/**
\r
272 * @cfg {Boolean} preserveRatio True to preserve the original ratio between height
\r
273 * and width during resize (defaults to false)
\r
275 preserveRatio : false,
\r
276 <div id="cfg-Ext.Resizable-resizeChild"></div>/**
\r
277 * @cfg {Boolean/String/Element} resizeChild True to resize the first child, or id/element to resize (defaults to false)
\r
279 resizeChild : false,
\r
280 <div id="cfg-Ext.Resizable-transparent"></div>/**
\r
281 * @cfg {Boolean} transparent True for transparent handles. This is only applied at config time. (defaults to false)
\r
283 transparent: false,
\r
284 <div id="cfg-Ext.Resizable-resizeRegion"></div>/**
\r
285 * @cfg {Ext.lib.Region} resizeRegion Constrain the resize to a particular region
\r
287 <div id="cfg-Ext.Resizable-wrap"></div>/**
\r
288 * @cfg {Boolean} wrap True to wrap an element with a div if needed (required for textareas and images, defaults to false)
\r
289 * in favor of the handles config option (defaults to false)
\r
293 <div id="method-Ext.Resizable-resizeTo"></div>/**
\r
294 * Perform a manual resize and fires the 'resize' event.
\r
295 * @param {Number} width
\r
296 * @param {Number} height
\r
298 resizeTo : function(width, height){
\r
299 this.el.setSize(width, height);
\r
300 this.updateChildSize();
\r
301 this.fireEvent('resize', this, width, height, null);
\r
305 startSizing : function(e, handle){
\r
306 this.fireEvent('beforeresize', this, e);
\r
307 if(this.enabled){ // 2nd enabled check in case disabled before beforeresize handler
\r
310 this.overlay = this.el.createProxy({tag: 'div', cls: 'x-resizable-overlay', html: ' '}, Ext.getBody());
\r
311 this.overlay.unselectable();
\r
312 this.overlay.enableDisplayMode('block');
\r
315 mousemove: this.onMouseMove,
\r
316 mouseup: this.onMouseUp
\r
319 this.overlay.setStyle('cursor', handle.el.getStyle('cursor'));
\r
321 this.resizing = true;
\r
322 this.startBox = this.el.getBox();
\r
323 this.startPoint = e.getXY();
\r
324 this.offsets = [(this.startBox.x + this.startBox.width) - this.startPoint[0],
\r
325 (this.startBox.y + this.startBox.height) - this.startPoint[1]];
\r
327 this.overlay.setSize(Ext.lib.Dom.getViewWidth(true), Ext.lib.Dom.getViewHeight(true));
\r
328 this.overlay.show();
\r
330 if(this.constrainTo) {
\r
331 var ct = Ext.get(this.constrainTo);
\r
332 this.resizeRegion = ct.getRegion().adjust(
\r
333 ct.getFrameWidth('t'),
\r
334 ct.getFrameWidth('l'),
\r
335 -ct.getFrameWidth('b'),
\r
336 -ct.getFrameWidth('r')
\r
340 this.proxy.setStyle('visibility', 'hidden'); // workaround display none
\r
342 this.proxy.setBox(this.startBox);
\r
344 this.proxy.setStyle('visibility', 'visible');
\r
350 onMouseDown : function(handle, e){
\r
353 this.activeHandle = handle;
\r
354 this.startSizing(e, handle);
\r
359 onMouseUp : function(e){
\r
360 this.activeHandle = null;
\r
361 var size = this.resizeElement();
\r
362 this.resizing = false;
\r
364 this.overlay.hide();
\r
366 this.fireEvent('resize', this, size.width, size.height, e);
\r
370 updateChildSize : function(){
\r
371 if(this.resizeChild){
\r
373 var child = this.resizeChild;
\r
374 var adj = this.adjustments;
\r
375 if(el.dom.offsetWidth){
\r
376 var b = el.getSize(true);
\r
377 child.setSize(b.width+adj[0], b.height+adj[1]);
\r
379 // Second call here for IE
\r
380 // The first call enables instant resizing and
\r
381 // the second call corrects scroll bars if they
\r
384 setTimeout(function(){
\r
385 if(el.dom.offsetWidth){
\r
386 var b = el.getSize(true);
\r
387 child.setSize(b.width+adj[0], b.height+adj[1]);
\r
395 snap : function(value, inc, min){
\r
396 if(!inc || !value){
\r
399 var newValue = value;
\r
400 var m = value % inc;
\r
403 newValue = value + (inc-m);
\r
405 newValue = value - m;
\r
408 return Math.max(min, newValue);
\r
411 <div id="method-Ext.Resizable-resizeElement"></div>/**
\r
412 * <p>Performs resizing of the associated Element. This method is called internally by this
\r
413 * class, and should not be called by user code.</p>
\r
414 * <p>If a Resizable is being used to resize an Element which encapsulates a more complex UI
\r
415 * component such as a Panel, this method may be overridden by specifying an implementation
\r
416 * as a config option to provide appropriate behaviour at the end of the resize operation on
\r
417 * mouseup, for example resizing the Panel, and relaying the Panel's content.</p>
\r
418 * <p>The new area to be resized to is available by examining the state of the {@link #proxy}
\r
419 * Element. Example:
\r
422 title: 'Resize me',
\r
425 renderTo: Ext.getBody(),
\r
431 render: function(p) {
\r
432 new Ext.Resizable(p.getEl(), {
\r
436 resizeElement: function() {
\r
437 var box = this.proxy.getBox();
\r
450 resizeElement : function(){
\r
451 var box = this.proxy.getBox();
\r
452 if(this.updateBox){
\r
453 this.el.setBox(box, false, this.animate, this.duration, null, this.easing);
\r
455 this.el.setSize(box.width, box.height, this.animate, this.duration, null, this.easing);
\r
457 this.updateChildSize();
\r
465 constrain : function(v, diff, m, mx){
\r
468 }else if(v - diff > mx){
\r
475 onMouseMove : function(e){
\r
476 if(this.enabled && this.activeHandle){
\r
477 try{// try catch so if something goes wrong the user doesn't get hung
\r
479 if(this.resizeRegion && !this.resizeRegion.contains(e.getPoint())) {
\r
483 //var curXY = this.startPoint;
\r
484 var curSize = this.curSize || this.startBox,
\r
485 x = this.startBox.x, y = this.startBox.y,
\r
488 w = curSize.width,
\r
489 h = curSize.height,
\r
492 mw = this.minWidth,
\r
493 mh = this.minHeight,
\r
494 mxw = this.maxWidth,
\r
495 mxh = this.maxHeight,
\r
496 wi = this.widthIncrement,
\r
497 hi = this.heightIncrement,
\r
498 eventXY = e.getXY(),
\r
499 diffX = -(this.startPoint[0] - Math.max(this.minX, eventXY[0])),
\r
500 diffY = -(this.startPoint[1] - Math.max(this.minY, eventXY[1])),
\r
501 pos = this.activeHandle.position,
\r
508 w = Math.min(Math.max(mw, w), mxw);
\r
512 h = Math.min(Math.max(mh, h), mxh);
\r
517 w = Math.min(Math.max(mw, w), mxw);
\r
518 h = Math.min(Math.max(mh, h), mxh);
\r
521 diffY = this.constrain(h, diffY, mh, mxh);
\r
526 diffX = this.constrain(w, diffX, mw, mxw);
\r
532 w = Math.min(Math.max(mw, w), mxw);
\r
533 diffY = this.constrain(h, diffY, mh, mxh);
\r
538 diffX = this.constrain(w, diffX, mw, mxw);
\r
539 diffY = this.constrain(h, diffY, mh, mxh);
\r
546 diffX = this.constrain(w, diffX, mw, mxw);
\r
548 h = Math.min(Math.max(mh, h), mxh);
\r
554 var sw = this.snap(w, wi, mw);
\r
555 var sh = this.snap(h, hi, mh);
\r
556 if(sw != w || sh != h){
\r
579 if(this.preserveRatio){
\r
584 h = Math.min(Math.max(mh, h), mxh);
\r
589 w = Math.min(Math.max(mw, w), mxw);
\r
594 w = Math.min(Math.max(mw, w), mxw);
\r
600 w = Math.min(Math.max(mw, w), mxw);
\r
606 h = Math.min(Math.max(mh, h), mxh);
\r
614 h = Math.min(Math.max(mh, h), mxh);
\r
624 h = Math.min(Math.max(mh, h), mxh);
\r
632 this.proxy.setBounds(x, y, w, h);
\r
634 this.resizeElement();
\r
641 handleOver : function(){
\r
643 this.el.addClass('x-resizable-over');
\r
648 handleOut : function(){
\r
649 if(!this.resizing){
\r
650 this.el.removeClass('x-resizable-over');
\r
654 <div id="method-Ext.Resizable-getEl"></div>/**
\r
655 * Returns the element this component is bound to.
\r
656 * @return {Ext.Element}
\r
658 getEl : function(){
\r
662 <div id="method-Ext.Resizable-getResizeChild"></div>/**
\r
663 * Returns the resizeChild element (or null).
\r
664 * @return {Ext.Element}
\r
666 getResizeChild : function(){
\r
667 return this.resizeChild;
\r
670 <div id="method-Ext.Resizable-destroy"></div>/**
\r
671 * Destroys this resizable. If the element was wrapped and
\r
672 * removeEl is not true then the element remains.
\r
673 * @param {Boolean} removeEl (optional) true to remove the element from the DOM
\r
675 destroy : function(removeEl){
\r
676 Ext.destroy(this.dd, this.overlay, this.proxy);
\r
677 this.overlay = null;
\r
680 var ps = Ext.Resizable.positions;
\r
682 if(typeof ps[k] != 'function' && this[ps[k]]){
\r
683 this[ps[k]].destroy();
\r
687 this.el.update('');
\r
688 Ext.destroy(this.el);
\r
691 this.purgeListeners();
\r
694 syncHandleHeight : function(){
\r
695 var h = this.el.getHeight(true);
\r
697 this.west.el.setHeight(h);
\r
700 this.east.el.setHeight(h);
\r
706 // hash to map config positions to true positions
\r
707 Ext.Resizable.positions = {
\r
708 n: 'north', s: 'south', e: 'east', w: 'west', se: 'southeast', sw: 'southwest', nw: 'northwest', ne: 'northeast'
\r
712 Ext.Resizable.Handle = function(rz, pos, disableTrackOver, transparent){
\r
714 // only initialize the template if resizable is used
\r
715 var tpl = Ext.DomHelper.createTemplate(
\r
716 {tag: 'div', cls: 'x-resizable-handle x-resizable-handle-{0}'}
\r
719 Ext.Resizable.Handle.prototype.tpl = tpl;
\r
721 this.position = pos;
\r
723 this.el = this.tpl.append(rz.el.dom, [this.position], true);
\r
724 this.el.unselectable();
\r
726 this.el.setOpacity(0);
\r
728 this.el.on('mousedown', this.onMouseDown, this);
\r
729 if(!disableTrackOver){
\r
732 mouseover: this.onMouseOver,
\r
733 mouseout: this.onMouseOut
\r
739 Ext.Resizable.Handle.prototype = {
\r
741 afterResize : function(rz){
\r
745 onMouseDown : function(e){
\r
746 this.rz.onMouseDown(this, e);
\r
749 onMouseOver : function(e){
\r
750 this.rz.handleOver(this, e);
\r
753 onMouseOut : function(e){
\r
754 this.rz.handleOut(this, e);
\r
757 destroy : function(){
\r
758 Ext.destroy(this.el);
\r