default_handlers.js 11 KB

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