diff --git a/public/language/en_GB/category.json b/public/language/en_GB/category.json index aa15153b0b..f8b02f32f8 100644 --- a/public/language/en_GB/category.json +++ b/public/language/en_GB/category.json @@ -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" } diff --git a/public/src/client/search.js b/public/src/client/search.js index adee1adaee..888914387d 100644 --- a/public/src/client/search.js +++ b/public/src/client/search.js @@ -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 ? '• ' : ''; - $('').appendTo(listEl); - - category.children.forEach(function(child) { - recursive(child, listEl, '    ' + level); - }); - } function highlightMatches(searchQuery) { if (!searchQuery) { diff --git a/src/controllers/search.js b/src/controllers/search.js index 874cbea1a2..dc58119a57 100644 --- a/src/controllers/search.js +++ b/src/controllers/search.js @@ -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;