Revamp /admin/plugins

- dry up the client-side code
- use the new saner API of pluginfw/installer.js on the server
- Improve UX: allow user to infinitely scroll to display their results
This commit is contained in:
Marcel Klehr 2013-03-25 17:20:10 +01:00
parent b297784288
commit 079fdf0f38
3 changed files with 105 additions and 169 deletions

View file

@ -27,48 +27,66 @@ exports.socketio = function (hook_name, args, cb) {
io.on('connection', function (socket) {
if (!socket.handshake.session.user || !socket.handshake.session.user.is_admin) return;
socket.on("load", function (query) {
socket.on("getInstalled", function (query) {
// send currently installed plugins
socket.emit("installed-results", {results: plugins.plugins});
socket.emit("progress", {progress:1});
var installed = Object.keys(plugins.plugins).map(function(plugin) {
return plugins.plugins[plugin].package
})
socket.emit("results:installed", {installed: installed});
});
socket.on("checkUpdates", function() {
socket.emit("progress", {progress:0, message:'Checking for plugin updates...'});
socket.on("getUpdatable", function() {
// Check plugins for updates
installer.search({offset: 0, pattern: '', limit: 500}, /*maxCacheAge:*/60*10, function(data) { // hacky
if (!data.results) return;
installer.getAvailable(/*maxCacheAge:*/60*10, function(er, results) {
if(er) {
console.warn(er);
socket.emit("results:updatable", {updatable: {}});
return;
}
var updatable = _(plugins.plugins).keys().filter(function(plugin) {
if(!data.results[plugin]) return false;
var latestVersion = data.results[plugin]['dist-tags'].latest
if(!results[plugin]) return false;
var latestVersion = results[plugin].version
var currentVersion = plugins.plugins[plugin].package.version
return semver.gt(latestVersion, currentVersion)
});
socket.emit("updatable", {updatable: updatable});
socket.emit("progress", {progress:1});
socket.emit("results:updatable", {updatable: updatable});
});
})
socket.on("getAvailable", function (query) {
installer.getAvailablePlugins(/*maxCacheAge:*/false, function (er, results) {
if(er) {
console.error(er)
results = {}
}
socket.emit("results:available", results);
});
});
socket.on("search", function (query) {
socket.emit("progress", {progress:0, message:'Fetching results...'});
installer.search(query, /*maxCacheAge:*/60*10, function (progress) {
if (progress.results)
socket.emit("search-result", progress);
socket.emit("progress", progress);
installer.search(query.searchTerm, /*maxCacheAge:*/60*10, function (er, results) {
if(er) {
console.error(er)
results = {}
}
var res = Object.keys(results)
.slice(query.offset, query.offset+query.length)
.map(function(pluginName) {
return results[pluginName]
});
socket.emit("results:search", {results: res, query: query});
});
});
socket.on("install", function (plugin_name) {
socket.emit("progress", {progress:0, message:'Downloading and installing ' + plugin_name + "..."});
installer.install(plugin_name, function (progress) {
socket.emit("progress", progress);
installer.install(plugin_name, function (er) {
socket.emit("finished:install", {error: er});
});
});
socket.on("uninstall", function (plugin_name) {
socket.emit("progress", {progress:0, message:'Uninstalling ' + plugin_name + "..."});
installer.uninstall(plugin_name, function (progress) {
socket.emit("progress", progress);
installer.uninstall(plugin_name, function (er) {
socket.emit("finished:uninstall", {error: er});
});
});
});