2
0

dragresize_commented.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. isHandle: null, // Function ref to test for move handle.
  69. element: null, // The currently selected element.
  70. handle: null, // Active handle reference of the element.
  71. minWidth: 10, minHeight: 10, // Minimum pixel size of elements.
  72. minLeft: 0, maxLeft: 9999, // Bounding box area, in pixels.
  73. minTop: 0, maxTop: 9999,
  74. zIndex: 1, // The highest Z-Index yet allocated.
  75. mouseX: 0, mouseY: 0, // Current mouse position, recorded live.
  76. lastMouseX: 0, lastMouseY: 0, // Last processed mouse positions.
  77. mOffX: 0, mOffY: 0, // A known offset between position & mouse.
  78. elmX: 0, elmY: 0, // Element position.
  79. elmW: 0, elmH: 0, // Element size.
  80. allowBlur: true, // Whether to allow automatic blur onclick.
  81. ondragfocus: null, // Event handler functions.
  82. ondragstart: null,
  83. ondragmove: null,
  84. ondragend: null,
  85. ondragblur: null
  86. };
  87. for (var p in props) {
  88. this[p] = (typeof config[p] == 'undefined') ? props[p] : config[p];
  89. }
  90. };
  91. DragResize.prototype.apply = function(node) {
  92. // Adds object event handlers to the specified DOM node.
  93. var obj = this;
  94. addEvent(node, 'mousedown', function(e) { obj.mouseDown(e) } );
  95. addEvent(node, 'mousemove', function(e) { obj.mouseMove(e) } );
  96. addEvent(node, 'mouseup', function(e) { obj.mouseUp(e) } );
  97. };
  98. DragResize.prototype.select = function(newElement) {
  99. with (this) {
  100. // Selects an element for dragging.
  101. if (!document.getElementById || !enabled) return;
  102. // Activate and record our new dragging element.
  103. if (newElement && (newElement != element) && enabled) {
  104. element = newElement;
  105. // Elevate it and give it resize handles.
  106. element.style.zIndex = ++zIndex;
  107. if (this.resizeHandleSet) this.resizeHandleSet(element, true);
  108. // Record element attributes for mouseMove().
  109. elmX = parseInt(element.style.left);
  110. elmY = parseInt(element.style.top);
  111. elmW = element.offsetWidth;
  112. elmH = element.offsetHeight;
  113. if (ondragfocus) this.ondragfocus();
  114. }
  115. }
  116. };
  117. DragResize.prototype.deselect = function(delHandles) {
  118. with (this) {
  119. // Immediately stops dragging an element. If 'delHandles' is true, this
  120. // remove the handles from the element and clears the element flag,
  121. // completely resetting the .
  122. if (!document.getElementById || !enabled) return;
  123. if (delHandles) {
  124. if (ondragblur) this.ondragblur();
  125. if (this.resizeHandleSet) this.resizeHandleSet(element, false);
  126. element = null;
  127. }
  128. handle = null;
  129. mOffX = 0;
  130. mOffY = 0;
  131. }
  132. };
  133. DragResize.prototype.mouseDown = function(e) {
  134. with (this) {
  135. // Suitable elements are selected for drag/resize on mousedown.
  136. // We also initialise the resize boxes, and drag parameters like mouse position etc.
  137. if (!document.getElementById || !enabled) return true;
  138. var elm = e.target || e.srcElement,
  139. newElement = null,
  140. newHandle = null,
  141. hRE = new RegExp(myName + '-([trmbl]{2})', '');
  142. while (elm) {
  143. // Loop up the DOM looking for matching elements. Remember one if found.
  144. if (elm.className) {
  145. if (!newHandle && (hRE.test(elm.className) || isHandle(elm))) newHandle = elm;
  146. if (isElement(elm)) { newElement = elm; break }
  147. }
  148. elm = elm.parentNode;
  149. }
  150. // If this isn't on the last dragged element, call deselect(),
  151. // which will hide its handles and clear element.
  152. if (element && (element != newElement) && allowBlur) deselect(true);
  153. // If we have a new matching element, call select().
  154. if (newElement && (!element || (newElement == element))) {
  155. // Stop mouse selections if we're dragging a handle.
  156. if (newHandle) cancelEvent(e);
  157. select(newElement, newHandle);
  158. handle = newHandle;
  159. if (handle && ondragstart) this.ondragstart(hRE.test(handle.className));
  160. }
  161. }
  162. };
  163. DragResize.prototype.mouseMove = function(e) { with (this) {
  164. // This continually offsets the dragged element by the difference between the
  165. // last recorded mouse position (mouseX/Y) and the current mouse position.
  166. if (!document.getElementById || !enabled) return true;
  167. // We always record the current mouse position.
  168. mouseX = e.pageX || e.clientX + document.documentElement.scrollLeft;
  169. mouseY = e.pageY || e.clientY + document.documentElement.scrollTop;
  170. // Record the relative mouse movement, in case we're dragging.
  171. // Add any previously stored & ignored offset to the calculations.
  172. var diffX = mouseX - lastMouseX + mOffX;
  173. var diffY = mouseY - lastMouseY + mOffY;
  174. mOffX = mOffY = 0;
  175. // Update last processed mouse positions.
  176. lastMouseX = mouseX;
  177. lastMouseY = mouseY;
  178. // That's all we do if we're not dragging anything.
  179. if (!handle) return true;
  180. // If included in the script, run the resize handle drag routine.
  181. // Let it create an object representing the drag offsets.
  182. var isResize = false;
  183. if (this.resizeHandleDrag && this.resizeHandleDrag(diffX, diffY)) {
  184. isResize = true;
  185. } else {
  186. // If the resize drag handler isn't set or returns fase (to indicate the drag was
  187. // not on a resize handle), we must be dragging the whole element, so move that.
  188. // Bounds check left-right...
  189. var dX = diffX, dY = diffY;
  190. if (elmX + dX < minLeft) mOffX = (dX - (diffX = minLeft - elmX));
  191. else if (elmX + elmW + dX > maxLeft) mOffX = (dX - (diffX = maxLeft - elmX - elmW));
  192. // ...and up-down.
  193. if (elmY + dY < minTop) mOffY = (dY - (diffY = minTop - elmY));
  194. else if (elmY + elmH + dY > maxTop) mOffY = (dY - (diffY = maxTop - elmY - elmH));
  195. elmX += diffX;
  196. elmY += diffY;
  197. }
  198. // Assign new info back to the element, with minimum dimensions.
  199. with (element.style) {
  200. left = elmX + 'px';
  201. width = elmW + 'px';
  202. top = elmY + 'px';
  203. height = elmH + 'px';
  204. }
  205. // Evil, dirty, hackish Opera select-as-you-drag fix.
  206. if (window.opera && document.documentElement) {
  207. var oDF = document.getElementById('op-drag-fix');
  208. if (!oDF) {
  209. var oDF = document.createElement('input');
  210. oDF.id = 'op-drag-fix';
  211. oDF.style.display = 'none';
  212. document.body.appendChild(oDF);
  213. }
  214. oDF.focus();
  215. }
  216. if (ondragmove) this.ondragmove(isResize);
  217. // Stop a normal drag event.
  218. cancelEvent(e);
  219. }};
  220. DragResize.prototype.mouseUp = function(e) { with (this) {
  221. // On mouseup, stop dragging, but don't reset handler visibility.
  222. if (!document.getElementById || !enabled) return;
  223. var hRE = new RegExp(myName + '-([trmbl]{2})', '');
  224. if (handle && ondragend) this.ondragend(hRE.test(handle.className));
  225. deselect(false);
  226. }};
  227. /* Resize Code -- can be deleted if you're not using it. */
  228. DragResize.prototype.resizeHandleSet = function(elm, show) { with (this) {
  229. // Either creates, shows or hides the resize handles within an element.
  230. // If we're showing them, and no handles have been created, create 4 new ones.
  231. if (!elm._handle_tr) {
  232. for (var h = 0; h < handles.length; h++) {
  233. // Create 4 news divs, assign each a generic + specific class.
  234. var hDiv = document.createElement('div');
  235. hDiv.className = myName + ' ' + myName + '-' + handles[h];
  236. elm['_handle_' + handles[h]] = elm.appendChild(hDiv);
  237. }
  238. }
  239. // We now have handles. Find them all and show/hide.
  240. for (var h = 0; h < handles.length; h++) {
  241. elm['_handle_' + handles[h]].style.visibility = show ? 'inherit' : 'hidden';
  242. }
  243. }};
  244. DragResize.prototype.resizeHandleDrag = function(diffX, diffY) { with (this) {
  245. // Passed the mouse movement amounts. This function checks to see whether the
  246. // drag is from a resize handle created above; if so, it changes the stored
  247. // elm* dimensions and mOffX/Y.
  248. var hClass = handle && handle.className &&
  249. handle.className.match(new RegExp(myName + '-([tmblr]{2})')) ? RegExp.$1 : '';
  250. // If the hClass is one of the resize handles, resize one or two dimensions.
  251. // Bounds checking is the hard bit -- basically for each edge, check that the
  252. // element doesn't go under minimum size, and doesn't go beyond its boundary.
  253. var dY = diffY, dX = diffX, processed = false;
  254. if (hClass.indexOf('t') >= 0) {
  255. rs = 1;
  256. if (elmH - dY < minHeight) mOffY = (dY - (diffY = elmH - minHeight));
  257. else if (elmY + dY < minTop) mOffY = (dY - (diffY = minTop - elmY));
  258. elmY += diffY;
  259. elmH -= diffY;
  260. processed = true;
  261. }
  262. if (hClass.indexOf('b') >= 0) {
  263. rs = 1;
  264. if (elmH + dY < minHeight) mOffY = (dY - (diffY = minHeight - elmH));
  265. else if (elmY + elmH + dY > maxTop) mOffY = (dY - (diffY = maxTop - elmY - elmH));
  266. elmH += diffY;
  267. processed = true;
  268. }
  269. if (hClass.indexOf('l') >= 0) {
  270. rs = 1;
  271. if (elmW - dX < minWidth) mOffX = (dX - (diffX = elmW - minWidth));
  272. else if (elmX + dX < minLeft) mOffX = (dX - (diffX = minLeft - elmX));
  273. elmX += diffX;
  274. elmW -= diffX;
  275. processed = true;
  276. }
  277. if (hClass.indexOf('r') >= 0) {
  278. rs = 1;
  279. if (elmW + dX < minWidth) mOffX = (dX - (diffX = minWidth - elmW));
  280. else if (elmX + elmW + dX > maxLeft) mOffX = (dX - (diffX = maxLeft - elmX - elmW));
  281. elmW += diffX;
  282. processed = true;
  283. }
  284. return processed;
  285. }};