forked from jamesshore/quixote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition.js
76 lines (59 loc) · 2.11 KB
/
position.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2014 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) {
return new Me(X_DIMENSION, value);
};
Me.y = function y(value) {
return new Me(Y_DIMENSION, value);
};
Me.prototype.compatibility = function compatibility() {
return [ Me, Size ];
};
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.diff = Value.safe(function diff(expected) {
checkAxis(this, expected);
var actualValue = this._value;
var expectedValue = expected._value;
if (actualValue.equals(expectedValue)) return "";
var direction;
var comparison = actualValue.compare(expectedValue);
if (this._dimension === X_DIMENSION) direction = comparison < 0 ? "further left" : "further right";
else direction = comparison < 0 ? "higher" : "lower";
return actualValue.diff(expectedValue) + " " + direction;
});
Me.prototype.toString = function toString() {
ensure.signature(arguments, []);
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");
}
}