2020-11-27 10:40:05 -05:00
|
|
|
'use strict';
|
|
|
|
|
2020-12-10 17:24:28 -05:00
|
|
|
/* global socketio */
|
2020-11-23 13:24:19 -05:00
|
|
|
|
2020-12-10 17:24:28 -05:00
|
|
|
$(document).ready(() => {
|
|
|
|
const socket = socketio.connect('..', '/pluginfw/installer');
|
2020-12-14 02:50:52 -05:00
|
|
|
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();
|
|
|
|
});
|
2012-04-11 18:07:19 +02:00
|
|
|
|
2020-11-27 10:40:05 -05:00
|
|
|
const search = (searchTerm, limit) => {
|
|
|
|
if (search.searchTerm !== searchTerm) {
|
2020-11-23 13:24:19 -05:00
|
|
|
search.offset = 0;
|
|
|
|
search.results = [];
|
|
|
|
search.end = false;
|
2013-03-25 17:20:10 +01:00
|
|
|
}
|
2020-11-23 13:24:19 -05:00
|
|
|
limit = limit ? limit : search.limit;
|
2013-03-25 17:20:10 +01:00
|
|
|
search.searchTerm = searchTerm;
|
2020-11-27 10:40:05 -05:00
|
|
|
socket.emit('search', {
|
|
|
|
searchTerm,
|
|
|
|
offset: search.offset,
|
|
|
|
limit,
|
|
|
|
sortBy: search.sortBy,
|
|
|
|
sortDir: search.sortDir,
|
|
|
|
});
|
2013-03-25 23:09:03 +01:00
|
|
|
search.offset += limit;
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
$('#search-progress').show();
|
|
|
|
search.messages.show('fetching');
|
|
|
|
search.searching = true;
|
2020-11-27 10:40:05 -05:00
|
|
|
};
|
2013-09-12 16:37:37 +02:00
|
|
|
search.searching = false;
|
2013-03-25 23:09:03 +01:00
|
|
|
search.offset = 0;
|
2015-01-18 23:03:54 +00:00
|
|
|
search.limit = 999;
|
2013-03-25 23:09:03 +01:00
|
|
|
search.results = [];
|
|
|
|
search.sortBy = 'name';
|
2020-11-23 13:24:19 -05:00
|
|
|
search.sortDir = /* DESC?*/true;
|
2013-03-25 23:09:03 +01:00
|
|
|
search.end = true;// have we received all results already?
|
2013-03-26 21:04:21 +01:00
|
|
|
search.messages = {
|
2020-11-27 10:40:05 -05:00
|
|
|
show: (msg) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
// $('.search-results .messages').show()
|
|
|
|
$(`.search-results .messages .${msg}`).show();
|
|
|
|
$(`.search-results .messages .${msg} *`).show();
|
2013-03-26 21:04:21 +01:00
|
|
|
},
|
2020-11-27 10:40:05 -05:00
|
|
|
hide: (msg) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
$('.search-results .messages').hide();
|
|
|
|
$(`.search-results .messages .${msg}`).hide();
|
|
|
|
$(`.search-results .messages .${msg} *`).hide();
|
|
|
|
},
|
|
|
|
};
|
2013-03-25 17:20:10 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
const installed = {
|
2013-03-26 15:11:30 +01:00
|
|
|
progress: {
|
2020-11-27 10:40:05 -05:00
|
|
|
show: (plugin, msg) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
$(`.installed-results .${plugin} .progress`).show();
|
|
|
|
$(`.installed-results .${plugin} .progress .message`).text(msg);
|
2020-11-27 10:40:05 -05:00
|
|
|
if ($(window).scrollTop() > $(`.${plugin}`).offset().top) {
|
|
|
|
$(window).scrollTop($(`.${plugin}`).offset().top - 100);
|
|
|
|
}
|
2020-11-23 13:24:19 -05:00
|
|
|
},
|
2020-11-27 10:40:05 -05:00
|
|
|
hide: (plugin) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
$(`.installed-results .${plugin} .progress`).hide();
|
|
|
|
$(`.installed-results .${plugin} .progress .message`).text('');
|
2013-03-26 15:11:30 +01:00
|
|
|
},
|
2013-03-26 21:04:21 +01:00
|
|
|
},
|
|
|
|
messages: {
|
2020-11-27 10:40:05 -05:00
|
|
|
show: (msg) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
$('.installed-results .messages').show();
|
|
|
|
$(`.installed-results .messages .${msg}`).show();
|
|
|
|
},
|
2020-11-27 10:40:05 -05:00
|
|
|
hide: (msg) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
$('.installed-results .messages').hide();
|
|
|
|
$(`.installed-results .messages .${msg}`).hide();
|
2013-03-26 21:04:21 +01:00
|
|
|
},
|
2013-03-26 15:11:30 +01:00
|
|
|
},
|
2020-11-23 13:24:19 -05:00
|
|
|
list: [],
|
|
|
|
};
|
2013-03-26 15:11:30 +01:00
|
|
|
|
2020-11-27 10:40:05 -05:00
|
|
|
const displayPluginList = (plugins, container, template) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
plugins.forEach((plugin) => {
|
|
|
|
const row = template.clone();
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2020-11-27 10:40:05 -05:00
|
|
|
for (const attr in plugin) {
|
|
|
|
if (attr === 'name') { // Hack to rewrite URLS into name
|
2021-02-22 04:30:52 -05:00
|
|
|
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));
|
2018-04-09 22:08:43 +02:00
|
|
|
row.find('.name').append(link);
|
|
|
|
} else {
|
2020-11-23 13:24:19 -05:00
|
|
|
row.find(`.${attr}`).text(plugin[attr]);
|
2013-03-25 17:20:10 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-23 13:24:19 -05:00
|
|
|
row.find('.version').text(plugin.version);
|
|
|
|
row.addClass(plugin.name);
|
|
|
|
row.data('plugin', plugin.name);
|
2013-03-25 17:20:10 +01:00
|
|
|
container.append(row);
|
2020-11-23 13:24:19 -05:00
|
|
|
});
|
2013-03-25 17:20:10 +01:00
|
|
|
updateHandlers();
|
2020-11-27 10:40:05 -05:00
|
|
|
};
|
2012-04-18 13:43:34 +02:00
|
|
|
|
2020-11-27 10:40:05 -05: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 = () => {
|
2013-03-25 17:20:10 +01:00
|
|
|
// Search
|
2020-11-23 13:24:19 -05:00
|
|
|
$('#search-query').unbind('keyup').keyup(() => {
|
|
|
|
search($('#search-query').val());
|
2012-04-11 18:07:19 +02:00
|
|
|
});
|
2019-04-16 00:34:29 +02:00
|
|
|
|
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);
|
2012-04-11 18:07:19 +02:00
|
|
|
|
2013-03-25 17:20:10 +01:00
|
|
|
// 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');
|
2012-04-11 18:07:19 +02:00
|
|
|
});
|
|
|
|
|
2013-03-25 17:20:10 +01:00
|
|
|
// 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');
|
2020-11-27 10:40:05 -05:00
|
|
|
installed.list = installed.list.filter((plugin) => plugin.name !== pluginName);
|
2012-04-11 18:07:19 +02:00
|
|
|
});
|
2012-04-18 13:43:34 +02:00
|
|
|
|
2013-03-25 23:09:03 +01:00
|
|
|
// Sort
|
2020-11-23 13:24:19 -05:00
|
|
|
$('.sort.up').unbind('click').click(function () {
|
2015-01-24 12:49:17 +01:00
|
|
|
search.sortBy = $(this).attr('data-label').toLowerCase();
|
2013-03-25 23:09:03 +01:00
|
|
|
search.sortDir = false;
|
|
|
|
search.offset = 0;
|
|
|
|
search(search.searchTerm, search.results.length);
|
2013-03-26 11:38:51 +01:00
|
|
|
search.results = [];
|
2020-11-23 13:24:19 -05:00
|
|
|
});
|
|
|
|
$('.sort.down, .sort.none').unbind('click').click(function () {
|
2015-01-24 12:49:17 +01:00
|
|
|
search.sortBy = $(this).attr('data-label').toLowerCase();
|
2013-03-25 23:09:03 +01:00
|
|
|
search.sortDir = true;
|
|
|
|
search.offset = 0;
|
2013-03-26 11:38:51 +01:00
|
|
|
search(search.searchTerm, search.results.length);
|
2013-03-25 23:09:03 +01:00
|
|
|
search.results = [];
|
2020-11-23 13:24:19 -05:00
|
|
|
});
|
2020-11-27 10:40:05 -05:00
|
|
|
};
|
2012-04-11 18:07:19 +02:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
socket.on('results:search', (data) => {
|
|
|
|
if (!data.results.length) search.end = true;
|
2020-11-27 10:40:05 -05:00
|
|
|
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');
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
console.log('got search results', data);
|
2013-03-25 23:09:03 +01:00
|
|
|
|
|
|
|
// 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');
|
2013-03-25 23:09:03 +01:00
|
|
|
|
|
|
|
// 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) {
|
2020-11-27 10:40:05 -05:00
|
|
|
displayPluginList(
|
|
|
|
search.results, searchWidget.find('.results'), searchWidget.find('.template tr'));
|
2020-11-23 13:24:19 -05:00
|
|
|
} else {
|
|
|
|
search.messages.show('nothing-found');
|
2013-03-26 16:23:47 +01:00
|
|
|
}
|
2020-11-23 13:24:19 -05:00
|
|
|
search.messages.hide('fetching');
|
|
|
|
$('#search-progress').hide();
|
|
|
|
search.searching = false;
|
2012-04-11 18:07:19 +02:00
|
|
|
});
|
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
socket.on('results:installed', (data) => {
|
|
|
|
installed.messages.hide('fetching');
|
|
|
|
installed.messages.hide('nothing-installed');
|
2013-03-26 16:23:47 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
installed.list = data.installed;
|
|
|
|
sortPluginList(installed.list, 'name', /* ASC?*/true);
|
2013-03-25 23:09:03 +01:00
|
|
|
|
2013-03-26 15:11:30 +01:00
|
|
|
// filter out epl
|
2020-11-27 10:40:05 -05:00
|
|
|
installed.list = installed.list.filter((plugin) => plugin.name !== 'ep_etherpad-lite');
|
2013-01-26 22:15:19 +01:00
|
|
|
|
2013-03-26 16:23:47 +01:00
|
|
|
// 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();
|
|
|
|
});
|
2013-03-26 16:23:47 +01:00
|
|
|
|
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');
|
2013-03-26 16:23:47 +01:00
|
|
|
}
|
2012-04-11 18:07:19 +02:00
|
|
|
});
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
socket.on('results:updatable', (data) => {
|
|
|
|
data.updatable.forEach((pluginName) => {
|
2020-11-27 11:17:03 -05:00
|
|
|
const actions = $(`#installed-plugins > tr.${pluginName} .actions`);
|
2020-11-27 11:04:05 -05:00
|
|
|
actions.find('.do-update').remove();
|
2020-11-27 11:03:42 -05:00
|
|
|
actions.append(
|
|
|
|
$('<input>').addClass('do-update').attr('type', 'button').attr('value', 'Update'));
|
2020-11-23 13:24:19 -05:00
|
|
|
});
|
2013-01-26 22:15:19 +01:00
|
|
|
updateHandlers();
|
2020-11-23 13:24:19 -05:00
|
|
|
});
|
2012-04-11 18:07:19 +02:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
socket.on('finished:install', (data) => {
|
|
|
|
if (data.error) {
|
|
|
|
if (data.code === 'EPEERINVALID') {
|
2015-01-25 02:44:10 +00:00
|
|
|
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();
|
2013-03-26 15:11:30 +01:00
|
|
|
}
|
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
socket.emit('getInstalled');
|
2013-03-26 11:58:31 +01:00
|
|
|
|
|
|
|
// update search results
|
|
|
|
search.offset = 0;
|
|
|
|
search(search.searchTerm, search.results.length);
|
|
|
|
search.results = [];
|
2020-11-23 13:24:19 -05:00
|
|
|
});
|
2013-03-26 11:19:36 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
socket.on('finished:uninstall', (data) => {
|
2020-11-27 10:40:05 -05:00
|
|
|
if (data.error) {
|
|
|
|
alert(`An error occurred while uninstalling the ${data.plugin} \n${data.error}`);
|
|
|
|
}
|
2013-03-26 15:11:30 +01:00
|
|
|
|
|
|
|
// remove plugin from installed list
|
2020-11-23 13:24:19 -05:00
|
|
|
$(`#installed-plugins .${data.plugin}`).remove();
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
socket.emit('getInstalled');
|
2013-03-26 15:11:30 +01:00
|
|
|
|
2013-03-26 11:58:31 +01:00
|
|
|
// update search results
|
|
|
|
search.offset = 0;
|
|
|
|
search(search.searchTerm, search.results.length);
|
|
|
|
search.results = [];
|
2020-11-23 13:24:19 -05:00
|
|
|
});
|
2013-03-26 11:19:36 +01:00
|
|
|
|
2020-12-14 02:50:52 -05:00
|
|
|
socket.on('connect', () => {
|
|
|
|
updateHandlers();
|
|
|
|
socket.emit('getInstalled');
|
|
|
|
search.searchTerm = null;
|
|
|
|
search($('#search-query').val());
|
|
|
|
});
|
2013-03-26 15:11:30 +01:00
|
|
|
|
2013-03-27 12:02:19 +01:00
|
|
|
// check for updates every 5mins
|
2020-11-23 13:24:19 -05:00
|
|
|
setInterval(() => {
|
2013-03-26 15:11:30 +01:00
|
|
|
socket.emit('checkUpdates');
|
2020-11-23 13:24:19 -05:00
|
|
|
}, 1000 * 60 * 5);
|
2012-04-11 18:07:19 +02:00
|
|
|
});
|