jquery.ui.resizable.js 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*!
  2. * jQuery UI Resizable v1.9 stable
  3. * http://jqueryui.com
  4. *
  5. * Copyright 2012 jQuery Foundation and other contributors
  6. * Released under the MIT license.
  7. * http://jquery.org/license
  8. *
  9. * http://api.jqueryui.com/resizable/
  10. *
  11. * Depends:
  12. * jquery.ui.core.js
  13. * jquery.ui.mouse.js
  14. * jquery.ui.widget.js
  15. */
  16. (function( $, undefined ) {
  17. $.widget("ui.resizable", $.ui.mouse, {
  18. version: "@VERSION",
  19. widgetEventPrefix: "resize",
  20. options: {
  21. alsoResize: false,
  22. animate: false,
  23. animateDuration: "slow",
  24. animateEasing: "swing",
  25. aspectRatio: false,
  26. autoHide: false,
  27. containment: false,
  28. ghost: false,
  29. grid: false,
  30. handles: "e,s,se",
  31. helper: false,
  32. maxHeight: null,
  33. maxWidth: null,
  34. minHeight: 10,
  35. minWidth: 10,
  36. zIndex: 1000
  37. },
  38. _create: function() {
  39. var that = this, o = this.options;
  40. this.element.addClass("ui-resizable");
  41. $.extend(this, {
  42. _aspectRatio: !!(o.aspectRatio),
  43. aspectRatio: o.aspectRatio,
  44. originalElement: this.element,
  45. _proportionallyResizeElements: [],
  46. _helper: o.helper || o.ghost || o.animate ? o.helper || 'ui-resizable-helper' : null
  47. });
  48. //Wrap the element if it cannot hold child nodes
  49. if(this.element[0].nodeName.match(/canvas|textarea|input|select|button|img/i)) {
  50. //Create a wrapper element and set the wrapper to the new current internal element
  51. this.element.wrap(
  52. $('<div class="ui-wrapper" style="overflow: hidden;"></div>').css({
  53. position: this.element.css('position'),
  54. width: this.element.outerWidth(),
  55. height: this.element.outerHeight(),
  56. top: this.element.css('top'),
  57. left: this.element.css('left')
  58. })
  59. );
  60. //Overwrite the original this.element
  61. this.element = this.element.parent().data(
  62. "resizable", this.element.data('resizable')
  63. );
  64. this.elementIsWrapper = true;
  65. //Move margins to the wrapper
  66. this.element.css({ marginLeft: this.originalElement.css("marginLeft"), marginTop: this.originalElement.css("marginTop"), marginRight: this.originalElement.css("marginRight"), marginBottom: this.originalElement.css("marginBottom") });
  67. this.originalElement.css({ marginLeft: 0, marginTop: 0, marginRight: 0, marginBottom: 0});
  68. //Prevent Safari textarea resize
  69. this.originalResizeStyle = this.originalElement.css('resize');
  70. this.originalElement.css('resize', 'none');
  71. //Push the actual element to our proportionallyResize internal array
  72. this._proportionallyResizeElements.push(this.originalElement.css({ position: 'static', zoom: 1, display: 'block' }));
  73. // avoid IE jump (hard set the margin)
  74. this.originalElement.css({ margin: this.originalElement.css('margin') });
  75. // fix handlers offset
  76. this._proportionallyResize();
  77. }
  78. this.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' });
  79. if(this.handles.constructor == String) {
  80. if(this.handles == 'all') this.handles = 'n,e,s,w,se,sw,ne,nw';
  81. var n = this.handles.split(","); this.handles = {};
  82. for(var i = 0; i < n.length; i++) {
  83. var handle = $.trim(n[i]), hname = 'ui-resizable-'+handle;
  84. var axis = $('<div class="ui-resizable-handle ' + hname + '"></div>');
  85. // Apply zIndex to all handles - see #7960
  86. axis.css({ zIndex: o.zIndex });
  87. //TODO : What's going on here?
  88. if ('se' == handle) {
  89. axis.addClass('ui-icon ui-icon-gripsmall-diagonal-se');
  90. };
  91. //Insert into internal handles object and append to element
  92. this.handles[handle] = '.ui-resizable-'+handle;
  93. this.element.append(axis);
  94. }
  95. }
  96. this._renderAxis = function(target) {
  97. target = target || this.element;
  98. for(var i in this.handles) {
  99. if(this.handles[i].constructor == String)
  100. this.handles[i] = $(this.handles[i], this.element).show();
  101. //Apply pad to wrapper element, needed to fix axis position (textarea, inputs, scrolls)
  102. if (this.elementIsWrapper && this.originalElement[0].nodeName.match(/textarea|input|select|button/i)) {
  103. var axis = $(this.handles[i], this.element), padWrapper = 0;
  104. //Checking the correct pad and border
  105. padWrapper = /sw|ne|nw|se|n|s/.test(i) ? axis.outerHeight() : axis.outerWidth();
  106. //The padding type i have to apply...
  107. var padPos = [ 'padding',
  108. /ne|nw|n/.test(i) ? 'Top' :
  109. /se|sw|s/.test(i) ? 'Bottom' :
  110. /^e$/.test(i) ? 'Right' : 'Left' ].join("");
  111. target.css(padPos, padWrapper);
  112. this._proportionallyResize();
  113. }
  114. //TODO: What's that good for? There's not anything to be executed left
  115. if(!$(this.handles[i]).length)
  116. continue;
  117. }
  118. };
  119. //TODO: make renderAxis a prototype function
  120. this._renderAxis(this.element);
  121. this._handles = $('.ui-resizable-handle', this.element)
  122. .disableSelection();
  123. //Matching axis name
  124. this._handles.mouseover(function() {
  125. if (!that.resizing) {
  126. if (this.className)
  127. var axis = this.className.match(/ui-resizable-(se|sw|ne|nw|n|e|s|w)/i);
  128. //Axis, default = se
  129. that.axis = axis && axis[1] ? axis[1] : 'se';
  130. }
  131. });
  132. //If we want to auto hide the elements
  133. if (o.autoHide) {
  134. this._handles.hide();
  135. $(this.element)
  136. .addClass("ui-resizable-autohide")
  137. .mouseenter(function() {
  138. if (o.disabled) return;
  139. $(this).removeClass("ui-resizable-autohide");
  140. that._handles.show();
  141. })
  142. .mouseleave(function(){
  143. if (o.disabled) return;
  144. if (!that.resizing) {
  145. $(this).addClass("ui-resizable-autohide");
  146. that._handles.hide();
  147. }
  148. });
  149. }
  150. //Initialize the mouse interaction
  151. this._mouseInit();
  152. },
  153. _destroy: function() {
  154. this._mouseDestroy();
  155. var _destroy = function(exp) {
  156. $(exp).removeClass("ui-resizable ui-resizable-disabled ui-resizable-resizing")
  157. .removeData("resizable").removeData("ui-resizable").unbind(".resizable").find('.ui-resizable-handle').remove();
  158. };
  159. //TODO: Unwrap at same DOM position
  160. if (this.elementIsWrapper) {
  161. _destroy(this.element);
  162. var wrapper = this.element;
  163. this.originalElement.css({
  164. position: wrapper.css('position'),
  165. width: wrapper.outerWidth(),
  166. height: wrapper.outerHeight(),
  167. top: wrapper.css('top'),
  168. left: wrapper.css('left')
  169. }).insertAfter( wrapper );
  170. wrapper.remove();
  171. }
  172. this.originalElement.css('resize', this.originalResizeStyle);
  173. _destroy(this.originalElement);
  174. return this;
  175. },
  176. _mouseCapture: function(event) {
  177. var handle = false;
  178. for (var i in this.handles) {
  179. if ($(this.handles[i])[0] == event.target) {
  180. handle = true;
  181. }
  182. }
  183. return !this.options.disabled && handle;
  184. },
  185. _mouseStart: function(event) {
  186. var o = this.options, iniPos = this.element.position(), el = this.element;
  187. this.resizing = true;
  188. this.documentScroll = { top: $(document).scrollTop(), left: $(document).scrollLeft() };
  189. // bugfix for http://dev.jquery.com/ticket/1749
  190. if (el.is('.ui-draggable') || (/absolute/).test(el.css('position'))) {
  191. el.css({ position: 'absolute', top: iniPos.top, left: iniPos.left });
  192. }
  193. this._renderProxy();
  194. var curleft = num(this.helper.css('left')), curtop = num(this.helper.css('top'));
  195. if (o.containment) {
  196. curleft += $(o.containment).scrollLeft() || 0;
  197. curtop += $(o.containment).scrollTop() || 0;
  198. }
  199. //Store needed variables
  200. this.offset = this.helper.offset();
  201. this.position = { left: curleft, top: curtop };
  202. this.size = this._helper ? { width: el.outerWidth(), height: el.outerHeight() } : { width: el.width(), height: el.height() };
  203. this.originalSize = this._helper ? { width: el.outerWidth(), height: el.outerHeight() } : { width: el.width(), height: el.height() };
  204. this.originalPosition = { left: curleft, top: curtop };
  205. this.sizeDiff = { width: el.outerWidth() - el.width(), height: el.outerHeight() - el.height() };
  206. this.originalMousePosition = { left: event.pageX, top: event.pageY };
  207. //Aspect Ratio
  208. this.aspectRatio = (typeof o.aspectRatio == 'number') ? o.aspectRatio : ((this.originalSize.width / this.originalSize.height) || 1);
  209. var cursor = $('.ui-resizable-' + this.axis).css('cursor');
  210. $('body').css('cursor', cursor == 'auto' ? this.axis + '-resize' : cursor);
  211. el.addClass("ui-resizable-resizing");
  212. this._propagate("start", event);
  213. return true;
  214. },
  215. _mouseDrag: function(event) {
  216. //Increase performance, avoid regex
  217. var el = this.helper, o = this.options, props = {},
  218. that = this, smp = this.originalMousePosition, a = this.axis;
  219. var dx = (event.pageX-smp.left)||0, dy = (event.pageY-smp.top)||0;
  220. var trigger = this._change[a];
  221. if (!trigger) return false;
  222. // Calculate the attrs that will be change
  223. var data = trigger.apply(this, [event, dx, dy]);
  224. // Put this in the mouseDrag handler since the user can start pressing shift while resizing
  225. this._updateVirtualBoundaries(event.shiftKey);
  226. if (this._aspectRatio || event.shiftKey)
  227. data = this._updateRatio(data, event);
  228. data = this._respectSize(data, event);
  229. // plugins callbacks need to be called first
  230. this._propagate("resize", event);
  231. el.css({
  232. top: this.position.top + "px", left: this.position.left + "px",
  233. width: this.size.width + "px", height: this.size.height + "px"
  234. });
  235. if (!this._helper && this._proportionallyResizeElements.length)
  236. this._proportionallyResize();
  237. this._updateCache(data);
  238. // calling the user callback at the end
  239. this._trigger('resize', event, this.ui());
  240. return false;
  241. },
  242. _mouseStop: function(event) {
  243. this.resizing = false;
  244. var o = this.options, that = this;
  245. if(this._helper) {
  246. var pr = this._proportionallyResizeElements, ista = pr.length && (/textarea/i).test(pr[0].nodeName),
  247. soffseth = ista && $.ui.hasScroll(pr[0], 'left') /* TODO - jump height */ ? 0 : that.sizeDiff.height,
  248. soffsetw = ista ? 0 : that.sizeDiff.width;
  249. var s = { width: (that.helper.width() - soffsetw), height: (that.helper.height() - soffseth) },
  250. left = (parseInt(that.element.css('left'), 10) + (that.position.left - that.originalPosition.left)) || null,
  251. top = (parseInt(that.element.css('top'), 10) + (that.position.top - that.originalPosition.top)) || null;
  252. if (!o.animate)
  253. this.element.css($.extend(s, { top: top, left: left }));
  254. that.helper.height(that.size.height);
  255. that.helper.width(that.size.width);
  256. if (this._helper && !o.animate) this._proportionallyResize();
  257. }
  258. $('body').css('cursor', 'auto');
  259. this.element.removeClass("ui-resizable-resizing");
  260. this._propagate("stop", event);
  261. if (this._helper) this.helper.remove();
  262. return false;
  263. },
  264. _updateVirtualBoundaries: function(forceAspectRatio) {
  265. var o = this.options, pMinWidth, pMaxWidth, pMinHeight, pMaxHeight, b;
  266. b = {
  267. minWidth: isNumber(o.minWidth) ? o.minWidth : 0,
  268. maxWidth: isNumber(o.maxWidth) ? o.maxWidth : Infinity,
  269. minHeight: isNumber(o.minHeight) ? o.minHeight : 0,
  270. maxHeight: isNumber(o.maxHeight) ? o.maxHeight : Infinity
  271. };
  272. if(this._aspectRatio || forceAspectRatio) {
  273. // We want to create an enclosing box whose aspect ration is the requested one
  274. // First, compute the "projected" size for each dimension based on the aspect ratio and other dimension
  275. pMinWidth = b.minHeight * this.aspectRatio;
  276. pMinHeight = b.minWidth / this.aspectRatio;
  277. pMaxWidth = b.maxHeight * this.aspectRatio;
  278. pMaxHeight = b.maxWidth / this.aspectRatio;
  279. if(pMinWidth > b.minWidth) b.minWidth = pMinWidth;
  280. if(pMinHeight > b.minHeight) b.minHeight = pMinHeight;
  281. if(pMaxWidth < b.maxWidth) b.maxWidth = pMaxWidth;
  282. if(pMaxHeight < b.maxHeight) b.maxHeight = pMaxHeight;
  283. }
  284. this._vBoundaries = b;
  285. },
  286. _updateCache: function(data) {
  287. var o = this.options;
  288. this.offset = this.helper.offset();
  289. if (isNumber(data.left)) this.position.left = data.left;
  290. if (isNumber(data.top)) this.position.top = data.top;
  291. if (isNumber(data.height)) this.size.height = data.height;
  292. if (isNumber(data.width)) this.size.width = data.width;
  293. },
  294. _updateRatio: function(data, event) {
  295. var o = this.options, cpos = this.position, csize = this.size, a = this.axis;
  296. if (isNumber(data.height)) data.width = (data.height * this.aspectRatio);
  297. else if (isNumber(data.width)) data.height = (data.width / this.aspectRatio);
  298. if (a == 'sw') {
  299. data.left = cpos.left + (csize.width - data.width);
  300. data.top = null;
  301. }
  302. if (a == 'nw') {
  303. data.top = cpos.top + (csize.height - data.height);
  304. data.left = cpos.left + (csize.width - data.width);
  305. }
  306. return data;
  307. },
  308. _respectSize: function(data, event) {
  309. var el = this.helper, o = this._vBoundaries, pRatio = this._aspectRatio || event.shiftKey, a = this.axis,
  310. ismaxw = isNumber(data.width) && o.maxWidth && (o.maxWidth < data.width), ismaxh = isNumber(data.height) && o.maxHeight && (o.maxHeight < data.height),
  311. isminw = isNumber(data.width) && o.minWidth && (o.minWidth > data.width), isminh = isNumber(data.height) && o.minHeight && (o.minHeight > data.height);
  312. if (isminw) data.width = o.minWidth;
  313. if (isminh) data.height = o.minHeight;
  314. if (ismaxw) data.width = o.maxWidth;
  315. if (ismaxh) data.height = o.maxHeight;
  316. var dw = this.originalPosition.left + this.originalSize.width, dh = this.position.top + this.size.height;
  317. var cw = /sw|nw|w/.test(a), ch = /nw|ne|n/.test(a);
  318. if (isminw && cw) data.left = dw - o.minWidth;
  319. if (ismaxw && cw) data.left = dw - o.maxWidth;
  320. if (isminh && ch) data.top = dh - o.minHeight;
  321. if (ismaxh && ch) data.top = dh - o.maxHeight;
  322. // fixing jump error on top/left - bug #2330
  323. var isNotwh = !data.width && !data.height;
  324. if (isNotwh && !data.left && data.top) data.top = null;
  325. else if (isNotwh && !data.top && data.left) data.left = null;
  326. return data;
  327. },
  328. _proportionallyResize: function() {
  329. var o = this.options;
  330. if (!this._proportionallyResizeElements.length) return;
  331. var element = this.helper || this.element;
  332. for (var i=0; i < this._proportionallyResizeElements.length; i++) {
  333. var prel = this._proportionallyResizeElements[i];
  334. if (!this.borderDif) {
  335. var b = [prel.css('borderTopWidth'), prel.css('borderRightWidth'), prel.css('borderBottomWidth'), prel.css('borderLeftWidth')],
  336. p = [prel.css('paddingTop'), prel.css('paddingRight'), prel.css('paddingBottom'), prel.css('paddingLeft')];
  337. this.borderDif = $.map(b, function(v, i) {
  338. var border = parseInt(v,10)||0, padding = parseInt(p[i],10)||0;
  339. return border + padding;
  340. });
  341. }
  342. prel.css({
  343. height: (element.height() - this.borderDif[0] - this.borderDif[2]) || 0,
  344. width: (element.width() - this.borderDif[1] - this.borderDif[3]) || 0
  345. });
  346. };
  347. },
  348. _renderProxy: function() {
  349. var el = this.element, o = this.options;
  350. this.elementOffset = el.offset();
  351. if(this._helper) {
  352. this.helper = this.helper || $('<div style="overflow:hidden;"></div>');
  353. // fix ie6 offset TODO: This seems broken
  354. var ie6offset = ($.ui.ie6 ? 1 : 0),
  355. pxyoffset = ( $.ui.ie6 ? 2 : -1 );
  356. this.helper.addClass(this._helper).css({
  357. width: this.element.outerWidth() + pxyoffset,
  358. height: this.element.outerHeight() + pxyoffset,
  359. position: 'absolute',
  360. left: this.elementOffset.left - ie6offset +'px',
  361. top: this.elementOffset.top - ie6offset +'px',
  362. zIndex: ++o.zIndex //TODO: Don't modify option
  363. });
  364. this.helper
  365. .appendTo("body")
  366. .disableSelection();
  367. } else {
  368. this.helper = this.element;
  369. }
  370. },
  371. _change: {
  372. e: function(event, dx, dy) {
  373. return { width: this.originalSize.width + dx };
  374. },
  375. w: function(event, dx, dy) {
  376. var o = this.options, cs = this.originalSize, sp = this.originalPosition;
  377. return { left: sp.left + dx, width: cs.width - dx };
  378. },
  379. n: function(event, dx, dy) {
  380. var o = this.options, cs = this.originalSize, sp = this.originalPosition;
  381. return { top: sp.top + dy, height: cs.height - dy };
  382. },
  383. s: function(event, dx, dy) {
  384. return { height: this.originalSize.height + dy };
  385. },
  386. se: function(event, dx, dy) {
  387. return $.extend(this._change.s.apply(this, arguments), this._change.e.apply(this, [event, dx, dy]));
  388. },
  389. sw: function(event, dx, dy) {
  390. return $.extend(this._change.s.apply(this, arguments), this._change.w.apply(this, [event, dx, dy]));
  391. },
  392. ne: function(event, dx, dy) {
  393. return $.extend(this._change.n.apply(this, arguments), this._change.e.apply(this, [event, dx, dy]));
  394. },
  395. nw: function(event, dx, dy) {
  396. return $.extend(this._change.n.apply(this, arguments), this._change.w.apply(this, [event, dx, dy]));
  397. }
  398. },
  399. _propagate: function(n, event) {
  400. $.ui.plugin.call(this, n, [event, this.ui()]);
  401. (n != "resize" && this._trigger(n, event, this.ui()));
  402. },
  403. plugins: {},
  404. ui: function() {
  405. return {
  406. originalElement: this.originalElement,
  407. element: this.element,
  408. helper: this.helper,
  409. position: this.position,
  410. size: this.size,
  411. originalSize: this.originalSize,
  412. originalPosition: this.originalPosition
  413. };
  414. }
  415. });
  416. /*
  417. * Resizable Extensions
  418. */
  419. $.ui.plugin.add("resizable", "alsoResize", {
  420. start: function (event, ui) {
  421. var that = $(this).data("resizable"), o = that.options;
  422. var _store = function (exp) {
  423. $(exp).each(function() {
  424. var el = $(this);
  425. el.data("resizable-alsoresize", {
  426. width: parseInt(el.width(), 10), height: parseInt(el.height(), 10),
  427. left: parseInt(el.css('left'), 10), top: parseInt(el.css('top'), 10)
  428. });
  429. });
  430. };
  431. if (typeof(o.alsoResize) == 'object' && !o.alsoResize.parentNode) {
  432. if (o.alsoResize.length) { o.alsoResize = o.alsoResize[0]; _store(o.alsoResize); }
  433. else { $.each(o.alsoResize, function (exp) { _store(exp); }); }
  434. }else{
  435. _store(o.alsoResize);
  436. }
  437. },
  438. resize: function (event, ui) {
  439. var that = $(this).data("resizable"), o = that.options, os = that.originalSize, op = that.originalPosition;
  440. var delta = {
  441. height: (that.size.height - os.height) || 0, width: (that.size.width - os.width) || 0,
  442. top: (that.position.top - op.top) || 0, left: (that.position.left - op.left) || 0
  443. },
  444. _alsoResize = function (exp, c) {
  445. $(exp).each(function() {
  446. var el = $(this), start = $(this).data("resizable-alsoresize"), style = {},
  447. css = c && c.length ? c : el.parents(ui.originalElement[0]).length ? ['width', 'height'] : ['width', 'height', 'top', 'left'];
  448. $.each(css, function (i, prop) {
  449. var sum = (start[prop]||0) + (delta[prop]||0);
  450. if (sum && sum >= 0)
  451. style[prop] = sum || null;
  452. });
  453. el.css(style);
  454. });
  455. };
  456. if (typeof(o.alsoResize) == 'object' && !o.alsoResize.nodeType) {
  457. $.each(o.alsoResize, function (exp, c) { _alsoResize(exp, c); });
  458. }else{
  459. _alsoResize(o.alsoResize);
  460. }
  461. },
  462. stop: function (event, ui) {
  463. $(this).removeData("resizable-alsoresize");
  464. }
  465. });
  466. $.ui.plugin.add("resizable", "animate", {
  467. stop: function(event, ui) {
  468. var that = $(this).data("resizable"), o = that.options;
  469. var pr = that._proportionallyResizeElements, ista = pr.length && (/textarea/i).test(pr[0].nodeName),
  470. soffseth = ista && $.ui.hasScroll(pr[0], 'left') /* TODO - jump height */ ? 0 : that.sizeDiff.height,
  471. soffsetw = ista ? 0 : that.sizeDiff.width;
  472. var style = { width: (that.size.width - soffsetw), height: (that.size.height - soffseth) },
  473. left = (parseInt(that.element.css('left'), 10) + (that.position.left - that.originalPosition.left)) || null,
  474. top = (parseInt(that.element.css('top'), 10) + (that.position.top - that.originalPosition.top)) || null;
  475. that.element.animate(
  476. $.extend(style, top && left ? { top: top, left: left } : {}), {
  477. duration: o.animateDuration,
  478. easing: o.animateEasing,
  479. step: function() {
  480. var data = {
  481. width: parseInt(that.element.css('width'), 10),
  482. height: parseInt(that.element.css('height'), 10),
  483. top: parseInt(that.element.css('top'), 10),
  484. left: parseInt(that.element.css('left'), 10)
  485. };
  486. if (pr && pr.length) $(pr[0]).css({ width: data.width, height: data.height });
  487. // propagating resize, and updating values for each animation step
  488. that._updateCache(data);
  489. that._propagate("resize", event);
  490. }
  491. }
  492. );
  493. }
  494. });
  495. $.ui.plugin.add("resizable", "containment", {
  496. start: function(event, ui) {
  497. var that = $(this).data("resizable"), o = that.options, el = that.element;
  498. var oc = o.containment, ce = (oc instanceof $) ? oc.get(0) : (/parent/.test(oc)) ? el.parent().get(0) : oc;
  499. if (!ce) return;
  500. that.containerElement = $(ce);
  501. if (/document/.test(oc) || oc == document) {
  502. that.containerOffset = { left: 0, top: 0 };
  503. that.containerPosition = { left: 0, top: 0 };
  504. that.parentData = {
  505. element: $(document), left: 0, top: 0,
  506. width: $(document).width(), height: $(document).height() || document.body.parentNode.scrollHeight
  507. };
  508. }
  509. // i'm a node, so compute top, left, right, bottom
  510. else {
  511. var element = $(ce), p = [];
  512. $([ "Top", "Right", "Left", "Bottom" ]).each(function(i, name) { p[i] = num(element.css("padding" + name)); });
  513. that.containerOffset = element.offset();
  514. that.containerPosition = element.position();
  515. that.containerSize = { height: (element.innerHeight() - p[3]), width: (element.innerWidth() - p[1]) };
  516. var co = that.containerOffset, ch = that.containerSize.height, cw = that.containerSize.width,
  517. width = ($.ui.hasScroll(ce, "left") ? ce.scrollWidth : cw ), height = ($.ui.hasScroll(ce) ? ce.scrollHeight : ch);
  518. that.parentData = {
  519. element: ce, left: co.left, top: co.top, width: width, height: height
  520. };
  521. }
  522. },
  523. resize: function(event, ui) {
  524. var that = $(this).data("resizable"), o = that.options,
  525. ps = that.containerSize, co = that.containerOffset, cs = that.size, cp = that.position,
  526. pRatio = that._aspectRatio || event.shiftKey, cop = { top:0, left:0 }, ce = that.containerElement;
  527. if (ce[0] != document && (/static/).test(ce.css('position'))) cop = co;
  528. if (cp.left < (that._helper ? co.left : 0)) {
  529. that.size.width = that.size.width + (that._helper ? (that.position.left - co.left) : (that.position.left - cop.left));
  530. if (pRatio) that.size.height = that.size.width / that.aspectRatio;
  531. that.position.left = o.helper ? co.left : 0;
  532. }
  533. if (cp.top < (that._helper ? co.top : 0)) {
  534. that.size.height = that.size.height + (that._helper ? (that.position.top - co.top) : that.position.top);
  535. if (pRatio) that.size.width = that.size.height * that.aspectRatio;
  536. that.position.top = that._helper ? co.top : 0;
  537. }
  538. that.offset.left = that.parentData.left+that.position.left;
  539. that.offset.top = that.parentData.top+that.position.top;
  540. var woset = Math.abs( (that._helper ? that.offset.left - cop.left : (that.offset.left - cop.left)) + that.sizeDiff.width ),
  541. hoset = Math.abs( (that._helper ? that.offset.top - cop.top : (that.offset.top - co.top)) + that.sizeDiff.height );
  542. var isParent = that.containerElement.get(0) == that.element.parent().get(0),
  543. isOffsetRelative = /relative|absolute/.test(that.containerElement.css('position'));
  544. if(isParent && isOffsetRelative) woset -= that.parentData.left;
  545. if (woset + that.size.width >= that.parentData.width) {
  546. that.size.width = that.parentData.width - woset;
  547. if (pRatio) that.size.height = that.size.width / that.aspectRatio;
  548. }
  549. if (hoset + that.size.height >= that.parentData.height) {
  550. that.size.height = that.parentData.height - hoset;
  551. if (pRatio) that.size.width = that.size.height * that.aspectRatio;
  552. }
  553. },
  554. stop: function(event, ui){
  555. var that = $(this).data("resizable"), o = that.options, cp = that.position,
  556. co = that.containerOffset, cop = that.containerPosition, ce = that.containerElement;
  557. var helper = $(that.helper), ho = helper.offset(), w = helper.outerWidth() - that.sizeDiff.width, h = helper.outerHeight() - that.sizeDiff.height;
  558. if (that._helper && !o.animate && (/relative/).test(ce.css('position')))
  559. $(this).css({ left: ho.left - cop.left - co.left, width: w, height: h });
  560. if (that._helper && !o.animate && (/static/).test(ce.css('position')))
  561. $(this).css({ left: ho.left - cop.left - co.left, width: w, height: h });
  562. }
  563. });
  564. $.ui.plugin.add("resizable", "ghost", {
  565. start: function(event, ui) {
  566. var that = $(this).data("resizable"), o = that.options, cs = that.size;
  567. that.ghost = that.originalElement.clone();
  568. that.ghost
  569. .css({ opacity: .25, display: 'block', position: 'relative', height: cs.height, width: cs.width, margin: 0, left: 0, top: 0 })
  570. .addClass('ui-resizable-ghost')
  571. .addClass(typeof o.ghost == 'string' ? o.ghost : '');
  572. that.ghost.appendTo(that.helper);
  573. },
  574. resize: function(event, ui){
  575. var that = $(this).data("resizable"), o = that.options;
  576. if (that.ghost) that.ghost.css({ position: 'relative', height: that.size.height, width: that.size.width });
  577. },
  578. stop: function(event, ui){
  579. var that = $(this).data("resizable"), o = that.options;
  580. if (that.ghost && that.helper) that.helper.get(0).removeChild(that.ghost.get(0));
  581. }
  582. });
  583. $.ui.plugin.add("resizable", "grid", {
  584. resize: function(event, ui) {
  585. var that = $(this).data("resizable"), o = that.options, cs = that.size, os = that.originalSize, op = that.originalPosition, a = that.axis, ratio = o._aspectRatio || event.shiftKey;
  586. o.grid = typeof o.grid == "number" ? [o.grid, o.grid] : o.grid;
  587. 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);
  588. if (/^(se|s|e)$/.test(a)) {
  589. that.size.width = os.width + ox;
  590. that.size.height = os.height + oy;
  591. }
  592. else if (/^(ne)$/.test(a)) {
  593. that.size.width = os.width + ox;
  594. that.size.height = os.height + oy;
  595. that.position.top = op.top - oy;
  596. }
  597. else if (/^(sw)$/.test(a)) {
  598. that.size.width = os.width + ox;
  599. that.size.height = os.height + oy;
  600. that.position.left = op.left - ox;
  601. }
  602. else {
  603. that.size.width = os.width + ox;
  604. that.size.height = os.height + oy;
  605. that.position.top = op.top - oy;
  606. that.position.left = op.left - ox;
  607. }
  608. }
  609. });
  610. var num = function(v) {
  611. return parseInt(v, 10) || 0;
  612. };
  613. var isNumber = function(value) {
  614. return !isNaN(parseInt(value, 10));
  615. };
  616. })(jQuery);