resizable.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*!
  2. * jQuery UI Resizable @VERSION
  3. * http://jqueryui.com
  4. *
  5. * Copyright 2014 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. (function( factory ) {
  12. if ( typeof define === "function" && define.amd ) {
  13. // AMD. Register as an anonymous module.
  14. define([
  15. "jquery",
  16. "./core",
  17. "./mouse",
  18. "./widget"
  19. ], factory );
  20. } else {
  21. // Browser globals
  22. factory( jQuery );
  23. }
  24. }(function( $ ) {
  25. $.widget("ui.resizable", $.ui.mouse, {
  26. version: "@VERSION",
  27. widgetEventPrefix: "resize",
  28. options: {
  29. alsoResize: false,
  30. animate: false,
  31. animateDuration: "slow",
  32. animateEasing: "swing",
  33. aspectRatio: false,
  34. autoHide: false,
  35. containment: false,
  36. ghost: false,
  37. grid: false,
  38. handles: "e,s,se",
  39. helper: false,
  40. maxHeight: null,
  41. maxWidth: null,
  42. minHeight: 10,
  43. minWidth: 10,
  44. // See #7960
  45. zIndex: 90,
  46. // callbacks
  47. resize: null,
  48. start: null,
  49. stop: null
  50. },
  51. _num: function( value ) {
  52. return parseInt( value, 10 ) || 0;
  53. },
  54. _isNumber: function( value ) {
  55. return !isNaN( parseInt( value , 10 ) );
  56. },
  57. _hasScroll: function( el, a ) {
  58. if ( $( el ).css( "overflow" ) === "hidden") {
  59. return false;
  60. }
  61. var scroll = ( a && a === "left" ) ? "scrollLeft" : "scrollTop",
  62. has = false;
  63. if ( el[ scroll ] > 0 ) {
  64. return true;
  65. }
  66. // TODO: determine which cases actually cause this to happen
  67. // if the element doesn't have the scroll set, see if it's possible to
  68. // set the scroll
  69. el[ scroll ] = 1;
  70. has = ( el[ scroll ] > 0 );
  71. el[ scroll ] = 0;
  72. return has;
  73. },
  74. _create: function() {
  75. var n, i, handle, axis, hname,
  76. that = this,
  77. o = this.options;
  78. this.element.addClass("ui-resizable");
  79. $.extend(this, {
  80. _aspectRatio: !!(o.aspectRatio),
  81. aspectRatio: o.aspectRatio,
  82. originalElement: this.element,
  83. _proportionallyResizeElements: [],
  84. _helper: o.helper || o.ghost || o.animate ? o.helper || "ui-resizable-helper" : null
  85. });
  86. // Wrap the element if it cannot hold child nodes
  87. if(this.element[0].nodeName.match(/canvas|textarea|input|select|button|img/i)) {
  88. this.element.wrap(
  89. $("<div class='ui-wrapper' style='overflow: hidden;'></div>").css({
  90. position: this.element.css("position"),
  91. width: this.element.outerWidth(),
  92. height: this.element.outerHeight(),
  93. top: this.element.css("top"),
  94. left: this.element.css("left")
  95. })
  96. );
  97. this.element = this.element.parent().data(
  98. "ui-resizable", this.element.resizable( "instance" )
  99. );
  100. this.elementIsWrapper = true;
  101. this.element.css({ marginLeft: this.originalElement.css("marginLeft"), marginTop: this.originalElement.css("marginTop"), marginRight: this.originalElement.css("marginRight"), marginBottom: this.originalElement.css("marginBottom") });
  102. this.originalElement.css({ marginLeft: 0, marginTop: 0, marginRight: 0, marginBottom: 0});
  103. // support: Safari
  104. // Prevent Safari textarea resize
  105. this.originalResizeStyle = this.originalElement.css("resize");
  106. this.originalElement.css("resize", "none");
  107. this._proportionallyResizeElements.push(this.originalElement.css({ position: "static", zoom: 1, display: "block" }));
  108. // support: IE9
  109. // avoid IE jump (hard set the margin)
  110. this.originalElement.css({ margin: this.originalElement.css("margin") });
  111. this._proportionallyResize();
  112. }
  113. 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" });
  114. if(this.handles.constructor === String) {
  115. if ( this.handles === "all") {
  116. this.handles = "n,e,s,w,se,sw,ne,nw";
  117. }
  118. n = this.handles.split(",");
  119. this.handles = {};
  120. for(i = 0; i < n.length; i++) {
  121. handle = $.trim(n[i]);
  122. hname = "ui-resizable-"+handle;
  123. axis = $("<div class='ui-resizable-handle " + hname + "'></div>");
  124. axis.css({ zIndex: o.zIndex });
  125. // TODO : What's going on here?
  126. if ("se" === handle) {
  127. axis.addClass("ui-icon ui-icon-gripsmall-diagonal-se");
  128. }
  129. this.handles[handle] = ".ui-resizable-"+handle;
  130. this.element.append(axis);
  131. }
  132. }
  133. this._renderAxis = function(target) {
  134. var i, axis, padPos, padWrapper;
  135. target = target || this.element;
  136. for(i in this.handles) {
  137. if(this.handles[i].constructor === String) {
  138. this.handles[i] = this.element.children( this.handles[ i ] ).first().show();
  139. }
  140. if (this.elementIsWrapper && this.originalElement[0].nodeName.match(/textarea|input|select|button/i)) {
  141. axis = $(this.handles[i], this.element);
  142. padWrapper = /sw|ne|nw|se|n|s/.test(i) ? axis.outerHeight() : axis.outerWidth();
  143. padPos = [ "padding",
  144. /ne|nw|n/.test(i) ? "Top" :
  145. /se|sw|s/.test(i) ? "Bottom" :
  146. /^e$/.test(i) ? "Right" : "Left" ].join("");
  147. target.css(padPos, padWrapper);
  148. this._proportionallyResize();
  149. }
  150. // TODO: What's that good for? There's not anything to be executed left
  151. if(!$(this.handles[i]).length) {
  152. continue;
  153. }
  154. }
  155. };
  156. // TODO: make renderAxis a prototype function
  157. this._renderAxis(this.element);
  158. this._handles = $(".ui-resizable-handle", this.element)
  159. .disableSelection();
  160. this._handles.mouseover(function() {
  161. if (!that.resizing) {
  162. if (this.className) {
  163. axis = this.className.match(/ui-resizable-(se|sw|ne|nw|n|e|s|w)/i);
  164. }
  165. that.axis = axis && axis[1] ? axis[1] : "se";
  166. }
  167. });
  168. if (o.autoHide) {
  169. this._handles.hide();
  170. $(this.element)
  171. .addClass("ui-resizable-autohide")
  172. .mouseenter(function() {
  173. if (o.disabled) {
  174. return;
  175. }
  176. $(this).removeClass("ui-resizable-autohide");
  177. that._handles.show();
  178. })
  179. .mouseleave(function(){
  180. if (o.disabled) {
  181. return;
  182. }
  183. if (!that.resizing) {
  184. $(this).addClass("ui-resizable-autohide");
  185. that._handles.hide();
  186. }
  187. });
  188. }
  189. this._mouseInit();
  190. },
  191. _destroy: function() {
  192. this._mouseDestroy();
  193. var wrapper,
  194. _destroy = function(exp) {
  195. $(exp).removeClass("ui-resizable ui-resizable-disabled ui-resizable-resizing")
  196. .removeData("resizable").removeData("ui-resizable").unbind(".resizable").find(".ui-resizable-handle").remove();
  197. };
  198. // TODO: Unwrap at same DOM position
  199. if (this.elementIsWrapper) {
  200. _destroy(this.element);
  201. wrapper = this.element;
  202. this.originalElement.css({
  203. position: wrapper.css("position"),
  204. width: wrapper.outerWidth(),
  205. height: wrapper.outerHeight(),
  206. top: wrapper.css("top"),
  207. left: wrapper.css("left")
  208. }).insertAfter( wrapper );
  209. wrapper.remove();
  210. }
  211. this.originalElement.css("resize", this.originalResizeStyle);
  212. _destroy(this.originalElement);
  213. return this;
  214. },
  215. _mouseCapture: function(event) {
  216. var i, handle,
  217. capture = false;
  218. for (i in this.handles) {
  219. handle = $(this.handles[i])[0];
  220. if (handle === event.target || $.contains(handle, event.target)) {
  221. capture = true;
  222. }
  223. }
  224. return !this.options.disabled && capture;
  225. },
  226. _mouseStart: function(event) {
  227. var curleft, curtop, cursor,
  228. o = this.options,
  229. el = this.element;
  230. this.resizing = true;
  231. this._renderProxy();
  232. curleft = this._num(this.helper.css("left"));
  233. curtop = this._num(this.helper.css("top"));
  234. if (o.containment) {
  235. curleft += $(o.containment).scrollLeft() || 0;
  236. curtop += $(o.containment).scrollTop() || 0;
  237. }
  238. this.offset = this.helper.offset();
  239. this.position = { left: curleft, top: curtop };
  240. this.size = this._helper ? { width: this.helper.width(), height: this.helper.height() } : { width: el.width(), height: el.height() };
  241. this.originalSize = this._helper ? { width: el.outerWidth(), height: el.outerHeight() } : { width: el.width(), height: el.height() };
  242. this.originalPosition = { left: curleft, top: curtop };
  243. this.sizeDiff = { width: el.outerWidth() - el.width(), height: el.outerHeight() - el.height() };
  244. this.originalMousePosition = { left: event.pageX, top: event.pageY };
  245. this.aspectRatio = (typeof o.aspectRatio === "number") ? o.aspectRatio : ((this.originalSize.width / this.originalSize.height) || 1);
  246. cursor = $(".ui-resizable-" + this.axis).css("cursor");
  247. $("body").css("cursor", cursor === "auto" ? this.axis + "-resize" : cursor);
  248. el.addClass("ui-resizable-resizing");
  249. this._propagate("start", event);
  250. return true;
  251. },
  252. _mouseDrag: function(event) {
  253. var data, props,
  254. smp = this.originalMousePosition,
  255. a = this.axis,
  256. dx = (event.pageX-smp.left)||0,
  257. dy = (event.pageY-smp.top)||0,
  258. trigger = this._change[a];
  259. this._updatePrevProperties();
  260. if (!trigger) {
  261. return false;
  262. }
  263. data = trigger.apply(this, [event, dx, dy]);
  264. this._updateVirtualBoundaries(event.shiftKey);
  265. if (this._aspectRatio || event.shiftKey) {
  266. data = this._updateRatio(data, event);
  267. }
  268. data = this._respectSize(data, event);
  269. this._updateCache(data);
  270. this._propagate("resize", event);
  271. props = this._applyChanges();
  272. if ( !this._helper && this._proportionallyResizeElements.length ) {
  273. this._proportionallyResize();
  274. }
  275. if ( !$.isEmptyObject( props ) ) {
  276. this._updatePrevProperties();
  277. this._trigger( "resize", event, this.ui() );
  278. this._applyChanges();
  279. }
  280. return false;
  281. },
  282. _mouseStop: function(event) {
  283. this.resizing = false;
  284. var pr, ista, soffseth, soffsetw, s, left, top,
  285. o = this.options, that = this;
  286. if(this._helper) {
  287. pr = this._proportionallyResizeElements;
  288. ista = pr.length && (/textarea/i).test(pr[0].nodeName);
  289. soffseth = ista && this._hasScroll(pr[0], "left") /* TODO - jump height */ ? 0 : that.sizeDiff.height;
  290. soffsetw = ista ? 0 : that.sizeDiff.width;
  291. s = { width: (that.helper.width() - soffsetw), height: (that.helper.height() - soffseth) };
  292. left = (parseInt(that.element.css("left"), 10) + (that.position.left - that.originalPosition.left)) || null;
  293. top = (parseInt(that.element.css("top"), 10) + (that.position.top - that.originalPosition.top)) || null;
  294. if (!o.animate) {
  295. this.element.css($.extend(s, { top: top, left: left }));
  296. }
  297. that.helper.height(that.size.height);
  298. that.helper.width(that.size.width);
  299. if (this._helper && !o.animate) {
  300. this._proportionallyResize();
  301. }
  302. }
  303. $("body").css("cursor", "auto");
  304. this.element.removeClass("ui-resizable-resizing");
  305. this._propagate("stop", event);
  306. if (this._helper) {
  307. this.helper.remove();
  308. }
  309. return false;
  310. },
  311. _updatePrevProperties: function() {
  312. this.prevPosition = {
  313. top: this.position.top,
  314. left: this.position.left
  315. };
  316. this.prevSize = {
  317. width: this.size.width,
  318. height: this.size.height
  319. };
  320. },
  321. _applyChanges: function() {
  322. var props = {};
  323. if ( this.position.top !== this.prevPosition.top ) {
  324. props.top = this.position.top + "px";
  325. }
  326. if ( this.position.left !== this.prevPosition.left ) {
  327. props.left = this.position.left + "px";
  328. }
  329. if ( this.size.width !== this.prevSize.width ) {
  330. props.width = this.size.width + "px";
  331. }
  332. if ( this.size.height !== this.prevSize.height ) {
  333. props.height = this.size.height + "px";
  334. }
  335. this.helper.css( props );
  336. return props;
  337. },
  338. _updateVirtualBoundaries: function(forceAspectRatio) {
  339. var pMinWidth, pMaxWidth, pMinHeight, pMaxHeight, b,
  340. o = this.options;
  341. b = {
  342. minWidth: this._isNumber(o.minWidth) ? o.minWidth : 0,
  343. maxWidth: this._isNumber(o.maxWidth) ? o.maxWidth : Infinity,
  344. minHeight: this._isNumber(o.minHeight) ? o.minHeight : 0,
  345. maxHeight: this._isNumber(o.maxHeight) ? o.maxHeight : Infinity
  346. };
  347. if(this._aspectRatio || forceAspectRatio) {
  348. pMinWidth = b.minHeight * this.aspectRatio;
  349. pMinHeight = b.minWidth / this.aspectRatio;
  350. pMaxWidth = b.maxHeight * this.aspectRatio;
  351. pMaxHeight = b.maxWidth / this.aspectRatio;
  352. if(pMinWidth > b.minWidth) {
  353. b.minWidth = pMinWidth;
  354. }
  355. if(pMinHeight > b.minHeight) {
  356. b.minHeight = pMinHeight;
  357. }
  358. if(pMaxWidth < b.maxWidth) {
  359. b.maxWidth = pMaxWidth;
  360. }
  361. if(pMaxHeight < b.maxHeight) {
  362. b.maxHeight = pMaxHeight;
  363. }
  364. }
  365. this._vBoundaries = b;
  366. },
  367. _updateCache: function(data) {
  368. this.offset = this.helper.offset();
  369. if (this._isNumber(data.left)) {
  370. this.position.left = data.left;
  371. }
  372. if (this._isNumber(data.top)) {
  373. this.position.top = data.top;
  374. }
  375. if (this._isNumber(data.height)) {
  376. this.size.height = data.height;
  377. }
  378. if (this._isNumber(data.width)) {
  379. this.size.width = data.width;
  380. }
  381. },
  382. _updateRatio: function( data ) {
  383. var cpos = this.position,
  384. csize = this.size,
  385. a = this.axis;
  386. if (this._isNumber(data.height)) {
  387. data.width = (data.height * this.aspectRatio);
  388. } else if (this._isNumber(data.width)) {
  389. data.height = (data.width / this.aspectRatio);
  390. }
  391. if (a === "sw") {
  392. data.left = cpos.left + (csize.width - data.width);
  393. data.top = null;
  394. }
  395. if (a === "nw") {
  396. data.top = cpos.top + (csize.height - data.height);
  397. data.left = cpos.left + (csize.width - data.width);
  398. }
  399. return data;
  400. },
  401. _respectSize: function( data ) {
  402. var o = this._vBoundaries,
  403. a = this.axis,
  404. ismaxw = this._isNumber(data.width) && o.maxWidth && (o.maxWidth < data.width), ismaxh = this._isNumber(data.height) && o.maxHeight && (o.maxHeight < data.height),
  405. isminw = this._isNumber(data.width) && o.minWidth && (o.minWidth > data.width), isminh = this._isNumber(data.height) && o.minHeight && (o.minHeight > data.height),
  406. dw = this.originalPosition.left + this.originalSize.width,
  407. dh = this.position.top + this.size.height,
  408. cw = /sw|nw|w/.test(a), ch = /nw|ne|n/.test(a);
  409. if (isminw) {
  410. data.width = o.minWidth;
  411. }
  412. if (isminh) {
  413. data.height = o.minHeight;
  414. }
  415. if (ismaxw) {
  416. data.width = o.maxWidth;
  417. }
  418. if (ismaxh) {
  419. data.height = o.maxHeight;
  420. }
  421. if (isminw && cw) {
  422. data.left = dw - o.minWidth;
  423. }
  424. if (ismaxw && cw) {
  425. data.left = dw - o.maxWidth;
  426. }
  427. if (isminh && ch) {
  428. data.top = dh - o.minHeight;
  429. }
  430. if (ismaxh && ch) {
  431. data.top = dh - o.maxHeight;
  432. }
  433. // Fixing jump error on top/left - bug #2330
  434. if (!data.width && !data.height && !data.left && data.top) {
  435. data.top = null;
  436. } else if (!data.width && !data.height && !data.top && data.left) {
  437. data.left = null;
  438. }
  439. return data;
  440. },
  441. _proportionallyResize: function() {
  442. if (!this._proportionallyResizeElements.length) {
  443. return;
  444. }
  445. var i, j, borders, paddings, prel,
  446. element = this.helper || this.element;
  447. for ( i=0; i < this._proportionallyResizeElements.length; i++) {
  448. prel = this._proportionallyResizeElements[i];
  449. if (!this.borderDif) {
  450. this.borderDif = [];
  451. borders = [prel.css("borderTopWidth"), prel.css("borderRightWidth"), prel.css("borderBottomWidth"), prel.css("borderLeftWidth")];
  452. paddings = [prel.css("paddingTop"), prel.css("paddingRight"), prel.css("paddingBottom"), prel.css("paddingLeft")];
  453. for ( j = 0; j < borders.length; j++ ) {
  454. this.borderDif[ j ] = ( parseInt( borders[ j ], 10 ) || 0 ) + ( parseInt( paddings[ j ], 10 ) || 0 );
  455. }
  456. }
  457. prel.css({
  458. height: (element.height() - this.borderDif[0] - this.borderDif[2]) || 0,
  459. width: (element.width() - this.borderDif[1] - this.borderDif[3]) || 0
  460. });
  461. }
  462. },
  463. _renderProxy: function() {
  464. var el = this.element, o = this.options;
  465. this.elementOffset = el.offset();
  466. if(this._helper) {
  467. this.helper = this.helper || $("<div style='overflow:hidden;'></div>");
  468. this.helper.addClass(this._helper).css({
  469. width: this.element.outerWidth() - 1,
  470. height: this.element.outerHeight() - 1,
  471. position: "absolute",
  472. left: this.elementOffset.left +"px",
  473. top: this.elementOffset.top +"px",
  474. zIndex: ++o.zIndex //TODO: Don't modify option
  475. });
  476. this.helper
  477. .appendTo("body")
  478. .disableSelection();
  479. } else {
  480. this.helper = this.element;
  481. }
  482. },
  483. _change: {
  484. e: function(event, dx) {
  485. return { width: this.originalSize.width + dx };
  486. },
  487. w: function(event, dx) {
  488. var cs = this.originalSize, sp = this.originalPosition;
  489. return { left: sp.left + dx, width: cs.width - dx };
  490. },
  491. n: function(event, dx, dy) {
  492. var cs = this.originalSize, sp = this.originalPosition;
  493. return { top: sp.top + dy, height: cs.height - dy };
  494. },
  495. s: function(event, dx, dy) {
  496. return { height: this.originalSize.height + dy };
  497. },
  498. se: function(event, dx, dy) {
  499. return $.extend(this._change.s.apply(this, arguments), this._change.e.apply(this, [event, dx, dy]));
  500. },
  501. sw: function(event, dx, dy) {
  502. return $.extend(this._change.s.apply(this, arguments), this._change.w.apply(this, [event, dx, dy]));
  503. },
  504. ne: function(event, dx, dy) {
  505. return $.extend(this._change.n.apply(this, arguments), this._change.e.apply(this, [event, dx, dy]));
  506. },
  507. nw: function(event, dx, dy) {
  508. return $.extend(this._change.n.apply(this, arguments), this._change.w.apply(this, [event, dx, dy]));
  509. }
  510. },
  511. _propagate: function(n, event) {
  512. $.ui.plugin.call(this, n, [event, this.ui()]);
  513. (n !== "resize" && this._trigger(n, event, this.ui()));
  514. },
  515. plugins: {},
  516. ui: function() {
  517. return {
  518. originalElement: this.originalElement,
  519. element: this.element,
  520. helper: this.helper,
  521. position: this.position,
  522. size: this.size,
  523. originalSize: this.originalSize,
  524. originalPosition: this.originalPosition
  525. };
  526. }
  527. });
  528. /*
  529. * Resizable Extensions
  530. */
  531. $.ui.plugin.add("resizable", "animate", {
  532. stop: function( event ) {
  533. var that = $(this).resizable( "instance" ),
  534. o = that.options,
  535. pr = that._proportionallyResizeElements,
  536. ista = pr.length && (/textarea/i).test(pr[0].nodeName),
  537. soffseth = ista && that._hasScroll(pr[0], "left") /* TODO - jump height */ ? 0 : that.sizeDiff.height,
  538. soffsetw = ista ? 0 : that.sizeDiff.width,
  539. style = { width: (that.size.width - soffsetw), height: (that.size.height - soffseth) },
  540. left = (parseInt(that.element.css("left"), 10) + (that.position.left - that.originalPosition.left)) || null,
  541. top = (parseInt(that.element.css("top"), 10) + (that.position.top - that.originalPosition.top)) || null;
  542. that.element.animate(
  543. $.extend(style, top && left ? { top: top, left: left } : {}), {
  544. duration: o.animateDuration,
  545. easing: o.animateEasing,
  546. step: function() {
  547. var data = {
  548. width: parseInt(that.element.css("width"), 10),
  549. height: parseInt(that.element.css("height"), 10),
  550. top: parseInt(that.element.css("top"), 10),
  551. left: parseInt(that.element.css("left"), 10)
  552. };
  553. if (pr && pr.length) {
  554. $(pr[0]).css({ width: data.width, height: data.height });
  555. }
  556. // propagating resize, and updating values for each animation step
  557. that._updateCache(data);
  558. that._propagate("resize", event);
  559. }
  560. }
  561. );
  562. }
  563. });
  564. $.ui.plugin.add( "resizable", "containment", {
  565. start: function() {
  566. var element, p, co, ch, cw, width, height,
  567. that = $( this ).resizable( "instance" ),
  568. o = that.options,
  569. el = that.element,
  570. oc = o.containment,
  571. ce = ( oc instanceof $ ) ? oc.get( 0 ) : ( /parent/.test( oc ) ) ? el.parent().get( 0 ) : oc;
  572. if ( !ce ) {
  573. return;
  574. }
  575. that.containerElement = $( ce );
  576. if ( /document/.test( oc ) || oc === document ) {
  577. that.containerOffset = {
  578. left: 0,
  579. top: 0
  580. };
  581. that.containerPosition = {
  582. left: 0,
  583. top: 0
  584. };
  585. that.parentData = {
  586. element: $( document ),
  587. left: 0,
  588. top: 0,
  589. width: $( document ).width(),
  590. height: $( document ).height() || document.body.parentNode.scrollHeight
  591. };
  592. } else {
  593. element = $( ce );
  594. p = [];
  595. $([ "Top", "Right", "Left", "Bottom" ]).each(function( i, name ) {
  596. p[ i ] = that._num( element.css( "padding" + name ) );
  597. });
  598. that.containerOffset = element.offset();
  599. that.containerPosition = element.position();
  600. that.containerSize = {
  601. height: ( element.innerHeight() - p[ 3 ] ),
  602. width: ( element.innerWidth() - p[ 1 ] )
  603. };
  604. co = that.containerOffset;
  605. ch = that.containerSize.height;
  606. cw = that.containerSize.width;
  607. width = ( that._hasScroll ( ce, "left" ) ? ce.scrollWidth : cw );
  608. height = ( that._hasScroll ( ce ) ? ce.scrollHeight : ch ) ;
  609. that.parentData = {
  610. element: ce,
  611. left: co.left,
  612. top: co.top,
  613. width: width,
  614. height: height
  615. };
  616. }
  617. },
  618. resize: function( event ) {
  619. var woset, hoset, isParent, isOffsetRelative,
  620. that = $( this ).resizable( "instance" ),
  621. o = that.options,
  622. co = that.containerOffset,
  623. cp = that.position,
  624. pRatio = that._aspectRatio || event.shiftKey,
  625. cop = {
  626. top: 0,
  627. left: 0
  628. },
  629. ce = that.containerElement,
  630. continueResize = true;
  631. if ( ce[ 0 ] !== document && ( /static/ ).test( ce.css( "position" ) ) ) {
  632. cop = co;
  633. }
  634. if ( cp.left < ( that._helper ? co.left : 0 ) ) {
  635. that.size.width = that.size.width + ( that._helper ? ( that.position.left - co.left ) : ( that.position.left - cop.left ) );
  636. if ( pRatio ) {
  637. that.size.height = that.size.width / that.aspectRatio;
  638. continueResize = false;
  639. }
  640. that.position.left = o.helper ? co.left : 0;
  641. }
  642. if ( cp.top < ( that._helper ? co.top : 0 ) ) {
  643. that.size.height = that.size.height + ( that._helper ? ( that.position.top - co.top ) : that.position.top );
  644. if ( pRatio ) {
  645. that.size.width = that.size.height * that.aspectRatio;
  646. continueResize = false;
  647. }
  648. that.position.top = that._helper ? co.top : 0;
  649. }
  650. that.offset.left = that.parentData.left + that.position.left;
  651. that.offset.top = that.parentData.top + that.position.top;
  652. woset = Math.abs( ( that._helper ? that.offset.left - cop.left : ( that.offset.left - co.left ) ) + that.sizeDiff.width );
  653. hoset = Math.abs( ( that._helper ? that.offset.top - cop.top : ( that.offset.top - co.top ) ) + that.sizeDiff.height );
  654. isParent = that.containerElement.get( 0 ) === that.element.parent().get( 0 );
  655. isOffsetRelative = /relative|absolute/.test( that.containerElement.css( "position" ) );
  656. if ( isParent && isOffsetRelative ) {
  657. woset -= Math.abs( that.parentData.left );
  658. }
  659. if ( woset + that.size.width >= that.parentData.width ) {
  660. that.size.width = that.parentData.width - woset;
  661. if ( pRatio ) {
  662. that.size.height = that.size.width / that.aspectRatio;
  663. continueResize = false;
  664. }
  665. }
  666. if ( hoset + that.size.height >= that.parentData.height ) {
  667. that.size.height = that.parentData.height - hoset;
  668. if ( pRatio ) {
  669. that.size.width = that.size.height * that.aspectRatio;
  670. continueResize = false;
  671. }
  672. }
  673. if ( !continueResize ){
  674. that.position.left = that.prevPosition.left;
  675. that.position.top = that.prevPosition.top;
  676. that.size.width = that.prevSize.width;
  677. that.size.height = that.prevSize.height;
  678. }
  679. },
  680. stop: function(){
  681. var that = $( this ).resizable( "instance" ),
  682. o = that.options,
  683. co = that.containerOffset,
  684. cop = that.containerPosition,
  685. ce = that.containerElement,
  686. helper = $( that.helper ),
  687. ho = helper.offset(),
  688. w = helper.outerWidth() - that.sizeDiff.width,
  689. h = helper.outerHeight() - that.sizeDiff.height;
  690. if ( that._helper && !o.animate && ( /relative/ ).test( ce.css( "position" ) ) ) {
  691. $( this ).css({
  692. left: ho.left - cop.left - co.left,
  693. width: w,
  694. height: h
  695. });
  696. }
  697. if ( that._helper && !o.animate && ( /static/ ).test( ce.css( "position" ) ) ) {
  698. $( this ).css({
  699. left: ho.left - cop.left - co.left,
  700. width: w,
  701. height: h
  702. });
  703. }
  704. }
  705. });
  706. $.ui.plugin.add("resizable", "alsoResize", {
  707. start: function () {
  708. var that = $(this).resizable( "instance" ),
  709. o = that.options,
  710. _store = function (exp) {
  711. $(exp).each(function() {
  712. var el = $(this);
  713. el.data("ui-resizable-alsoresize", {
  714. width: parseInt(el.width(), 10), height: parseInt(el.height(), 10),
  715. left: parseInt(el.css("left"), 10), top: parseInt(el.css("top"), 10)
  716. });
  717. });
  718. };
  719. if (typeof(o.alsoResize) === "object" && !o.alsoResize.parentNode) {
  720. if (o.alsoResize.length) { o.alsoResize = o.alsoResize[0]; _store(o.alsoResize); }
  721. else { $.each(o.alsoResize, function (exp) { _store(exp); }); }
  722. }else{
  723. _store(o.alsoResize);
  724. }
  725. },
  726. resize: function (event, ui) {
  727. var that = $(this).resizable( "instance" ),
  728. o = that.options,
  729. os = that.originalSize,
  730. op = that.originalPosition,
  731. delta = {
  732. height: (that.size.height - os.height) || 0, width: (that.size.width - os.width) || 0,
  733. top: (that.position.top - op.top) || 0, left: (that.position.left - op.left) || 0
  734. },
  735. _alsoResize = function (exp, c) {
  736. $(exp).each(function() {
  737. var el = $(this), start = $(this).data("ui-resizable-alsoresize"), style = {},
  738. css = c && c.length ? c : el.parents(ui.originalElement[0]).length ? ["width", "height"] : ["width", "height", "top", "left"];
  739. $.each(css, function (i, prop) {
  740. var sum = (start[prop]||0) + (delta[prop]||0);
  741. if (sum && sum >= 0) {
  742. style[prop] = sum || null;
  743. }
  744. });
  745. el.css(style);
  746. });
  747. };
  748. if (typeof(o.alsoResize) === "object" && !o.alsoResize.nodeType) {
  749. $.each(o.alsoResize, function (exp, c) { _alsoResize(exp, c); });
  750. }else{
  751. _alsoResize(o.alsoResize);
  752. }
  753. },
  754. stop: function () {
  755. $(this).removeData("resizable-alsoresize");
  756. }
  757. });
  758. $.ui.plugin.add("resizable", "ghost", {
  759. start: function() {
  760. var that = $(this).resizable( "instance" ), o = that.options, cs = that.size;
  761. that.ghost = that.originalElement.clone();
  762. that.ghost
  763. .css({ opacity: 0.25, display: "block", position: "relative", height: cs.height, width: cs.width, margin: 0, left: 0, top: 0 })
  764. .addClass("ui-resizable-ghost")
  765. .addClass(typeof o.ghost === "string" ? o.ghost : "");
  766. that.ghost.appendTo(that.helper);
  767. },
  768. resize: function(){
  769. var that = $(this).resizable( "instance" );
  770. if (that.ghost) {
  771. that.ghost.css({ position: "relative", height: that.size.height, width: that.size.width });
  772. }
  773. },
  774. stop: function() {
  775. var that = $(this).resizable( "instance" );
  776. if (that.ghost && that.helper) {
  777. that.helper.get(0).removeChild(that.ghost.get(0));
  778. }
  779. }
  780. });
  781. $.ui.plugin.add("resizable", "grid", {
  782. resize: function() {
  783. var that = $(this).resizable( "instance" ),
  784. o = that.options,
  785. cs = that.size,
  786. os = that.originalSize,
  787. op = that.originalPosition,
  788. a = that.axis,
  789. grid = typeof o.grid === "number" ? [o.grid, o.grid] : o.grid,
  790. gridX = (grid[0]||1),
  791. gridY = (grid[1]||1),
  792. ox = Math.round((cs.width - os.width) / gridX) * gridX,
  793. oy = Math.round((cs.height - os.height) / gridY) * gridY,
  794. newWidth = os.width + ox,
  795. newHeight = os.height + oy,
  796. isMaxWidth = o.maxWidth && (o.maxWidth < newWidth),
  797. isMaxHeight = o.maxHeight && (o.maxHeight < newHeight),
  798. isMinWidth = o.minWidth && (o.minWidth > newWidth),
  799. isMinHeight = o.minHeight && (o.minHeight > newHeight);
  800. o.grid = grid;
  801. if (isMinWidth) {
  802. newWidth = newWidth + gridX;
  803. }
  804. if (isMinHeight) {
  805. newHeight = newHeight + gridY;
  806. }
  807. if (isMaxWidth) {
  808. newWidth = newWidth - gridX;
  809. }
  810. if (isMaxHeight) {
  811. newHeight = newHeight - gridY;
  812. }
  813. if (/^(se|s|e)$/.test(a)) {
  814. that.size.width = newWidth;
  815. that.size.height = newHeight;
  816. } else if (/^(ne)$/.test(a)) {
  817. that.size.width = newWidth;
  818. that.size.height = newHeight;
  819. that.position.top = op.top - oy;
  820. } else if (/^(sw)$/.test(a)) {
  821. that.size.width = newWidth;
  822. that.size.height = newHeight;
  823. that.position.left = op.left - ox;
  824. } else {
  825. if ( newHeight - gridY > 0 ) {
  826. that.size.height = newHeight;
  827. that.position.top = op.top - oy;
  828. } else {
  829. that.size.height = gridY;
  830. that.position.top = op.top + os.height - gridY;
  831. }
  832. if ( newWidth - gridX > 0 ) {
  833. that.size.width = newWidth;
  834. that.position.left = op.left - ox;
  835. } else {
  836. that.size.width = gridX;
  837. that.position.left = op.left + os.width - gridX;
  838. }
  839. }
  840. }
  841. });
  842. return $.ui.resizable;
  843. }));