forked from pubkey/rxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext-button.js
130 lines (109 loc) · 3.59 KB
/
next-button.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
function runAddingNextButtons() {
function addNextButton() {
var id = 'rxdb-custom-next-button';
var alreadyThere = document.querySelector('#' + id);
if (alreadyThere) {
return;
}
var block = document.querySelector('.normal.markdown-section');
var path = window.location.pathname;
var page = path.split('/').pop();
var chapters = document.querySelectorAll('.chapter');
var dataPaths = [];
chapters.forEach(function (chapter) {
var dataPath = chapter.dataset.path;
if (dataPath) {
dataPaths.push(dataPath);
}
});
var lastIndex = dataPaths.lastIndexOf(page);
var nextPath = dataPaths[lastIndex + 1];
if (!nextPath) {
return;
}
var hr = document.createElement('hr');
block.appendChild(hr);
var span = document.createElement('p');
span.id = id;
span.innerHTML = 'If you are new to RxDB, you should continue ';
var link = document.createElement('a');
link.href = nextPath;
link.innerHTML = 'here';
span.appendChild(link);
block.appendChild(span);
}
addNextButton();
/**
* Gitbook does a strange page change handling,
* so we have to re-run the function
* because listening to history changes did not work.
*/
setInterval(function () {
addNextButton();
}, 100);
}
runAddingNextButtons();
var callToActions = [
{
text: 'Follow at ',
keyword: '@twitter',
url: 'https://twitter.com/intent/user?screen_name=rxdbjs'
},
{
text: 'Chat at ',
keyword: '@discord',
url: 'https://rxdb.info/chat.html'
},
{
text: 'Star at ',
keyword: '@github',
url: 'https://github.com/pubkey/rxdb'
},
{
text: 'Subscribe',
keyword: '@newsletter',
url: 'https://rxdb.info/newsletter.html'
}
];
function insertAfter(referenceNode, newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
var callToActionButtonId = 'rxdb-call-to-action-button';
function setCallToActionOnce() {
var randId = new Date().getTime() % callToActions.length;
var callToAction = callToActions[randId];
var alreadyThere = document.querySelector('#' + callToActionButtonId);
if (alreadyThere) {
alreadyThere.parentNode.removeChild(alreadyThere);
}
var positionReferenceElement = document.querySelector('.btn.pull-left.js-toolbar-action');
if (!positionReferenceElement) {
// not loaded yet!
return;
}
var newElement = document.createElement('a');
newElement.classList.add('btn');
newElement.classList.add('pull-left');
newElement.classList.add('js-toolbar-action');
newElement.id = callToActionButtonId;
newElement.innerHTML = callToAction.text + ' <b>' + callToAction.keyword + '</b>';
newElement.href = callToAction.url;
newElement.target = '_blank';
insertAfter(positionReferenceElement, newElement);
}
function runSettingCallToActionButton() {
setCallToActionOnce();
/**
* Gitbook does a strange page change handling,
* so we have to re-run the function
* because listening to history changes did not work.
*/
setInterval(function () {
var alreadyThere = document.querySelector('#' + callToActionButtonId);
// only add if not exists already, like on a page change.
if (!alreadyThere) {
setCallToActionOnce();
}
}, 100);
}
runSettingCallToActionButton();