-
Notifications
You must be signed in to change notification settings - Fork 107
/
main.js
120 lines (99 loc) · 4.21 KB
/
main.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
/**
* When the document is ready check to see if we are on an element or getting started page.
* If we are on an element or getting started page, load the content from the HTML file.
* If not, load the list of elements and getting started items from the JSON file.
*/
document.addEventListener('DOMContentLoaded', function() {
let element = getQueryParameter('element');
const start = getQueryParameter('start');
if(element || start){
document.getElementById('list').remove();
load_file('./data.json', function(text){
let data = JSON.parse(text);
let folderLocation = (element) ? 'elements' : 'getting-started';
let file = (element) ? element : start;
if(element){
if(element.includes('-examples')){
element = element.replace('-examples', '').split('/')[0];
}
document.getElementById('content').className = data.container[element];
} else {
document.getElementById('content').className = 'max-w-3xl p-10 prose';
}
load_file('./' + folderLocation + '/' + file + '.html', function(text){
document.getElementById('content').innerHTML = text;
document.body.style = '';
});
});
} else {
document.getElementById('content').remove();
load_file('./data.json', function(text){
let data = JSON.parse(text);
addItemsToList(data.elements, document.getElementById('elements'), 'element', data.examples);
addItemsToList(data.start, document.getElementById('gettingStarted'), 'start');
// for(let i=0; i < data.examples.length; i++){
// }
document.body.style = '';
});
}
});
/**
* Get the value of a query parameter from the URL. ?element=accordion or ?start=introduction, etc.
*/
function getQueryParameter(parameter){
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const element = urlParams.get(parameter);
return element;
}
/**
* Add element and getting started items from the JSON file to the html list
*/
function addItemsToList(myObject, myList, param, examples = null){
for (const key in myObject) {
const listItem = document.createElement('li');
const link = document.createElement('a');
link.className = 'text-blue-500 underline';
link.href = '?' + param + '=' + key;
link.textContent = myObject[key];
listItem.appendChild(link);
if(examples){
if(examples[key]){
let exampleList = document.createElement('ul');
exampleList.classList.add('list-disc', 'pl-4');
let exampleFiles = examples[key];
for(let i=0; i < exampleFiles.length; i++){
let exampleListItem = document.createElement('li');
let exampleLink = document.createElement('a');
exampleLink.className = 'text-blue-500 underline';
exampleLink.href = '?' + param + '=' + key + '-examples/example-' + exampleFiles[i];
exampleLink.textContent = 'Example ' + exampleFiles[i];
exampleListItem.appendChild(exampleLink);
exampleList.appendChild(exampleListItem);
}
listItem.appendChild(exampleList);
}
}
myList.appendChild(listItem);
}
}
/**
* Load the contents of a file using the Fetch API and passes it to a callback function.
*/
function get_element(filename, callback) {
fetch(filename).then(response => response.text()).then(text => callback(text));
}
/**
* Load the contents of a file using the Fetch API and passes it to a callback function.
*/
function load_file(filename, callback) {
fetch(filename).then(response => response.text()).then(text => callback(text));
}
/**
* Format lowercase string to title case.
*/
function formatString(str) {
let formattedStr = str.replace(/-/g, ' ');
formattedStr = formattedStr.replace(/\b\w/g, (match) => match.toUpperCase());
return formattedStr;
}