Repository navigation
Expand file tree
/
Copy pathhdocs.js
More file actions
323 lines (298 loc) · 11.7 KB
/
Copy pathhdocs.js
File metadata and controls
323 lines (298 loc) · 11.7 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
window.hdocs = (function () {
const DB_URL = HDOCS_BASE_DOMAIN ? 'https://data.' + HDOCS_BASE_DOMAIN + '/v1/query' : 'https://data.hasura-stg.hasura-app.io/v1/query';
return {
setup: function () {
Array.from(document.getElementsByClassName('menuLink')).forEach(function (el) { el.addEventListener('click', hdocs.toggleMenu) });
Array.from(document.getElementsByClassName('tracked')).forEach(function (el) { el.addEventListener('click', function () {
const dataLabel = el.getAttribute('data-label');
hdocs.trackGA(dataLabel);
hdocs.saTrack(dataLabel);
}) });
document.getElementById('nav_tree_icon').addEventListener('click', hdocs.handleNavClick);
document.getElementById('sidebar-close').addEventListener('click', hdocs.handleNavClick);
// document.getElementById('close-banner').addEventListener('click', hdocs.closeBanner);
// document.getElementById('thumb_up_button').addEventListener('click', function () { hdocs.sendFeedback('positive', 'Great to hear that! If you have any other feedback, please share here:') });
// document.getElementById('thumb_down_button').addEventListener('click', function () { hdocs.sendFeedback('negative', 'Sorry to hear that. Please tell us what you were looking for:') });
// document.getElementById('feedback_btn').addEventListener('click', hdocs.handleSubmitFeedback);
docsearch({
appId: 'WCBB1VVLRC',
apiKey: HDOCS_ALGOLIA_API_KEY,
indexName: HDOCS_ALGOLIA_INDEX,
inputSelector: '#search_element',
transformData: hdocs.transformSearchData,
debug: false
});
hdocs.getGithubStarCount();
hdocs.setReleaseTags();
hdocs.setExternalLinks();
hdocs.setupGraphiQL();
hdocs.newsletterForm();
},
newsletterForm: function () {
const searchParams = new URLSearchParams(window.location.search);
const searchAliId = searchParams.get("aliId");
var marketoForm = document.getElementById("mktoForm_1011");
var marketoSuccess = document.getElementById("marketo-success");
if (searchAliId || searchAliId === '') {
marketoForm.classList.add('hide');
marketoSuccess.classList.remove('hide');
marketoSuccess.scrollIntoView({ behavior: "smooth", block: "start", inline: "start" });
} else {
marketoForm.classList.remove('hide');
marketoSuccess.classList.add('hide');
}
},
trackGA: function (label, action) {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'Click Events',
category: 'Docs Custom',
action: action || 'Link Click',
label: label
})
},
saTrack: function (label) {
window.analytics = window.analytics || [];
window.analytics.track("click", {
category: 'docs',
action: 'click',
label: 'Clicked ' + label,
placement: 'header',
})
},
toggleMenu: function () {
var x = document.getElementById("navbar")
if (x.className === "topnav") {
x.className += " responsive"
} else {
x.className = "topnav"
}
},
closeBanner: function() {
var banner = document.getElementById("banner-stripe");
banner.className += " hide";
},
request: function (url, data, type) {
return new Promise(function (resolve, reject) {
var req = new XMLHttpRequest();
req.open(type, url, true);
req.setRequestHeader("Content-type", "application/json");
req.onload = function () {
if (req.status === 200) {
resolve(req.response);
} else {
reject(req.response);
}
};
req.onerror = function () {
reject(Error("Network Error"));
};
// Make the request
req.send(JSON.stringify(data));
});
},
getGithubStarCount: function () {
const bodyObj = {
type: "select",
args: {
table: "github_repos",
columns: ["star_count"],
where: { $or: [{ name: "graphql-engine" }] },
}
}
const options = {
method: "POST",
credentials: "include",
body: JSON.stringify(bodyObj),
headers: {
"Content-Type": "application/json",
},
}
fetch(DB_URL, options)
.then(function (response) {
return response.json();
})
.then(function (data) {
const githubStar = data[0].star_count;
document.getElementById("gitHubCount").innerHTML = hdocs.formatNumber(githubStar);
document.getElementById("gitHubBtn").classList.remove("hide");
})
.catch(function (e) { {console.error(e);} })
},
setReleaseTags: function () {
if (document.getElementsByClassName('latest-release-tag').length > 0) {
hdocs.request('https://releases.hasura.io/graphql-engine?agent=docs.hasura.io', null, 'GET')
.then(function (response) {
const data = JSON.parse(response);
Array.from(document.getElementsByClassName('latest-release-tag')).forEach(function (el) { el.innerHTML = data.latest });
Array.from(document.getElementsByClassName('latest-prerelease-tag')).forEach(function (el) { el.innerHTML = data.prerelease });
})
.catch(function (err) {
console.log(err);
});
}
},
setExternalLinks: function () {
Array.from(document.getElementsByClassName('external')).forEach(function (el) { el.setAttribute('target', '_blank') });
},
transformSearchData: function (suggestions) {
if (window.location.origin !== 'https://hasura.io') {
suggestions.forEach(function (suggestion) {
suggestion.url = suggestion.url.replace(/https:\/\/hasura.io\/docs\/[^\/]*/, window.location.origin);
});
}
const searchField = document.getElementById('search_element');
const searchHelp = document.getElementById('search_help');
const hideHelp = function () { searchHelp.classList.add('hide'); }
if (suggestions.length === 0) {
setTimeout(function () { searchHelp.classList.remove('hide'); }, 100);
searchField.addEventListener('blur', function () {
setTimeout(hideHelp, 100);
}, { once: true });
searchField.addEventListener('input', function () {
if (searchField.value === '') {
setTimeout(hideHelp, 100);
}
}, { once: true });
} else if (!searchHelp.classList.contains('hide')) {
hideHelp();
}
return suggestions;
},
handleNavClick: function (e) {
const sidebar = document.getElementById('sidebar');
const background = document.getElementById('content_inner_wrapper');
const body = document.querySelector('body');
if (sidebar.classList.contains('mobile-hide')) {
sidebar.classList.remove('mobile-hide');
background.classList.add('no_scroll');
body.classList.add('no_scroll');
} else {
sidebar.classList.add('mobile-hide');
background.classList.remove('no_scroll');
body.classList.remove('no_scroll');
}
},
// sendFeedback: function (feedback, feedbackDetailMsg) {
// const insertQuery = {
// 'type': 'insert',
// 'args': {
// 'table': 'docs_feedback',
// 'objects': [
// {
// 'page': window.location.pathname,
// 'feedback': feedback
// }
// ],
// 'returning': ['id']
// }
// };
//
// hdocs.request(DB_URL, insertQuery, 'POST')
// .then(function (response) {
// const data = JSON.parse(response);
//
// if (feedback == 'positive' || feedback == 'negative') {
// document.getElementById('feedback_box').setAttribute('data-id', data['returning'][0]['id']);
// document.getElementById('feedback_detail_msg').innerHTML = feedbackDetailMsg;
// document.getElementById('feedback').classList.add('hide');
// document.getElementById('detailed_feedback').classList.remove('hide');
// }
// })
// .catch(function (err) {
// alert('Error capturing feedback');
// console.log(err);
// });
// },
// handleSubmitFeedback: function () {
// const feedbackBox = document.getElementById('feedback_box');
// const feedbackId = feedbackBox.getAttribute('data-id');
// const feedbackContent = feedbackBox.value;
//
// const updateQuery = {
// 'type': 'update',
// 'args': {
// 'table': 'docs_feedback',
// '$set': {
// 'feedback_content': feedbackContent
// },
// 'where': { 'id': feedbackId }
// }
// };
//
// hdocs.request(DB_URL, updateQuery, 'POST')
// .then(function (response) {
// document.getElementById('detailed_feedback').classList.add('hide');
// document.getElementById('thank_you').classList.remove('hide');
// })
// .catch(function (err) {
// alert('Error capturing feedback');
// console.log(err);
// });
// },
graphQLFetcher: function (endpoint) {
endpoint = endpoint || HDOCS_GRAPHIQL_DEFAULT_ENDPOINT;
return function (graphQLParams) {
const params = {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(graphQLParams),
credentials: 'include'
};
return fetch(endpoint, params)
.then(function (response) {
return response.text();
})
.then(function (responseBody) {
try {
return JSON.parse(responseBody);
} catch (error) {
return responseBody;
}
});
}
},
setupGraphiQL: function () {
if (typeof (React) === 'undefined' || typeof (ReactDOM) === 'undefined' || typeof (GraphiQL) === 'undefined') {
return;
}
const targets = document.getElementsByClassName('graphiql');
for (var i = 0; i < targets.length; i++) {
const target = targets[i];
const endpoint = target.getElementsByClassName("endpoint")[0].innerHTML.trim();
const query = target.getElementsByClassName("query")[0].innerHTML.trim();
const response = target.getElementsByClassName("response")[0].innerHTML.trim();
const variables = target.getElementsByClassName("variables")[0].innerHTML.trim();
const graphiQLElement = React.createElement(GraphiQL, {
fetcher: hdocs.graphQLFetcher(endpoint),
schema: null, // TODO: pass undefined if introspection supported
query: query,
response: response,
variables: variables
});
ReactDOM.render(graphiQLElement, target);
}
Array.from(document.getElementsByClassName('variable-editor')).forEach(function (el) { el.style.height = '120px' });
},
formatNumber: function (number) {
if (typeof number !== "number") return number;
const SIsymbol = ["", "k", "M", "G", "T", "P", "E"];
const absNumber = Math.abs(number);
const sign = Math.sign(number);
// what tier? (determines SI symbol)
const tier = Math.log10(absNumber) / 3 | 0;
// if zero, we don't need a suffix
if (tier === 0) return sign * absNumber;
// get suffix and determine scale
const suffix = SIsymbol[tier];
const scale = Math.pow(10, tier * 3);
// scale the number
const scaled = absNumber / scale;
// format number and add suffix
return sign * scaled.toFixed(1) + suffix;
}
}
})();