2023-01-15 21:47:19 +00:00
|
|
|
|
const request = new XMLHttpRequest();
|
|
|
|
|
|
|
|
|
|
const training_handler = () => {
|
2023-01-18 11:43:45 +00:00
|
|
|
|
|
|
|
|
|
const url = (condition) => {
|
|
|
|
|
return condition ? '/api/training/complete' : '/api/training/go_back';
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-08 15:14:19 +00:00
|
|
|
|
request.open('POST', url(training_data().answer == null || training_data().answer == get_user_answer()))
|
2023-01-15 21:47:19 +00:00
|
|
|
|
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
|
2023-01-18 11:43:45 +00:00
|
|
|
|
request.onreadystatechange = function () {//Call a function when the state changes.
|
2023-04-08 15:14:19 +00:00
|
|
|
|
if (request.readyState == 4) {
|
|
|
|
|
if(request.status == 200) {
|
2023-01-18 11:43:45 +00:00
|
|
|
|
parent.frames['main'].location.reload();
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 15:14:19 +00:00
|
|
|
|
if(request.responseText != '') {
|
2023-01-18 11:43:45 +00:00
|
|
|
|
let response = JSON.parse(request.responseText);
|
|
|
|
|
|
2023-04-08 15:14:19 +00:00
|
|
|
|
if (response.message != undefined) {
|
2023-01-18 11:43:45 +00:00
|
|
|
|
alert(response.message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-15 21:47:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request.send(JSON.stringify({
|
|
|
|
|
time: training_data().time,
|
|
|
|
|
token: training_data().token,
|
2023-01-18 11:43:45 +00:00
|
|
|
|
short_name: training_data().short_name,
|
|
|
|
|
answer: get_user_answer(),
|
2023-01-15 21:47:19 +00:00
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-18 11:43:45 +00:00
|
|
|
|
function get_user_answer() {
|
2023-04-08 15:14:19 +00:00
|
|
|
|
if(training_data().answer == '') {
|
2023-01-18 11:43:45 +00:00
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return document.getElementsByName('user_answer')[0].value;
|
|
|
|
|
}
|
2023-01-15 21:47:19 +00:00
|
|
|
|
|
2023-01-12 20:38:13 +00:00
|
|
|
|
(function () {
|
2023-04-08 15:14:19 +00:00
|
|
|
|
if (typeof window.CustomEvent == "function") return false;
|
2023-01-12 20:38:13 +00:00
|
|
|
|
function CustomEvent(event, params) {
|
|
|
|
|
params = params || { bubbles: false, cancelable: false, detail: null };
|
|
|
|
|
var evt = document.createEvent('CustomEvent');
|
|
|
|
|
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
|
|
|
|
|
return evt;
|
|
|
|
|
}
|
|
|
|
|
window.CustomEvent = CustomEvent;
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
$modal = function (options) {
|
|
|
|
|
var
|
|
|
|
|
_elemModal,
|
|
|
|
|
_eventShowModal,
|
|
|
|
|
_eventHideModal,
|
|
|
|
|
_hiding = false,
|
|
|
|
|
_destroyed = false,
|
|
|
|
|
_animationSpeed = 200;
|
|
|
|
|
|
|
|
|
|
function _createModal(options) {
|
|
|
|
|
var
|
|
|
|
|
elemModal = document.createElement('div'),
|
|
|
|
|
modalTemplate = '<div class="modal__backdrop" data-dismiss="modal"><div class="modal__content"><div class="modal__header"><div class="modal__title" data-modal="title">{{title}}</div><span class="modal__btn-close" data-dismiss="modal" title="Закрыть">×</span></div><div id="modal_content" class="modal__body" data-modal="content">{{content}}</div>{{footer}}</div></div>',
|
|
|
|
|
modalFooterTemplate = '<div class="modal__footer">{{buttons}}</div>',
|
2023-01-18 11:43:45 +00:00
|
|
|
|
modalInputAnswer = '',
|
2023-01-12 20:38:13 +00:00
|
|
|
|
modalButtonTemplate = '<button type="button" class="{{button_class}}" data-handler={{button_handler}}>{{button_text}}</button>',
|
|
|
|
|
modalHTML,
|
|
|
|
|
modal_content,
|
|
|
|
|
modalFooterHTML = '';
|
|
|
|
|
|
2023-04-08 15:14:19 +00:00
|
|
|
|
if(training_data().answer != '') {
|
2023-01-18 11:43:45 +00:00
|
|
|
|
modalInputAnswer = '<br><br><input name="user_answer" placeholder="Укажите ответ"/>';
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 20:38:13 +00:00
|
|
|
|
elemModal.classList.add('modal');
|
|
|
|
|
modalHTML = modalTemplate.replace('{{title}}', options.title || 'Новое окно');
|
|
|
|
|
|
|
|
|
|
if (options.footerButtons) {
|
|
|
|
|
for (var i = 0, length = options.footerButtons.length; i < length; i++) {
|
|
|
|
|
var modalFooterButton = modalButtonTemplate.replace('{{button_class}}', options.footerButtons[i].class);
|
|
|
|
|
modalFooterButton = modalFooterButton.replace('{{button_handler}}', options.footerButtons[i].handler);
|
2023-01-18 11:43:45 +00:00
|
|
|
|
modalFooterButton = modalFooterButton.replace('{{button_text}}', options.footerButtons[i].text) + modalInputAnswer;
|
2023-01-12 20:38:13 +00:00
|
|
|
|
modalFooterHTML += modalFooterButton;
|
|
|
|
|
}
|
|
|
|
|
modalFooterHTML = modalFooterTemplate.replace('{{buttons}}', modalFooterHTML);
|
|
|
|
|
}
|
|
|
|
|
modalHTML = modalHTML.replace('{{footer}}', modalFooterHTML);
|
|
|
|
|
elemModal.innerHTML = modalHTML;
|
|
|
|
|
document.body.appendChild(elemModal);
|
|
|
|
|
|
|
|
|
|
modal_content = document.getElementById('modal_content')
|
|
|
|
|
modal_content.innerHTML = options.content
|
|
|
|
|
|
|
|
|
|
return elemModal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _showModal() {
|
|
|
|
|
if (!_destroyed && !_hiding) {
|
|
|
|
|
_elemModal.classList.add('modal__show');
|
|
|
|
|
document.dispatchEvent(_eventShowModal);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _hideModal() {
|
|
|
|
|
_hiding = true;
|
|
|
|
|
_elemModal.classList.remove('modal__show');
|
|
|
|
|
_elemModal.classList.add('modal__hiding');
|
|
|
|
|
setTimeout(function () {
|
|
|
|
|
_elemModal.classList.remove('modal__hiding');
|
|
|
|
|
_hiding = false;
|
|
|
|
|
}, _animationSpeed);
|
|
|
|
|
document.dispatchEvent(_eventHideModal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _handlerCloseModal(e) {
|
2023-04-08 15:14:19 +00:00
|
|
|
|
if (e.target.dataset.dismiss == 'modal') {
|
2023-01-12 20:38:13 +00:00
|
|
|
|
_hideModal();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_elemModal = _createModal(options || {});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_elemModal.addEventListener('click', _handlerCloseModal);
|
|
|
|
|
_eventShowModal = new CustomEvent('show.modal', { detail: _elemModal });
|
|
|
|
|
_eventHideModal = new CustomEvent('hide.modal', { detail: _elemModal });
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
show: _showModal,
|
|
|
|
|
hide: _hideModal,
|
|
|
|
|
destroy: function () {
|
|
|
|
|
_elemModal.parentElement.removeChild(_elemModal),
|
|
|
|
|
_elemModal.removeEventListener('click', _handlerCloseModal),
|
|
|
|
|
_destroyed = true;
|
|
|
|
|
},
|
|
|
|
|
setContent: function (html) {
|
|
|
|
|
_elemModal.querySelector('[data-modal="content"]').innerHTML = html;
|
|
|
|
|
},
|
|
|
|
|
setTitle: function (text) {
|
|
|
|
|
_elemModal.querySelector('[data-modal="title"]').innerHTML = text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let modal = $modal({
|
|
|
|
|
title: training_data().title,
|
|
|
|
|
content: training_data().content,
|
|
|
|
|
footerButtons: [
|
|
|
|
|
{
|
|
|
|
|
class: 'btn btn__ok',
|
|
|
|
|
text: 'Продолжить',
|
|
|
|
|
handler: 'next_step'
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
document.addEventListener('click', function (e) {
|
2023-04-08 15:14:19 +00:00
|
|
|
|
if (e.target.dataset.handler == 'next_step') {
|
2023-01-12 20:38:13 +00:00
|
|
|
|
training_handler()
|
|
|
|
|
}
|
|
|
|
|
})
|