forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfocus-history.js
151 lines (138 loc) · 4.06 KB
/
focus-history.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
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Observable} from './core/data-structures/observable';
import {isElement} from './core/types';
import {dev} from './log';
import {Services} from './service';
/**
* FocusHistory keeps track of recent focused elements. This history can be
* purged using `purgeBefore` method.
*/
export class FocusHistory {
/**
* @param {!Window} win
* @param {number} purgeTimeout
*/
constructor(win, purgeTimeout) {
/** @const {!Window} */
this.win = win;
/** @private @const {number} */
this.purgeTimeout_ = purgeTimeout;
/** @private @const {!Array<!{el: !Element, time: time}>} */
this.history_ = [];
/** @private @const {!Observable<!Element>} */
this.observeFocus_ = new Observable();
/**
* @private
* @param {!Event} e
*/
this.captureFocus_ = (e) => {
// Hack (#15079) due to Firefox firing focus events on the entire page
if (isElement(e.target)) {
this.pushFocus_(dev().assertElement(e.target));
}
};
/**
* @private
* @param {*} unusedE
*/
this.captureBlur_ = (unusedE) => {
// IFrame elements do not receive `focus` event. An alternative way is
// implemented here. We wait for a blur to arrive on the main window
// and after a short time check which element is active.
Services.timerFor(win).delay(() => {
if (this.win.document.activeElement) {
this.pushFocus_(this.win.document.activeElement);
}
}, 500);
};
this.win.document.addEventListener('focus', this.captureFocus_, true);
this.win.addEventListener('blur', this.captureBlur_);
}
/** @visibleForTesting */
cleanup_() {
this.win.document.removeEventListener('focus', this.captureFocus_, true);
this.win.removeEventListener('blur', this.captureBlur_);
}
/**
* Add a listener for focus events.
* @param {function(!Element)} handler
* @return {!UnlistenDef}
*/
onFocus(handler) {
return this.observeFocus_.add(handler);
}
/**
* @param {!Element} element
* @private
*/
pushFocus_(element) {
const now = Date.now();
if (
this.history_.length == 0 ||
this.history_[this.history_.length - 1].el != element
) {
this.history_.push({el: element, time: now});
} else {
this.history_[this.history_.length - 1].time = now;
}
this.purgeBefore(now - this.purgeTimeout_);
this.observeFocus_.fire(element);
}
/**
* Returns the element that was focused last.
* @return {?Element}
*/
getLast() {
if (this.history_.length == 0) {
return null;
}
return this.history_[this.history_.length - 1].el;
}
/**
* Removes elements from the history older than the specified time.
* @param {time} time
*/
purgeBefore(time) {
let index = this.history_.length - 1;
for (let i = 0; i < this.history_.length; i++) {
if (this.history_[i].time >= time) {
index = i - 1;
break;
}
}
if (index != -1) {
this.history_.splice(0, index + 1);
}
}
/**
* Returns `true` if the specified element contains any of the elements in
* the history.
* @param {!Element} element
* @return {boolean}
*/
hasDescendantsOf(element) {
if (this.win.document.activeElement) {
this.pushFocus_(this.win.document.activeElement);
}
for (let i = 0; i < this.history_.length; i++) {
if (element.contains(this.history_[i].el)) {
return true;
}
}
return false;
}
}