This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
generator.js
165 lines (134 loc) · 4.46 KB
/
generator.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @fileoverview Contains the generic interface for iterating over the dom path
* an event has traveled. These generators are meant to be singletons so you
* should not construct them yourself. You should use the static factory method
* getGenerator instead.
*/
goog.provide('jsaction.domGenerator');
goog.provide('jsaction.domGenerator.Ancestors');
goog.provide('jsaction.domGenerator.EventPath');
goog.provide('jsaction.domGenerator.Generator');
goog.require('jsaction.Property');
/** @interface */
jsaction.domGenerator.Generator = function() {};
/**
* @return {Element} The next element in the generator or null if none found.
*/
jsaction.domGenerator.Generator.prototype.next = function() {};
/**
* Constructs a generator of all the ancestors of an element.
* @constructor
* @implements {jsaction.domGenerator.Generator}
*/
jsaction.domGenerator.Ancestors = function() {
/** @private {?Element} */
this.node_ = null;
/** @private {?Element} */
this.container_ = null;
};
/**
* Resets an ancestors generator of an element with a new target and container.
* @param {!Element} target the element to start walking ancestors at.
* @param {!Element} container the element to stop walking ancestors at.
* @return {!jsaction.domGenerator.Generator}
* @private
*/
jsaction.domGenerator.Ancestors.prototype.reset_ =
function(target, container) {
this.node_ = target;
this.container_ = container;
return this;
};
/** @override */
jsaction.domGenerator.Ancestors.prototype.next = function() {
// Walk to the parent node, unless the node has a different owner in
// which case we walk to the owner.
const curr = this.node_;
if (this.node_ && this.node_ != this.container_) {
this.node_ = this.node_[jsaction.Property.OWNER] || this.node_.parentNode;
} else {
this.node_ = null;
}
return curr;
};
/**
* Constructs a generator of all elements in a path array.
* Correctly handles jsaction.Property.OWNER on elements.
* @constructor
* @implements {jsaction.domGenerator.Generator}
*/
jsaction.domGenerator.EventPath = function() {
/** @private {!Array.<!Element>} */
this.path_ = [];
/** @private {number} */
this.idx_ = 0;
/** @private {?Element} */
this.container_ = null;
/** @private {boolean} */
this.usingAncestors_ = false;
};
/**
* Resets an EventPath with a new path and container.
* @param {!Array.<!Element>} path
* @param {!Element} container
* @return {!jsaction.domGenerator.Generator}
* @private
*/
jsaction.domGenerator.EventPath.prototype.reset_ =
function(path, container) {
this.path_ = path;
this.idx_ = 0;
this.container_ = container;
this.usingAncestors_ = false;
return this;
};
/** @override */
jsaction.domGenerator.EventPath.prototype.next = function() {
// TODO(user): If we could ban OWNERS for all users of event.path
// then you could greatly simplify the code here.
if (this.usingAncestors_) {
return jsaction.domGenerator.ancestors_.next();
}
if (this.idx_ != this.path_.length) {
const curr = this.path_[this.idx_];
this.idx_++;
if (curr != this.container_) {
// NOTE(user): The presence of the OWNER property indicates that
// the user wants to override the browsers expected event path with
// one of their own. The eventpath generator still needs to respect
// the OWNER property since this is used by a lot of jsactions
// consumers.
if (curr && curr[jsaction.Property.OWNER]) {
this.usingAncestors_ = true;
jsaction.domGenerator.ancestors_.reset_(
curr[jsaction.Property.OWNER],
/** @type {!Element} */(this.container_));
}
}
return curr;
}
return null;
};
/**
* A reusable generator for dom ancestor walks.
* @private {!jsaction.domGenerator.Ancestors}
*/
jsaction.domGenerator.ancestors_ =
new jsaction.domGenerator.Ancestors();
/**
* A reusable generator for dom ancestor walks.
* @private {!jsaction.domGenerator.EventPath}
*/
jsaction.domGenerator.eventPath_ =
new jsaction.domGenerator.EventPath();
/**
* Return the correct dom generator for a given event.
* @param {!Event} e the event.
* @param {!Element} target the events target element.
* @param {!Element} container the jsaction container.
* @return {!jsaction.domGenerator.Generator}
*/
jsaction.domGenerator.getGenerator = function(e, target, container) {
return e.path ? jsaction.domGenerator.eventPath_.reset_(e.path, container) :
jsaction.domGenerator.ancestors_.reset_(target, container);
};