default_handlers.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. var exports = ["select", "startSelect", "selectEnd", "selectAllEnd", "selectByWordsEnd",
  45. "selectByLinesEnd", "dragWait", "dragWaitEnd", "focusWait"];
  46. exports.forEach(function(x) {
  47. mouseHandler[x] = this[x];
  48. }, this);
  49. mouseHandler.selectByLines = this.extendSelectionBy.bind(mouseHandler, "getLineRange");
  50. mouseHandler.selectByWords = this.extendSelectionBy.bind(mouseHandler, "getWordRange");
  51. }
  52. (function() {
  53. this.onMouseDown = function(ev) {
  54. var inSelection = ev.inSelection();
  55. var pos = ev.getDocumentPosition();
  56. this.mousedownEvent = ev;
  57. var editor = this.editor;
  58. var button = ev.getButton();
  59. if (button !== 0) {
  60. var selectionRange = editor.getSelectionRange();
  61. var selectionEmpty = selectionRange.isEmpty();
  62. if (selectionEmpty) {
  63. editor.moveCursorToPosition(pos);
  64. editor.selection.clearSelection();
  65. }
  66. // 2: contextmenu, 1: linux paste
  67. editor.textInput.onContextMenu(ev.domEvent);
  68. return; // stopping event here breaks contextmenu on ff mac
  69. }
  70. // if this click caused the editor to be focused should not clear the
  71. // selection
  72. if (inSelection && !editor.isFocused()) {
  73. editor.focus();
  74. if (this.$focusTimout && !this.$clickSelection && !editor.inMultiSelectMode) {
  75. this.mousedownEvent.time = (new Date()).getTime();
  76. this.setState("focusWait");
  77. this.captureMouse(ev);
  78. return;
  79. }
  80. }
  81. if (!inSelection || this.$clickSelection || ev.getShiftKey() || editor.inMultiSelectMode) {
  82. // Directly pick STATE_SELECT, since the user is not clicking inside
  83. // a selection.
  84. this.startSelect(pos);
  85. } else if (inSelection) {
  86. this.mousedownEvent.time = (new Date()).getTime();
  87. this.startSelect(pos);
  88. }
  89. this.captureMouse(ev);
  90. return ev.preventDefault();
  91. };
  92. this.startSelect = function(pos) {
  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. var shiftPressed = this.mousedownEvent.getShiftKey();
  97. setTimeout(function(){
  98. if (shiftPressed) {
  99. editor.selection.selectToPosition(pos);
  100. }
  101. else if (!this.$clickSelection) {
  102. editor.moveCursorToPosition(pos);
  103. editor.selection.clearSelection();
  104. }
  105. }.bind(this), 0);
  106. if (editor.renderer.scroller.setCapture) {
  107. editor.renderer.scroller.setCapture();
  108. }
  109. editor.setStyle("ace_selecting");
  110. this.setState("select");
  111. };
  112. this.select = function() {
  113. var anchor, editor = this.editor;
  114. var cursor = editor.renderer.screenToTextCoordinates(this.x, this.y);
  115. if (this.$clickSelection) {
  116. var cmp = this.$clickSelection.comparePoint(cursor);
  117. if (cmp == -1) {
  118. anchor = this.$clickSelection.end;
  119. } else if (cmp == 1) {
  120. anchor = this.$clickSelection.start;
  121. } else {
  122. var orientedRange = calcRangeOrientation(this.$clickSelection, cursor);
  123. cursor = orientedRange.cursor;
  124. anchor = orientedRange.anchor;
  125. }
  126. editor.selection.setSelectionAnchor(anchor.row, anchor.column);
  127. }
  128. editor.selection.selectToPosition(cursor);
  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. if (this.$clickSelection) {
  136. var cmpStart = this.$clickSelection.comparePoint(range.start);
  137. var cmpEnd = this.$clickSelection.comparePoint(range.end);
  138. if (cmpStart == -1 && cmpEnd <= 0) {
  139. anchor = this.$clickSelection.end;
  140. if (range.end.row != cursor.row || range.end.column != cursor.column)
  141. cursor = range.start;
  142. } else if (cmpEnd == 1 && cmpStart >= 0) {
  143. anchor = this.$clickSelection.start;
  144. if (range.start.row != cursor.row || range.start.column != cursor.column)
  145. cursor = range.end;
  146. } else if (cmpStart == -1 && cmpEnd == 1) {
  147. cursor = range.end;
  148. anchor = range.start;
  149. } else {
  150. var orientedRange = calcRangeOrientation(this.$clickSelection, cursor);
  151. cursor = orientedRange.cursor;
  152. anchor = orientedRange.anchor;
  153. }
  154. editor.selection.setSelectionAnchor(anchor.row, anchor.column);
  155. }
  156. editor.selection.selectToPosition(cursor);
  157. editor.renderer.scrollCursorIntoView();
  158. };
  159. this.selectEnd =
  160. this.selectAllEnd =
  161. this.selectByWordsEnd =
  162. this.selectByLinesEnd = function() {
  163. this.editor.unsetStyle("ace_selecting");
  164. if (this.editor.renderer.scroller.releaseCapture) {
  165. this.editor.renderer.scroller.releaseCapture();
  166. }
  167. };
  168. this.focusWait = function() {
  169. var distance = calcDistance(this.mousedownEvent.x, this.mousedownEvent.y, this.x, this.y);
  170. var time = (new Date()).getTime();
  171. if (distance > DRAG_OFFSET || time - this.mousedownEvent.time > this.$focusTimout)
  172. this.startSelect(this.mousedownEvent.getDocumentPosition());
  173. };
  174. this.onDoubleClick = function(ev) {
  175. var pos = ev.getDocumentPosition();
  176. var editor = this.editor;
  177. var session = editor.session;
  178. var range = session.getBracketRange(pos);
  179. if (range) {
  180. if (range.isEmpty()) {
  181. range.start.column--;
  182. range.end.column++;
  183. }
  184. this.$clickSelection = range;
  185. this.setState("select");
  186. return;
  187. }
  188. this.$clickSelection = editor.selection.getWordRange(pos.row, pos.column);
  189. this.setState("selectByWords");
  190. };
  191. this.onTripleClick = function(ev) {
  192. var pos = ev.getDocumentPosition();
  193. var editor = this.editor;
  194. this.setState("selectByLines");
  195. this.$clickSelection = editor.selection.getLineRange(pos.row);
  196. };
  197. this.onQuadClick = function(ev) {
  198. var editor = this.editor;
  199. editor.selectAll();
  200. this.$clickSelection = editor.getSelectionRange();
  201. this.setState("selectAll");
  202. };
  203. this.onMouseWheel = function(ev) {
  204. if (ev.getShiftKey() || ev.getAccelKey())
  205. return;
  206. var t = ev.domEvent.timeStamp;
  207. var dt = t - (this.$lastScrollTime||0);
  208. var editor = this.editor;
  209. var isScrolable = editor.renderer.isScrollableBy(ev.wheelX * ev.speed, ev.wheelY * ev.speed);
  210. if (isScrolable || dt < 200) {
  211. this.$lastScrollTime = t;
  212. editor.renderer.scrollBy(ev.wheelX * ev.speed, ev.wheelY * ev.speed);
  213. return ev.stop();
  214. }
  215. };
  216. }).call(DefaultHandlers.prototype);
  217. exports.DefaultHandlers = DefaultHandlers;
  218. function calcDistance(ax, ay, bx, by) {
  219. return Math.sqrt(Math.pow(bx - ax, 2) + Math.pow(by - ay, 2));
  220. }
  221. function calcRangeOrientation(range, cursor) {
  222. if (range.start.row == range.end.row)
  223. var cmp = 2 * cursor.column - range.start.column - range.end.column;
  224. else if (range.start.row == range.end.row - 1 && !range.start.column && !range.end.column)
  225. var cmp = cursor.column - 4;
  226. else
  227. var cmp = 2 * cursor.row - range.start.row - range.end.row;
  228. if (cmp < 0)
  229. return {cursor: range.start, anchor: range.end};
  230. else
  231. return {cursor: range.end, anchor: range.start};
  232. }
  233. });