ui.resizable.js 26 KB

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