ligerResizable.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * jQuery ligerUI 1.1.9
  3. *
  4. * http://ligerui.com
  5. *
  6. * Author daomi 2012 [ gd_star@163.com ]
  7. *
  8. */
  9. (function ($)
  10. {
  11. $.fn.ligerResizable = function (options)
  12. {
  13. return $.ligerui.run.call(this, "ligerResizable", arguments,
  14. {
  15. idAttrName: 'ligeruiresizableid', hasElement: false, propertyToElemnt: 'target'
  16. });
  17. };
  18. $.fn.ligerGetResizableManager = function ()
  19. {
  20. return $.ligerui.run.call(this, "ligerGetResizableManager", arguments,
  21. {
  22. idAttrName: 'ligeruiresizableid', hasElement: false, propertyToElemnt: 'target'
  23. });
  24. };
  25. $.ligerDefaults.Resizable = {
  26. handles: 'n, e, s, w, ne, se, sw, nw',
  27. maxWidth: 2000,
  28. maxHeight: 2000,
  29. minWidth: 20,
  30. minHeight: 20,
  31. scope: 3,
  32. animate: false,
  33. onStartResize: function (e) { },
  34. onResize: function (e) { },
  35. onStopResize: function (e) { },
  36. onEndResize: null
  37. };
  38. $.ligerui.controls.Resizable = function (options)
  39. {
  40. $.ligerui.controls.Resizable.base.constructor.call(this, null, options);
  41. };
  42. $.ligerui.controls.Resizable.ligerExtend($.ligerui.core.UIComponent, {
  43. __getType: function ()
  44. {
  45. return 'Resizable';
  46. },
  47. __idPrev: function ()
  48. {
  49. return 'Resizable';
  50. },
  51. _render: function ()
  52. {
  53. var g = this, p = this.options;
  54. g.target = $(p.target);
  55. g.set(p);
  56. g.target.mousemove(function (e)
  57. {
  58. if (p.disabled) return;
  59. g.dir = g._getDir(e);
  60. if (g.dir)
  61. g.target.css('cursor', g.dir + '-resize');
  62. else if (g.target.css('cursor').indexOf('-resize') > 0)
  63. g.target.css('cursor', 'default');
  64. if (p.target.ligeruidragid)
  65. {
  66. var drag = $.ligerui.get(p.target.ligeruidragid);
  67. if (drag && g.dir)
  68. {
  69. drag.set('disabled', true);
  70. } else if (drag)
  71. {
  72. drag.set('disabled', false);
  73. }
  74. }
  75. }).mousedown(function (e)
  76. {
  77. if (p.disabled) return;
  78. if (g.dir)
  79. {
  80. g._start(e);
  81. }
  82. });
  83. },
  84. _rendered: function ()
  85. {
  86. this.options.target.ligeruiresizableid = this.id;
  87. },
  88. _getDir: function (e)
  89. {
  90. var g = this, p = this.options;
  91. var dir = '';
  92. var xy = g.target.offset();
  93. var width = g.target.width();
  94. var height = g.target.height();
  95. var scope = p.scope;
  96. var pageX = e.pageX || e.screenX;
  97. var pageY = e.pageY || e.screenY;
  98. if (pageY >= xy.top && pageY < xy.top + scope)
  99. {
  100. dir += 'n';
  101. }
  102. else if (pageY <= xy.top + height && pageY > xy.top + height - scope)
  103. {
  104. dir += 's';
  105. }
  106. if (pageX >= xy.left && pageX < xy.left + scope)
  107. {
  108. dir += 'w';
  109. }
  110. else if (pageX <= xy.left + width && pageX > xy.left + width - scope)
  111. {
  112. dir += 'e';
  113. }
  114. if (p.handles == "all" || dir == "") return dir;
  115. if ($.inArray(dir, g.handles) != -1) return dir;
  116. return '';
  117. },
  118. _setHandles: function (handles)
  119. {
  120. if (!handles) return;
  121. this.handles = handles.replace(/(\s*)/g, '').split(',');
  122. },
  123. _createProxy: function ()
  124. {
  125. var g = this;
  126. g.proxy = $('<div class="l-resizable"></div>');
  127. g.proxy.width(g.target.width()).height(g.target.height())
  128. g.proxy.attr("resizableid", g.id).appendTo('body');
  129. },
  130. _removeProxy: function ()
  131. {
  132. var g = this;
  133. if (g.proxy)
  134. {
  135. g.proxy.remove();
  136. g.proxy = null;
  137. }
  138. },
  139. _start: function (e)
  140. {
  141. var g = this, p = this.options;
  142. g._createProxy();
  143. g.proxy.css({
  144. left: g.target.offset().left,
  145. top: g.target.offset().top,
  146. position: 'absolute'
  147. });
  148. g.current = {
  149. dir: g.dir,
  150. left: g.target.offset().left,
  151. top: g.target.offset().top,
  152. startX: e.pageX || e.screenX,
  153. startY: e.pageY || e.clientY,
  154. width: g.target.width(),
  155. height: g.target.height()
  156. };
  157. $(document).bind("selectstart.resizable", function () { return false; });
  158. $(document).bind('mouseup.resizable', function ()
  159. {
  160. g._stop.apply(g, arguments);
  161. });
  162. $(document).bind('mousemove.resizable', function ()
  163. {
  164. g._drag.apply(g, arguments);
  165. });
  166. g.proxy.show();
  167. g.trigger('startResize', [g.current, e]);
  168. },
  169. changeBy: {
  170. t: ['n', 'ne', 'nw'],
  171. l: ['w', 'sw', 'nw'],
  172. w: ['w', 'sw', 'nw', 'e', 'ne', 'se'],
  173. h: ['n', 'ne', 'nw', 's', 'se', 'sw']
  174. },
  175. _drag: function (e)
  176. {
  177. var g = this, p = this.options;
  178. if (!g.current) return;
  179. if (!g.proxy) return;
  180. g.proxy.css('cursor', g.current.dir == '' ? 'default' : g.current.dir + '-resize');
  181. var pageX = e.pageX || e.screenX;
  182. var pageY = e.pageY || e.screenY;
  183. g.current.diffX = pageX - g.current.startX;
  184. g.current.diffY = pageY - g.current.startY;
  185. g._applyResize(g.proxy);
  186. g.trigger('resize', [g.current, e]);
  187. },
  188. _stop: function (e)
  189. {
  190. var g = this, p = this.options;
  191. if (g.hasBind('stopResize'))
  192. {
  193. if (g.trigger('stopResize', [g.current, e]) != false)
  194. g._applyResize();
  195. }
  196. else
  197. {
  198. g._applyResize();
  199. }
  200. g._removeProxy();
  201. g.trigger('endResize', [g.current, e]);
  202. $(document).unbind("selectstart.resizable");
  203. $(document).unbind('mousemove.resizable');
  204. $(document).unbind('mouseup.resizable');
  205. },
  206. _applyResize: function (applyResultBody)
  207. {
  208. var g = this, p = this.options;
  209. var cur = {
  210. left: g.current.left,
  211. top: g.current.top,
  212. width: g.current.width,
  213. height: g.current.height
  214. };
  215. var applyToTarget = false;
  216. if (!applyResultBody)
  217. {
  218. applyResultBody = g.target;
  219. applyToTarget = true;
  220. if (!isNaN(parseInt(g.target.css('top'))))
  221. cur.top = parseInt(g.target.css('top'));
  222. else
  223. cur.top = 0;
  224. if (!isNaN(parseInt(g.target.css('left'))))
  225. cur.left = parseInt(g.target.css('left'));
  226. else
  227. cur.left = 0;
  228. }
  229. if ($.inArray(g.current.dir, g.changeBy.l) > -1)
  230. {
  231. cur.left += g.current.diffX;
  232. g.current.diffLeft = g.current.diffX;
  233. }
  234. else if (applyToTarget)
  235. {
  236. delete cur.left;
  237. }
  238. if ($.inArray(g.current.dir, g.changeBy.t) > -1)
  239. {
  240. cur.top += g.current.diffY;
  241. g.current.diffTop = g.current.diffY;
  242. }
  243. else if (applyToTarget)
  244. {
  245. delete cur.top;
  246. }
  247. if ($.inArray(g.current.dir, g.changeBy.w) > -1)
  248. {
  249. cur.width += (g.current.dir.indexOf('w') == -1 ? 1 : -1) * g.current.diffX;
  250. g.current.newWidth = cur.width;
  251. }
  252. else if (applyToTarget)
  253. {
  254. delete cur.width;
  255. }
  256. if ($.inArray(g.current.dir, g.changeBy.h) > -1)
  257. {
  258. cur.height += (g.current.dir.indexOf('n') == -1 ? 1 : -1) * g.current.diffY;
  259. g.current.newHeight = cur.height;
  260. }
  261. else if (applyToTarget)
  262. {
  263. delete cur.height;
  264. }
  265. if (applyToTarget && p.animate)
  266. applyResultBody.animate(cur);
  267. else
  268. applyResultBody.css(cur);
  269. }
  270. });
  271. })(jQuery);