pad.libre-service.eu-etherpad/src/static/js/admin/plugins.js

234 lines
7.3 KiB
JavaScript
Raw Normal View History

$(document).ready(function () {
2012-04-29 19:54:38 +02:00
var socket,
loc = document.location,
port = loc.port == "" ? (loc.protocol == "https:" ? 443 : 80) : loc.port,
url = loc.protocol + "//" + loc.hostname + ":" + port + "/",
pathComponents = location.pathname.split('/'),
2012-04-29 20:12:15 +02:00
// Strip admin/plugins
baseURL = pathComponents.slice(0,pathComponents.length-2).join('/') + '/',
2012-04-29 19:54:38 +02:00
resource = baseURL.substring(1) + "socket.io";
//connect
socket = io.connect(url, {resource : resource}).of("/pluginfw/installer");
function search(searchTerm, limit) {
if(search.searchTerm != searchTerm) {
search.offset = 0
search.results = []
search.end = false
}
limit = limit? limit : search.limit
search.searchTerm = searchTerm;
socket.emit("search", {searchTerm: searchTerm, offset:search.offset, limit: limit, sortBy: search.sortBy, sortDir: search.sortDir});
search.offset += limit;
$('#search-progress').show()
}
search.offset = 0;
search.limit = 12;
search.results = [];
search.sortBy = 'name';
search.sortDir = /*DESC?*/true;
search.end = true;// have we received all results already?
var installed = {
progress: {
show: function(plugin, msg) {
$('#installed-plugins .'+plugin+' .progress').show()
$('#installed-plugins .'+plugin+' .progress .message').text(msg)
},
hide: function(plugin) {
$('#installed-plugins .'+plugin+' .progress').hide()
$('#installed-plugins .'+plugin+' .progress .message').text('')
}
},
list: []
}
function displayPluginList(plugins, container, template) {
plugins.forEach(function(plugin) {
var row = template.clone();
for (attr in plugin) {
if(attr == "name"){ // Hack to rewrite URLS into name
row.find(".name").html("<a target='_blank' href='https://npmjs.org/package/"+plugin['name']+"'>"+plugin['name'].substr(3)+"</a>"); // remove 'ep_'
}else{
row.find("." + attr).html(plugin[attr]);
}
}
row.find(".version").html( plugin.version );
row.addClass(plugin.name)
row.data('plugin', plugin.name)
container.append(row);
})
updateHandlers();
2012-04-18 13:43:34 +02:00
}
function sortPluginList(plugins, property, /*ASC?*/dir) {
return plugins.sort(function(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;
})
}
2012-04-18 13:43:34 +02:00
function updateHandlers() {
// Search
$("#search-query").unbind('keyup').keyup(function () {
search($("#search-query").val());
});
// update & install
$(".do-install, .do-update").unbind('click').click(function (e) {
var $row = $(e.target).closest("tr")
, plugin = $row.data('plugin');
$row.remove().appendTo('#installed-plugins')
socket.emit("install", plugin);
installed.progress.show(plugin, 'Installing')
$(".installed-results .nothing-installed").hide()
});
// uninstall
$(".do-uninstall").unbind('click').click(function (e) {
var $row = $(e.target).closest("tr")
, pluginName = $row.data('plugin');
socket.emit("uninstall", pluginName);
installed.progress.show(pluginName, 'Uninstalling')
installed.list = installed.list.filter(function(plugin) {
return plugin.name != pluginName
})
});
2012-04-18 13:43:34 +02:00
// Infinite scroll
$(window).unbind('scroll').scroll(function() {
if(search.end) return;// don't keep requesting if there are no more results
var top = $('.search-results .results > tr:last').offset().top
if($(window).scrollTop()+$(window).height() > top) search(search.searchTerm)
})
// Sort
$('.sort.up').unbind('click').click(function() {
search.sortBy = $(this).text().toLowerCase();
search.sortDir = false;
search.offset = 0;
search(search.searchTerm, search.results.length);
search.results = [];
})
$('.sort.down, .sort.none').unbind('click').click(function() {
search.sortBy = $(this).text().toLowerCase();
search.sortDir = true;
search.offset = 0;
search(search.searchTerm, search.results.length);
search.results = [];
})
}
socket.on('results:search', function (data) {
if(!data.results.length) search.end = true;
$(".search-results .nothing-found").hide()
$(".search-results .fetching").hide()
$("#search-query").removeAttr('disabled')
console.log('got search results', data)
// add to results
search.results = search.results.concat(data.results);
// Update sorting head
$('.sort')
.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
var searchWidget = $(".search-results");
searchWidget.find(".results *").remove();
if(search.results.length > 0) {
displayPluginList(search.results, searchWidget.find(".results"), searchWidget.find(".template tr"))
}else {
$(".search-results .nothing-found").show()
}
$('#search-progress').hide()
});
socket.on('results:installed', function (data) {
$(".installed-results .nothing-installed").hide()
$(".installed-results .fetching").hide()
installed.list = data.installed
sortPluginList(installed.list, 'name', /*ASC?*/true);
// filter out epl
installed.list = installed.list.filter(function(plugin) {
return plugin.name != 'ep_etherpad-lite'
})
// remove all installed plugins (leave plugins that are still being installed)
installed.list.forEach(function(plugin) {
$('#installed-plugins .'+plugin.name).remove()
})
if(installed.list.length > 0) {
displayPluginList(installed.list, $("#installed-plugins"), $("#installed-plugin-template"));
}else {
$(".installed-results .nothing-installed").show()
}
});
socket.on('results:updatable', function(data) {
$('#installed-plugins > tr').each(function(i, tr) {
var pluginName = $(tr).find('.name').text()
if (data.updatable.indexOf(pluginName) >= 0) {
var actions = $(tr).find('.actions')
actions.append('<input class="do-update" type="button" value="Update" />')
actions.css('width', 200)
}
})
updateHandlers();
})
socket.on('finished:install', function(data) {
if(data.error) {
alert('An error occured while installing '+data.plugin+' \n'+data.error)
$('#installed-plugins .'+data.plugin).remove()
}
socket.emit("getInstalled");
// update search results
search.offset = 0;
search(search.searchTerm, search.results.length);
search.results = [];
})
socket.on('finished:uninstall', function(data) {
if(data.error) alert('An error occured while uninstalling the '+data.plugin+' \n'+data.error)
// remove plugin from installed list
$('#installed-plugins .'+data.plugin).remove()
socket.emit("getInstalled");
// update search results
search.offset = 0;
search(search.searchTerm, search.results.length);
search.results = [];
})
// init
updateHandlers();
socket.emit("getInstalled");
search('');
// check for updates every 15mins
setInterval(function() {
socket.emit('checkUpdates');
}, 1000*60*15)
});