This commit is contained in:
a-ill 2023-07-04 21:38:05 +03:00
parent 3515603e93
commit e803d25a37
6 changed files with 29 additions and 10 deletions

View File

@ -38,7 +38,6 @@ export function px2rem(px) {
* *
* @see https://stackoverflow.com/questions/118241/calculate-text-width-with-javascript/21015393#21015393 * @see https://stackoverflow.com/questions/118241/calculate-text-width-with-javascript/21015393#21015393
*/ */
export function getTextWidth(text, font) { export function getTextWidth(text, font) {
// re-use canvas object for better performance // re-use canvas object for better performance
const canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas")); const canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas"));
@ -110,3 +109,10 @@ export function validatePosNumber(event,input,callback,max) {
callback(input.value) callback(input.value)
} }
} }
export function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}

View File

@ -92,10 +92,10 @@ export function loadLocaleContent(content,componentName,loaded,callback) {
} }
} }
getData("/locales/" + locale + "/" + componentName + ".json" ,function(response) { getData("/locales/" + locale + "/" + componentName + ".json" ,function(response) {
if (callback!=undefined) {
callback(locale)
}
let parsed = JSON.parse(response) let parsed = JSON.parse(response)
if (callback!=undefined) {
callback(parsed)
}
content.set(parsed) content.set(parsed)
loaded = 1 loaded = 1
}) })

View File

@ -5,13 +5,14 @@
import { onMount } from 'svelte' import { onMount } from 'svelte'
import { writable } from 'svelte/store'; import { writable } from 'svelte/store';
import { loadLocaleContent } from "/js/libraries/serverTools.js" import { loadLocaleContent } from "/js/libraries/serverTools.js"
import { shuffleArray } from "/js/libraries/miscTools.js"
// Import components // Import components
// Main code // Main code
let loaded let loaded
let content = writable({}) let content = writable({})
let locale = loadLocaleContent(content,"compass-component",loaded) let locale = loadLocaleContent(content,"compass-component",loaded,contentCallback)
let qTag = 0 let qTag = 0
let answers = [] let answers = []
@ -20,6 +21,12 @@
let vanguardismScore = "" let vanguardismScore = ""
let socialismScore = "" let socialismScore = ""
function contentCallback(content) {
for (let q of content.qs) {
shuffleArray(q.as)
}
}
function next(i) { function next(i) {
if (answers[i]!=undefined) { if (answers[i]!=undefined) {
if (qTag < $content.qs.length) { if (qTag < $content.qs.length) {

File diff suppressed because one or more lines are too long

View File

@ -38,7 +38,6 @@ export function px2rem(px) {
* *
* @see https://stackoverflow.com/questions/118241/calculate-text-width-with-javascript/21015393#21015393 * @see https://stackoverflow.com/questions/118241/calculate-text-width-with-javascript/21015393#21015393
*/ */
export function getTextWidth(text, font) { export function getTextWidth(text, font) {
// re-use canvas object for better performance // re-use canvas object for better performance
const canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas")); const canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas"));
@ -110,3 +109,10 @@ export function validatePosNumber(event,input,callback,max) {
callback(input.value) callback(input.value)
} }
} }
export function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}

View File

@ -92,10 +92,10 @@ export function loadLocaleContent(content,componentName,loaded,callback) {
} }
} }
getData("/locales/" + locale + "/" + componentName + ".json" ,function(response) { getData("/locales/" + locale + "/" + componentName + ".json" ,function(response) {
if (callback!=undefined) {
callback(locale)
}
let parsed = JSON.parse(response) let parsed = JSON.parse(response)
if (callback!=undefined) {
callback(parsed)
}
content.set(parsed) content.set(parsed)
loaded = 1 loaded = 1
}) })