DragHelper.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * DragHelper.js
  3. *
  4. * Copyright, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /**
  11. * Drag/drop helper class.
  12. *
  13. * @example
  14. * var dragHelper = new tinymce.ui.DragHelper('mydiv', {
  15. * start: function(evt) {
  16. * },
  17. *
  18. * drag: function(evt) {
  19. * },
  20. *
  21. * end: function(evt) {
  22. * }
  23. * });
  24. *
  25. * @class tinymce.ui.DragHelper
  26. */
  27. define("tinymce/ui/DragHelper", [
  28. "tinymce/ui/DomUtils"
  29. ], function(DomUtils) {
  30. "use strict";
  31. function getDocumentSize() {
  32. var doc = document, documentElement, body, scrollWidth, clientWidth;
  33. var offsetWidth, scrollHeight, clientHeight, offsetHeight, max = Math.max;
  34. documentElement = doc.documentElement;
  35. body = doc.body;
  36. scrollWidth = max(documentElement.scrollWidth, body.scrollWidth);
  37. clientWidth = max(documentElement.clientWidth, body.clientWidth);
  38. offsetWidth = max(documentElement.offsetWidth, body.offsetWidth);
  39. scrollHeight = max(documentElement.scrollHeight, body.scrollHeight);
  40. clientHeight = max(documentElement.clientHeight, body.clientHeight);
  41. offsetHeight = max(documentElement.offsetHeight, body.offsetHeight);
  42. return {
  43. width: scrollWidth < offsetWidth ? clientWidth : scrollWidth,
  44. height: scrollHeight < offsetHeight ? clientHeight : scrollHeight
  45. };
  46. }
  47. return function(id, settings) {
  48. var eventOverlayElm, doc = document, downButton, start, stop, drag, startX, startY;
  49. settings = settings || {};
  50. function getHandleElm() {
  51. return doc.getElementById(settings.handle || id);
  52. }
  53. start = function(e) {
  54. var docSize = getDocumentSize(), handleElm, cursor;
  55. e.preventDefault();
  56. downButton = e.button;
  57. handleElm = getHandleElm();
  58. startX = e.screenX;
  59. startY = e.screenY;
  60. // Grab cursor from handle
  61. if (window.getComputedStyle) {
  62. cursor = window.getComputedStyle(handleElm, null).getPropertyValue("cursor");
  63. } else {
  64. cursor = handleElm.runtimeStyle.cursor;
  65. }
  66. // Create event overlay and add it to document
  67. eventOverlayElm = doc.createElement('div');
  68. DomUtils.css(eventOverlayElm, {
  69. position: "absolute",
  70. top: 0, left: 0,
  71. width: docSize.width,
  72. height: docSize.height,
  73. zIndex: 0x7FFFFFFF,
  74. opacity: 0.0001,
  75. background: 'red',
  76. cursor: cursor
  77. });
  78. doc.body.appendChild(eventOverlayElm);
  79. // Bind mouse events
  80. DomUtils.on(doc, 'mousemove', drag);
  81. DomUtils.on(doc, 'mouseup', stop);
  82. // Begin drag
  83. settings.start(e);
  84. };
  85. drag = function(e) {
  86. if (e.button !== downButton) {
  87. return stop(e);
  88. }
  89. e.deltaX = e.screenX - startX;
  90. e.deltaY = e.screenY - startY;
  91. e.preventDefault();
  92. settings.drag(e);
  93. };
  94. stop = function(e) {
  95. DomUtils.off(doc, 'mousemove', drag);
  96. DomUtils.off(doc, 'mouseup', stop);
  97. eventOverlayElm.parentNode.removeChild(eventOverlayElm);
  98. if (settings.stop) {
  99. settings.stop(e);
  100. }
  101. };
  102. /**
  103. * Destroys the drag/drop helper instance.
  104. *
  105. * @method destroy
  106. */
  107. this.destroy = function() {
  108. DomUtils.off(getHandleElm());
  109. };
  110. DomUtils.on(getHandleElm(), 'mousedown', start);
  111. };
  112. });