mouse_handler.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var event = require("../lib/event");
  33. var useragent = require("../lib/useragent");
  34. var DefaultHandlers = require("./default_handlers").DefaultHandlers;
  35. var DefaultGutterHandler = require("./default_gutter_handler").GutterHandler;
  36. var MouseEvent = require("./mouse_event").MouseEvent;
  37. var DragdropHandler = require("./dragdrop_handler").DragdropHandler;
  38. var config = require("../config");
  39. var MouseHandler = function(editor) {
  40. this.editor = editor;
  41. new DefaultHandlers(this);
  42. new DefaultGutterHandler(this);
  43. new DragdropHandler(this);
  44. var mouseTarget = editor.renderer.getMouseEventTarget();
  45. event.addListener(mouseTarget, "click", this.onMouseEvent.bind(this, "click"));
  46. event.addListener(mouseTarget, "mousemove", this.onMouseMove.bind(this, "mousemove"));
  47. event.addMultiMouseDownListener(mouseTarget, [300, 300, 250], this, "onMouseEvent");
  48. if (editor.renderer.scrollBarV) {
  49. event.addMultiMouseDownListener(editor.renderer.scrollBarV.inner, [300, 300, 250], this, "onMouseEvent");
  50. event.addMultiMouseDownListener(editor.renderer.scrollBarH.inner, [300, 300, 250], this, "onMouseEvent");
  51. }
  52. event.addMouseWheelListener(editor.container, this.onMouseWheel.bind(this, "mousewheel"));
  53. var gutterEl = editor.renderer.$gutter;
  54. event.addListener(gutterEl, "mousedown", this.onMouseEvent.bind(this, "guttermousedown"));
  55. event.addListener(gutterEl, "click", this.onMouseEvent.bind(this, "gutterclick"));
  56. event.addListener(gutterEl, "dblclick", this.onMouseEvent.bind(this, "gutterdblclick"));
  57. event.addListener(gutterEl, "mousemove", this.onMouseEvent.bind(this, "guttermousemove"));
  58. event.addListener(mouseTarget, "mousedown", function(e) {
  59. editor.focus();
  60. });
  61. event.addListener(gutterEl, "mousedown", function(e) {
  62. editor.focus();
  63. return event.preventDefault(e);
  64. });
  65. };
  66. (function() {
  67. this.onMouseEvent = function(name, e) {
  68. this.editor._emit(name, new MouseEvent(e, this.editor));
  69. };
  70. this.onMouseMove = function(name, e) {
  71. // optimization, because mousemove doesn't have a default handler.
  72. var listeners = this.editor._eventRegistry && this.editor._eventRegistry.mousemove;
  73. if (!listeners || !listeners.length)
  74. return;
  75. this.editor._emit(name, new MouseEvent(e, this.editor));
  76. };
  77. this.onMouseWheel = function(name, e) {
  78. var mouseEvent = new MouseEvent(e, this.editor);
  79. mouseEvent.speed = this.$scrollSpeed * 2;
  80. mouseEvent.wheelX = e.wheelX;
  81. mouseEvent.wheelY = e.wheelY;
  82. this.editor._emit(name, mouseEvent);
  83. };
  84. this.setState = function(state) {
  85. this.state = state;
  86. };
  87. this.captureMouse = function(ev, mouseMoveHandler) {
  88. this.x = ev.x;
  89. this.y = ev.y;
  90. this.isMousePressed = true;
  91. // do not move textarea during selection
  92. var renderer = this.editor.renderer;
  93. if (renderer.$keepTextAreaAtCursor)
  94. renderer.$keepTextAreaAtCursor = null;
  95. var self = this;
  96. var onMouseMove = function(e) {
  97. self.x = e.clientX;
  98. self.y = e.clientY;
  99. mouseMoveHandler && mouseMoveHandler(e);
  100. self.mouseEvent = new MouseEvent(e, self.editor);
  101. self.$mouseMoved = true;
  102. };
  103. var onCaptureEnd = function(e) {
  104. clearInterval(timerId);
  105. onCaptureInterval();
  106. self[self.state + "End"] && self[self.state + "End"](e);
  107. self.$clickSelection = null;
  108. if (renderer.$keepTextAreaAtCursor == null) {
  109. renderer.$keepTextAreaAtCursor = true;
  110. renderer.$moveTextAreaToCursor();
  111. }
  112. self.isMousePressed = false;
  113. self.$onCaptureMouseMove = self.releaseMouse = null;
  114. self.onMouseEvent("mouseup", e);
  115. };
  116. var onCaptureInterval = function() {
  117. self[self.state] && self[self.state]();
  118. self.$mouseMoved = false;
  119. };
  120. if (useragent.isOldIE && ev.domEvent.type == "dblclick") {
  121. return setTimeout(function() {onCaptureEnd(ev);});
  122. }
  123. self.$onCaptureMouseMove = onMouseMove;
  124. self.releaseMouse = event.capture(this.editor.container, onMouseMove, onCaptureEnd);
  125. var timerId = setInterval(onCaptureInterval, 20);
  126. };
  127. this.releaseMouse = null;
  128. }).call(MouseHandler.prototype);
  129. config.defineOptions(MouseHandler.prototype, "mouseHandler", {
  130. scrollSpeed: {initialValue: 2},
  131. dragDelay: {initialValue: 150},
  132. dragEnabled: {initialValue: true},
  133. focusTimout: {initialValue: 0}
  134. });
  135. exports.MouseHandler = MouseHandler;
  136. });