mirror of
https://github.com/nasa/openmct.git
synced 2025-06-17 14:48:13 +00:00
[Fixed Position] Add unit tests for new code
Refactored ElementProxy and UnitAccessorMutator slightly to improve encasulation. Added unit tests for UnitAccessorMutator
This commit is contained in:
@ -174,54 +174,6 @@ define(
|
||||
return this.resizeHandles;
|
||||
};
|
||||
|
||||
/**
|
||||
* Set whether this elements's position is determined in terms of grid
|
||||
* units or pixels.
|
||||
* @param {string} key Which unit to use, px or grid
|
||||
*/
|
||||
ElementProxy.prototype.setUnits = function (key) {
|
||||
if (key === 'px' && this.element.useGrid === true) {
|
||||
this.element.useGrid = false;
|
||||
this.convertCoordsTo('px');
|
||||
} else if (key === 'grid' && this.element.useGrid === false) {
|
||||
this.element.useGrid = true;
|
||||
this.convertCoordsTo('grid');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert this element's coordinates and size from pixels to grid units,
|
||||
* or vice-versa.
|
||||
* @param {string} unit When called with 'px', converts grid units to
|
||||
* pixels; when called with 'grid', snaps element
|
||||
* to grid units
|
||||
*/
|
||||
ElementProxy.prototype.convertCoordsTo = function (unit) {
|
||||
var gridSize = this.gridSize;
|
||||
var element = this.element;
|
||||
var minWidth = this.getMinWidth();
|
||||
var minHeight = this.getMinHeight();
|
||||
if (unit === 'px') {
|
||||
element.x = element.x * gridSize[0];
|
||||
element.y = element.y * gridSize[1];
|
||||
element.width = element.width * gridSize[0];
|
||||
element.height = element.height * gridSize[1];
|
||||
if (element.x2 && element.y2) {
|
||||
element.x2 = element.x2 * gridSize[0];
|
||||
element.y2 = element.y2 * gridSize[1];
|
||||
}
|
||||
} else if (unit === 'grid') {
|
||||
element.x = Math.round(element.x / gridSize[0]);
|
||||
element.y = Math.round(element.y / gridSize[1]);
|
||||
element.width = Math.max(Math.round(element.width / gridSize[0]), minWidth);
|
||||
element.height = Math.max(Math.round(element.height / gridSize[1]), minHeight);
|
||||
if (element.x2 && element.y2) {
|
||||
element.x2 = Math.round(element.x2 / gridSize[0]);
|
||||
element.y2 = Math.round(element.y2 / gridSize[1]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns which grid size the element is currently using.
|
||||
* @return {number[]} The current grid size in [x,y] form if the element
|
||||
|
@ -35,14 +35,17 @@ define(
|
||||
* upon
|
||||
*/
|
||||
function UnitAccessorMutator(elementProxy) {
|
||||
var self = this;
|
||||
|
||||
this.elementProxy = elementProxy;
|
||||
return function (useGrid) {
|
||||
var current = elementProxy.element.useGrid;
|
||||
if (arguments.length > 0) {
|
||||
elementProxy.element.useGrid = useGrid;
|
||||
if (useGrid && !current) {
|
||||
elementProxy.convertCoordsTo('grid');
|
||||
self.convertCoordsTo('grid');
|
||||
} else if (!useGrid && current) {
|
||||
elementProxy.convertCoordsTo('px');
|
||||
self.convertCoordsTo('px');
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,6 +53,40 @@ define(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* For the elementProxy object called upon, convert its element's
|
||||
* coordinates and size from pixels to grid units, or vice-versa.
|
||||
* @param {string} unit When called with 'px', converts grid units to
|
||||
* pixels; when called with 'grid', snaps element
|
||||
* to grid units
|
||||
*/
|
||||
UnitAccessorMutator.prototype.convertCoordsTo = function (unit) {
|
||||
var proxy = this.elementProxy,
|
||||
gridSize = proxy.gridSize,
|
||||
element = proxy.element,
|
||||
minWidth = proxy.getMinWidth(),
|
||||
minHeight = proxy.getMinHeight();
|
||||
if (unit === 'px') {
|
||||
element.x = element.x * gridSize[0];
|
||||
element.y = element.y * gridSize[1];
|
||||
element.width = element.width * gridSize[0];
|
||||
element.height = element.height * gridSize[1];
|
||||
if (element.x2 && element.y2) {
|
||||
element.x2 = element.x2 * gridSize[0];
|
||||
element.y2 = element.y2 * gridSize[1];
|
||||
}
|
||||
} else if (unit === 'grid') {
|
||||
element.x = Math.round(element.x / gridSize[0]);
|
||||
element.y = Math.round(element.y / gridSize[1]);
|
||||
element.width = Math.max(Math.round(element.width / gridSize[0]), minWidth);
|
||||
element.height = Math.max(Math.round(element.height / gridSize[1]), minHeight);
|
||||
if (element.x2 && element.y2) {
|
||||
element.x2 = Math.round(element.x2 / gridSize[0]);
|
||||
element.y2 = Math.round(element.y2 / gridSize[1]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return UnitAccessorMutator;
|
||||
}
|
||||
);
|
||||
|
Reference in New Issue
Block a user