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

274 lines
8.4 KiB
JavaScript
Raw Normal View History

'use strict';
/* global socketio */
2020-11-23 13:24:19 -05:00
$(document).ready(() => {
const socket = socketio.connect('..', '/pluginfw/installer');
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();
});
const search = (searchTerm, limit) => {
if (search.searchTerm !== searchTerm) {
2020-11-23 13:24:19 -05:00
search.offset = 0;
search.results = [];
search.end = false;
}
2020-11-23 13:24:19 -05:00
limit = limit ? limit : search.limit;
search.searchTerm = searchTerm;
socket.emit('search', {
searchTerm,
offset: search.offset,
limit,
sortBy: search.sortBy,
sortDir: search.sortDir,
});
search.offset += limit;
2020-11-23 13:24:19 -05:00
$('#search-progress').show();
search.messages.show('fetching');
search.searching = true;
};
2013-09-12 16:37:37 +02:00
search.searching = false;
search.offset = 0;
2015-01-18 23:03:54 +00:00
search.limit = 999;
search.results = [];
search.sortBy = 'name';
2020-11-23 13:24:19 -05:00
search.sortDir = /* DESC?*/true;
search.end = true;// have we received all results already?
search.messages = {
show: (msg) => {
2020-11-23 13:24:19 -05:00
// $('.search-results .messages').show()
$(`.search-results .messages .${msg}`).show();
$(`.search-results .messages .${msg} *`).show();
},
hide: (msg) => {
2020-11-23 13:24:19 -05:00
$('.search-results .messages').hide();
$(`.search-results .messages .${msg}`).hide();
$(`.search-results .messages .${msg} *`).hide();
},
};
2020-11-23 13:24:19 -05:00
const installed = {
progress: {
show: (plugin, msg) => {
2020-11-23 13:24:19 -05:00
$(`.installed-results .${plugin} .progress`).show();
$(`.installed-results .${plugin} .progress .message`).text(msg);
if ($(window).scrollTop() > $(`.${plugin}`).offset().top) {
$(window).scrollTop($(`.${plugin}`).offset().top - 100);
}
2020-11-23 13:24:19 -05:00
},
hide: (plugin) => {
2020-11-23 13:24:19 -05:00
$(`.installed-results .${plugin} .progress`).hide();
$(`.installed-results .${plugin} .progress .message`).text('');
},
},
messages: {
show: (msg) => {
2020-11-23 13:24:19 -05:00
$('.installed-results .messages').show();
$(`.installed-results .messages .${msg}`).show();
},
hide: (msg) => {
2020-11-23 13:24:19 -05:00
$('.installed-results .messages').hide();
$(`.installed-results .messages .${msg}`).hide();
},
},
2020-11-23 13:24:19 -05:00
list: [],
};
const displayPluginList = (plugins, container, template) => {
2020-11-23 13:24:19 -05:00
plugins.forEach((plugin) => {
const row = template.clone();
for (const attr in plugin) {
if (attr === 'name') { // Hack to rewrite URLS into name
const link = $('<a>')
.attr('href', `https://npmjs.org/package/${plugin.name}`)
.attr('plugin', 'Plugin details')
.attr('rel', 'noopener noreferrer')
.attr('target', '_blank')
.text(plugin.name.substr(3));
row.find('.name').append(link);
} else {
2020-11-23 13:24:19 -05:00
row.find(`.${attr}`).text(plugin[attr]);
}
}
2020-11-23 13:24:19 -05:00
row.find('.version').text(plugin.version);
row.addClass(plugin.name);
row.data('plugin', plugin.name);
container.append(row);
2020-11-23 13:24:19 -05:00
});
updateHandlers();
};
2012-04-18 13:43:34 +02:00
const sortPluginList = (plugins, property, /* ASC?*/dir) => plugins.sort((a, b) => {
if (a[property] < b[property]) return dir ? -1 : 1;
if (a[property] > b[property]) return dir ? 1 : -1;
// a must be equal to b
return 0;
});
const updateHandlers = () => {
// Search
2020-11-23 13:24:19 -05:00
$('#search-query').unbind('keyup').keyup(() => {
search($('#search-query').val());
});
2015-01-18 12:15:41 +01:00
// Prevent form submit
2020-11-23 13:24:19 -05:00
$('#search-query').parent().bind('submit', () => false);
// update & install
2020-11-23 13:24:19 -05:00
$('.do-install, .do-update').unbind('click').click(function (e) {
const $row = $(e.target).closest('tr');
const plugin = $row.data('plugin');
if ($(this).hasClass('do-install')) {
$row.remove().appendTo('#installed-plugins');
installed.progress.show(plugin, 'Installing');
} else {
installed.progress.show(plugin, 'Updating');
2013-03-27 12:02:19 +01:00
}
2020-11-23 13:24:19 -05:00
socket.emit('install', plugin);
installed.messages.hide('nothing-installed');
});
// uninstall
2020-11-23 13:24:19 -05:00
$('.do-uninstall').unbind('click').click((e) => {
const $row = $(e.target).closest('tr');
const pluginName = $row.data('plugin');
socket.emit('uninstall', pluginName);
installed.progress.show(pluginName, 'Uninstalling');
installed.list = installed.list.filter((plugin) => plugin.name !== pluginName);
});
2012-04-18 13:43:34 +02:00
// Sort
2020-11-23 13:24:19 -05:00
$('.sort.up').unbind('click').click(function () {
search.sortBy = $(this).attr('data-label').toLowerCase();
search.sortDir = false;
search.offset = 0;
search(search.searchTerm, search.results.length);
search.results = [];
2020-11-23 13:24:19 -05:00
});
$('.sort.down, .sort.none').unbind('click').click(function () {
search.sortBy = $(this).attr('data-label').toLowerCase();
search.sortDir = true;
search.offset = 0;
search(search.searchTerm, search.results.length);
search.results = [];
2020-11-23 13:24:19 -05:00
});
};
2020-11-23 13:24:19 -05:00
socket.on('results:search', (data) => {
if (!data.results.length) search.end = true;
if (data.query.offset === 0) search.results = [];
2020-11-23 13:24:19 -05:00
search.messages.hide('nothing-found');
search.messages.hide('fetching');
$('#search-query').removeAttr('disabled');
2020-11-23 13:24:19 -05:00
console.log('got search results', data);
// add to results
search.results = search.results.concat(data.results);
// Update sorting head
$('.sort')
2020-11-23 13:24:19 -05:00
.removeClass('up down')
.addClass('none');
$(`.search-results thead th[data-label=${data.query.sortBy}]`)
.removeClass('none')
.addClass(data.query.sortDir ? 'up' : 'down');
// re-render search results
2020-11-23 13:24:19 -05:00
const searchWidget = $('.search-results');
searchWidget.find('.results *').remove();
if (search.results.length > 0) {
displayPluginList(
search.results, searchWidget.find('.results'), searchWidget.find('.template tr'));
2020-11-23 13:24:19 -05:00
} else {
search.messages.show('nothing-found');
}
2020-11-23 13:24:19 -05:00
search.messages.hide('fetching');
$('#search-progress').hide();
search.searching = false;
});
2020-11-23 13:24:19 -05:00
socket.on('results:installed', (data) => {
installed.messages.hide('fetching');
installed.messages.hide('nothing-installed');
2020-11-23 13:24:19 -05:00
installed.list = data.installed;
sortPluginList(installed.list, 'name', /* ASC?*/true);
// filter out epl
installed.list = installed.list.filter((plugin) => plugin.name !== 'ep_etherpad-lite');
// remove all installed plugins (leave plugins that are still being installed)
2020-11-23 13:24:19 -05:00
installed.list.forEach((plugin) => {
$(`#installed-plugins .${plugin.name}`).remove();
});
2020-11-23 13:24:19 -05:00
if (installed.list.length > 0) {
displayPluginList(installed.list, $('#installed-plugins'), $('#installed-plugin-template'));
2013-03-27 12:02:19 +01:00
socket.emit('checkUpdates');
2020-11-23 13:24:19 -05:00
} else {
installed.messages.show('nothing-installed');
}
});
2020-11-23 13:24:19 -05:00
socket.on('results:updatable', (data) => {
data.updatable.forEach((pluginName) => {
const actions = $(`#installed-plugins > tr.${pluginName} .actions`);
actions.find('.do-update').remove();
actions.append(
$('<input>').addClass('do-update').attr('type', 'button').attr('value', 'Update'));
2020-11-23 13:24:19 -05:00
});
updateHandlers();
2020-11-23 13:24:19 -05:00
});
2020-11-23 13:24:19 -05:00
socket.on('finished:install', (data) => {
if (data.error) {
if (data.code === 'EPEERINVALID') {
alert("This plugin requires that you update Etherpad so it can operate in it's true glory");
}
2020-11-23 13:24:19 -05:00
alert(`An error occurred while installing ${data.plugin} \n${data.error}`);
$(`#installed-plugins .${data.plugin}`).remove();
}
2020-11-23 13:24:19 -05:00
socket.emit('getInstalled');
// update search results
search.offset = 0;
search(search.searchTerm, search.results.length);
search.results = [];
2020-11-23 13:24:19 -05:00
});
2020-11-23 13:24:19 -05:00
socket.on('finished:uninstall', (data) => {
if (data.error) {
alert(`An error occurred while uninstalling the ${data.plugin} \n${data.error}`);
}
// remove plugin from installed list
2020-11-23 13:24:19 -05:00
$(`#installed-plugins .${data.plugin}`).remove();
2020-11-23 13:24:19 -05:00
socket.emit('getInstalled');
// update search results
search.offset = 0;
search(search.searchTerm, search.results.length);
search.results = [];
2020-11-23 13:24:19 -05:00
});
socket.on('connect', () => {
updateHandlers();
socket.emit('getInstalled');
search.searchTerm = null;
search($('#search-query').val());
});
2013-03-27 12:02:19 +01:00
// check for updates every 5mins
2020-11-23 13:24:19 -05:00
setInterval(() => {
socket.emit('checkUpdates');
2020-11-23 13:24:19 -05:00
}, 1000 * 60 * 5);
});