extract mouse event class

This commit is contained in:
Fabian Jakobs 2011-09-24 11:55:19 +00:00
commit d0ced941ef
3 changed files with 173 additions and 84 deletions

View file

@ -42,6 +42,7 @@ define(function(require, exports, module) {
var event = require("pilot/event");
var dom = require("pilot/dom");
var EventEmitter = require("pilot/event_emitter").EventEmitter;
var BrowserFocus = require("pilot/browser_focus").BrowserFocus;
var STATE_UNKNOWN = 0;
var STATE_SELECT = 1;
@ -50,12 +51,10 @@ var STATE_DRAG = 2;
var DRAG_TIMER = 250; // milliseconds
var DRAG_OFFSET = 5; // pixels
function calcDistance(ax, ay, bx, by) {
return Math.sqrt(Math.pow(bx - ax, 2) + Math.pow(by - ay, 2));
}
function DefaultHandlers(editor) {
this.editor = editor;
this.$clickSelection = null;
this.browserFocus = new BrowserFocus();
editor.setDefaultHandler("mousedown", this.onMouseDown.bind(this));
editor.setDefaultHandler("dblclick", this.onDoubleClick.bind(this));
@ -67,12 +66,12 @@ function DefaultHandlers(editor) {
(function() {
this.onMouseDown = function(ev) {
var e = ev.DOMEvent;
var inSelection = ev.inSelection;
var inSelection = ev.inSelection();
var pageX = ev.pageX;
var pageY = ev.pageY
var pos = ev.pos;
var pageY = ev.pageY;
var pos = ev.getDocumentPosition();
var editor = this.editor;
var _self = this;
var selectionRange = editor.getSelectionRange();
var selectionEmpty = selectionRange.isEmpty();
@ -82,8 +81,8 @@ function DefaultHandlers(editor) {
// selection
if (
inSelection && (
!editor.$mouseHandler.browserFocus.isFocused()
|| new Date().getTime() - editor.$mouseHandler.browserFocus.lastFocus < 20
!this.browserFocus.isFocused()
|| new Date().getTime() - this.browserFocus.lastFocus < 20
|| !editor.isFocused()
)
) {
@ -91,7 +90,7 @@ function DefaultHandlers(editor) {
return;
}
var button = event.getButton(e);
var button = ev.getButton();
if (button !== 0) {
if (selectionEmpty) {
editor.moveCursorToPosition(pos);
@ -134,7 +133,7 @@ function DefaultHandlers(editor) {
else if (state == STATE_DRAG)
onMouseDragSelectionEnd();
editor.$mouseHandler.$clickSelection = null;
_self.$clickSelection = null;
state = STATE_UNKNOWN;
};
@ -199,11 +198,11 @@ function DefaultHandlers(editor) {
};
function onStartSelect(pos) {
if (e.shiftKey) {
if (ev.getShiftKey()) {
editor.selection.selectToPosition(pos);
}
else {
if (!editor.$mouseHandler.$clickSelection) {
if (!_self.$clickSelection) {
editor.moveCursorToPosition(pos);
editor.selection.clearSelection(pos.row, pos.column);
}
@ -216,16 +215,16 @@ function DefaultHandlers(editor) {
var cursor = editor.renderer.screenToTextCoordinates(mousePageX, mousePageY);
cursor.row = Math.max(0, Math.min(cursor.row, editor.session.getLength()-1));
if (editor.$mouseHandler.$clickSelection) {
if (editor.$mouseHandler.$clickSelection.contains(cursor.row, cursor.column)) {
editor.selection.setSelectionRange(editor.$mouseHandler.$clickSelection);
if (_self.$clickSelection) {
if (_self.$clickSelection.contains(cursor.row, cursor.column)) {
editor.selection.setSelectionRange(_self.$clickSelection);
}
else {
if (editor.$mouseHandler.$clickSelection.compare(cursor.row, cursor.column) == -1) {
anchor = editor.$mouseHandler.$clickSelection.end;
if (_self.$clickSelection.compare(cursor.row, cursor.column) == -1) {
anchor = _self.$clickSelection.end;
}
else {
anchor = editor.$mouseHandler.$clickSelection.start;
anchor = _self.$clickSelection.start;
}
editor.selection.setSelectionAnchor(anchor.row, anchor.column);
editor.selection.selectToPosition(cursor);
@ -248,12 +247,11 @@ function DefaultHandlers(editor) {
event.capture(editor.container, onMouseSelection, onMouseSelectionEnd);
var timerId = setInterval(onSelectionInterval, 20);
return event.preventDefault(e);
return ev.preventDefault();
};
this.onDoubleClick = function(ev) {
var e = ev.DOMEvent;
var pos = ev.pos;
var pos = ev.getDocumentPosition();
var editor = this.editor;
// If the user dclicked on a fold, then expand it.
@ -264,34 +262,30 @@ function DefaultHandlers(editor) {
else {
editor.moveCursorToPosition(pos);
editor.selection.selectWord();
editor.$mouseHandler.$clickSelection = editor.getSelectionRange();
this.$clickSelection = editor.getSelectionRange();
}
};
this.onTripleClick = function(ev) {
var e = ev.DOMEvent;
var pos = ev.pos;
var pos = ev.getDocumentPosition();
var editor = this.editor;
editor.moveCursorToPosition(pos);
editor.selection.selectLine();
editor.$mouseHandler.$clickSelection = editor.getSelectionRange();
this.$clickSelection = editor.getSelectionRange();
};
this.onQuadClick = function(ev) {
var e = ev.DOMEvent;
var editor = this.editor;
editor.selectAll();
editor.$mouseHandler.$clickSelection = editor.getSelectionRange();
this.$clickSelection = editor.getSelectionRange();
};
this.onScroll = function(ev) {
var e = ev.DOMEvent;
var speed = ev.speed;
var editor = this.editor;
editor.renderer.scrollBy(e.wheelX * speed, e.wheelY * speed);
editor.renderer.scrollBy(e.wheelX * ev.speed, e.wheelY * speed);
if (editor.renderer.isScrollableBy(e.wheelX, e.wheelY))
return event.preventDefault(e);
};
@ -300,4 +294,8 @@ function DefaultHandlers(editor) {
exports.DefaultHandlers = DefaultHandlers;
function calcDistance(ax, ay, bx, by) {
return Math.sqrt(Math.pow(bx - ax, 2) + Math.pow(by - ay, 2));
}
});

View file

@ -0,0 +1,133 @@
/* vim:ts=4:sts=4:sw=4:
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Ajax.org Code Editor (ACE).
*
* The Initial Developer of the Original Code is
* Ajax.org B.V.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Fabian Jakobs <fabian AT ajax DOT org>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
var event = require("pilot/event");
var dom = require("pilot/dom");
/**
* Custom Ace mouse event
*/
var MouseEvent = exports.MouseEvent = function(domEvent, editor) {
this.domEvent = domEvent;
this.editor = editor;
this.pageX = event.getDocumentX(domEvent);
this.pageY = event.getDocumentY(domEvent);
this.$pos = null;
this.$inSelection = null;
this.propagationStopped = false;
this.defaultPrevented = false;
};
(function() {
this.stopPropagation = function() {
event.stopPropagation(this.domEvent);
this.propagationStopped = true;
};
this.preventDefault = function() {
event.preventDefault(this.domEvent);
this.defaultPrevented = true;
};
/**
* Get the document position below the mouse cursor
*
* @return {Object} 'row' and 'column' of the document position
*/
this.getDocumentPosition = function() {
if (this.$pos)
return this.$pos;
var pageX = event.getDocumentX(this.domEvent);
var pageY = event.getDocumentY(this.domEvent);
this.$pos = this.editor.renderer.screenToTextCoordinates(pageX, pageY);
this.$pos.row = Math.max(0, Math.min(this.$pos.row, this.editor.session.getLength()-1));
return this.$pos;
};
/**
* Check if the mouse cursor is inside of the text selection
*
* @return {Boolean} whether the mouse cursor is inside of the selection
*/
this.inSelection = function() {
if (this.$inSelection !== null)
return this.$inSelection;
var editor = this.editor;
if (editor.getReadOnly()) {
this.$inSelection = false;
}
else {
var selectionRange = editor.getSelectionRange();
if (selectionRange.isEmpty())
this.$inSelection = false;
else {
var pos = this.getDocumentPosition();
this.$inSelection = selectionRange.contains(pos.row, pos.column);
}
}
return this.$inSelection;
};
/**
* Get the clicked mouse button
*
* @return {Number} 0 for left button, 1 for middle button, 2 for right button
*/
this.getButton = function() {
return event.getButton(this.domEvent);
};
/**
* @return {Boolean} whether the shift key was pressed when the event was emitted
*/
this.getShiftKey = function() {
return this.domEvent.shiftKey;
};
}).call(MouseEvent.prototype);
});

View file

@ -40,13 +40,12 @@
define(function(require, exports, module) {
var event = require("pilot/event");
var BrowserFocus = require("pilot/browser_focus").BrowserFocus;
var DefaultHandlers = require("ace/mouse/default_handlers").DefaultHandlers;
var MouseEvent = require("ace/mouse/mouse_event").MouseEvent;
var MouseHandler = function(editor) {
this.editor = editor;
this.browserFocus = new BrowserFocus();
this.defaultHandlers = new DefaultHandlers(editor);
event.addListener(editor.container, "mousedown", function(e) {
editor.focus();
@ -76,77 +75,36 @@ var MouseHandler = function(editor) {
return this.$scrollSpeed;
};
this.$getEventPosition = function(e) {
var pageX = event.getDocumentX(e);
var pageY = event.getDocumentY(e);
var pos = this.editor.renderer.screenToTextCoordinates(pageX, pageY);
pos.row = Math.max(0, Math.min(pos.row, this.editor.session.getLength()-1));
return pos;
};
this.onMouseDown = function(e) {
var pageX = event.getDocumentX(e);
var pageY = event.getDocumentY(e);
var pos = this.$getEventPosition(e);
var editor = this.editor;
var self = this;
var selectionRange = editor.getSelectionRange();
var selectionEmpty = selectionRange.isEmpty();
var inSelection = !editor.getReadOnly()
&& !selectionEmpty
&& selectionRange.contains(pos.row, pos.column);
this.editor._dispatchEvent("mousedown", {
DOMEvent: e,
pageX: pageX,
pageY: pageY,
pos: pos,
inSelection: inSelection
});
this.editor._dispatchEvent("mousedown", new MouseEvent(e, this.editor));
};
this.onMouseMove = function(e) {
// optimization, because mousemove doesn't have a default handler.
var listeners = this.editor._eventRegistry["mousemove"];
var listeners = this.editor._eventRegistry && this.editor._eventRegistry["mousemove"];
if (!listeners || !listeners.length)
return;
this.editor._dispatchEvent("mousemove", {
DOMEvent: e,
pos: this.$getEventPosition(e)
});
this.editor._dispatchEvent("mousemove", new MouseEvent(e, this.editor));
};
this.onMouseDoubleClick = function(e) {
this.editor._dispatchEvent("dblclick", {
DOMEvent: e,
pos: this.$getEventPosition(e)
});
this.editor._dispatchEvent("dblclick", new MouseEvent(e, this.editor));
};
this.onMouseTripleClick = function(e) {
this.editor._dispatchEvent("tripleclick", {
DOMEvent: e,
pos: this.$getEventPosition(e)
});
this.editor._dispatchEvent("tripleclick", new MouseEvent(e, this.editor));
};
this.onMouseQuadClick = function(e) {
this.editor._dispatchEvent("quadclick", {
DOMEvent: e,
pos: this.$getEventPosition(e)
});
this.editor._dispatchEvent("quadclick", new MouseEvent(e, this.editor));
};
this.onMouseWheel = function(e) {
var speed = this.$scrollSpeed * 2;
var mouseEvent = new MouseEvent(e, this.editor);
mouseEvent.speed = this.$scrollSpeed * 2;
this.editor._dispatchEvent("scroll", {
DOMEvent: e,
speed: speed
});
this.editor._dispatchEvent("quadclick", mouseEvent);
};
}).call(MouseHandler.prototype);