2020-12-08 09:20:59 +01:00
|
|
|
'use strict';
|
|
|
|
|
2020-12-10 23:24:28 +01:00
|
|
|
/* global socketio */
|
2012-11-02 14:16:15 +01:00
|
|
|
|
2020-12-10 23:24:28 +01:00
|
|
|
$(document).ready(() => {
|
|
|
|
const socket = socketio.connect('..', '/settings');
|
2012-11-02 14:16:15 +01:00
|
|
|
|
2020-12-14 08:50:52 +01:00
|
|
|
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 19:24:19 +01:00
|
|
|
socket.on('settings', (settings) => {
|
2016-05-22 17:28:51 +02:00
|
|
|
/* Check whether the settings.json is authorized to be viewed */
|
2020-11-23 19:24:19 +01:00
|
|
|
if (settings.results === 'NOT_ALLOWED') {
|
2016-05-22 17:28:51 +02:00
|
|
|
$('.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!!');
|
2016-05-22 17:28:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-02 15:31:52 +01:00
|
|
|
/* Check to make sure the JSON is clean before proceeding */
|
2020-11-23 19:24:19 +01:00
|
|
|
if (isJSONClean(settings.results)) {
|
2012-11-02 15:31:52 +01:00
|
|
|
$('.settings').append(settings.results);
|
2012-11-02 16:05:47 +01:00
|
|
|
$('.settings').focus();
|
2016-05-22 17:28:51 +02:00
|
|
|
$('.settings').autosize();
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
|
|
|
alert('YOUR JSON IS BAD AND YOU SHOULD FEEL BAD');
|
2019-04-16 00:34:29 +02:00
|
|
|
}
|
2012-11-02 16:05:47 +01:00
|
|
|
});
|
2012-11-02 15:31:52 +01:00
|
|
|
|
2012-11-02 16:05:47 +01:00
|
|
|
/* 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)) {
|
2012-11-02 16:05:47 +01:00
|
|
|
// 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');
|
2012-11-02 16:05:47 +01:00
|
|
|
$('.settings').focus();
|
|
|
|
}
|
|
|
|
});
|
2012-11-02 15:31:52 +01:00
|
|
|
|
2012-11-02 16:05:47 +01:00
|
|
|
/* Tell Etherpad Server to restart */
|
2020-11-23 19:24:19 +01:00
|
|
|
$('#restartEtherpad').on('click', () => {
|
|
|
|
socket.emit('restartServer');
|
2012-11-02 14:16:15 +01:00
|
|
|
});
|
|
|
|
|
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');
|
|
|
|
});
|
2012-11-02 14:16:15 +01:00
|
|
|
});
|
|
|
|
|
2012-11-02 16:05:47 +01:00
|
|
|
|
2020-12-08 09:20:59 +01:00
|
|
|
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 {
|
2020-12-08 09:20:59 +01:00
|
|
|
return typeof jQuery.parseJSON(cleanSettings) === 'object';
|
2020-11-23 19:24:19 +01:00
|
|
|
} catch (e) {
|
2012-11-02 15:31:52 +01:00
|
|
|
return false; // the JSON failed to be parsed
|
|
|
|
}
|
2020-12-08 09:20:59 +01:00
|
|
|
};
|