GUACAMOLE-1204: Extract Guacamole.Position base class from Guacamole.Mouse.State.

This commit is contained in:
Michael Jumper
2021-02-06 22:56:54 -08:00
parent 156a19967a
commit 7d53fe4f09
2 changed files with 166 additions and 78 deletions

View File

@@ -65,10 +65,7 @@ Guacamole.Mouse = function(element) {
* *
* @type {Guacamole.Mouse.State} * @type {Guacamole.Mouse.State}
*/ */
this.currentState = new Guacamole.Mouse.State( this.currentState = new Guacamole.Mouse.State();
0, 0,
false, false, false, false, false
);
/** /**
* Fired whenever the user presses a mouse button down over the element * Fired whenever the user presses a mouse button down over the element
@@ -395,109 +392,114 @@ Guacamole.Mouse = function(element) {
}; };
/** /**
* Simple container for properties describing the state of a mouse. * The current state of a mouse, including position and buttons.
* *
* @constructor * @constructor
* @param {Number} x The X position of the mouse pointer in pixels. * @augments Guacamole.Position
* @param {Number} y The Y position of the mouse pointer in pixels. * @param {Guacamole.Mouse.State|Object} [template={}]
* @param {Boolean} left Whether the left mouse button is pressed. * The object whose properties should be copied within the new
* @param {Boolean} middle Whether the middle mouse button is pressed. * Guacamole.Mouse.State.
* @param {Boolean} right Whether the right mouse button is pressed.
* @param {Boolean} up Whether the up mouse button is pressed (the fourth
* button, usually part of a scroll wheel).
* @param {Boolean} down Whether the down mouse button is pressed (the fifth
* button, usually part of a scroll wheel).
*/ */
Guacamole.Mouse.State = function(x, y, left, middle, right, up, down) { Guacamole.Mouse.State = function State(template) {
/** /**
* Reference to this Guacamole.Mouse.State. * Returns the template object that would be provided to the
* Guacamole.Mouse.State constructor to produce a new Guacamole.Mouse.State
* object with the properties specified. The order and type of arguments
* used by this function are identical to those accepted by the
* Guacamole.Mouse.State constructor of Apache Guacamole 1.3.0 and older.
*
* @private * @private
* @param {Number} x
* The X position of the mouse pointer in pixels.
*
* @param {Number} y
* The Y position of the mouse pointer in pixels.
*
* @param {Boolean} left
* Whether the left mouse button is pressed.
*
* @param {Boolean} middle
* Whether the middle mouse button is pressed.
*
* @param {Boolean} right
* Whether the right mouse button is pressed.
*
* @param {Boolean} up
* Whether the up mouse button is pressed (the fourth button, usually
* part of a scroll wheel).
*
* @param {Boolean} down
* Whether the down mouse button is pressed (the fifth button, usually
* part of a scroll wheel).
*
* @return {Object}
* The equivalent template object that would be passed to the new
* Guacamole.Mouse.State constructor.
*/ */
var guac_state = this; var legacyConstructor = function legacyConstructor(x, y, left, middle, right, up, down) {
return {
x : x,
y : y,
left : left,
middle : middle,
right : right,
up : up,
down : down
};
};
/** // Accept old-style constructor, as well
* The current X position of the mouse pointer. if (arguments.length > 1)
* @type {Number} template = legacyConstructor.apply(this, arguments);
*/ else
this.x = x; template = template || {};
/** Guacamole.Position.call(this, template);
* The current Y position of the mouse pointer.
* @type {Number}
*/
this.y = y;
/** /**
* Whether the left mouse button is currently pressed. * Whether the left mouse button is currently pressed.
*
* @type {Boolean} * @type {Boolean}
* @default false
*/ */
this.left = left; this.left = template.left || false;
/** /**
* Whether the middle mouse button is currently pressed. * Whether the middle mouse button is currently pressed.
*
* @type {Boolean} * @type {Boolean}
* @default false
*/ */
this.middle = middle; this.middle = template.middle || false;
/** /**
* Whether the right mouse button is currently pressed. * Whether the right mouse button is currently pressed.
*
* @type {Boolean} * @type {Boolean}
* @default false
*/ */
this.right = right; this.right = template.right || false;
/** /**
* Whether the up mouse button is currently pressed. This is the fourth * Whether the up mouse button is currently pressed. This is the fourth
* mouse button, associated with upward scrolling of the mouse scroll * mouse button, associated with upward scrolling of the mouse scroll
* wheel. * wheel.
*
* @type {Boolean} * @type {Boolean}
* @default false
*/ */
this.up = up; this.up = template.up || false;
/** /**
* Whether the down mouse button is currently pressed. This is the fifth * Whether the down mouse button is currently pressed. This is the fifth
* mouse button, associated with downward scrolling of the mouse scroll * mouse button, associated with downward scrolling of the mouse scroll
* wheel. * wheel.
* @type {Boolean}
*/
this.down = down;
/**
* Updates the position represented within this state object by the given
* element and clientX/clientY coordinates (commonly available within event
* objects). Position is translated from clientX/clientY (relative to
* viewport) to element-relative coordinates.
* *
* @param {Element} element The element the coordinates should be relative * @type {Boolean}
* to. * @default false
* @param {Number} clientX The X coordinate to translate, viewport-relative.
* @param {Number} clientY The Y coordinate to translate, viewport-relative.
*/ */
this.fromClientPosition = function(element, clientX, clientY) { this.down = template.down || false;
guac_state.x = clientX - element.offsetLeft;
guac_state.y = clientY - element.offsetTop;
// This is all JUST so we can get the mouse position within the element
var parent = element.offsetParent;
while (parent && !(parent === document.body)) {
guac_state.x -= parent.offsetLeft - parent.scrollLeft;
guac_state.y -= parent.offsetTop - parent.scrollTop;
parent = parent.offsetParent;
}
// Element ultimately depends on positioning within document body,
// take document scroll into account.
if (parent) {
var documentScrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
var documentScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
guac_state.x -= parent.offsetLeft - documentScrollLeft;
guac_state.y -= parent.offsetTop - documentScrollTop;
}
};
}; };
@@ -543,10 +545,7 @@ Guacamole.Mouse.Touchpad = function(element) {
* *
* @type {Guacamole.Mouse.State} * @type {Guacamole.Mouse.State}
*/ */
this.currentState = new Guacamole.Mouse.State( this.currentState = new Guacamole.Mouse.State();
0, 0,
false, false, false, false, false
);
/** /**
* Fired whenever a mouse button is effectively pressed. This can happen * Fired whenever a mouse button is effectively pressed. This can happen
@@ -850,10 +849,7 @@ Guacamole.Mouse.Touchscreen = function(element) {
* *
* @type {Guacamole.Mouse.State} * @type {Guacamole.Mouse.State}
*/ */
this.currentState = new Guacamole.Mouse.State( this.currentState = new Guacamole.Mouse.State();
0, 0,
false, false, false, false, false
);
/** /**
* Fired whenever a mouse button is effectively pressed. This can happen * Fired whenever a mouse button is effectively pressed. This can happen

View File

@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var Guacamole = Guacamole || {};
/**
* A position in 2-D space.
*
* @constructor
* @param {Guacamole.Position|Object} [template={}]
* The object whose properties should be copied within the new
* Guacamole.Position.
*/
Guacamole.Position = function Position(template) {
template = template || {};
/**
* The current X position, in pixels.
*
* @type {Number}
* @default 0
*/
this.x = template.x || 0;
/**
* The current Y position, in pixels.
*
* @type {Number}
* @default 0
*/
this.y = template.y || 0;
/**
* Assigns the position represented by the given element and
* clientX/clientY coordinates. The clientX and clientY coordinates are
* relative to the browser viewport and are commonly available within
* JavaScript event objects. The final position is translated to
* coordinates that are relative the given element.
*
* @param {Element} element
* The element the coordinates should be relative to.
*
* @param {Number} clientX
* The viewport-relative X coordinate to translate.
*
* @param {Number} clientY
* The viewport-relative Y coordinate to translate.
*/
this.fromClientPosition = function fromClientPosition(element, clientX, clientY) {
this.x = clientX - element.offsetLeft;
this.y = clientY - element.offsetTop;
// This is all JUST so we can get the position within the element
var parent = element.offsetParent;
while (parent && !(parent === document.body)) {
this.x -= parent.offsetLeft - parent.scrollLeft;
this.y -= parent.offsetTop - parent.scrollTop;
parent = parent.offsetParent;
}
// Element ultimately depends on positioning within document body,
// take document scroll into account.
if (parent) {
var documentScrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
var documentScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
this.x -= parent.offsetLeft - documentScrollLeft;
this.y -= parent.offsetTop - documentScrollTop;
}
};
};