-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathhead-layout.js
More file actions
56 lines (48 loc) · 1.52 KB
/
head-layout.js
File metadata and controls
56 lines (48 loc) · 1.52 KB
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
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
export default class HeadLayout extends Component {
@service('-document') document;
/**
* If true, this will tear down any existing head on init of this component.
* This is useful if there is a head built with fastboot - it will then be torn down when this is initialized in the browser.
* If you do not want this behavior, you can set this to false.
* @public
*/
shouldTearDownOnInit = true;
/**
* The element to render into. Defaults to <head> in `init`, overridable for our own tests only.
* @private
*/
headElement = this.args.headElement || this.document.head;
constructor() {
super(...arguments);
if (this.shouldTearDownOnInit) {
this._tearDownHead();
}
}
/**
* Tear down any previous head, if there was one.
* @private
*/
_tearDownHead() {
if (this._isFastboot()) {
return;
}
// clear fast booted head (if any)
let document = this.document;
let startMeta = document.querySelector('meta[name="ember-cli-head-start"]');
let endMeta = document.querySelector('meta[name="ember-cli-head-end"]');
if (startMeta && endMeta) {
let el = startMeta.nextSibling;
while (el && el !== endMeta) {
document.head.removeChild(el);
el = startMeta.nextSibling;
}
document.head.removeChild(startMeta);
document.head.removeChild(endMeta);
}
}
_isFastboot() {
return typeof FastBoot !== 'undefined';
}
}