-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathdom.js
63 lines (51 loc) · 1.51 KB
/
dom.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
/*
* Implement some helpers methods for interacting with the DOM,
* be it Fastboot's SimpleDOM or the browser's version.
*/
import { getOwner } from '@ember/application';
export function getActiveElement() {
if (typeof document === 'undefined') {
return null;
} else {
return document.activeElement;
}
}
function childNodesOfElement(element) {
let children = [];
let child = element.firstChild;
while (child) {
children.push(child);
child = child.nextSibling;
}
return children;
}
export function findElementById(doc, id) {
if (doc.getElementById) {
return doc.getElementById(id);
}
let nodes = childNodesOfElement(doc);
let node;
while (nodes.length) {
node = nodes.shift();
if (node.getAttribute && node.getAttribute('id') === id) {
return node;
}
nodes = childNodesOfElement(node).concat(nodes);
}
}
// Private Ember API usage. Get the dom implementation used by the current
// renderer, be it native browser DOM or Fastboot SimpleDOM
export function getDOM(context) {
let { renderer } = context;
if (!renderer._dom) { // pre glimmer2
let container = getOwner ? getOwner(context) : context.container;
let documentService = container.lookup('service:-document');
if (documentService) { return documentService; }
renderer = container.lookup('renderer:-dom');
}
if (renderer._dom && renderer._dom.document) { // pre Ember 2.6
return renderer._dom.document;
} else {
throw new Error('ember-wormhole could not get DOM');
}
}