commit 0f58fbe47407d53e35c7d9402eaef0ce17bc6965
parent fc546880d80659b3f0d787950d1cf7c90d6c2534
Author: AsherMorgan <59518073+AsherMorgan@users.noreply.github.com>
Date: Sun, 13 Dec 2020 19:38:32 -0800
Implement loadVocab function in global.js.
Diffstat:
3 files changed, 56 insertions(+), 43 deletions(-)
diff --git a/js/app.js b/js/app.js
@@ -138,7 +138,7 @@ function loadVue() {
/**
* Load the document.
*/
-function Load() {
+async function Load() {
// Call LoadPage method from global.js
LoadPage();
@@ -155,20 +155,8 @@ function Load() {
// Add event Listeners
document.addEventListener("keydown", KeyDown);
- // Load CSVs
- Sets = [];
- let setNames = ["Adjectives", "Adverbs", "Prepositions", "Transitions", "Verbs",
- "Colors", "Days", "Months", "Questions",
- "Childhood", "Clothes", "Family", "Food", "Health", "House", "Nature", "Professions", "Vacation", "Weather"];
- for (let setName of setNames) {
- Papa.parse(`vocab/${setName}.csv`, {
- download: true,
- complete: function(results) {
- // Set verbs
- Sets[setName] = results.data;
- }
- });
- }
+ // Load vocab
+ Sets = await loadVocab();
}
diff --git a/js/global.js b/js/global.js
@@ -137,3 +137,37 @@ function setSettings(value) {
// Apply theme
SetTheme(value.darkTheme);
}
+
+
+
+/**
+ * Loads the vocab sets.
+ * @param {Function} callback A callback function with two parameters: name (String) and data (Array).
+ */
+function loadVocab() {
+ return new Promise(function(resolve, reject) {
+ // Initialize variables
+ let setNames = [
+ "Adjectives", "Adverbs", "Prepositions", "Transitions", "Verbs", // Common words
+ "Colors", "Days", "Months", "Questions", // Basic words
+ "Childhood", "Clothes", "Family", "Food", "Health", "House", "Nature", "Professions", "Vacation", "Weather", // Advanced words
+ ];
+ let progress = 0;
+ let sets = {};
+
+ // Load vocab
+ for (let setName of setNames) {
+ Papa.parse(`vocab/${setName}.csv`, {
+ download: true,
+ complete: function(results) {
+ sets[setName] = results.data;
+ progress++;
+
+ if (progress === setNames.length) {
+ resolve(sets);
+ }
+ }
+ });
+ }
+ });
+}
diff --git a/js/reference.js b/js/reference.js
@@ -23,7 +23,7 @@ function loadVue() {
/**
* Load the document
*/
-function Load() {
+async function Load() {
// Call LoadPage method from global.js
LoadPage();
@@ -40,36 +40,27 @@ function Load() {
// Set table height
setTableHeight();
- // Load CSVs
- let setNames = ["Adjectives", "Adverbs", "Prepositions", "Transitions", "Verbs",
- "Colors", "Days", "Months", "Questions",
- "Childhood", "Clothes", "Family", "Food", "Health", "House", "Nature", "Professions", "Vacation", "Weather"];
- for (let setName of setNames) {
- Papa.parse(`vocab/${setName}.csv`, {
- download: true,
- complete: function(results) {
- // Add Set
- app.sets[setName] = results.data;
+ // Load vocab
+ app.sets = {...app.sets, ...await loadVocab()};
- // Add data to "All Sets"
- if (setName === "Verbs") {
- for (let row of results.data.slice(1)) {
- app.sets["All Sets"].push([row[0], row[1], "Verb"]);
- }
- }
- else {
- app.sets["All Sets"].push(...results.data.slice(1));
- }
-
- // Sort "All Sets"
- app.sets["All Sets"].sort(function(a, b) {
- if (a[0] === "English") return false; // Header row should be at the top
- else if (b[0] === "English") return true; // Header row should be at the top
- else return a[0] > b[0]; // Sort other rows by 1st item
- })
+ // Add data to "All Sets"
+ for (let set in app.sets) {
+ if (set === "Verbs") {
+ for (let row of app.sets[set].slice(1)) {
+ app.sets["All Sets"].push([row[0], row[1], "Verb"]);
}
- });
+ }
+ else {
+ app.sets["All Sets"].push(...app.sets[set].slice(1));
+ }
}
+
+ // Sort "All Sets"
+ app.sets["All Sets"].sort(function(a, b) {
+ if (a[0] === "English") return false; // Header row should be at the top
+ else if (b[0] === "English") return true; // Header row should be at the top
+ else return a[0] > b[0]; // Sort other rows by 1st item
+ })
}