admin/plugins: Allow people to sort search results

This commit is contained in:
Marcel Klehr 2013-03-25 23:09:03 +01:00
parent 1ebbcd2f30
commit 773293991b
4 changed files with 84 additions and 15 deletions

View file

@ -70,10 +70,11 @@ exports.socketio = function (hook_name, args, cb) {
results = {} results = {}
} }
var res = Object.keys(results) var res = Object.keys(results)
.slice(query.offset, query.offset+query.length)
.map(function(pluginName) { .map(function(pluginName) {
return results[pluginName] return results[pluginName]
}); });
res = sortPluginList(res, query.sortBy, query.sortDir)
.slice(query.offset, query.offset+query.limit);
socket.emit("results:search", {results: res, query: query}); socket.emit("results:search", {results: res, query: query});
}); });
}); });
@ -91,3 +92,14 @@ exports.socketio = function (hook_name, args, cb) {
}); });
}); });
} }
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;
})
}

View file

@ -102,6 +102,19 @@ input[type="text"] {
max-width: 500px; max-width: 500px;
} }
.sort {
cursor: pointer;
}
.sort:after {
content: '▲▼'
}
.sort.up:after {
content:'▲'
}
.sort.down:after {
content:'▼'
}
table { table {
border: 1px solid #ddd; border: 1px solid #ddd;
border-radius: 3px; border-radius: 3px;

View file

@ -12,20 +12,23 @@ $(document).ready(function () {
//connect //connect
socket = io.connect(url, {resource : resource}).of("/pluginfw/installer"); socket = io.connect(url, {resource : resource}).of("/pluginfw/installer");
function search(searchTerm) { function search(searchTerm, limit) {
if(search.searchTerm != searchTerm) { if(search.searchTerm != searchTerm) {
search.offset = 0 search.offset = 0
search.results = [] search.results = []
search.end = false search.end = false
} }
limit = limit? limit : search.limit
search.searchTerm = searchTerm; search.searchTerm = searchTerm;
socket.emit("search", {searchTerm: searchTerm, offset:search.offset, length: search.limit}); socket.emit("search", {searchTerm: searchTerm, offset:search.offset, limit: limit, sortBy: search.sortBy, sortDir: search.sortDir});
search.offset += search.limit; search.offset += limit;
} }
search.offset = 0 search.offset = 0;
search.limit = 12 search.limit = 12;
search.results = [] search.results = [];
search.end = true// have we received all results already? search.sortBy = 'name';
search.sortDir = /*DESC?*/true;
search.end = true;// have we received all results already?
function displayPluginList(plugins, container, template) { function displayPluginList(plugins, container, template) {
plugins.forEach(function(plugin) { plugins.forEach(function(plugin) {
@ -44,6 +47,17 @@ $(document).ready(function () {
}) })
updateHandlers(); updateHandlers();
} }
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;
})
}
function updateHandlers() { function updateHandlers() {
// Search // Search
@ -66,24 +80,54 @@ $(document).ready(function () {
// Infinite scroll // Infinite scroll
$(window).unbind('scroll').scroll(function() { $(window).unbind('scroll').scroll(function() {
if(search.end) return;// don't keep requesting if there are no more results if(search.end) return;// don't keep requesting if there are no more results
var top = $('.search-results .results > tr').last().offset().top var top = $('.search-results .results > tr:last').offset().top
if($(window).scrollTop()+$(window).height() > top) search(search.searchTerm) 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.results = [];
search(search.searchTerm, search.results.length);
})
$('.sort.down, .sort.none').unbind('click').click(function() {
search.sortBy = $(this).text().toLowerCase();
search.sortDir = true;
search.offset = 0;
search.results = [];
search(search.searchTerm);
})
} }
socket.on('results:search', function (data) { socket.on('results:search', function (data) {
console.log('got search results', data)
search.results = search.results.concat(data.results);
if(!data.results.length) search.end = true; if(!data.results.length) search.end = true;
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"); var searchWidget = $(".search-results");
searchWidget.find(".results *").remove(); searchWidget.find(".results *").remove();
displayPluginList(search.results, searchWidget.find(".results"), searchWidget.find(".template tr")) displayPluginList(search.results, searchWidget.find(".results"), searchWidget.find(".template tr"))
}); });
socket.on('results:installed', function (data) { socket.on('results:installed', function (data) {
sortPluginList(data.installed, 'name', /*ASC?*/true);
$("#installed-plugins *").remove(); $("#installed-plugins *").remove();
displayPluginList(data.installed, $("#installed-plugins"), $("#installed-plugin-template")) displayPluginList(data.installed, $("#installed-plugins"), $("#installed-plugin-template"));
setTimeout(function() { setTimeout(function() {
socket.emit('checkUpdates'); socket.emit('checkUpdates');

View file

@ -67,9 +67,9 @@
<table> <table>
<thead> <thead>
<tr> <tr>
<th>Name</th> <th class="sort up" data-label="name">Name</th>
<th>Description</th> <th class="sort none" data-label="description">Description</th>
<th>Version</th> <th class="sort none" data-label="Version">Version</th>
<td></td> <td></td>
</tr> </tr>
</thead> </thead>