v1.18.x
barisusakli 11 years ago
parent ba4b98de0e
commit 94c765fa25

@ -1,8 +1,9 @@
"use strict"; "use strict";
/*global define, socket, app, admin*/ /*global define, socket, app, admin, utils*/
define('forum/admin/tags', [], function() { define('forum/admin/tags', ['forum/infinitescroll'], function(infinitescroll) {
var Tags = {}; var Tags = {};
var timeoutId = 0;
Tags.init = function() { Tags.init = function() {
handleColorPickers(); handleColorPickers();
@ -12,10 +13,22 @@ define('forum/admin/tags', [], function() {
}); });
$('#tag-search').on('input propertychange', function() { $('#tag-search').on('input propertychange', function() {
$('.tag-list').children().each(function() { if (timeoutId) {
var $this = $(this); clearTimeout(timeoutId);
$this.toggleClass('hide', $this.attr('data-tag').indexOf($('#tag-search').val()) === -1); timeoutId = 0;
}); }
timeoutId = setTimeout(function() {
socket.emit('topics.searchAndLoadTags', {query: $('#tag-search').val()}, function(err, tags) {
if (err) {
return app.alertError(err.message);
}
infinitescroll.parseAndTranslate('admin/tags', 'tags', {tags: tags}, function(html) {
$('.tag-list').html(html);
utils.makeNumbersHumanReadable(html.find('.human-readable-number'));
timeoutId = 0;
});
});
}, 100);
}); });
}; };

@ -1,20 +1,59 @@
'use strict'; 'use strict';
/* globals define, app */ /* globals define, app, utils, socket */
define('forum/tags', function() { define('forum/tags', ['forum/infinitescroll'], function(infinitescroll) {
var Tags = {}; var Tags = {};
var timeoutId = 0;
Tags.init = function() { Tags.init = function() {
app.enterRoom('tags'); app.enterRoom('tags');
$('#tag-search').on('input propertychange', function() { $('#tag-search').on('input propertychange', function() {
$('.tag-list').children().each(function() { if (timeoutId) {
var $this = $(this); clearTimeout(timeoutId);
$this.toggleClass('hide', $this.find('a').attr('data-value').indexOf($('#tag-search').val()) === -1); timeoutId = 0;
}) }
timeoutId = setTimeout(function() {
socket.emit('topics.searchAndLoadTags', {query: $('#tag-search').val()}, function(err, results) {
if (err) {
return app.alertError(err.message);
}
onTagsLoaded(results, true, function() {
timeoutId = 0;
});
});
}, 100);
}); });
infinitescroll.init(Tags.loadMoreTags);
}; };
Tags.loadMoreTags = function(direction) {
if(direction < 0 || !$('.tag-list').length) {
return;
}
infinitescroll.loadMore('topics.loadMoreTags', {
after: $('.tag-list').attr('data-nextstart')
}, function(data, done) {
if (data && data.tags && data.tags.length) {
onTagsLoaded(data.tags, false, done);
$('.tag-list').attr('data-nextstart', data.nextStart);
} else {
done();
}
});
};
function onTagsLoaded(tags, replace, callback) {
callback = callback || function() {};
infinitescroll.parseAndTranslate('tags', 'tags', {tags: tags}, function(html) {
$('.tag-list')[replace ? 'html' : 'append'](html);
utils.makeNumbersHumanReadable(html.find('.human-readable-number'));
callback();
});
}
return Tags; return Tags;
}); });

@ -132,7 +132,7 @@ function filterAndRenderCategories(req, res, next, active) {
} }
adminController.tags.get = function(req, res, next) { adminController.tags.get = function(req, res, next) {
topics.getTags(0, -1, function(err, tags) { topics.getTags(0, 99, function(err, tags) {
if (err) { if (err) {
return next(err); return next(err);
} }

@ -47,14 +47,13 @@ tagsController.getTag = function(req, res, next) {
}; };
tagsController.getTags = function(req, res, next) { tagsController.getTags = function(req, res, next) {
topics.getTags(0, -1, function(err, tags) { topics.getTags(0, 99, function(err, tags) {
if (err) { if (err) {
return next(err); return next(err);
} }
res.render('tags', {tags: tags}); res.render('tags', {tags: tags, nextStart: 100});
}); });
}; };
module.exports = tagsController; module.exports = tagsController;

@ -518,4 +518,53 @@ SocketTopics.searchTags = function(socket, data, callback) {
topics.searchTags(data, callback); topics.searchTags(data, callback);
}; };
SocketTopics.searchAndLoadTags = function(socket, data, callback) {
topics.searchTags(data, function(err, tags) {
if (err) {
return callback(err);
}
async.parallel({
counts: function(next) {
db.sortedSetScores('tags:topic:count', tags, next);
},
tagData: function(next) {
tags = tags.map(function(tag) {
return {value: tag};
});
topics.getTagData(tags, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
results.tagData.forEach(function(tag, index) {
tag.score = results.counts[index];
});
results.tagData.sort(function(a, b) {
return parseInt(b.score, 10) - parseInt(a.score, 10);
});
callback(null, results.tagData);
});
});
};
SocketTopics.loadMoreTags = function(socket, data, callback) {
if(!data || !data.after) {
return callback(new Error('[[error:invalid-data]]'));
}
var start = parseInt(data.after, 10),
end = start + 99;
topics.getTags(start, end, function(err, tags) {
if (err) {
return callback(err);
}
callback(null, {tags: tags, nextStart: end + 1});
});
};
module.exports = SocketTopics; module.exports = SocketTopics;

@ -89,11 +89,11 @@ module.exports = function(Topics) {
return callback(err); return callback(err);
} }
addTagData(tags, callback); Topics.getTagData(tags, callback);
}); });
}; };
function addTagData(tags, callback) { Topics.getTagData = function(tags, callback) {
var keys = tags.map(function(tag) { var keys = tags.map(function(tag) {
return 'tag:' + tag.value; return 'tag:' + tag.value;
}); });
@ -109,7 +109,7 @@ module.exports = function(Topics) {
}); });
callback(null, tags); callback(null, tags);
}); });
} };
Topics.getTopicTags = function(tid, callback) { Topics.getTopicTags = function(tid, callback) {
db.getSetMembers('topic:' + tid + ':tags', callback); db.getSetMembers('topic:' + tid + ':tags', callback);
@ -139,7 +139,7 @@ module.exports = function(Topics) {
async.parallel({ async.parallel({
tagData: function(next) { tagData: function(next) {
addTagData(tags, next); Topics.getTagData(tags, next);
}, },
counts: function(next) { counts: function(next) {
db.sortedSetScores('tags:topic:count', uniqueTopicTags, next); db.sortedSetScores('tags:topic:count', uniqueTopicTags, next);
@ -214,12 +214,13 @@ module.exports = function(Topics) {
return callback(null, []); return callback(null, []);
} }
db.getSortedSetRevRange('tags:topic:count', 0, -1, function(err, tags) { db.getSortedSetRevRange('tags:topic:count', 0, -1, function(err, tags) {
if (err) { if (err) {
return callback(null, []); return callback(null, []);
} }
if (data.query === '') {
return callback(null, tags);
}
data.query = data.query.toLowerCase(); data.query = data.query.toLowerCase();
var matches = []; var matches = [];
for(var i=0; i<tags.length; ++i) { for(var i=0; i<tags.length; ++i) {

Loading…
Cancel
Save