etherpad-lite/src/static/js/admin/settings.js

70 lines
2 KiB
JavaScript
Raw Normal View History

'use strict';
$(document).ready(() => {
const socket = window.socketio.connect('..', '/settings');
socket.on('connect', () => {
socket.emit('load');
});
socket.on('disconnect', (reason) => {
// The socket.io client will automatically try to reconnect for all reasons other than "io
// server disconnect".
if (reason === 'io server disconnect') socket.connect();
});
2020-11-23 13:24:19 -05:00
socket.on('settings', (settings) => {
/* Check whether the settings.json is authorized to be viewed */
2020-11-23 13:24:19 -05:00
if (settings.results === 'NOT_ALLOWED') {
$('.innerwrapper').hide();
$('.innerwrapper-err').show();
2020-11-23 13:24:19 -05: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 13:24:19 -05:00
if (isJSONClean(settings.results)) {
$('.settings').append(settings.results);
$('.settings').trigger('focus');
$('.settings').autosize();
2020-11-23 13:24:19 -05:00
} else {
alert('Invalid JSON');
}
});
/* When the admin clicks save Settings check the JSON then send the JSON back to the server */
2020-11-23 13:24:19 -05:00
$('#saveSettings').on('click', () => {
const editedSettings = $('.settings').val();
if (isJSONClean(editedSettings)) {
// JSON is clean so emit it to the server
2020-11-23 13:24:19 -05:00
socket.emit('saveSettings', $('.settings').val());
} else {
alert('Invalid JSON');
$('.settings').trigger('focus');
}
});
/* Tell Etherpad Server to restart */
2020-11-23 13:24:19 -05:00
$('#restartEtherpad').on('click', () => {
socket.emit('restartServer');
});
2020-11-23 13:24:19 -05:00
socket.on('saveprogress', (progress) => {
2012-11-02 15:15:13 +00:00
$('#response').show();
$('#response').text(progress);
$('#response').fadeOut('slow');
});
});
const isJSONClean = (data) => {
2020-11-23 13:24:19 -05:00
let cleanSettings = JSON.minify(data);
2013-11-19 18:16:59 +02:00
// this is a bit naive. In theory some key/value might contain the sequences ',]' or ',}'
2020-11-23 13:24:19 -05:00
cleanSettings = cleanSettings.replace(',]', ']').replace(',}', '}');
try {
return typeof JSON.parseJSON(cleanSettings) === 'object';
2020-11-23 13:24:19 -05:00
} catch (e) {
return false; // the JSON failed to be parsed
}
};