-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathboth.js
88 lines (86 loc) · 2.24 KB
/
both.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
var FlowRouter = null;
if (Package['kadira:flow-router-ssr']) {
FlowRouter = Package['kadira:flow-router-ssr'].FlowRouter;
}
if (Meteor.isClient) {
var titleDependency = new Tracker.Dependency();
}
DocHead = {
currentTitle: null,
setTitle(title) {
if (Meteor.isClient) {
titleDependency.changed();
document.title = title;
} else {
this.currentTitle = title;
const titleHtml = `<title>${title}</title>`;
this._addToHead(titleHtml);
}
},
addMeta(info) {
this._addTag(info, 'meta');
},
addLink(info) {
this._addTag(info, 'link');
},
getTitle() {
if (Meteor.isClient) {
titleDependency.depend();
return document.title;
}
return this.currentTitle;
},
addLdJsonScript(jsonObj) {
const strObj = JSON.stringify(jsonObj);
this._addLdJsonScript(strObj);
},
loadScript(url, options, callback) {
if (Meteor.isClient) {
npmLoadScript(url, options, callback);
}
},
_addTag(info, tag) {
const meta = this._buildTag(info, tag);
if (Meteor.isClient) {
document.getElementsByTagName('head')[0].insertAdjacentHTML('beforeend', meta);
} else {
this._addToHead(meta);
}
},
_addToHead(html) {
// only work there is kadira:flow-router-ssr
if (!FlowRouter) {
return;
}
let ssrContext = FlowRouter.ssrContext.get();
if (ssrContext) {
ssrContext.addToHead(html);
}
},
_buildTag(metaInfo, type) {
let props = "";
for (let key in metaInfo) {
props += `${key}="${metaInfo[key]}" `;
}
props += 'data-dochead="1"';
var meta = `<${type} ${props}/>`;
return meta;
},
_addLdJsonScript(stringifiedObject) {
const scriptTag = `<script type="application/ld+json" data-dochead="1">${stringifiedObject}</script>`;
if (Meteor.isClient) {
document.getElementsByTagName('head')[0].insertAdjacentHTML('beforeend', scriptTag);
} else {
this._addToHead(scriptTag);
}
},
removeDocHeadAddedTags() {
if (Meteor.isClient) {
const elements = document.querySelectorAll('[data-dochead="1"]');
// We use for-of here to loop only over iterable objects
for (let element of elements) {
element.parentNode.removeChild(element);
}
}
}
};