mirror of
https://github.com/nasa/openmct.git
synced 2025-06-18 07:08:12 +00:00
[Fixed Position] Keep elements in view
Prevent elements from being positioned at negative x/y locations in a fixed position view, WTD-882.
This commit is contained in:
@ -8,14 +8,26 @@ define(
|
|||||||
/**
|
/**
|
||||||
* Utility function for creating getter-setter functions,
|
* Utility function for creating getter-setter functions,
|
||||||
* since these are frequently useful for element proxies.
|
* since these are frequently useful for element proxies.
|
||||||
|
*
|
||||||
|
* An optional third argument may be supplied in order to
|
||||||
|
* constrain or modify arguments when using as a setter;
|
||||||
|
* this argument is a function which takes two arguments
|
||||||
|
* (the current value for the property, and the requested
|
||||||
|
* new value.) This is useful when values need to be kept
|
||||||
|
* in certain ranges; specifically, to keep x/y positions
|
||||||
|
* non-negative in a fixed position view.
|
||||||
|
*
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {Object} object the object to get/set values upon
|
* @param {Object} object the object to get/set values upon
|
||||||
* @param {string} key the property to get/set
|
* @param {string} key the property to get/set
|
||||||
|
* @param {function} [updater] function used to process updates
|
||||||
*/
|
*/
|
||||||
function AccessorMutator(object, key) {
|
function AccessorMutator(object, key, updater) {
|
||||||
return function (value) {
|
return function (value) {
|
||||||
if (arguments.length > 0) {
|
if (arguments.length > 0) {
|
||||||
object[key] = value;
|
object[key] = updater ?
|
||||||
|
updater(value, object[key]) :
|
||||||
|
value;
|
||||||
}
|
}
|
||||||
return object[key];
|
return object[key];
|
||||||
};
|
};
|
||||||
|
@ -13,6 +13,11 @@ define(
|
|||||||
bottom: Number.NEGATIVE_INFINITY
|
bottom: Number.NEGATIVE_INFINITY
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Ensure a value is non-negative (for x/y setters)
|
||||||
|
function clamp(value) {
|
||||||
|
return Math.max(value, 0);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract superclass for other classes which provide useful
|
* Abstract superclass for other classes which provide useful
|
||||||
* interfaces upon an elements in a fixed position view.
|
* interfaces upon an elements in a fixed position view.
|
||||||
@ -42,14 +47,14 @@ define(
|
|||||||
* @param {number} [x] the new x position (if setting)
|
* @param {number} [x] the new x position (if setting)
|
||||||
* @returns {number} the x position
|
* @returns {number} the x position
|
||||||
*/
|
*/
|
||||||
x: new AccessorMutator(element, 'x'),
|
x: new AccessorMutator(element, 'x', clamp),
|
||||||
/**
|
/**
|
||||||
* Get and/or set the y position of this element.
|
* Get and/or set the y position of this element.
|
||||||
* Units are in fixed position grid space.
|
* Units are in fixed position grid space.
|
||||||
* @param {number} [y] the new y position (if setting)
|
* @param {number} [y] the new y position (if setting)
|
||||||
* @returns {number} the y position
|
* @returns {number} the y position
|
||||||
*/
|
*/
|
||||||
y: new AccessorMutator(element, 'y'),
|
y: new AccessorMutator(element, 'y', clamp),
|
||||||
/**
|
/**
|
||||||
* Get and/or set the stroke color of this element.
|
* Get and/or set the stroke color of this element.
|
||||||
* @param {string} [stroke] the new stroke color (if setting)
|
* @param {string} [stroke] the new stroke color (if setting)
|
||||||
|
@ -24,7 +24,7 @@ define(
|
|||||||
*/
|
*/
|
||||||
proxy.x = function (v) {
|
proxy.x = function (v) {
|
||||||
var x = Math.min(element.x, element.x2),
|
var x = Math.min(element.x, element.x2),
|
||||||
delta = v - x;
|
delta = Math.max(v, 0) - x;
|
||||||
if (arguments.length > 0 && delta) {
|
if (arguments.length > 0 && delta) {
|
||||||
element.x += delta;
|
element.x += delta;
|
||||||
element.x2 += delta;
|
element.x2 += delta;
|
||||||
@ -39,7 +39,7 @@ define(
|
|||||||
*/
|
*/
|
||||||
proxy.y = function (v) {
|
proxy.y = function (v) {
|
||||||
var y = Math.min(element.y, element.y2),
|
var y = Math.min(element.y, element.y2),
|
||||||
delta = v - y;
|
delta = Math.max(v, 0) - y;
|
||||||
if (arguments.length > 0 && delta) {
|
if (arguments.length > 0 && delta) {
|
||||||
element.y += delta;
|
element.y += delta;
|
||||||
element.y2 += delta;
|
element.y2 += delta;
|
||||||
|
Reference in New Issue
Block a user