dragresize_commented.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 (!newHandle && (hRE.test(elm.className))) newHandle = elm;
  143. if (isElement(elm)) { newElement = elm; break }
  144. }
  145. elm = elm.parentNode;
  146. }
  147. // If this isn't on the last dragged element, call deselect(),
  148. // which will hide its handles and clear element.
  149. if (element && (element != newElement) && allowBlur) deselect(true);
  150. // If we have a new matching element, call select().
  151. if (newElement && (!element || (newElement == element))) {
  152. // Stop mouse selections if we're dragging a handle.
  153. if (newHandle) cancelEvent(e);
  154. select(newElement, newHandle);
  155. handle = newHandle;
  156. if (handle && ondragstart) this.ondragstart(hRE.test(handle.className));
  157. }
  158. }
  159. };
  160. DragResize.prototype.mouseMove = function(e) { with (this) {
  161. // This continually offsets the dragged element by the difference between the
  162. // last recorded mouse position (mouseX/Y) and the current mouse position.
  163. if (!document.getElementById || !enabled) return true;
  164. // We always record the current mouse position.
  165. mouseX = e.pageX || e.clientX + document.documentElement.scrollLeft;
  166. mouseY = e.pageY || e.clientY + document.documentElement.scrollTop;
  167. // Record the relative mouse movement, in case we're dragging.
  168. // Add any previously stored & ignored offset to the calculations.
  169. var diffX = mouseX - lastMouseX + mOffX;
  170. var diffY = mouseY - lastMouseY + mOffY;
  171. mOffX = mOffY = 0;
  172. // Update last processed mouse positions.
  173. lastMouseX = mouseX;
  174. lastMouseY = mouseY;
  175. // That's all we do if we're not dragging anything.
  176. if (!handle) return true;
  177. // If included in the script, run the resize handle drag routine.
  178. // Let it create an object representing the drag offsets.
  179. var isResize = false;
  180. if (this.resizeHandleDrag && this.resizeHandleDrag(diffX, diffY)) {
  181. isResize = true;
  182. } else {
  183. // If the resize drag handler isn't set or returns fase (to indicate the drag was
  184. // not on a resize handle), we must be dragging the whole element, so move that.
  185. // Bounds check left-right...
  186. var dX = diffX, dY = diffY;
  187. if (elmX + dX < minLeft) mOffX = (dX - (diffX = minLeft - elmX));
  188. else if (elmX + elmW + dX > maxLeft) mOffX = (dX - (diffX = maxLeft - elmX - elmW));
  189. // ...and up-down.
  190. if (elmY + dY < minTop) mOffY = (dY - (diffY = minTop - elmY));
  191. else if (elmY + elmH + dY > maxTop) mOffY = (dY - (diffY = maxTop - elmY - elmH));
  192. elmX += diffX;
  193. elmY += diffY;
  194. }
  195. // Assign new info back to the element, with minimum dimensions.
  196. with (element.style) {
  197. left = elmX + 'px';
  198. width = elmW + 'px';
  199. top = elmY + 'px';
  200. height = elmH + 'px';
  201. }
  202. // Evil, dirty, hackish Opera select-as-you-drag fix.
  203. if (window.opera && document.documentElement) {
  204. var oDF = document.getElementById('op-drag-fix');
  205. if (!oDF) {
  206. var oDF = document.createElement('input');
  207. oDF.id = 'op-drag-fix';
  208. oDF.style.display = 'none';
  209. document.body.appendChild(oDF);
  210. }
  211. oDF.focus();
  212. }
  213. if (ondragmove) this.ondragmove(isResize);
  214. // Stop a normal drag event.
  215. cancelEvent(e);
  216. }};
  217. DragResize.prototype.mouseUp = function(e) { with (this) {
  218. // On mouseup, stop dragging, but don't reset handler visibility.
  219. if (!document.getElementById || !enabled) return;
  220. var hRE = new RegExp(myName + '-([trmbl]{2})', '');
  221. if (handle && ondragend) this.ondragend(hRE.test(handle.className));
  222. deselect(false);
  223. }};
  224. DragResize.prototype.resizeHandleDrag = function(diffX, diffY) { with (this) {
  225. // Passed the mouse movement amounts. This function checks to see whether the
  226. // drag is from a resize handle created above; if so, it changes the stored
  227. // elm* dimensions and mOffX/Y.
  228. var hClass = handle && handle.className &&
  229. handle.className.match(new RegExp(myName + '-([tmblr]{2})')) ? RegExp.$1 : '';
  230. // If the hClass is one of the resize handles, resize one or two dimensions.
  231. // Bounds checking is the hard bit -- basically for each edge, check that the
  232. // element doesn't go under minimum size, and doesn't go beyond its boundary.
  233. var dY = diffY, dX = diffX, processed = false;
  234. if (hClass.indexOf('t') >= 0) {
  235. rs = 1;
  236. if (elmH - dY < minHeight) mOffY = (dY - (diffY = elmH - minHeight));
  237. else if (elmY + dY < minTop) mOffY = (dY - (diffY = minTop - elmY));
  238. elmY += diffY;
  239. elmH -= diffY;
  240. processed = true;
  241. }
  242. if (hClass.indexOf('b') >= 0) {
  243. rs = 1;
  244. if (elmH + dY < minHeight) mOffY = (dY - (diffY = minHeight - elmH));
  245. else if (elmY + elmH + dY > maxTop) mOffY = (dY - (diffY = maxTop - elmY - elmH));
  246. elmH += diffY;
  247. processed = true;
  248. }
  249. if (hClass.indexOf('l') >= 0) {
  250. rs = 1;
  251. if (elmW - dX < minWidth) mOffX = (dX - (diffX = elmW - minWidth));
  252. else if (elmX + dX < minLeft) mOffX = (dX - (diffX = minLeft - elmX));
  253. elmX += diffX;
  254. elmW -= diffX;
  255. processed = true;
  256. }
  257. if (hClass.indexOf('r') >= 0) {
  258. rs = 1;
  259. if (elmW + dX < minWidth) mOffX = (dX - (diffX = minWidth - elmW));
  260. else if (elmX + elmW + dX > maxLeft) mOffX = (dX - (diffX = maxLeft - elmX - elmW));
  261. elmW += diffX;
  262. processed = true;
  263. }
  264. return processed;
  265. }};