mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-08 06:55:46 +01:00
build search categories server side
This commit is contained in:
@@ -15,5 +15,7 @@
|
||||
"ignore": "Ignore",
|
||||
|
||||
"watch.message": "You are now watching updates from this category",
|
||||
"ignore.message": "You are now ignoring updates from this category"
|
||||
"ignore.message": "You are now ignoring updates from this category",
|
||||
|
||||
"watched-categories": "Watched categories"
|
||||
}
|
||||
|
||||
@@ -80,11 +80,10 @@ define('forum/search', ['search', 'autocomplete'], function(searchModule, autoco
|
||||
$('#posted-by-user').val(params.by);
|
||||
}
|
||||
|
||||
loadCategories(function() {
|
||||
if (params.categories) {
|
||||
$('#posted-in-categories').val(params.categories);
|
||||
}
|
||||
});
|
||||
|
||||
if (params.categories) {
|
||||
$('#posted-in-categories').val(params.categories);
|
||||
}
|
||||
|
||||
if (params.searchChildren) {
|
||||
$('#search-children').prop('checked', true);
|
||||
@@ -114,37 +113,7 @@ define('forum/search', ['search', 'autocomplete'], function(searchModule, autoco
|
||||
}
|
||||
}
|
||||
|
||||
function loadCategories(callback) {
|
||||
var listEl = $('#posted-in-categories');
|
||||
|
||||
socket.emit('categories.getCategoriesByPrivilege', 'read', function(err, categories) {
|
||||
if (err) {
|
||||
return app.alertError(err.message);
|
||||
}
|
||||
|
||||
categories = categories.filter(function(category) {
|
||||
return !category.link && !parseInt(category.parentCid, 10);
|
||||
});
|
||||
|
||||
categories.forEach(function(category) {
|
||||
recursive(category, listEl, '');
|
||||
});
|
||||
listEl.attr('size', listEl.find('option').length);
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
function recursive(category, listEl, level) {
|
||||
if (category.link) {
|
||||
return;
|
||||
}
|
||||
var bullet = level ? '• ' : '';
|
||||
$('<option value="' + category.cid + '">' + level + bullet + category.name + '</option>').appendTo(listEl);
|
||||
|
||||
category.children.forEach(function(child) {
|
||||
recursive(child, listEl, ' ' + level);
|
||||
});
|
||||
}
|
||||
|
||||
function highlightMatches(searchQuery) {
|
||||
if (!searchQuery) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var searchController = {},
|
||||
var async = require('async'),
|
||||
validator = require('validator'),
|
||||
plugins = require('../plugins'),
|
||||
search = require('../search'),
|
||||
@@ -10,6 +10,8 @@ var searchController = {},
|
||||
helpers = require('./helpers');
|
||||
|
||||
|
||||
var searchController = {};
|
||||
|
||||
searchController.search = function(req, res, next) {
|
||||
if (!plugins.hasListeners('filter:search.query')) {
|
||||
return next();
|
||||
@@ -37,18 +39,23 @@ searchController.search = function(req, res, next) {
|
||||
uid: req.uid
|
||||
};
|
||||
|
||||
search.search(data, function(err, results) {
|
||||
async.parallel({
|
||||
categories: async.apply(buildCategories, req.uid),
|
||||
search: async.apply(search.search, data)
|
||||
}, function(err, results) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
var searchData = results.search;
|
||||
searchData.categories = results.categories;
|
||||
searchData.categoriesCount = results.categories.length;
|
||||
searchData.pagination = pagination.create(page, searchData.pageCount, req.query);
|
||||
searchData.showAsPosts = !req.query.showAs || req.query.showAs === 'posts';
|
||||
searchData.showAsTopics = req.query.showAs === 'topics';
|
||||
searchData.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[global:search]]'}]);
|
||||
searchData.expandSearch = !req.params.term;
|
||||
|
||||
results.pagination = pagination.create(page, results.pageCount, req.query);
|
||||
results.showAsPosts = !req.query.showAs || req.query.showAs === 'posts';
|
||||
results.showAsTopics = req.query.showAs === 'topics';
|
||||
results.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[global:search]]'}]);
|
||||
results.expandSearch = !req.params.term;
|
||||
|
||||
plugins.fireHook('filter:search.build', {data: data, results: results}, function(err, data) {
|
||||
plugins.fireHook('filter:search.build', {data: data, results: searchData}, function(err, data) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
@@ -57,4 +64,44 @@ searchController.search = function(req, res, next) {
|
||||
});
|
||||
};
|
||||
|
||||
function buildCategories(uid, callback) {
|
||||
categories.getCategoriesByPrivilege('cid:0:children', uid, 'read', function(err, categories) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var categoriesData = [
|
||||
{value: 'all', text: '[[unread:all_categories]]'},
|
||||
{value: 'watched', text: '[[category:watched-categories]]'}
|
||||
];
|
||||
|
||||
categories = categories.filter(function(category) {
|
||||
return !category.link && !parseInt(category.parentCid, 10);
|
||||
});
|
||||
|
||||
categories.forEach(function(category) {
|
||||
recursive(category, categoriesData, '');
|
||||
});
|
||||
callback(null, categoriesData);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function recursive(category, categoriesData, level) {
|
||||
if (category.link) {
|
||||
return;
|
||||
}
|
||||
|
||||
var bullet = level ? '• ' : '';
|
||||
|
||||
categoriesData.push({
|
||||
value: category.cid,
|
||||
text: level + bullet + category.name
|
||||
});
|
||||
|
||||
category.children.forEach(function(child) {
|
||||
recursive(child, categoriesData, ' ' + level);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = searchController;
|
||||
|
||||
Reference in New Issue
Block a user