mouse_handler.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. var _self = this;
  41. this.editor = editor;
  42. new DefaultHandlers(this);
  43. new DefaultGutterHandler(this);
  44. new DragdropHandler(this);
  45. var focusEditor = function(e) {
  46. // because we have to call event.preventDefault() any window on ie and iframes
  47. // on other browsers do not get focus, so we have to call window.focus() here
  48. if (!document.hasFocus || !document.hasFocus())
  49. window.focus();
  50. editor.focus();
  51. };
  52. var mouseTarget = editor.renderer.getMouseEventTarget();
  53. event.addListener(mouseTarget, "click", this.onMouseEvent.bind(this, "click"));
  54. event.addListener(mouseTarget, "mousemove", this.onMouseMove.bind(this, "mousemove"));
  55. event.addMultiMouseDownListener(mouseTarget, [400, 300, 250], this, "onMouseEvent");
  56. if (editor.renderer.scrollBarV) {
  57. event.addMultiMouseDownListener(editor.renderer.scrollBarV.inner, [400, 300, 250], this, "onMouseEvent");
  58. event.addMultiMouseDownListener(editor.renderer.scrollBarH.inner, [400, 300, 250], this, "onMouseEvent");
  59. if (useragent.isIE) {
  60. event.addListener(editor.renderer.scrollBarV.element, "mousedown", focusEditor);
  61. event.addListener(editor.renderer.scrollBarH.element, "mousedown", focusEditor);
  62. }
  63. }
  64. event.addMouseWheelListener(editor.container, this.onMouseWheel.bind(this, "mousewheel"));
  65. event.addTouchMoveListener(editor.container, this.onTouchMove.bind(this, "touchmove"));
  66. var gutterEl = editor.renderer.$gutter;
  67. event.addListener(gutterEl, "mousedown", this.onMouseEvent.bind(this, "guttermousedown"));
  68. event.addListener(gutterEl, "click", this.onMouseEvent.bind(this, "gutterclick"));
  69. event.addListener(gutterEl, "dblclick", this.onMouseEvent.bind(this, "gutterdblclick"));
  70. event.addListener(gutterEl, "mousemove", this.onMouseEvent.bind(this, "guttermousemove"));
  71. event.addListener(mouseTarget, "mousedown", focusEditor);
  72. event.addListener(gutterEl, "mousedown", function(e) {
  73. editor.focus();
  74. return event.preventDefault(e);
  75. });
  76. editor.on("mousemove", function(e){
  77. if (_self.state || _self.$dragDelay || !_self.$dragEnabled)
  78. return;
  79. var character = editor.renderer.screenToTextCoordinates(e.x, e.y);
  80. var range = editor.session.selection.getRange();
  81. var renderer = editor.renderer;
  82. if (!range.isEmpty() && range.insideStart(character.row, character.column)) {
  83. renderer.setCursorStyle("default");
  84. } else {
  85. renderer.setCursorStyle("");
  86. }
  87. });
  88. };
  89. (function() {
  90. this.onMouseEvent = function(name, e) {
  91. this.editor._emit(name, new MouseEvent(e, this.editor));
  92. };
  93. this.onMouseMove = function(name, e) {
  94. // optimization, because mousemove doesn't have a default handler.
  95. var listeners = this.editor._eventRegistry && this.editor._eventRegistry.mousemove;
  96. if (!listeners || !listeners.length)
  97. return;
  98. this.editor._emit(name, new MouseEvent(e, this.editor));
  99. };
  100. this.onMouseWheel = function(name, e) {
  101. var mouseEvent = new MouseEvent(e, this.editor);
  102. mouseEvent.speed = this.$scrollSpeed * 2;
  103. mouseEvent.wheelX = e.wheelX;
  104. mouseEvent.wheelY = e.wheelY;
  105. this.editor._emit(name, mouseEvent);
  106. };
  107. this.onTouchMove = function (name, e) {
  108. var mouseEvent = new MouseEvent(e, this.editor);
  109. mouseEvent.speed = 1;//this.$scrollSpeed * 2;
  110. mouseEvent.wheelX = e.wheelX;
  111. mouseEvent.wheelY = e.wheelY;
  112. this.editor._emit(name, mouseEvent);
  113. };
  114. this.setState = function(state) {
  115. this.state = state;
  116. };
  117. this.captureMouse = function(ev, mouseMoveHandler) {
  118. this.x = ev.x;
  119. this.y = ev.y;
  120. this.isMousePressed = true;
  121. // do not move textarea during selection
  122. var renderer = this.editor.renderer;
  123. if (renderer.$keepTextAreaAtCursor)
  124. renderer.$keepTextAreaAtCursor = null;
  125. var self = this;
  126. var onMouseMove = function(e) {
  127. if (!e) return;
  128. // if editor is loaded inside iframe, and mouseup event is outside
  129. // we won't recieve it, so we cancel on first mousemove without button
  130. if (useragent.isWebKit && !e.which && self.releaseMouse)
  131. return self.releaseMouse();
  132. self.x = e.clientX;
  133. self.y = e.clientY;
  134. mouseMoveHandler && mouseMoveHandler(e);
  135. self.mouseEvent = new MouseEvent(e, self.editor);
  136. self.$mouseMoved = true;
  137. };
  138. var onCaptureEnd = function(e) {
  139. clearInterval(timerId);
  140. onCaptureInterval();
  141. self[self.state + "End"] && self[self.state + "End"](e);
  142. self.state = "";
  143. if (renderer.$keepTextAreaAtCursor == null) {
  144. renderer.$keepTextAreaAtCursor = true;
  145. renderer.$moveTextAreaToCursor();
  146. }
  147. self.isMousePressed = false;
  148. self.$onCaptureMouseMove = self.releaseMouse = null;
  149. e && self.onMouseEvent("mouseup", e);
  150. };
  151. var onCaptureInterval = function() {
  152. self[self.state] && self[self.state]();
  153. self.$mouseMoved = false;
  154. };
  155. if (useragent.isOldIE && ev.domEvent.type == "dblclick") {
  156. return setTimeout(function() {onCaptureEnd(ev);});
  157. }
  158. self.$onCaptureMouseMove = onMouseMove;
  159. self.releaseMouse = event.capture(this.editor.container, onMouseMove, onCaptureEnd);
  160. var timerId = setInterval(onCaptureInterval, 20);
  161. };
  162. this.releaseMouse = null;
  163. this.cancelContextMenu = function() {
  164. var stop = function(e) {
  165. if (e && e.domEvent && e.domEvent.type != "contextmenu")
  166. return;
  167. this.editor.off("nativecontextmenu", stop);
  168. if (e && e.domEvent)
  169. event.stopEvent(e.domEvent);
  170. }.bind(this);
  171. setTimeout(stop, 10);
  172. this.editor.on("nativecontextmenu", stop);
  173. };
  174. }).call(MouseHandler.prototype);
  175. config.defineOptions(MouseHandler.prototype, "mouseHandler", {
  176. scrollSpeed: {initialValue: 2},
  177. dragDelay: {initialValue: (useragent.isMac ? 150 : 0)},
  178. dragEnabled: {initialValue: true},
  179. focusTimout: {initialValue: 0},
  180. tooltipFollowsMouse: {initialValue: true}
  181. });
  182. exports.MouseHandler = MouseHandler;
  183. });