source: trunk/template-common/lib/ui/ui.resizable.js @ 3282

Last change on this file since 3282 was 3282, checked in by plg, 15 years ago

change: according to topic:15067, svn:keywords property was removed

  • Property svn:eol-style set to LF
File size: 26.6 KB
Line 
1/*
2 * jQuery UI Resizable
3 *
4 * Copyright (c) 2008 Paul Bakaus
5 * Dual licensed under the MIT (MIT-LICENSE.txt)
6 * and GPL (GPL-LICENSE.txt) licenses.
7 *
8 * http://docs.jquery.com/UI/Resizables
9 *
10 * Depends:
11 *      ui.core.js
12 */
13(function($) {
14
15$.widget("ui.resizable", $.extend({}, $.ui.mouse, {
16        init: function() {
17
18                var self = this, o = this.options;
19
20                var elpos = this.element.css('position');
21               
22                this.originalElement = this.element;
23               
24                // simulate .ui-resizable { position: relative; }
25                this.element.addClass("ui-resizable").css({ position: /static/.test(elpos) ? 'relative' : elpos });
26               
27                $.extend(o, {
28                        _aspectRatio: !!(o.aspectRatio),
29                        helper: o.helper || o.ghost || o.animate ? o.helper || 'proxy' : null,
30                        knobHandles: o.knobHandles === true ? 'ui-resizable-knob-handle' : o.knobHandles
31                });
32               
33                //Default Theme
34                var aBorder = '1px solid #DEDEDE';
35               
36                o.defaultTheme = {
37                        'ui-resizable': { display: 'block' },
38                        'ui-resizable-handle': { position: 'absolute', background: '#F2F2F2', fontSize: '0.1px' },
39                        'ui-resizable-n': { cursor: 'n-resize', height: '4px', left: '0px', right: '0px', borderTop: aBorder },
40                        'ui-resizable-s': { cursor: 's-resize', height: '4px', left: '0px', right: '0px', borderBottom: aBorder },
41                        'ui-resizable-e': { cursor: 'e-resize', width: '4px', top: '0px', bottom: '0px', borderRight: aBorder },
42                        'ui-resizable-w': { cursor: 'w-resize', width: '4px', top: '0px', bottom: '0px', borderLeft: aBorder },
43                        'ui-resizable-se': { cursor: 'se-resize', width: '4px', height: '4px', borderRight: aBorder, borderBottom: aBorder },
44                        'ui-resizable-sw': { cursor: 'sw-resize', width: '4px', height: '4px', borderBottom: aBorder, borderLeft: aBorder },
45                        'ui-resizable-ne': { cursor: 'ne-resize', width: '4px', height: '4px', borderRight: aBorder, borderTop: aBorder },
46                        'ui-resizable-nw': { cursor: 'nw-resize', width: '4px', height: '4px', borderLeft: aBorder, borderTop: aBorder }
47                };
48               
49                o.knobTheme = {
50                        'ui-resizable-handle': { background: '#F2F2F2', border: '1px solid #808080', height: '8px', width: '8px' },
51                        'ui-resizable-n': { cursor: 'n-resize', top: '0px', left: '45%' },
52                        'ui-resizable-s': { cursor: 's-resize', bottom: '0px', left: '45%' },
53                        'ui-resizable-e': { cursor: 'e-resize', right: '0px', top: '45%' },
54                        'ui-resizable-w': { cursor: 'w-resize', left: '0px', top: '45%' },
55                        'ui-resizable-se': { cursor: 'se-resize', right: '0px', bottom: '0px' },
56                        'ui-resizable-sw': { cursor: 'sw-resize', left: '0px', bottom: '0px' },
57                        'ui-resizable-nw': { cursor: 'nw-resize', left: '0px', top: '0px' },
58                        'ui-resizable-ne': { cursor: 'ne-resize', right: '0px', top: '0px' }
59                };
60               
61                o._nodeName = this.element[0].nodeName;
62               
63                //Wrap the element if it cannot hold child nodes
64                if(o._nodeName.match(/canvas|textarea|input|select|button|img/i)) {
65                        var el = this.element;
66                       
67                        //Opera fixing relative position
68                        if (/relative/.test(el.css('position')) && $.browser.opera)
69                                el.css({ position: 'relative', top: 'auto', left: 'auto' });
70                       
71                        //Create a wrapper element and set the wrapper to the new current internal element
72                        el.wrap(
73                                $('<div class="ui-wrapper"      style="overflow: hidden;"></div>').css( {
74                                        position: el.css('position'),
75                                        width: el.outerWidth(),
76                                        height: el.outerHeight(),
77                                        top: el.css('top'),
78                                        left: el.css('left')
79                                })
80                        );
81                       
82                        var oel = this.element; this.element = this.element.parent();
83                       
84                        // store instance on wrapper
85                        this.element.data('resizable', this); 
86                       
87                        //Move margins to the wrapper
88                        this.element.css({ marginLeft: oel.css("marginLeft"), marginTop: oel.css("marginTop"),
89                                marginRight: oel.css("marginRight"), marginBottom: oel.css("marginBottom")
90                        });
91                       
92                        oel.css({ marginLeft: 0, marginTop: 0, marginRight: 0, marginBottom: 0});
93                       
94                        //Prevent Safari textarea resize
95                        if ($.browser.safari && o.preventDefault) oel.css('resize', 'none');
96                       
97                        o.proportionallyResize = oel.css({ position: 'static', zoom: 1, display: 'block' });
98                       
99                        // avoid IE jump
100                        this.element.css({ margin: oel.css('margin') });
101                       
102                        // fix handlers offset
103                        this._proportionallyResize();
104                }
105               
106                if(!o.handles) o.handles = !$('.ui-resizable-handle', this.element).length ? "e,s,se" : { n: '.ui-resizable-n', e: '.ui-resizable-e', s: '.ui-resizable-s', w: '.ui-resizable-w', se: '.ui-resizable-se', sw: '.ui-resizable-sw', ne: '.ui-resizable-ne', nw: '.ui-resizable-nw' };
107                if(o.handles.constructor == String) {
108                       
109                        o.zIndex = o.zIndex || 1000;
110                       
111                        if(o.handles == 'all') o.handles = 'n,e,s,w,se,sw,ne,nw';
112                       
113                        var n = o.handles.split(","); o.handles = {};
114                       
115                        // insertions are applied when don't have theme loaded
116                        var insertionsDefault = {
117                                handle: 'position: absolute; display: none; overflow:hidden;',
118                                n: 'top: 0pt; width:100%;',
119                                e: 'right: 0pt; height:100%;',
120                                s: 'bottom: 0pt; width:100%;',
121                                w: 'left: 0pt; height:100%;',
122                                se: 'bottom: 0pt; right: 0px;',
123                                sw: 'bottom: 0pt; left: 0px;',
124                                ne: 'top: 0pt; right: 0px;',
125                                nw: 'top: 0pt; left: 0px;'
126                        };
127                       
128                        for(var i = 0; i < n.length; i++) {
129                                var handle = $.trim(n[i]), dt = o.defaultTheme, hname = 'ui-resizable-'+handle, loadDefault = !$.ui.css(hname) && !o.knobHandles, userKnobClass = $.ui.css('ui-resizable-knob-handle'), 
130                                                        allDefTheme = $.extend(dt[hname], dt['ui-resizable-handle']), allKnobTheme = $.extend(o.knobTheme[hname], !userKnobClass ? o.knobTheme['ui-resizable-handle'] : {});
131                               
132                                // increase zIndex of sw, se, ne, nw axis
133                                var applyZIndex = /sw|se|ne|nw/.test(handle) ? { zIndex: ++o.zIndex } : {};
134                               
135                                var defCss = (loadDefault ? insertionsDefault[handle] : ''), 
136                                        axis = $(['<div class="ui-resizable-handle ', hname, '" style="', defCss, insertionsDefault.handle, '"></div>'].join('')).css( applyZIndex );
137                                o.handles[handle] = '.ui-resizable-'+handle;
138                               
139                                this.element.append(
140                                        //Theme detection, if not loaded, load o.defaultTheme
141                                        axis.css( loadDefault ? allDefTheme : {} )
142                                                // Load the knobHandle css, fix width, height, top, left...
143                                                .css( o.knobHandles ? allKnobTheme : {} ).addClass(o.knobHandles ? 'ui-resizable-knob-handle' : '').addClass(o.knobHandles)
144                                );
145                        }
146                       
147                        if (o.knobHandles) this.element.addClass('ui-resizable-knob').css( !$.ui.css('ui-resizable-knob') ? { /*border: '1px #fff dashed'*/ } : {} );
148                }
149               
150                this._renderAxis = function(target) {
151                        target = target || this.element;
152                       
153                        for(var i in o.handles) {
154                                if(o.handles[i].constructor == String) 
155                                        o.handles[i] = $(o.handles[i], this.element).show();
156                               
157                                if (o.transparent)
158                                        o.handles[i].css({opacity:0});
159                               
160                                //Apply pad to wrapper element, needed to fix axis position (textarea, inputs, scrolls)
161                                if (this.element.is('.ui-wrapper') && 
162                                        o._nodeName.match(/textarea|input|select|button/i)) {
163                                       
164                                        var axis = $(o.handles[i], this.element), padWrapper = 0;
165                                       
166                                        //Checking the correct pad and border
167                                        padWrapper = /sw|ne|nw|se|n|s/.test(i) ? axis.outerHeight() : axis.outerWidth();
168                                       
169                                        //The padding type i have to apply...
170                                        var padPos = [ 'padding', 
171                                                /ne|nw|n/.test(i) ? 'Top' :
172                                                /se|sw|s/.test(i) ? 'Bottom' : 
173                                                /^e$/.test(i) ? 'Right' : 'Left' ].join(""); 
174                                       
175                                        if (!o.transparent)
176                                                target.css(padPos, padWrapper);
177                                       
178                                        this._proportionallyResize();
179                                }
180                                if(!$(o.handles[i]).length) continue;
181                        }
182                };
183               
184                this._renderAxis(this.element);
185                o._handles = $('.ui-resizable-handle', self.element);
186               
187                if (o.disableSelection)
188                        o._handles.each(function(i, e) { $.ui.disableSelection(e); });
189               
190                //Matching axis name
191                o._handles.mouseover(function() {
192                        if (!o.resizing) {
193                                if (this.className) 
194                                        var axis = this.className.match(/ui-resizable-(se|sw|ne|nw|n|e|s|w)/i);
195                                //Axis, default = se
196                                self.axis = o.axis = axis && axis[1] ? axis[1] : 'se';
197                        }
198                });
199               
200                //If we want to auto hide the elements
201                if (o.autoHide) {
202                        o._handles.hide();
203                        $(self.element).addClass("ui-resizable-autohide").hover(function() {
204                                $(this).removeClass("ui-resizable-autohide");
205                                o._handles.show();
206                        },
207                        function(){
208                                if (!o.resizing) {
209                                        $(this).addClass("ui-resizable-autohide");
210                                        o._handles.hide();
211                                }
212                        });
213                }
214               
215                this.mouseInit();
216        },
217        plugins: {},
218        ui: function() {
219                return {
220                        originalElement: this.originalElement,
221                        element: this.element,
222                        helper: this.helper,
223                        position: this.position,
224                        size: this.size,
225                        options: this.options,
226                        originalSize: this.originalSize,
227                        originalPosition: this.originalPosition
228                };
229        },
230        propagate: function(n,e) {
231                $.ui.plugin.call(this, n, [e, this.ui()]);
232                if (n != "resize") this.element.triggerHandler(["resize", n].join(""), [e, this.ui()], this.options[n]);
233        },
234        destroy: function() {
235                var el = this.element, wrapped = el.children(".ui-resizable").get(0);
236               
237                this.mouseDestroy();
238               
239                var _destroy = function(exp) {
240                        $(exp).removeClass("ui-resizable ui-resizable-disabled")
241                                .removeData("resizable").unbind(".resizable").find('.ui-resizable-handle').remove();
242                };
243               
244                _destroy(el);
245               
246                if (el.is('.ui-wrapper') && wrapped) {
247                        el.parent().append(
248                                $(wrapped).css({
249                                        position: el.css('position'),
250                                        width: el.outerWidth(),
251                                        height: el.outerHeight(),
252                                        top: el.css('top'),
253                                        left: el.css('left')
254                                })
255                        ).end().remove();
256                       
257                        _destroy(wrapped);
258                }
259        },
260        mouseStart: function(e) {
261                if(this.options.disabled) return false;
262               
263                var handle = false;
264                for(var i in this.options.handles) {
265                        if($(this.options.handles[i])[0] == e.target) handle = true;
266                }
267                if (!handle) return false;
268               
269                var o = this.options, iniPos = this.element.position(), el = this.element, 
270                        num = function(v) { return parseInt(v, 10) || 0; }, ie6 = $.browser.msie && $.browser.version < 7;
271                o.resizing = true;
272                o.documentScroll = { top: $(document).scrollTop(), left: $(document).scrollLeft() };
273               
274                // bugfix #1749
275                if (el.is('.ui-draggable') || (/absolute/).test(el.css('position'))) {
276                       
277                        // sOffset decides if document scrollOffset will be added to the top/left of the resizable element
278                        var sOffset = $.browser.msie && !o.containment && (/absolute/).test(el.css('position')) && !(/relative/).test(el.parent().css('position'));
279                        var dscrollt = sOffset ? o.documentScroll.top : 0, dscrolll = sOffset ? o.documentScroll.left : 0;
280                       
281                        el.css({ position: 'absolute', top: (iniPos.top + dscrollt), left: (iniPos.left + dscrolll) });
282                }
283               
284                //Opera fixing relative position
285                if ($.browser.opera && /relative/.test(el.css('position')))
286                        el.css({ position: 'relative', top: 'auto', left: 'auto' });
287               
288                this._renderProxy();
289               
290                var curleft = num(this.helper.css('left')), curtop = num(this.helper.css('top'));
291               
292                if (o.containment) {
293                        curleft += $(o.containment).scrollLeft()||0;
294                        curtop += $(o.containment).scrollTop()||0;
295                }
296               
297                //Store needed variables
298                this.offset = this.helper.offset();
299                this.position = { left: curleft, top: curtop };
300                this.size = o.helper || ie6 ? { width: el.outerWidth(), height: el.outerHeight() } : { width: el.width(), height: el.height() };
301                this.originalSize = o.helper || ie6 ? { width: el.outerWidth(), height: el.outerHeight() } : { width: el.width(), height: el.height() };
302                this.originalPosition = { left: curleft, top: curtop };
303                this.sizeDiff = { width: el.outerWidth() - el.width(), height: el.outerHeight() - el.height() };
304                this.originalMousePosition = { left: e.pageX, top: e.pageY };
305               
306                //Aspect Ratio
307                o.aspectRatio = (typeof o.aspectRatio == 'number') ? o.aspectRatio : ((this.originalSize.height / this.originalSize.width)||1);
308               
309                if (o.preserveCursor)
310                        $('body').css('cursor', this.axis + '-resize');
311                       
312                this.propagate("start", e);
313                return true;
314        },
315        mouseDrag: function(e) {
316               
317                //Increase performance, avoid regex
318                var el = this.helper, o = this.options, props = {},
319                        self = this, smp = this.originalMousePosition, a = this.axis;
320               
321                var dx = (e.pageX-smp.left)||0, dy = (e.pageY-smp.top)||0;
322                var trigger = this._change[a];
323                if (!trigger) return false;
324               
325                // Calculate the attrs that will be change
326                var data = trigger.apply(this, [e, dx, dy]), ie6 = $.browser.msie && $.browser.version < 7, csdif = this.sizeDiff;
327               
328                if (o._aspectRatio || e.shiftKey)
329                        data = this._updateRatio(data, e);
330               
331                data = this._respectSize(data, e);
332               
333                // plugins callbacks need to be called first
334                this.propagate("resize", e);
335               
336                el.css({
337                        top: this.position.top + "px", left: this.position.left + "px", 
338                        width: this.size.width + "px", height: this.size.height + "px"
339                });
340               
341                if (!o.helper && o.proportionallyResize)
342                        this._proportionallyResize();
343               
344                this._updateCache(data);
345               
346                // calling the user callback at the end
347                this.element.triggerHandler("resize", [e, this.ui()], this.options["resize"]);
348               
349                return false;
350        },
351        mouseStop: function(e) {
352               
353                this.options.resizing = false;
354                var o = this.options, num = function(v) { return parseInt(v, 10) || 0; }, self = this;
355               
356                if(o.helper) {
357                        var pr = o.proportionallyResize, ista = pr && (/textarea/i).test(pr.get(0).nodeName), 
358                                                soffseth = ista && $.ui.hasScroll(pr.get(0), 'left') /* TODO - jump height */ ? 0 : self.sizeDiff.height,
359                                                        soffsetw = ista ? 0 : self.sizeDiff.width;
360                       
361                        var s = { width: (self.size.width - soffsetw), height: (self.size.height - soffseth) },
362                                left = (parseInt(self.element.css('left'), 10) + (self.position.left - self.originalPosition.left)) || null, 
363                                top = (parseInt(self.element.css('top'), 10) + (self.position.top - self.originalPosition.top)) || null;
364                       
365                        if (!o.animate)
366                                this.element.css($.extend(s, { top: top, left: left }));
367                       
368                        if (o.helper && !o.animate) this._proportionallyResize();
369                }
370               
371                if (o.preserveCursor)
372                        $('body').css('cursor', 'auto');
373               
374                this.propagate("stop", e);
375               
376                if (o.helper) this.helper.remove();
377               
378                return false;
379        },
380        _updateCache: function(data) {
381                var o = this.options;
382                this.offset = this.helper.offset();
383                if (data.left) this.position.left = data.left;
384                if (data.top) this.position.top = data.top;
385                if (data.height) this.size.height = data.height;
386                if (data.width) this.size.width = data.width;
387        },
388        _updateRatio: function(data, e) {
389                var o = this.options, cpos = this.position, csize = this.size, a = this.axis;
390               
391                if (data.height) data.width = (csize.height / o.aspectRatio);
392                else if (data.width) data.height = (csize.width * o.aspectRatio);
393               
394                if (a == 'sw') {
395                        data.left = cpos.left + (csize.width - data.width);
396                        data.top = null;
397                }
398                if (a == 'nw') { 
399                        data.top = cpos.top + (csize.height - data.height);
400                        data.left = cpos.left + (csize.width - data.width);
401                }
402               
403                return data;
404        },
405        _respectSize: function(data, e) {
406               
407                var el = this.helper, o = this.options, pRatio = o._aspectRatio || e.shiftKey, a = this.axis, 
408                                ismaxw = data.width && o.maxWidth && o.maxWidth < data.width, ismaxh = data.height && o.maxHeight && o.maxHeight < data.height,
409                                        isminw = data.width && o.minWidth && o.minWidth > data.width, isminh = data.height && o.minHeight && o.minHeight > data.height;
410               
411                if (isminw) data.width = o.minWidth;
412                if (isminh) data.height = o.minHeight;
413                if (ismaxw) data.width = o.maxWidth;
414                if (ismaxh) data.height = o.maxHeight;
415               
416                var dw = this.originalPosition.left + this.originalSize.width, dh = this.position.top + this.size.height;
417                var cw = /sw|nw|w/.test(a), ch = /nw|ne|n/.test(a);
418               
419                if (isminw && cw) data.left = dw - o.minWidth;
420                if (ismaxw && cw) data.left = dw - o.maxWidth;
421                if (isminh && ch)       data.top = dh - o.minHeight;
422                if (ismaxh && ch)       data.top = dh - o.maxHeight;
423               
424                // fixing jump error on top/left - bug #2330
425                var isNotwh = !data.width && !data.height;
426                if (isNotwh && !data.left && data.top) data.top = null;
427                else if (isNotwh && !data.top && data.left) data.left = null;
428               
429                return data;
430        },
431        _proportionallyResize: function() {
432                var o = this.options;
433                if (!o.proportionallyResize) return;
434                var prel = o.proportionallyResize, el = this.helper || this.element;
435               
436                if (!o.borderDif) {
437                        var b = [prel.css('borderTopWidth'), prel.css('borderRightWidth'), prel.css('borderBottomWidth'), prel.css('borderLeftWidth')],
438                                p = [prel.css('paddingTop'), prel.css('paddingRight'), prel.css('paddingBottom'), prel.css('paddingLeft')];
439                       
440                        o.borderDif = $.map(b, function(v, i) {
441                                var border = parseInt(v,10)||0, padding = parseInt(p[i],10)||0;
442                                return border + padding; 
443                        });
444                }
445                prel.css({
446                        height: (el.height() - o.borderDif[0] - o.borderDif[2]) + "px",
447                        width: (el.width() - o.borderDif[1] - o.borderDif[3]) + "px"
448                });
449        },
450        _renderProxy: function() {
451                var el = this.element, o = this.options;
452                this.elementOffset = el.offset();
453               
454                if(o.helper) {
455                        this.helper = this.helper || $('<div style="overflow:hidden;"></div>');
456                       
457                        // fix ie6 offset
458                        var ie6 = $.browser.msie && $.browser.version < 7, ie6offset = (ie6 ? 1 : 0),
459                        pxyoffset = ( ie6 ? 2 : -1 );
460                       
461                        this.helper.addClass(o.helper).css({
462                                width: el.outerWidth() + pxyoffset,
463                                height: el.outerHeight() + pxyoffset,
464                                position: 'absolute',
465                                left: this.elementOffset.left - ie6offset +'px',
466                                top: this.elementOffset.top - ie6offset +'px',
467                                zIndex: ++o.zIndex
468                        });
469                       
470                        this.helper.appendTo("body");
471                       
472                        if (o.disableSelection)
473                                $.ui.disableSelection(this.helper.get(0));
474                       
475                } else {
476                        this.helper = el; 
477                }
478        },
479        _change: {
480                e: function(e, dx, dy) {
481                        return { width: this.originalSize.width + dx };
482                },
483                w: function(e, dx, dy) {
484                        var o = this.options, cs = this.originalSize, sp = this.originalPosition;
485                        return { left: sp.left + dx, width: cs.width - dx };
486                },
487                n: function(e, dx, dy) {
488                        var o = this.options, cs = this.originalSize, sp = this.originalPosition;
489                        return { top: sp.top + dy, height: cs.height - dy };
490                },
491                s: function(e, dx, dy) {
492                        return { height: this.originalSize.height + dy };
493                },
494                se: function(e, dx, dy) {
495                        return $.extend(this._change.s.apply(this, arguments), this._change.e.apply(this, [e, dx, dy]));
496                },
497                sw: function(e, dx, dy) {
498                        return $.extend(this._change.s.apply(this, arguments), this._change.w.apply(this, [e, dx, dy]));
499                },
500                ne: function(e, dx, dy) {
501                        return $.extend(this._change.n.apply(this, arguments), this._change.e.apply(this, [e, dx, dy]));
502                },
503                nw: function(e, dx, dy) {
504                        return $.extend(this._change.n.apply(this, arguments), this._change.w.apply(this, [e, dx, dy]));
505                }
506        }
507}));
508
509$.extend($.ui.resizable, {
510        defaults: {
511                cancel: ":input",
512                distance: 1,
513                delay: 0,
514                preventDefault: true,
515                transparent: false,
516                minWidth: 10,
517                minHeight: 10,
518                aspectRatio: false,
519                disableSelection: true,
520                preserveCursor: true,
521                autoHide: false,
522                knobHandles: false
523        }
524});
525
526/*
527 * Resizable Extensions
528 */
529
530$.ui.plugin.add("resizable", "containment", {
531       
532        start: function(e, ui) {
533                var o = ui.options, self = $(this).data("resizable"), el = self.element;
534                var oc = o.containment, ce = (oc instanceof $) ? oc.get(0) : (/parent/.test(oc)) ? el.parent().get(0) : oc;
535                if (!ce) return;
536               
537                self.containerElement = $(ce);
538               
539                if (/document/.test(oc) || oc == document) {
540                        self.containerOffset = { left: 0, top: 0 };
541                        self.containerPosition = { left: 0, top: 0 };
542                       
543                        self.parentData = { 
544                                element: $(document), left: 0, top: 0, 
545                                width: $(document).width(), height: $(document).height() || document.body.parentNode.scrollHeight
546                        };
547                }
548               
549                               
550                // i'm a node, so compute top, left, right, bottom
551                else{
552                        self.containerOffset = $(ce).offset();
553                        self.containerPosition = $(ce).position();
554                        self.containerSize = { height: $(ce).innerHeight(), width: $(ce).innerWidth() };
555               
556                        var co = self.containerOffset, ch = self.containerSize.height,  cw = self.containerSize.width, 
557                                                width = ($.ui.hasScroll(ce, "left") ? ce.scrollWidth : cw ), height = ($.ui.hasScroll(ce) ? ce.scrollHeight : ch);
558               
559                        self.parentData = { 
560                                element: ce, left: co.left, top: co.top, width: width, height: height
561                        };
562                }
563        },
564       
565        resize: function(e, ui) {
566                var o = ui.options, self = $(this).data("resizable"), 
567                                ps = self.containerSize, co = self.containerOffset, cs = self.size, cp = self.position,
568                                pRatio = o._aspectRatio || e.shiftKey, cop = { top:0, left:0 }, ce = self.containerElement;
569               
570                if (ce[0] != document && /static/.test(ce.css('position')))
571                        cop = self.containerPosition;
572               
573                if (cp.left < (o.helper ? co.left : cop.left)) {
574                        self.size.width = self.size.width + (o.helper ? (self.position.left - co.left) : (self.position.left - cop.left));
575                        if (pRatio) self.size.height = self.size.width * o.aspectRatio;
576                        self.position.left = o.helper ? co.left : cop.left;
577                }
578               
579                if (cp.top < (o.helper ? co.top : 0)) {
580                        self.size.height = self.size.height + (o.helper ? (self.position.top - co.top) : self.position.top);
581                        if (pRatio) self.size.width = self.size.height / o.aspectRatio;
582                        self.position.top = o.helper ? co.top : 0;
583                }
584               
585                var woset = (o.helper ? self.offset.left - co.left : (self.position.left - cop.left)) + self.sizeDiff.width, 
586                                        hoset = (o.helper ? self.offset.top - co.top : self.position.top) + self.sizeDiff.height;
587               
588                if (woset + self.size.width >= self.parentData.width) {
589                        self.size.width = self.parentData.width - woset;
590                        if (pRatio) self.size.height = self.size.width * o.aspectRatio;
591                }
592               
593                if (hoset + self.size.height >= self.parentData.height) {
594                        self.size.height = self.parentData.height - hoset;
595                        if (pRatio) self.size.width = self.size.height / o.aspectRatio;
596                }
597        },
598       
599        stop: function(e, ui){
600                var o = ui.options, self = $(this).data("resizable"), cp = self.position,
601                                co = self.containerOffset, cop = self.containerPosition, ce = self.containerElement;
602               
603                var helper = $(self.helper), ho = helper.offset(), w = helper.innerWidth(), h = helper.innerHeight();
604               
605               
606                if (o.helper && !o.animate && /relative/.test(ce.css('position')))
607                        $(this).css({ left: (ho.left - co.left), top: (ho.top - co.top), width: w, height: h });
608               
609                if (o.helper && !o.animate && /static/.test(ce.css('position')))
610                        $(this).css({ left: cop.left + (ho.left - co.left), top: cop.top + (ho.top - co.top), width: w, height: h });
611               
612        }
613});
614
615$.ui.plugin.add("resizable", "grid", {
616       
617        resize: function(e, ui) {
618                var o = ui.options, self = $(this).data("resizable"), cs = self.size, os = self.originalSize, op = self.originalPosition, a = self.axis, ratio = o._aspectRatio || e.shiftKey;
619                o.grid = typeof o.grid == "number" ? [o.grid, o.grid] : o.grid;
620                var ox = Math.round((cs.width - os.width) / (o.grid[0]||1)) * (o.grid[0]||1), oy = Math.round((cs.height - os.height) / (o.grid[1]||1)) * (o.grid[1]||1);
621               
622                if (/^(se|s|e)$/.test(a)) {
623                        self.size.width = os.width + ox;
624                        self.size.height = os.height + oy;
625                }
626                else if (/^(ne)$/.test(a)) {
627                        self.size.width = os.width + ox;
628                        self.size.height = os.height + oy;
629                        self.position.top = op.top - oy;
630                }
631                else if (/^(sw)$/.test(a)) {
632                        self.size.width = os.width + ox;
633                        self.size.height = os.height + oy;
634                        self.position.left = op.left - ox;
635                }
636                else {
637                        self.size.width = os.width + ox;
638                        self.size.height = os.height + oy;
639                        self.position.top = op.top - oy;
640                        self.position.left = op.left - ox;
641                }
642        }
643       
644});
645
646$.ui.plugin.add("resizable", "animate", {
647       
648        stop: function(e, ui) {
649                var o = ui.options, self = $(this).data("resizable");
650               
651                var pr = o.proportionallyResize, ista = pr && (/textarea/i).test(pr.get(0).nodeName), 
652                                                soffseth = ista && $.ui.hasScroll(pr.get(0), 'left') /* TODO - jump height */ ? 0 : self.sizeDiff.height,
653                                                        soffsetw = ista ? 0 : self.sizeDiff.width;
654               
655                var style = { width: (self.size.width - soffsetw), height: (self.size.height - soffseth) },
656                                        left = (parseInt(self.element.css('left'), 10) + (self.position.left - self.originalPosition.left)) || null, 
657                                                top = (parseInt(self.element.css('top'), 10) + (self.position.top - self.originalPosition.top)) || null; 
658               
659                self.element.animate(
660                        $.extend(style, top && left ? { top: top, left: left } : {}), { 
661                                duration: o.animateDuration || "slow", easing: o.animateEasing || "swing", 
662                                step: function() {
663                                       
664                                        var data = {
665                                                width: parseInt(self.element.css('width'), 10),
666                                                height: parseInt(self.element.css('height'), 10),
667                                                top: parseInt(self.element.css('top'), 10),
668                                                left: parseInt(self.element.css('left'), 10)
669                                        };
670                                       
671                                        if (pr) pr.css({ width: data.width, height: data.height });
672                                       
673                                        // propagating resize, and updating values for each animation step
674                                        self._updateCache(data);
675                                        self.propagate("animate", e);
676                                       
677                                }
678                        }
679                );
680        }
681       
682});
683
684$.ui.plugin.add("resizable", "ghost", {
685       
686        start: function(e, ui) {
687                var o = ui.options, self = $(this).data("resizable"), pr = o.proportionallyResize, cs = self.size;
688               
689                if (!pr) self.ghost = self.element.clone();
690                else self.ghost = pr.clone();
691               
692                self.ghost.css(
693                        { opacity: .25, display: 'block', position: 'relative', height: cs.height, width: cs.width, margin: 0, left: 0, top: 0 }
694                )
695                .addClass('ui-resizable-ghost').addClass(typeof o.ghost == 'string' ? o.ghost : '');
696               
697                self.ghost.appendTo(self.helper);
698               
699        },
700       
701        resize: function(e, ui){
702                var o = ui.options, self = $(this).data("resizable"), pr = o.proportionallyResize;
703               
704                if (self.ghost) self.ghost.css({ position: 'relative', height: self.size.height, width: self.size.width });
705               
706        },
707       
708        stop: function(e, ui){
709                var o = ui.options, self = $(this).data("resizable"), pr = o.proportionallyResize;
710                if (self.ghost && self.helper) self.helper.get(0).removeChild(self.ghost.get(0));
711        }
712       
713});
714
715$.ui.plugin.add("resizable", "alsoResize", {
716       
717        start: function(e, ui) {
718                var o = ui.options, self = $(this).data("resizable"), 
719               
720                _store = function(exp) {
721                        $(exp).each(function() {
722                                $(this).data("resizable-alsoresize", {
723                                        width: parseInt($(this).width(), 10), height: parseInt($(this).height(), 10),
724                                        left: parseInt($(this).css('left'), 10), top: parseInt($(this).css('top'), 10)
725                                });
726                        });
727                };
728               
729                if (typeof(o.alsoResize) == 'object') {
730                        if (o.alsoResize.length) { o.alsoResize = o.alsoResize[0];      _store(o.alsoResize); }
731                        else { $.each(o.alsoResize, function(exp, c) { _store(exp); }); }
732                }else{
733                        _store(o.alsoResize);
734                } 
735        },
736       
737        resize: function(e, ui){
738                var o = ui.options, self = $(this).data("resizable"), os = self.originalSize, op = self.originalPosition;
739               
740                var delta = { 
741                        height: (self.size.height - os.height) || 0, width: (self.size.width - os.width) || 0,
742                        top: (self.position.top - op.top) || 0, left: (self.position.left - op.left) || 0
743                },
744               
745                _alsoResize = function(exp, c) {
746                        $(exp).each(function() {
747                                var start = $(this).data("resizable-alsoresize"), style = {}, css = c && c.length ? c : ['width', 'height', 'top', 'left'];
748                               
749                                $.each(css || ['width', 'height', 'top', 'left'], function(i, prop) {
750                                        var sum = (start[prop]||0) + (delta[prop]||0);
751                                        if (sum && sum >= 0)
752                                                style[prop] = sum || null;
753                                });
754                                $(this).css(style);
755                        });
756                };
757               
758                if (typeof(o.alsoResize) == 'object') {
759                        $.each(o.alsoResize, function(exp, c) { _alsoResize(exp, c); });
760                }else{
761                        _alsoResize(o.alsoResize);
762                }
763        },
764       
765        stop: function(e, ui){
766                $(this).removeData("resizable-alsoresize-start");
767        }
768});
769
770})(jQuery);
Note: See TracBrowser for help on using the repository browser.