default_handlers.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 dom = require("../lib/dom");
  33. var event = require("../lib/event");
  34. var useragent = require("../lib/useragent");
  35. var DRAG_OFFSET = 0; // pixels
  36. function DefaultHandlers(mouseHandler) {
  37. mouseHandler.$clickSelection = null;
  38. var editor = mouseHandler.editor;
  39. editor.setDefaultHandler("mousedown", this.onMouseDown.bind(mouseHandler));
  40. editor.setDefaultHandler("dblclick", this.onDoubleClick.bind(mouseHandler));
  41. editor.setDefaultHandler("tripleclick", this.onTripleClick.bind(mouseHandler));
  42. editor.setDefaultHandler("quadclick", this.onQuadClick.bind(mouseHandler));
  43. editor.setDefaultHandler("mousewheel", this.onMouseWheel.bind(mouseHandler));
  44. editor.setDefaultHandler("touchmove", this.onTouchMove.bind(mouseHandler));
  45. var exports = ["select", "startSelect", "selectEnd", "selectAllEnd", "selectByWordsEnd",
  46. "selectByLinesEnd", "dragWait", "dragWaitEnd", "focusWait"];
  47. exports.forEach(function(x) {
  48. mouseHandler[x] = this[x];
  49. }, this);
  50. mouseHandler.selectByLines = this.extendSelectionBy.bind(mouseHandler, "getLineRange");
  51. mouseHandler.selectByWords = this.extendSelectionBy.bind(mouseHandler, "getWordRange");
  52. }
  53. (function() {
  54. this.onMouseDown = function(ev) {
  55. var inSelection = ev.inSelection();
  56. var pos = ev.getDocumentPosition();
  57. this.mousedownEvent = ev;
  58. var editor = this.editor;
  59. var button = ev.getButton();
  60. if (button !== 0) {
  61. var selectionRange = editor.getSelectionRange();
  62. var selectionEmpty = selectionRange.isEmpty();
  63. editor.$blockScrolling++;
  64. if (selectionEmpty || button == 1)
  65. editor.selection.moveToPosition(pos);
  66. editor.$blockScrolling--;
  67. // 2: contextmenu, 1: linux paste
  68. if (button == 2) {
  69. editor.textInput.onContextMenu(ev.domEvent);
  70. if (!useragent.isMozilla)
  71. ev.preventDefault();
  72. }
  73. // stopping event here breaks contextmenu on ff mac
  74. // not stopping breaks it on chrome mac
  75. return;
  76. }
  77. this.mousedownEvent.time = Date.now();
  78. // if this click caused the editor to be focused should not clear the
  79. // selection
  80. if (inSelection && !editor.isFocused()) {
  81. editor.focus();
  82. if (this.$focusTimout && !this.$clickSelection && !editor.inMultiSelectMode) {
  83. this.setState("focusWait");
  84. this.captureMouse(ev);
  85. return;
  86. }
  87. }
  88. this.captureMouse(ev);
  89. this.startSelect(pos, ev.domEvent._clicks > 1);
  90. return ev.preventDefault();
  91. };
  92. this.startSelect = function(pos, waitForClickSelection) {
  93. pos = pos || this.editor.renderer.screenToTextCoordinates(this.x, this.y);
  94. var editor = this.editor;
  95. // allow double/triple click handlers to change selection
  96. editor.$blockScrolling++;
  97. if (this.mousedownEvent.getShiftKey())
  98. editor.selection.selectToPosition(pos);
  99. else if (!waitForClickSelection)
  100. editor.selection.moveToPosition(pos);
  101. if (!waitForClickSelection)
  102. this.select();
  103. if (editor.renderer.scroller.setCapture) {
  104. editor.renderer.scroller.setCapture();
  105. }
  106. editor.setStyle("ace_selecting");
  107. this.setState("select");
  108. editor.$blockScrolling--;
  109. };
  110. this.select = function() {
  111. var anchor, editor = this.editor;
  112. var cursor = editor.renderer.screenToTextCoordinates(this.x, this.y);
  113. editor.$blockScrolling++;
  114. if (this.$clickSelection) {
  115. var cmp = this.$clickSelection.comparePoint(cursor);
  116. if (cmp == -1) {
  117. anchor = this.$clickSelection.end;
  118. } else if (cmp == 1) {
  119. anchor = this.$clickSelection.start;
  120. } else {
  121. var orientedRange = calcRangeOrientation(this.$clickSelection, cursor);
  122. cursor = orientedRange.cursor;
  123. anchor = orientedRange.anchor;
  124. }
  125. editor.selection.setSelectionAnchor(anchor.row, anchor.column);
  126. }
  127. editor.selection.selectToPosition(cursor);
  128. editor.$blockScrolling--;
  129. editor.renderer.scrollCursorIntoView();
  130. };
  131. this.extendSelectionBy = function(unitName) {
  132. var anchor, editor = this.editor;
  133. var cursor = editor.renderer.screenToTextCoordinates(this.x, this.y);
  134. var range = editor.selection[unitName](cursor.row, cursor.column);
  135. editor.$blockScrolling++;
  136. if (this.$clickSelection) {
  137. var cmpStart = this.$clickSelection.comparePoint(range.start);
  138. var cmpEnd = this.$clickSelection.comparePoint(range.end);
  139. if (cmpStart == -1 && cmpEnd <= 0) {
  140. anchor = this.$clickSelection.end;
  141. if (range.end.row != cursor.row || range.end.column != cursor.column)
  142. cursor = range.start;
  143. } else if (cmpEnd == 1 && cmpStart >= 0) {
  144. anchor = this.$clickSelection.start;
  145. if (range.start.row != cursor.row || range.start.column != cursor.column)
  146. cursor = range.end;
  147. } else if (cmpStart == -1 && cmpEnd == 1) {
  148. cursor = range.end;
  149. anchor = range.start;
  150. } else {
  151. var orientedRange = calcRangeOrientation(this.$clickSelection, cursor);
  152. cursor = orientedRange.cursor;
  153. anchor = orientedRange.anchor;
  154. }
  155. editor.selection.setSelectionAnchor(anchor.row, anchor.column);
  156. }
  157. editor.selection.selectToPosition(cursor);
  158. editor.$blockScrolling--;
  159. editor.renderer.scrollCursorIntoView();
  160. };
  161. this.selectEnd =
  162. this.selectAllEnd =
  163. this.selectByWordsEnd =
  164. this.selectByLinesEnd = function() {
  165. this.$clickSelection = null;
  166. this.editor.unsetStyle("ace_selecting");
  167. if (this.editor.renderer.scroller.releaseCapture) {
  168. this.editor.renderer.scroller.releaseCapture();
  169. }
  170. };
  171. this.focusWait = function() {
  172. var distance = calcDistance(this.mousedownEvent.x, this.mousedownEvent.y, this.x, this.y);
  173. var time = Date.now();
  174. if (distance > DRAG_OFFSET || time - this.mousedownEvent.time > this.$focusTimout)
  175. this.startSelect(this.mousedownEvent.getDocumentPosition());
  176. };
  177. this.onDoubleClick = function(ev) {
  178. var pos = ev.getDocumentPosition();
  179. var editor = this.editor;
  180. var session = editor.session;
  181. var range = session.getBracketRange(pos);
  182. if (range) {
  183. if (range.isEmpty()) {
  184. range.start.column--;
  185. range.end.column++;
  186. }
  187. this.setState("select");
  188. } else {
  189. range = editor.selection.getWordRange(pos.row, pos.column);
  190. this.setState("selectByWords");
  191. }
  192. this.$clickSelection = range;
  193. this.select();
  194. };
  195. this.onTripleClick = function(ev) {
  196. var pos = ev.getDocumentPosition();
  197. var editor = this.editor;
  198. this.setState("selectByLines");
  199. var range = editor.getSelectionRange();
  200. if (range.isMultiLine() && range.contains(pos.row, pos.column)) {
  201. this.$clickSelection = editor.selection.getLineRange(range.start.row);
  202. this.$clickSelection.end = editor.selection.getLineRange(range.end.row).end;
  203. } else {
  204. this.$clickSelection = editor.selection.getLineRange(pos.row);
  205. }
  206. this.select();
  207. };
  208. this.onQuadClick = function(ev) {
  209. var editor = this.editor;
  210. editor.selectAll();
  211. this.$clickSelection = editor.getSelectionRange();
  212. this.setState("selectAll");
  213. };
  214. this.onMouseWheel = function(ev) {
  215. if (ev.getAccelKey())
  216. return;
  217. //shift wheel to horiz scroll
  218. if (ev.getShiftKey() && ev.wheelY && !ev.wheelX) {
  219. ev.wheelX = ev.wheelY;
  220. ev.wheelY = 0;
  221. }
  222. var t = ev.domEvent.timeStamp;
  223. var dt = t - (this.$lastScrollTime||0);
  224. var editor = this.editor;
  225. var isScrolable = editor.renderer.isScrollableBy(ev.wheelX * ev.speed, ev.wheelY * ev.speed);
  226. if (isScrolable || dt < 200) {
  227. this.$lastScrollTime = t;
  228. editor.renderer.scrollBy(ev.wheelX * ev.speed, ev.wheelY * ev.speed);
  229. return ev.stop();
  230. }
  231. };
  232. this.onTouchMove = function (ev) {
  233. var t = ev.domEvent.timeStamp;
  234. var dt = t - (this.$lastScrollTime || 0);
  235. var editor = this.editor;
  236. var isScrolable = editor.renderer.isScrollableBy(ev.wheelX * ev.speed, ev.wheelY * ev.speed);
  237. if (isScrolable || dt < 200) {
  238. this.$lastScrollTime = t;
  239. editor.renderer.scrollBy(ev.wheelX * ev.speed, ev.wheelY * ev.speed);
  240. return ev.stop();
  241. }
  242. };
  243. }).call(DefaultHandlers.prototype);
  244. exports.DefaultHandlers = DefaultHandlers;
  245. function calcDistance(ax, ay, bx, by) {
  246. return Math.sqrt(Math.pow(bx - ax, 2) + Math.pow(by - ay, 2));
  247. }
  248. function calcRangeOrientation(range, cursor) {
  249. if (range.start.row == range.end.row)
  250. var cmp = 2 * cursor.column - range.start.column - range.end.column;
  251. else if (range.start.row == range.end.row - 1 && !range.start.column && !range.end.column)
  252. var cmp = cursor.column - 4;
  253. else
  254. var cmp = 2 * cursor.row - range.start.row - range.end.row;
  255. if (cmp < 0)
  256. return {cursor: range.start, anchor: range.end};
  257. else
  258. return {cursor: range.end, anchor: range.start};
  259. }
  260. });