(function () { 'use strict'; if (window.fiSchoolServices) { return; } var token = null; function label(ru, en) { return document.documentElement.lang.toLowerCase().indexOf('en') === 0 ? en : ru; } function escape(value) { return String(value == null ? '' : value).replace(/[&<>"']/g, function (c) { return {'&': '&', '<': '<', '>': '>', '"': '"', "'": '''}[c]; }); } function bootstrap() { if (!token) { token = fetch('/services/api.php?bootstrap=1', {credentials: 'same-origin', cache: 'no-store'}) .then(function (response) { return response.json(); }); } return token; } function request(payload) { return bootstrap().then(function (config) { payload.token = config.token; return fetch('/services/api.php', {method: 'POST', credentials: 'same-origin', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(payload)}); }).then(function (response) { return response.json().then(function (data) { if (!response.ok || data.error) { throw new Error(data.error || 'Ошибка запроса'); } return data; }); }); } function popup(title, content) { var dialog = document.createElement('dialog'); dialog.className = 'fi-services-dialog'; dialog.innerHTML = '

' + escape(title) + '

' + content + '

'; document.body.appendChild(dialog); dialog.querySelector('[data-service-close]').addEventListener('click', function () { dialog.close(); }); dialog.addEventListener('close', function () { dialog.remove(); }); dialog.showModal(); return dialog; } function showMatches(matches) { var dialog = popup('Танцор уже есть в базе', matches.map(function (row) { return '
' + escape(row.name) + '

' + escape(row.school || 'Без школы') + (row.last_activity ? ' · Последний феш: ' + row.last_activity.split('-').reverse().join('.') : '') + '

' + (row.action === 'same_school' ? '

Танцор уже состоит в вашей школе.' + (Number(row.archived) ? ' Найдите его в архиве и восстановите.' : ' Используйте существующую запись.') + '

' : '

' + (row.action === 'transfer' ? 'Участия в фешах за последние 12 месяцев нет. Можно перенести ученика в вашу школу.' : 'Танцор состоит в другой школе. Отправьте запрос администратору на трансфер.') + '

' + '') + '
'; }).join('')); dialog.addEventListener('click', function (event) { var button = event.target.closest('[data-transfer-student]'); if (!button || button.disabled) { return; } if (!window.confirm(button.dataset.transferAction === 'transfer' ? 'Перенести ученика в вашу школу?' : 'Отправить заявку администратору?')) { return; } button.disabled = true; request({action: button.dataset.transferAction, student: Number(button.dataset.transferStudent)}) .then(function (data) { dialog.querySelector('[data-service-status]').textContent = data.message + ' Обновите список учеников.'; button.remove(); }).catch(function (error) { button.disabled = false; dialog.querySelector('[data-service-status]').textContent = error.message; }); }); } function contact() { var dialog = popup(label('Сообщение администратору', 'Message to the administrator'), '
' + '' + '' + '
'); bootstrap().then(function (config) { dialog.querySelector('[name="email"]').value = config.email || ''; }); dialog.querySelector('form').addEventListener('submit', function (event) { event.preventDefault(); var button = this.querySelector('button'); if (button.disabled) { return; } button.disabled = true; request({action: 'contact', email: this.elements.email.value, message: this.elements.message.value, website: this.elements.website.value, source: window.location.pathname}) .then(function (data) { dialog.querySelector('form').hidden = true; dialog.querySelector('[data-service-status]').textContent = label(data.message, 'Your message has been queued for the administrator.'); }) .catch(function (error) { button.disabled = false; dialog.querySelector('[data-service-status]').textContent = error.message; }); }); } function initialize() { if (document.querySelector('.fi-contact-entry')) { return; } var entry = document.createElement('div'); entry.className = 'fi-contact-entry'; var button = document.createElement('button'); button.type = 'button'; button.textContent = document.documentElement.lang.toLowerCase().indexOf('en') === 0 ? 'Send a message to the administrator' : 'Отправить сообщение администратору'; button.addEventListener('click', contact); entry.appendChild(button); document.body.appendChild(entry); } window.fiSchoolServices = {request: request, showMatches: showMatches, escape: escape, popup: popup}; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initialize); } else { initialize(); } })();