Files
lpra/internal/server/web/static/updatenote.js
T

48 lines
2.5 KiB
JavaScript

// A new release of the server: an administrator is offered it when they come
// in - after the PIN, or at once when there is none - once for each tab. The
// window says the versions and has «Обновить» and «Отказаться»; a refusal is
// kept for that release (a still newer one asks again). Only when the server
// is set to look for releases (Настройки › Автообновление).
(function () {
'use strict';
var root = document.documentElement;
var SHOWN = 'lpra.upd.shown';
// The PIN lock hides the page until it is unlocked.
function whenUnlocked(fn) {
if (!root.classList.contains('pin-wait')) { fn(); return; }
var mo = new MutationObserver(function () {
if (!root.classList.contains('pin-wait')) { mo.disconnect(); fn(); }
});
mo.observe(root, {attributes: true, attributeFilter: ['class']});
}
function toast(text, bad) {
if (window.FileViews && FileViews.toast) FileViews.toast(text, bad);
}
function post(url, body) {
return fetch(url, {method: 'POST', headers: {'Content-Type': 'application/json'}, credentials: 'same-origin',
body: JSON.stringify(body || {})}).then(function (r) { return r.json(); })
.catch(function () { return {ok: false, error: 'нет связи с сервером'}; });
}
function offer() {
try { if (sessionStorage.getItem(SHOWN) === '1') return; } catch (e) { /* offered again */ }
fetch('/api/panel/update/offer', {cache: 'no-store', credentials: 'same-origin'})
.then(function (r) { return r.ok ? r.json() : {}; })
.then(function (d) {
if (!d.offer || !window.UI) return;
try { sessionStorage.setItem(SHOWN, '1'); } catch (e) { /* not remembered */ }
UI.confirm('Вышла новая версия LPRA {chip}\nОбновить сервер сейчас? Панель будет недоступна около минуты.',
{ok: 'Обновить', cancel: 'Отказаться', chip: d.current + ' → ' + d.latest}).then(function (yes) {
if (!yes) { post('/api/panel/update/decline', {version: d.latest}); return; }
post('/api/panel/update/run').then(function (r) {
if (r.ok) toast('Обновление до ' + r.latest + ' запущено: сервер перезапустится в течение минуты - потом обновите страницу');
else toast(r.error || 'обновить не получилось', true);
});
});
})
.catch(function () { /* no offer */ });
}
whenUnlocked(offer);
})();