PageRenderTime 1485ms CodeModel.GetById 42ms RepoModel.GetById 6ms app.codeStats 0ms

/CS198/WebContent/js/jqueryui/jquery.ui.resizable.js

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