Quiz
This commit is contained in:
parent
5d2fb7b107
commit
7fe9c546c0
2
DOCS.md
2
DOCS.md
@ -146,6 +146,8 @@ map.tag(/* TAG элемента */); // Вернет экземпляр map дл
|
|||||||
map.attach(/* событие */, /* функция */); // EventHandler
|
map.attach(/* событие */, /* функция */); // EventHandler
|
||||||
map.attribute(/* Имя */, /* Значение */); // Задание атрибута
|
map.attribute(/* Имя */, /* Значение */); // Задание атрибута
|
||||||
map.add(/* HTML */);
|
map.add(/* HTML */);
|
||||||
|
map.all(/* CSS-селектор */); // Вернет массив экземпляров map для выбранных элементов
|
||||||
|
map.property(/* свойство элемента */[, /* значение */]);
|
||||||
/*
|
/*
|
||||||
* Функция в attach может принимать параметр.
|
* Функция в attach может принимать параметр.
|
||||||
* Этот параметр - обьект Find, map которого был вызван
|
* Этот параметр - обьект Find, map которого был вызван
|
||||||
|
|||||||
28
Find/Find.js
28
Find/Find.js
@ -31,6 +31,26 @@ class FindObjectMap {
|
|||||||
this._num = num;
|
this._num = num;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
all(q) {
|
||||||
|
const o = this.root.querySelectorAll(q);
|
||||||
|
if (typeof o === "undefined") {
|
||||||
|
PrivateFind.error("Нет обьектов для селектора!");
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
let arr = new Array();
|
||||||
|
for (let i = 0; i < o.length; i ++) {
|
||||||
|
arr[i] = new FindObjectMap(o[i], this._app, this._num + 1)
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
property(property, to) {
|
||||||
|
if (typeof to === "undefined") {
|
||||||
|
return this.root[property];
|
||||||
|
} else {
|
||||||
|
this.root[property] = to;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
select(q) {
|
select(q) {
|
||||||
const o = this.root.querySelector(q);
|
const o = this.root.querySelector(q);
|
||||||
if (typeof o === "undefined") {
|
if (typeof o === "undefined") {
|
||||||
@ -39,6 +59,12 @@ class FindObjectMap {
|
|||||||
}
|
}
|
||||||
return new FindObjectMap(o, this._app, this._num + 1);
|
return new FindObjectMap(o, this._app, this._num + 1);
|
||||||
}
|
}
|
||||||
|
status() {
|
||||||
|
if (typeof this.root === "object" && this.root !== null && this.root !== undefined) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
read() {
|
read() {
|
||||||
return this.root.innerHTML;
|
return this.root.innerHTML;
|
||||||
}
|
}
|
||||||
@ -85,7 +111,7 @@ class FindObjectMap {
|
|||||||
}
|
}
|
||||||
attach(event, func) {
|
attach(event, func) {
|
||||||
this.root.addEventListener(event, () => {
|
this.root.addEventListener(event, () => {
|
||||||
func(this._app);
|
func(this._app, this);
|
||||||
});
|
});
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
demos/Quiz/image.avif
Normal file
BIN
demos/Quiz/image.avif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
@ -1,20 +1,33 @@
|
|||||||
const qestions = [
|
const questions = [
|
||||||
{
|
{
|
||||||
qestion: "Мой вопрос",
|
image: "image.avif",
|
||||||
|
question: "Мой вопрос",
|
||||||
variants: [
|
variants: [
|
||||||
"Вариант 1",
|
"Вариант 1",
|
||||||
"Вариант 2",
|
"Вариант 2",
|
||||||
"Вариант 3"
|
"Вариант 3"
|
||||||
],
|
],
|
||||||
correct: 0
|
correct: 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
question: "Мой вопрос",
|
||||||
|
variants: [
|
||||||
|
"Вариант 1",
|
||||||
|
"Вариант 2",
|
||||||
|
"Вариант 3"
|
||||||
|
],
|
||||||
|
correct: [0, 1]
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
class Quiz extends Find.createApp() {
|
class Quiz extends Find.createApp() {
|
||||||
constructor(qestions) {
|
constructor(questions) {
|
||||||
const root = Find.search('root');
|
const root = Find.search('root');
|
||||||
super(root);
|
super(root);
|
||||||
this.qestions = qestions;
|
this.question_type = undefined;
|
||||||
|
this.questions = questions;
|
||||||
|
this.question = null;
|
||||||
|
this.correctCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
@ -22,11 +35,12 @@ class Quiz extends Find.createApp() {
|
|||||||
this.setMeta();
|
this.setMeta();
|
||||||
this.setCard();
|
this.setCard();
|
||||||
this.stable();
|
this.stable();
|
||||||
this.createQestions();
|
this.createQuestions();
|
||||||
}
|
}
|
||||||
|
|
||||||
setCard() {
|
setCard() {
|
||||||
this.add([
|
this.add([
|
||||||
|
Find.content('<div id="message"></div>'),
|
||||||
Find.content(
|
Find.content(
|
||||||
`<div class="card">
|
`<div class="card">
|
||||||
<div class="card-header">
|
<div class="card-header">
|
||||||
@ -41,7 +55,7 @@ class Quiz extends Find.createApp() {
|
|||||||
<button
|
<button
|
||||||
class="button"
|
class="button"
|
||||||
style="width: 100%;"
|
style="width: 100%;"
|
||||||
id="test"
|
id="test_select"
|
||||||
>Проверить</button>
|
>Проверить</button>
|
||||||
</div>
|
</div>
|
||||||
</div>`
|
</div>`
|
||||||
@ -52,63 +66,274 @@ class Quiz extends Find.createApp() {
|
|||||||
setMeta() {
|
setMeta() {
|
||||||
this.meta([
|
this.meta([
|
||||||
Find.content('<meta name="viewport" content="width=device-width, initial-scale=1">'),
|
Find.content('<meta name="viewport" content="width=device-width, initial-scale=1">'),
|
||||||
Find.content('<style>body { margin: 20px; } </style>')
|
Find.content('<style>body { margin: 20px; } </style>'),
|
||||||
|
Find.content(
|
||||||
|
`<style>
|
||||||
|
.fit-image {
|
||||||
|
object-fit: contain;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
</style>`
|
||||||
|
)
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
createQestions() {
|
createQuestions() {
|
||||||
this.number = 0;
|
this.number = 0;
|
||||||
for (let i = 0; i < this.qestions.length; i ++) {
|
this.questionsWaiter(null);
|
||||||
this.select('qestion' + this.number);
|
}
|
||||||
|
|
||||||
|
questionsWaiter(status) {
|
||||||
|
const results = [
|
||||||
|
{
|
||||||
|
source: "no_select",
|
||||||
|
text: "Не выбран не один вариант!",
|
||||||
|
type: "error"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
source: "incorrect",
|
||||||
|
text: "Неправильный ответ!",
|
||||||
|
type: "error"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
source: "correct",
|
||||||
|
text: "Правильный ответ!",
|
||||||
|
type: "success"
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const isEnd = (this.questions.length === this.number);
|
||||||
|
if (!isEnd) this.question = this.questions[this.number];
|
||||||
|
this.select('question' + this.number);
|
||||||
this.setTitle();
|
this.setTitle();
|
||||||
this.render();
|
this.render();
|
||||||
this.setForms(this.qestions[i]);
|
this.setStatus(status);
|
||||||
this.setEvents(this.qestions[i])
|
this.setForms(isEnd);
|
||||||
.then((result) => {
|
this.attachDeleteButton();
|
||||||
|
if (isEnd) return;
|
||||||
|
this.setEvents().then((result) => {
|
||||||
|
if (result !== 'no_select') {
|
||||||
this.number ++;
|
this.number ++;
|
||||||
})
|
}
|
||||||
.catch((error) => {
|
|
||||||
|
|
||||||
|
let obj = undefined;
|
||||||
|
for (obj of results) {
|
||||||
|
if (obj.source == result) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.questionsWaiter(obj);
|
||||||
});
|
});
|
||||||
this.number ++;
|
}
|
||||||
|
|
||||||
|
setStatus(status) {
|
||||||
|
const root = this.component();
|
||||||
|
const message = root.id('message');
|
||||||
|
if (status !== null) {
|
||||||
|
const elem = status.type === 'error' ?
|
||||||
|
'is-danger' :
|
||||||
|
'is-success';
|
||||||
|
message.html(
|
||||||
|
Find.content(
|
||||||
|
`<div
|
||||||
|
class="notification ${elem}"
|
||||||
|
style="margin-bottom: 10px;">
|
||||||
|
<button class="delete" id="delete-info"></button>
|
||||||
|
${status.text}
|
||||||
|
</div>`
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setEvents() {}
|
attachDeleteButton() {
|
||||||
|
const root = this.component();
|
||||||
|
const button = root.id('delete-info');
|
||||||
|
if (button.status()) button.attach('click', app => {
|
||||||
|
const root = app.component();
|
||||||
|
root.id("message").html('<!-- deleted -->');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
setEvents() {
|
||||||
|
const root = this.component();
|
||||||
|
const que = this.question;
|
||||||
|
const main = this;
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const get_select = function() {
|
||||||
|
const button = root.id('test_select');
|
||||||
|
button.attach('click', () => {
|
||||||
|
const name = 'question';
|
||||||
|
const type = main.question_type;
|
||||||
|
let correct = false;
|
||||||
|
|
||||||
|
if (type === 'radio') {
|
||||||
|
const elem = root.select(`input[name="${name}"]:checked`);
|
||||||
|
|
||||||
|
if (!elem.status()) {
|
||||||
|
resolve('no_select');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const answer = Number(elem.value());
|
||||||
|
|
||||||
|
if (answer === que.correct) correct = true;
|
||||||
|
} else {
|
||||||
|
const elements = root.all('input[type="checkbox"]:checked');
|
||||||
|
|
||||||
|
if (elements.length === 0) {
|
||||||
|
resolve('no_select');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const answer = elements.map(elem => Number(elem.property('id').slice(7)));
|
||||||
|
|
||||||
|
let correct_count = 0;
|
||||||
|
for (let i = 0; i < que.correct.length; i ++) {
|
||||||
|
|
||||||
|
if (
|
||||||
|
answer[i] === undefined ||
|
||||||
|
answer[i] === null
|
||||||
|
) break;
|
||||||
|
|
||||||
|
if (answer[i] === que.correct[i]) {
|
||||||
|
correct_count ++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (correct_count === que.correct.length) {
|
||||||
|
correct = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (correct) {
|
||||||
|
main.correctCount ++;
|
||||||
|
resolve('correct');
|
||||||
|
} else {
|
||||||
|
resolve('incorrect');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
get_select();
|
||||||
|
} catch (err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
setTitle() {
|
setTitle() {
|
||||||
this.title('Quiz qestion ' + this.number + ' - Find.js demo');
|
const isEnd = (this.questions.length === this.number);
|
||||||
|
if (!isEnd) {
|
||||||
|
this.title(`Quiz вопрос ${String(this.number + 1)}/${this.questions.length} - Find.js демо`);
|
||||||
|
} else {
|
||||||
|
this.title('Результаты!');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setForms(qe) {
|
setForms(isDone) {
|
||||||
|
if (isDone) {
|
||||||
|
this.viewResults();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const que = this.question;
|
||||||
let input_id = 0;
|
let input_id = 0;
|
||||||
const root = this.component();
|
const root = this.component();
|
||||||
const content = root.id("content");
|
const content = root.id("content");
|
||||||
const title = root.id("title");
|
const title = root.id("title");
|
||||||
title.text(qe.qestion);
|
title.text(que.question);
|
||||||
for (let val in (qe.variants)) {
|
|
||||||
|
if (Array.isArray(que.correct)) {
|
||||||
|
this.question_type = 'checkbox';
|
||||||
|
} else {
|
||||||
|
this.question_type = 'radio';
|
||||||
|
}
|
||||||
|
|
||||||
|
let image = '<!-- no image -->';
|
||||||
|
if (typeof que.image !== "undefined") {
|
||||||
|
image = Find.content(
|
||||||
|
`<figure
|
||||||
|
class="fit-image"
|
||||||
|
style="margin-bottom: 20px;">
|
||||||
|
<img
|
||||||
|
src="${que.image}"
|
||||||
|
style="border-radius: 20px;"
|
||||||
|
alt="Изображение к вопросу">
|
||||||
|
</img>
|
||||||
|
</figure>`
|
||||||
|
);
|
||||||
|
content.add(image);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let val in (que.variants)) {
|
||||||
|
if (Array.isArray(que.correct)) {
|
||||||
|
this.question_type = 'checkbox';
|
||||||
|
content.add(
|
||||||
|
Find.content(
|
||||||
|
`<label class="checkbox">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
id="variant${input_id}"
|
||||||
|
value="${input_id}"
|
||||||
|
name="question">
|
||||||
|
</input>
|
||||||
|
<span
|
||||||
|
class="has-text-weight-medium"
|
||||||
|
id="text${input_id}">
|
||||||
|
</span>
|
||||||
|
</label>
|
||||||
|
<br />`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
content.add(
|
content.add(
|
||||||
Find.content(
|
Find.content(
|
||||||
`<input
|
`<input
|
||||||
type="radio"
|
type="radio"
|
||||||
id="variant${input_id}"
|
id="variant${input_id}"
|
||||||
name="radiobutton">
|
value="${input_id}"
|
||||||
|
name="question">
|
||||||
<label class="radio" for="variant${input_id}">
|
<label class="radio" for="variant${input_id}">
|
||||||
<span class="has-text-weight-medium" id="text${input_id}"></span>
|
<span class="has-text-weight-medium" id="text${input_id}"></span>
|
||||||
</label>
|
</label>
|
||||||
</input>
|
</input>
|
||||||
<br>`
|
<br />`
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
content.id('text' + input_id).text(qe.variants[val]);
|
}
|
||||||
|
content.id('text' + input_id).text(que.variants[val]);
|
||||||
input_id ++;
|
input_id ++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setEvents(r) {
|
viewResults() {
|
||||||
|
const root = this.component();
|
||||||
|
const button = root.id("test_select");
|
||||||
|
const content = root.id("content");
|
||||||
|
const title = root.id("title");
|
||||||
|
let text = 'Попробовать снова';
|
||||||
|
let out = ` - ${this.correctCount / (this.questions.length / 100)}%`;
|
||||||
|
const user =
|
||||||
|
this.correctCount !== this.questions.length ?
|
||||||
|
`${this.correctCount} вопросов из ${this.questions.length}.` :
|
||||||
|
'все вопросы!';
|
||||||
|
|
||||||
|
if (this.correctCount > (this.questions.length / 2)) {
|
||||||
|
text = 'На главную';
|
||||||
|
}
|
||||||
|
title.text(`Ваш результат ${out}`);
|
||||||
|
button.text(text);
|
||||||
|
content.html(
|
||||||
|
Find.content(
|
||||||
|
`<span class="has-text-weight-bold">
|
||||||
|
Вы правильно ответили на ${user}
|
||||||
|
</span>`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
button.attach('click', () => location.reload());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const quiz = new Quiz(qestions);
|
const quiz = new Quiz(questions);
|
||||||
quiz.init();
|
quiz.init();
|
||||||
2
demos/Quiz/nomodule-find.min.js
vendored
2
demos/Quiz/nomodule-find.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user