resizing.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Library functions for resizing widgets
  3. *
  4. */
  5. // event handler temporary storage variables for IE 4
  6. //
  7. var old_mousemove_handler;
  8. var old_mouseup_handler;
  9. // define variables used in vertical resize handlers
  10. //
  11. var element_being_vert_resized;
  12. var element_being_vert_resized_orig_opacity;
  13. var min_vert_resize_height;
  14. var max_vert_resize_height;
  15. var original_vert_click_pos;
  16. var original_vert_height;
  17. /**
  18. * Begin a (vertical only) resize of some element
  19. *
  20. * Note that this event handler is only intended
  21. * to be called for an event (usually mousedown)
  22. * that is registered as a tag attribute (not with
  23. * addEventListener() and ilk).
  24. *
  25. * @param evt The JavaScript event object
  26. * @param resize_element The element being resized
  27. * @param min_height The minimum allowable resize height
  28. * @param max_height The maximum allowable resize height
  29. * @param handle_element The handle element that was clicked
  30. * upon to initiate the resize
  31. *
  32. */
  33. function startVertResizeDrag(evt, resize_element, min_height,
  34. max_height, handle_element)
  35. {
  36. // assign variables used in other handlers
  37. //
  38. element_being_vert_resized = resize_element;
  39. min_vert_resize_height = min_height;
  40. max_vert_resize_height = max_height;
  41. // dim element to emphasize effect
  42. //
  43. element_being_vert_resized_orig_opacity = element_being_vert_resized.style.opacity;
  44. element_being_vert_resized.style.opacity = 0.25;
  45. // detemine original vertical click coordinates
  46. //
  47. original_vert_click_pos = evt.clientY;
  48. original_vert_height = element_being_vert_resized.offsetHeight;
  49. // add drag and finish drag listeners
  50. //
  51. if (document.addEventListener) // DOM Level 2 Event Model
  52. {
  53. document.addEventListener('mousemove', continueVertResizeDrag, true);
  54. document.addEventListener('mouseup', finishVertResizeDrag, true);
  55. }
  56. else if (document.attachEvent) // IE 5+ Event Model
  57. {
  58. document.attachEvent("onmousemove", continueVertResizeDrag);
  59. document.attachEvent("onmouseup", finishVertResizeDrag);
  60. }
  61. else // IE 4 Event Model
  62. {
  63. old_mousemove_handler = document.onmousemove;
  64. old_mouseup_handler = document.onmouseup;
  65. document.onmousemove = continueVertResizeDrag;
  66. document.onmouseup = finishVertResizeDrag;
  67. }
  68. // indicate that the event has been handled
  69. //
  70. if (evt.stopPropagation) evt.stopPropagation(); // DOM Level 2
  71. else evt.cancelBubble = true; // IE
  72. // don't let any default action happen
  73. //
  74. if (evt.preventDefault) evt.preventDefault(); // DOM Level 2
  75. else evt.returnValue = false; // IE
  76. // to top it all off, return false too
  77. //
  78. return false;
  79. }
  80. /**
  81. * Continue a (vertical only) resize of some element
  82. *
  83. * @param evt The JavaScript event object
  84. *
  85. */
  86. function continueVertResizeDrag(evt)
  87. {
  88. // IE blows
  89. //
  90. if (!evt) evt = window.event;
  91. if (!evt) return true;
  92. // adjust height of resize item according to current mouse position
  93. //
  94. delta_resize = original_vert_click_pos - evt.clientY;
  95. new_height = original_vert_height - delta_resize;
  96. if (new_height >= min_vert_resize_height && new_height <= max_vert_resize_height)
  97. element_being_vert_resized.style.height = new_height + 'px';
  98. // indicate that the event has been handled
  99. //
  100. if (evt.stopPropagation) evt.stopPropagation(); // DOM Level 2
  101. else evt.cancelBubble = true; // IE
  102. // to top it all off, return false too
  103. //
  104. return false;
  105. }
  106. /**
  107. * Finish a (vertical only) resize of some element
  108. *
  109. * @param evt The JavaScript event object
  110. *
  111. */
  112. function finishVertResizeDrag(evt)
  113. {
  114. // IE blows
  115. //
  116. if (!evt) evt = window.event;
  117. if (!evt) return true;
  118. // restore element's original opacity
  119. //
  120. element_being_vert_resized.style.opacity = element_being_vert_resized_orig_opacity;
  121. // unregister all event listeners
  122. //
  123. if (document.removeEventListener) // DOM Event Model
  124. {
  125. document.removeEventListener("mousemove", continueVertResizeDrag, true);
  126. document.removeEventListener("mouseup", finishVertResizeDrag, true);
  127. }
  128. else if (document.detachEvent) // IE 5+ Event Model
  129. {
  130. document.detachEvent("onmousemove", continueVertResizeDrag);
  131. document.detachEvent("onmouseup", finishVertResizeDrag);
  132. }
  133. else // IE 4 Event Model
  134. {
  135. document.onmousemove = old_mousemove_handler;
  136. document.onmouseup = old_mouseup_handler;
  137. }
  138. // indicate that the event has been handled
  139. //
  140. if (evt.stopPropagation) evt.stopPropagation(); // DOM Level 2
  141. else evt.cancelBubble = true; // IE
  142. // to top it all off, return false too
  143. //
  144. return false;
  145. }