forked from jamesshore/quixote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
q_element.js
83 lines (67 loc) · 2.56 KB
/
q_element.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
77
78
79
80
81
82
83
// Copyright (c) 2014-2015 Titanium I.T. LLC. All rights reserved. For license, see "README" or "LICENSE" file.
"use strict";
var ensure = require("./util/ensure.js");
var camelcase = require("../vendor/camelcase-1.0.1-modified.js");
var ElementEdge = require("./descriptors/element_edge.js");
var Center = require("./descriptors/center.js");
var ElementSize = require("./descriptors/element_size.js");
var Assertable = require("./assertable.js");
var Me = module.exports = function QElement(domElement, frame, nickname) {
var QFrame = require("./q_frame.js"); // break circular dependency
ensure.signature(arguments, [ Object, QFrame, String ]);
this._domElement = domElement;
this._nickname = nickname;
this.frame = frame;
// properties
this.top = ElementEdge.top(this);
this.right = ElementEdge.right(this);
this.bottom = ElementEdge.bottom(this);
this.left = ElementEdge.left(this);
this.center = Center.x(this.left, this.right, "center of '" + nickname + "'");
this.middle = Center.y(this.top, this.bottom, "middle of '" + nickname + "'");
this.width = ElementSize.x(this);
this.height = ElementSize.y(this);
};
Assertable.extend(Me);
Me.prototype.getRawStyle = function getRawStyle(styleName) {
ensure.signature(arguments, [ String ]);
var styles;
var result;
// WORKAROUND IE 8: no getComputedStyle()
if (window.getComputedStyle) {
// WORKAROUND Firefox 40.0.3: must use frame's contentWindow (ref https://bugzilla.mozilla.org/show_bug.cgi?id=1204062)
styles = this.frame.toDomElement().contentWindow.getComputedStyle(this._domElement);
result = styles.getPropertyValue(styleName);
}
else {
styles = this._domElement.currentStyle;
result = styles[camelcase(styleName)];
}
if (result === null || result === undefined) result = "";
return result;
};
Me.prototype.getRawPosition = function getRawPosition() {
ensure.signature(arguments, []);
// WORKAROUND IE 8: No TextRectangle.height or .width
var rect = this._domElement.getBoundingClientRect();
return {
left: rect.left,
right: rect.right,
width: rect.width !== undefined ? rect.width : rect.right - rect.left,
top: rect.top,
bottom: rect.bottom,
height: rect.height !== undefined ? rect.height : rect.bottom - rect.top
};
};
Me.prototype.toDomElement = function toDomElement() {
ensure.signature(arguments, []);
return this._domElement;
};
Me.prototype.toString = function toString() {
ensure.signature(arguments, []);
return "'" + this._nickname + "'";
};
Me.prototype.equals = function equals(that) {
ensure.signature(arguments, [ Me ]);
return this._domElement === that._domElement;
};