-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
193 lines (155 loc) · 6.38 KB
/
index.js
File metadata and controls
193 lines (155 loc) · 6.38 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
"use strict";
// import { vars, numbers, strings, arrays, objects } from "./js/listEls.js";
const languageForm = document.getElementById('languageForm');
const select = document.getElementById('primaryLang');
const h1 = document.querySelector('h1');
/**
* * FUNCTIONS
*/
// set local storage for primary & secondary languages, and for H1 text
function setLocalStorage(key, val) {
return localStorage.setItem(key, JSON.stringify(val));
}
function getLocalStorage(key) {
return JSON.parse(localStorage.getItem(key));
}
// On page visit
function initHomePage() {
const h1Text = getLocalStorage('h1');
if (!h1Text) {
setLocalStorage('h1', 'Choose a primary language and at least 1 secondary language');
} else {
h1.textContent = getLocalStorage('h1');
// make the selected primary language be selected on page load
const selectedIdx = Number(getLocalStorage('selection'));
select.options[selectedIdx].selected = true;
const langs = getLocalStorage('checkedLangs');
// check the secondary languages the user checked
langs.forEach(val => {
const checkbox = document.querySelector(`input[type="checkbox"][value="${val}"]`);
if (checkbox) checkbox.checked = true;
});
createLiSection(vars, 'vars', 'Variables + Miscellaneous');
createLiSection(numbers, 'numbers', 'Numbers');
createLiSection(strings, 'strings', 'Strings');
createLiSection(arrays, 'arrays', 'Arrays');
createLiSection(objects, 'objects', 'Objects');
createLiSection(conditionals, 'conditionals', 'Conditionals');
createLiSection(loops, 'loops', 'Loops');
createLiSection(functions, 'functions', 'Functions');
createLiSection(classes, 'classes', 'Classes');
}
}
// for the 5 ol > li sections
function createLiSection(obj, sectionId, h3Text) {
const section = document.getElementById(sectionId);
section.textContent = '';
const primary = getLocalStorage('primary'); // this line is bad for C#
const secondary = getLocalStorage('checkedLangs');
const num = secondary.length + 1;
const grid = document.createElement('div');
grid.className = `grid-${num}`;
const primaryChild = document.createElement('div');
primaryChild.className = 'primary';
languageListItems(obj, primary, h3Text, section, grid, primaryChild);
secondary.forEach(lang => {
const secondaryChild = document.createElement('div');
secondaryChild.className = 'secondary';
languageListItems(obj, lang, h3Text, section, grid, secondaryChild);
})
}
// HELPER function: used in createLiSection
function languageListItems(obj, string, text, el1, el2, el3) {
const h2 = document.createElement('h2');
h2.textContent = string;
const h3 = document.createElement('h3');
h3.textContent = text;
el1.append(el2);
el2.append(el3);
const preTagConcepts = [conditionals, loops, functions, classes];
// The difference starts here with the list items
// I need pre
if (!preTagConcepts.includes(obj)) {
const list = document.createElement('ol');
el3.append(h2, h3, list);
// Friggin C#/CSharp/csharp - total nightmare
obj[string].forEach(item => {
const li = document.createElement('li');
const code = document.createElement('code');
code.className = `custom-${string.toLowerCase()}`;
code.textContent = item;
li.append(code);
list.append(li);
})
} else {
const pre = document.createElement('pre');
pre.className = `language-${string.toLowerCase()}`;
pre.tabIndex = 0
el3.append(h2, h3, pre);
const code = document.createElement('code');
if (string === 'Dart') {
code.className = `language-javascript`;
} else {
code.className = `language-${string.toLowerCase()}`;
}
// code.textContent = obj[string];
code.textContent = obj[string][0];
pre.append(code);
}
}
/**
* * EVENT LISTENERS
*/
// 1. Load localStorage objects if they exist
document.addEventListener('DOMContentLoaded', initHomePage);
// Select list
select.addEventListener('change', e => {
const checkedLanguages = getLocalStorage('checkedLangs') || []
if (checkedLanguages.length > 0) {
setLocalStorage('checkedLangs', [])
}
const selectedvalue = select.options[select.selectedIndex].value;
console.log(selectedvalue) // this is correct
setLocalStorage('selected-primary', selectedvalue)
const boxes = document.querySelectorAll('input[type="checkbox"]');
const disabled = [...boxes].filter(box => box.id === selectedvalue).map(box => box.disabled = true);
const primarySelection = [...boxes].filter(box => box.id === selectedvalue)
// I need to somehow maintain the disabled attribute and only remove it when the user picks a different primary language or clears LS
})
// 2. form listener
// Why are my form listeners always so busy?
languageForm.addEventListener('submit', e => {
e.preventDefault();
// Get the text for the selected primary language - save to LS
const selectedText = select.options[select.selectedIndex].text;
console.log('selectedText: ', selectedText)
// above is wrong! It is whatever was previously selected, not the user selection when they submit the form
setLocalStorage('selection', select.selectedIndex)
setLocalStorage('primary', selectedText)
// Get the checked boxes and save to LS
const checkedBoxes = document.querySelectorAll('input[type="checkbox"]:checked');
const checkedValues = [...checkedBoxes]
.filter(box => box.value !== selectedText)
.map(box => box.value);
setLocalStorage('checkedLangs', checkedValues)
// Set the h1 textContent for the user choices
const headingText = `Compare ${selectedText} to ${checkedValues.join(' and ')}`;
// Save the heading text to LS
setLocalStorage('h1', headingText)
h1.textContent = getLocalStorage('h1');
// ol > li page content
createLiSection(vars, 'vars', 'Variables + Miscellaneous');
createLiSection(numbers, 'numbers', 'Numbers');
createLiSection(strings, 'strings', 'Strings');
/**
* The title 'Arrays' is a problem - some languages use 'Lists'
* The title 'Objects' is a problem - Associative arrrays, Dictionaries
* I need to change the way I update the h3 textContent
*/
createLiSection(arrays, 'arrays', 'Arrays');
createLiSection(objects, 'objects', 'Objects');
createLiSection(conditionals, 'conditionals', 'Conditionals');
createLiSection(loops, 'loops', 'Loops');
createLiSection(functions, 'functions', 'Functions');
createLiSection(classes, 'classes', 'Classes');
})