mouse_handler.js 8.5 KB

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