pad.libre-service.eu-etherpad/src/static/js/admin/settings.js

72 lines
2.3 KiB
JavaScript
Raw Normal View History

'use strict';
2020-11-23 19:24:19 +01:00
$(document).ready(() => {
const loc = document.location;
const port = loc.port === '' ? (loc.protocol === 'https:' ? 443 : 80) : loc.port;
2020-11-23 19:24:19 +01:00
const url = `${loc.protocol}//${loc.hostname}:${port}/`;
const pathComponents = location.pathname.split('/');
// Strip admin/plugins
const baseURL = `${pathComponents.slice(0, pathComponents.length - 2).join('/')}/`;
const resource = `${baseURL.substring(1)}socket.io`;
2020-11-23 19:24:19 +01:00
// connect
const room = `${url}settings`;
const socket = io.connect(room, {path: `${baseURL}socket.io`, resource});
2020-11-23 19:24:19 +01:00
socket.on('settings', (settings) => {
/* Check whether the settings.json is authorized to be viewed */
2020-11-23 19:24:19 +01:00
if (settings.results === 'NOT_ALLOWED') {
$('.innerwrapper').hide();
$('.innerwrapper-err').show();
2020-11-23 19:24:19 +01:00
$('.err-message').html('Settings json is not authorized to be viewed in Admin page!!');
return;
}
/* Check to make sure the JSON is clean before proceeding */
2020-11-23 19:24:19 +01:00
if (isJSONClean(settings.results)) {
$('.settings').append(settings.results);
$('.settings').focus();
$('.settings').autosize();
2020-11-23 19:24:19 +01:00
} else {
alert('YOUR JSON IS BAD AND YOU SHOULD FEEL BAD');
}
});
/* When the admin clicks save Settings check the JSON then send the JSON back to the server */
2020-11-23 19:24:19 +01:00
$('#saveSettings').on('click', () => {
const editedSettings = $('.settings').val();
if (isJSONClean(editedSettings)) {
// JSON is clean so emit it to the server
2020-11-23 19:24:19 +01:00
socket.emit('saveSettings', $('.settings').val());
} else {
alert('YOUR JSON IS BAD AND YOU SHOULD FEEL BAD');
$('.settings').focus();
}
});
/* Tell Etherpad Server to restart */
2020-11-23 19:24:19 +01:00
$('#restartEtherpad').on('click', () => {
socket.emit('restartServer');
});
2020-11-23 19:24:19 +01:00
socket.on('saveprogress', (progress) => {
2012-11-02 16:15:13 +01:00
$('#response').show();
$('#response').text(progress);
$('#response').fadeOut('slow');
});
2020-11-23 19:24:19 +01:00
socket.emit('load'); // Load the JSON from the server
});
const isJSONClean = (data) => {
2020-11-23 19:24:19 +01:00
let cleanSettings = JSON.minify(data);
2013-11-19 17:16:59 +01:00
// this is a bit naive. In theory some key/value might contain the sequences ',]' or ',}'
2020-11-23 19:24:19 +01:00
cleanSettings = cleanSettings.replace(',]', ']').replace(',}', '}');
try {
return typeof jQuery.parseJSON(cleanSettings) === 'object';
2020-11-23 19:24:19 +01:00
} catch (e) {
return false; // the JSON failed to be parsed
}
};