This commit is contained in:
Klemek
2023-08-29 16:44:25 +02:00
parent 233f4b1c3a
commit 3239a8293c
2 changed files with 130 additions and 129 deletions
+1
View File
@@ -6,6 +6,7 @@ module.exports = {
}, },
globals: { globals: {
Vue: 'readonly', Vue: 'readonly',
LZString: 'readonly',
}, },
extends: [ 'eslint:recommended' ], extends: [ 'eslint:recommended' ],
parserOptions: { parserOptions: {
+129 -129
View File
@@ -1,144 +1,144 @@
/* exported app, utils */ /* exported app, utils */
const utils = { const utils = {
cloneObject: function (obj) { cloneObject: function (obj) {
return JSON.parse(JSON.stringify(obj)); return JSON.parse(JSON.stringify(obj));
}, },
serialize: function (list) { serialize: function (list) {
return LZString.compressToBase64(JSON.stringify(list)); return LZString.compressToBase64(JSON.stringify(list));
}, },
deserialize: function (rawData) { deserialize: function (rawData) {
return JSON.parse(LZString.decompressFromBase64(rawData)); return JSON.parse(LZString.decompressFromBase64(rawData));
}, },
randint: function (min, max) { randint: function (min, max) {
return Math.floor(Math.random() * (max - min)) + min; return Math.floor(Math.random() * (max - min)) + min;
}, },
randindex: function (array, ...toIgnore) { randindex: function (array, ...toIgnore) {
let index; let index;
do { do {
index = this.randint(0, array.length); index = this.randint(0, array.length);
} while (this.contains(toIgnore, index)); } while (this.contains(toIgnore, index));
return index; return index;
}, },
randitem: function (array) { randitem: function (array) {
return array[this.randindex(array)]; return array[this.randindex(array)];
}, },
randindexes: function (array, number, ...toIgnore) { randindexes: function (array, number, ...toIgnore) {
const output = []; const output = [];
for (let i = 0; i < number; i++) { for (let i = 0; i < number; i++) {
output.push(this.randindex(array, ...output, ...toIgnore)); output.push(this.randindex(array, ...output, ...toIgnore));
} }
return output; return output;
}, },
shuffle: function (array) { shuffle: function (array) {
const output = [...array]; const output = [ ...array ];
if (output.length < 2) { if (output.length < 2) {
return output; return output;
} }
for (let i = 0; i < array.length; i++) { for (let i = 0; i < array.length; i++) {
const i1 = this.randindex(array); const i1 = this.randindex(array);
const i2 = this.randindex(array, i1); const i2 = this.randindex(array, i1);
[output[i1], output[i2]] = [output[i2], output[i1]]; [ output[i1], output[i2] ] = [ output[i2], output[i1] ];
} }
return output; return output;
}, },
contains: function (array, item) { contains: function (array, item) {
return array.indexOf(item) >= 0; return array.indexOf(item) >= 0;
}, },
}; };
let app = { let app = {
data() { data() {
return { return {
question: "", question: '',
answer: "", answer: '',
showAnswer: false, showAnswer: false,
available: [], available: [],
current: [], current: [],
failed: [], failed: [],
done: [], done: [],
newRow: ["", ""], newRow: [ '', '' ],
showConfig: true, showConfig: true,
}; };
},
computed: {
currentYear() {
return new Date().getFullYear();
}, },
urlLength() { computed: {
return window.location.toString().length; currentYear() {
return new Date().getFullYear();
},
urlLength() {
return window.location.toString().length;
},
}, },
}, methods: {
methods: { showApp() {
showApp() { document.getElementById('app').setAttribute('style', '');
document.getElementById("app").setAttribute("style", ""); },
}, show() {
show() { this.showAnswer = true;
this.showAnswer = true; },
}, right() {
right() { this.done.push(this.current.shift());
this.done.push(this.current.shift()); console.log(this.current);
console.log(this.current); this.nextQuestion();
this.nextQuestion(); },
}, wrong() {
wrong() { this.failed.push(this.current.shift());
this.failed.push(this.current.shift()); console.log(this.current);
console.log(this.current); this.nextQuestion();
this.nextQuestion(); },
}, deleteRow(i) {
deleteRow(i) { this.available.splice(i, 1);
this.available.splice(i, 1); this.reset();
this.reset(); },
}, addRow() {
addRow() { if (this.newRow[0] && this.newRow[1]) {
if (this.newRow[0] && this.newRow[1]) { this.available.push(utils.cloneObject(this.newRow));
this.available.push(utils.cloneObject(this.newRow)); this.newRow = [ '', '' ];
this.newRow = ["", ""]; }
} this.reset();
this.reset(); },
}, reset() {
reset() { this.current = utils.shuffle(utils.cloneObject(this.available));
this.current = utils.shuffle(utils.cloneObject(this.available)); this.done = [];
this.done = []; this.failed = [];
this.failed = []; this.nextQuestion();
this.nextQuestion(); },
}, nextQuestion() {
nextQuestion() { this.showAnswer = false;
this.showAnswer = false;
if (this.current.length === 0 && this.failed.length > 0) { if (this.current.length === 0 && this.failed.length > 0) {
this.current = utils.shuffle(utils.cloneObject(this.failed)); this.current = utils.shuffle(utils.cloneObject(this.failed));
this.failed = []; this.failed = [];
} }
if (this.current.length > 0) { if (this.current.length > 0) {
this.question = this.current[0][0]; this.question = this.current[0][0];
this.answer = this.current[0][1]; this.answer = this.current[0][1];
} }
},
},
beforeMount() {
const url = new URL(window.location);
if (url.searchParams.get('d')) {
this.available = utils.deserialize(url.searchParams.get('d'));
this.showConfig = false;
this.reset();
}
},
updated() {
const data = utils.serialize(this.available);
const url = new URL(window.location);
if (url.searchParams.get('d') !== data) {
url.searchParams.set('d', data);
window.history.pushState({}, '', url);
}
},
mounted: function () {
setTimeout(this.showApp);
}, },
},
beforeMount() {
const url = new URL(window.location);
if (url.searchParams.get("d")) {
this.available = utils.deserialize(url.searchParams.get("d"));
this.showConfig = false;
this.reset();
}
},
updated() {
const data = utils.serialize(this.available);
const url = new URL(window.location);
if (url.searchParams.get("d") !== data) {
url.searchParams.set("d", data);
window.history.pushState({}, "", url);
}
},
mounted: function () {
setTimeout(this.showApp);
},
}; };
window.onload = () => { window.onload = () => {
app = Vue.createApp(app); app = Vue.createApp(app);
app.mount("#app"); app.mount('#app');
}; };