UNPKG

quixote

Version:

CSS unit and integration testing

127 lines (96 loc) 3.5 kB
// Copyright (c) 2014-2016 Titanium I.T. LLC. All rights reserved. For license, see "README" or "LICENSE" file. "use strict"; var ensure = require("../util/ensure.js"); var Value = require("./value.js"); var Pixels = require("./pixels.js"); var Size = require("./size.js"); var X_DIMENSION = "x"; var Y_DIMENSION = "y"; var Me = module.exports = function Position(dimension, value) { ensure.signature(arguments, [ String, [ Number, Pixels ] ]); this._dimension = dimension; this._value = (typeof value === "number") ? Pixels.create(value) : value; }; Value.extend(Me); Me.x = function x(value) { ensure.signature(arguments, [ [ Number, Pixels ] ]); return new Me(X_DIMENSION, value); }; Me.y = function y(value) { ensure.signature(arguments, [ [ Number, Pixels ] ]); return new Me(Y_DIMENSION, value); }; Me.noX = function noX() { ensure.signature(arguments, []); return new Me(X_DIMENSION, Pixels.NONE); }; Me.noY = function noY() { ensure.signature(arguments, []); return new Me(Y_DIMENSION, Pixels.NONE); }; Me.prototype.compatibility = function compatibility() { return [ Me, Size ]; }; Me.prototype.isNone = function isNone() { return this._value.isNone(); }; Me.prototype.distanceTo = function(operand) { ensure.signature(arguments, [ Me ]); checkAxis(this, operand); return Size.create(this._value.difference(operand.toPixels())); }; Me.prototype.plus = Value.safe(function plus(operand) { checkAxis(this, operand); return new Me(this._dimension, this._value.plus(operand.toPixels())); }); Me.prototype.minus = Value.safe(function minus(operand) { checkAxis(this, operand); return new Me(this._dimension, this._value.minus(operand.toPixels())); }); Me.prototype.midpoint = Value.safe(function midpoint(operand) { checkAxis(this, operand); return new Me(this._dimension, this._value.average(operand.toPixels())); }); Me.prototype.compare = Value.safe(function compare(operand) { checkAxis(this, operand); return this._value.compare(operand.toPixels()); }); Me.prototype.min = Value.safe(function min(operand) { checkAxis(this, operand); return new Me(this._dimension, Pixels.min(this._value, operand.toPixels())); }); Me.prototype.max = Value.safe(function max(operand) { checkAxis(this, operand); return new Me(this._dimension, Pixels.max(this._value, operand.toPixels())); }); Me.prototype.diff = Value.safe(function diff(expected) { ensure.signature(arguments, [ Me ]); checkAxis(this, expected); var actualValue = this._value; var expectedValue = expected._value; if (actualValue.equals(expectedValue)) return ""; else if (isNone(expected) && !isNone(this)) return "rendered"; else if (!isNone(expected) && isNone(this)) return "not rendered"; var direction; var comparison = actualValue.compare(expectedValue); if (this._dimension === X_DIMENSION) direction = comparison < 0 ? "to left" : "to right"; else direction = comparison < 0 ? "higher" : "lower"; return actualValue.diff(expectedValue) + " " + direction; }); Me.prototype.toString = function toString() { ensure.signature(arguments, []); if (isNone(this)) return "not rendered"; else return this._value.toString(); }; Me.prototype.toPixels = function toPixels() { ensure.signature(arguments, []); return this._value; }; function checkAxis(self, other) { if (other instanceof Me) { ensure.that(self._dimension === other._dimension, "Can't compare X coordinate to Y coordinate"); } } function isNone(position) { return position._value.equals(Pixels.NONE); }