js: Simplify auto-refresh page logic

- Implement ability to refresh page at the framework level so that every page
does not need to handle it.

- Refresh after number of seconds specified in context of the view.

Tests performed:

- Trigger the following functions and ensure that page reload after 3 seconds
during the running operation while it does refresh before and after the
operation.
  - Diagnostics tests from the module.
  - Gitweb repository cloning.
  - Monkeysphere publish key to server.
  - OpenVPN setup.
  - Tor configuration update.
  - Manual software update.
  - App installation.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
Sunil Mohan Adapa authored and Veiko Aasa committed 2020-06-29 10:09:04 +03:00
1 parent 1b5a10a628
commit 3efff2fa42
18 files changed
+45 -176

No files matched your search

+18
View File
@@ -22,6 +22,24 @@
for the JavaScript code in this page.
*/
/*
* Refresh page if marked for refresh.
*/
document.addEventListener('DOMContentLoaded', function() {
const body = document.querySelector('body');
if (body.hasAttribute('data-refresh-page-sec')) {
let seconds = body.getAttribute('data-refresh-page-sec');
seconds = parseInt(seconds, 10);
if (isNaN(seconds))
return;
window.setTimeout(() => {
// Refresh the page without resubmitting the POST data.
window.location = window.location.href;
}, seconds * 1000);
}
});
/*
* Disable submit button on click.
*/
-36
View File
@@ -1,36 +0,0 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
/**
* @licstart The following is the entire license notice for the JavaScript
* code in this page.
*
* This file is part of FreedomBox.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @licend The above is the entire license notice for the JavaScript code
* in this page.
*/
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Refresh the page once in n seconds
async function refresh(ms) {
await sleep(3000);
// Refresh the page without resubmitting the POST data.
window.location = window.location.href;
}
refresh();
@@ -1,25 +0,0 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
/**
* @licstart The following is the entire license notice for the JavaScript
* code in this page.
*
* This file is part of FreedomBox.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @licend The above is the entire license notice for the JavaScript code
* in this page.
*/
window.location.reload();