2
0

dragresize_commented.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. DragResize v1.0
  3. (c) 2005-2006 Angus Turnbull, TwinHelix Designs http://www.twinhelix.com
  4. Licensed under the CC-GNU LGPL, version 2.1 or later:
  5. http://creativecommons.org/licenses/LGPL/2.1/
  6. This is distributed WITHOUT ANY WARRANTY; without even the implied
  7. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. */
  9. // Common API code.
  10. if (typeof addEvent != 'function') {
  11. var removeEvent = function(o, t, f, l) {
  12. var d = 'removeEventListener';
  13. if (o[d] && !l) {
  14. return o[d](t, f, false);
  15. }
  16. if (o._evts && o._evts[t] && f._i) {
  17. delete o._evts[t][f._i];
  18. }
  19. };
  20. var addEvent = function(o, t, f, l) {
  21. var d = 'addEventListener', n = 'on' + t, rO = o, rT = t, rF = f, rL = l;
  22. if (o[d] && !l) {
  23. return o[d](t, f, false);
  24. }
  25. if (!o._evts) {
  26. o._evts = {};
  27. }
  28. if (!o._evts[t]) {
  29. o._evts[t] = o[n] ? { b: o[n] } : {};
  30. o[n] = new Function('e',
  31. 'var r = true, o = this, a = o._evts["' + t + '"], i; for (i in a) {' +
  32. 'o._f = a[i]; r = o._f(e||window.event) != false && r; o._f = null;' +
  33. '} return r'
  34. );
  35. if (t != 'unload') {
  36. addEvent(window, 'unload', function() {
  37. removeEvent(rO, rT, rF, rL);
  38. });
  39. }
  40. }
  41. if (!f._i) {
  42. f._i = addEvent._i++;
  43. }
  44. o._evts[t][f._i] = f;
  45. };
  46. addEvent._i = 1;
  47. }
  48. function cancelEvent(e, c) {
  49. e.returnValue = false;
  50. if (e.preventDefault) {
  51. e.preventDefault();
  52. }
  53. if (c) {
  54. e.cancelBubble = true;
  55. if (e.stopPropagation) {
  56. e.stopPropagation();
  57. }
  58. }
  59. }
  60. // *** DRAG/RESIZE CODE ***
  61. function DragResize(myName, config) {
  62. var props = {
  63. myName: myName, // Name of the object.
  64. enabled: true, // Global toggle of drag/resize.
  65. handles: ['tl', 'tm', 'tr',
  66. 'ml', 'mr', 'bl', 'bm', 'br'], // Array of drag handles: top/mid/bot/right.
  67. isElement: null, // Function ref to test for an element.
  68. element: null, // The currently selected element.
  69. handle: null, // Active handle reference of the element.
  70. minWidth: 10, minHeight: 10, // Minimum pixel size of elements.
  71. minLeft: 0, maxLeft: 9999, // Bounding box area, in pixels.
  72. minTop: 0, maxTop: 9999,
  73. zIndex: 1, // The highest Z-Index yet allocated.
  74. mouseX: 0, mouseY: 0, // Current mouse position, recorded live.
  75. lastMouseX: 0, lastMouseY: 0, // Last processed mouse positions.
  76. mOffX: 0, mOffY: 0, // A known offset between position & mouse.
  77. elmX: 0, elmY: 0, // Element position.
  78. elmW: 0, elmH: 0, // Element size.
  79. allowBlur: true, // Whether to allow automatic blur onclick.
  80. ondragfocus: null, // Event handler functions.
  81. ondragstart: null,
  82. ondragmove: null,
  83. ondragend: null,
  84. ondragblur: null
  85. };
  86. for (var p in props) {
  87. this[p] = (typeof config[p] == 'undefined') ? props[p] : config[p];
  88. }
  89. };
  90. DragResize.prototype.apply = function(node) {
  91. // Adds object event handlers to the specified DOM node.
  92. var obj = this;
  93. addEvent(node, 'mousedown', function(e) { obj.mouseDown(e) } );
  94. addEvent(node, 'mousemove', function(e) { obj.mouseMove(e) } );
  95. addEvent(node, 'mouseup', function(e) { obj.mouseUp(e) } );
  96. };
  97. DragResize.prototype.select = function(newElement) {
  98. with (this) {
  99. // Selects an element for dragging.
  100. if (!document.getElementById || !enabled) return;
  101. // Activate and record our new dragging element.
  102. if (newElement && (newElement != element) && enabled) {
  103. element = newElement;
  104. // Elevate it
  105. element.style.zIndex = ++zIndex;
  106. // Record element attributes for mouseMove().
  107. elmX = parseInt(element.style.left);
  108. elmY = parseInt(element.style.top);
  109. elmW = element.offsetWidth;
  110. elmH = element.offsetHeight;
  111. if (ondragfocus) this.ondragfocus();
  112. }
  113. }
  114. };
  115. DragResize.prototype.deselect = function(delHandles) {
  116. with (this) {
  117. // Immediately stops dragging an element. If 'delHandles' is true, this
  118. // remove the handles from the element and clears the element flag,
  119. // completely resetting the .
  120. if (!document.getElementById || !enabled) return;
  121. if (delHandles) {
  122. if (ondragblur) this.ondragblur();
  123. element = null;
  124. }
  125. handle = null;
  126. mOffX = 0;
  127. mOffY = 0;
  128. }
  129. };
  130. DragResize.prototype.mouseDown = function(e) {
  131. with (this) {
  132. // Suitable elements are selected for drag/resize on mousedown.
  133. // We also initialise the resize boxes, and drag parameters like mouse position etc.
  134. if (!document.getElementById || !enabled) return true;
  135. var elm = e.target || e.srcElement,
  136. newElement = null,
  137. newHandle = null,
  138. hRE = new RegExp(myName + '-([trmbl]{2})', '');
  139. while (elm) {
  140. // Loop up the DOM looking for matching elements. Remember one if found.
  141. if (elm.className) {
  142. if (isElement(elm)) { newElement = elm; break }
  143. }
  144. elm = elm.parentNode;
  145. }
  146. // If this isn't on the last dragged element, call deselect(),
  147. // which will hide its handles and clear element.
  148. if (element && (element != newElement) && allowBlur) deselect(true);
  149. // If we have a new matching element, call select().
  150. if (newElement && (!element || (newElement == element))) {
  151. // Stop mouse selections if we're dragging a handle.
  152. if (newHandle) cancelEvent(e);
  153. select(newElement, newHandle);
  154. handle = newHandle;
  155. if (handle && ondragstart) this.ondragstart(hRE.test(handle.className));
  156. }
  157. }
  158. };
  159. DragResize.prototype.mouseMove = function(e) { with (this) {
  160. // This continually offsets the dragged element by the difference between the
  161. // last recorded mouse position (mouseX/Y) and the current mouse position.
  162. if (!document.getElementById || !enabled) return true;
  163. // We always record the current mouse position.
  164. mouseX = e.pageX || e.clientX + document.documentElement.scrollLeft;
  165. mouseY = e.pageY || e.clientY + document.documentElement.scrollTop;
  166. // Record the relative mouse movement, in case we're dragging.
  167. // Add any previously stored & ignored offset to the calculations.
  168. var diffX = mouseX - lastMouseX + mOffX;
  169. var diffY = mouseY - lastMouseY + mOffY;
  170. mOffX = mOffY = 0;
  171. // Update last processed mouse positions.
  172. lastMouseX = mouseX;
  173. lastMouseY = mouseY;
  174. // That's all we do if we're not dragging anything.
  175. if (!handle) return true;
  176. // If included in the script, run the resize handle drag routine.
  177. // Let it create an object representing the drag offsets.
  178. var isResize = false;
  179. if (this.resizeHandleDrag && this.resizeHandleDrag(diffX, diffY)) {
  180. isResize = true;
  181. } else {
  182. // If the resize drag handler isn't set or returns fase (to indicate the drag was
  183. // not on a resize handle), we must be dragging the whole element, so move that.
  184. // Bounds check left-right...
  185. var dX = diffX, dY = diffY;
  186. if (elmX + dX < minLeft) mOffX = (dX - (diffX = minLeft - elmX));
  187. else if (elmX + elmW + dX > maxLeft) mOffX = (dX - (diffX = maxLeft - elmX - elmW));
  188. // ...and up-down.
  189. if (elmY + dY < minTop) mOffY = (dY - (diffY = minTop - elmY));
  190. else if (elmY + elmH + dY > maxTop) mOffY = (dY - (diffY = maxTop - elmY - elmH));
  191. elmX += diffX;
  192. elmY += diffY;
  193. }
  194. // Assign new info back to the element, with minimum dimensions.
  195. with (element.style) {
  196. left = elmX + 'px';
  197. width = elmW + 'px';
  198. top = elmY + 'px';
  199. height = elmH + 'px';
  200. }
  201. // Evil, dirty, hackish Opera select-as-you-drag fix.
  202. if (window.opera && document.documentElement) {
  203. var oDF = document.getElementById('op-drag-fix');
  204. if (!oDF) {
  205. var oDF = document.createElement('input');
  206. oDF.id = 'op-drag-fix';
  207. oDF.style.display = 'none';
  208. document.body.appendChild(oDF);
  209. }
  210. oDF.focus();
  211. }
  212. if (ondragmove) this.ondragmove(isResize);
  213. // Stop a normal drag event.
  214. cancelEvent(e);
  215. }};
  216. DragResize.prototype.mouseUp = function(e) { with (this) {
  217. // On mouseup, stop dragging, but don't reset handler visibility.
  218. if (!document.getElementById || !enabled) return;
  219. var hRE = new RegExp(myName + '-([trmbl]{2})', '');
  220. if (handle && ondragend) this.ondragend(hRE.test(handle.className));
  221. deselect(false);
  222. }};
  223. DragResize.prototype.resizeHandleDrag = function(diffX, diffY) { with (this) {
  224. // Passed the mouse movement amounts. This function checks to see whether the
  225. // drag is from a resize handle created above; if so, it changes the stored
  226. // elm* dimensions and mOffX/Y.
  227. var hClass = handle && handle.className &&
  228. handle.className.match(new RegExp(myName + '-([tmblr]{2})')) ? RegExp.$1 : '';
  229. // If the hClass is one of the resize handles, resize one or two dimensions.
  230. // Bounds checking is the hard bit -- basically for each edge, check that the
  231. // element doesn't go under minimum size, and doesn't go beyond its boundary.
  232. var dY = diffY, dX = diffX, processed = false;
  233. if (hClass.indexOf('t') >= 0) {
  234. rs = 1;
  235. if (elmH - dY < minHeight) mOffY = (dY - (diffY = elmH - minHeight));
  236. else if (elmY + dY < minTop) mOffY = (dY - (diffY = minTop - elmY));
  237. elmY += diffY;
  238. elmH -= diffY;
  239. processed = true;
  240. }
  241. if (hClass.indexOf('b') >= 0) {
  242. rs = 1;
  243. if (elmH + dY < minHeight) mOffY = (dY - (diffY = minHeight - elmH));
  244. else if (elmY + elmH + dY > maxTop) mOffY = (dY - (diffY = maxTop - elmY - elmH));
  245. elmH += diffY;
  246. processed = true;
  247. }
  248. if (hClass.indexOf('l') >= 0) {
  249. rs = 1;
  250. if (elmW - dX < minWidth) mOffX = (dX - (diffX = elmW - minWidth));
  251. else if (elmX + dX < minLeft) mOffX = (dX - (diffX = minLeft - elmX));
  252. elmX += diffX;
  253. elmW -= diffX;
  254. processed = true;
  255. }
  256. if (hClass.indexOf('r') >= 0) {
  257. rs = 1;
  258. if (elmW + dX < minWidth) mOffX = (dX - (diffX = minWidth - elmW));
  259. else if (elmX + elmW + dX > maxLeft) mOffX = (dX - (diffX = maxLeft - elmX - elmW));
  260. elmW += diffX;
  261. processed = true;
  262. }
  263. return processed;
  264. }};