From 6502dd256112386ee8747ec0f55629b4a68ff604 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 21 Jan 2015 15:43:05 -0500 Subject: [PATCH] group searching --- public/src/client/groups/list.js | 28 +++++++++++++++++++++++++++- src/groups.js | 19 +++++++++++++++++++ src/socket.io/groups.js | 8 ++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/public/src/client/groups/list.js b/public/src/client/groups/list.js index d11eb7c8e6..5fac3e7c22 100644 --- a/public/src/client/groups/list.js +++ b/public/src/client/groups/list.js @@ -1,5 +1,5 @@ "use strict"; -/* globals app, define, ajaxify, socket, bootbox, utils */ +/* globals app, define, ajaxify, socket, bootbox, utils, templates */ define('forum/groups/list', function() { var Groups = {}; @@ -29,6 +29,32 @@ define('forum/groups/list', function() { } }); }); + + // Group searching + $('#search-text').on('keydown', function(e) { + if (e.keyCode === 13) { Groups.search($(this).val()); } + }); + + $('#search-button').on('click', function() { + Groups.search($(this).siblings('input').val()); + }); + }; + + Groups.search = function(query) { + var groupsEl = $('.groups.row'); + + socket.emit('groups.search', { + query: query, + options: { + expand: true + } + }, function(err, groups) { + templates.parse('partials/group_list', { + groups: groups + }, function(html) { + groupsEl.empty().append(html); + }); + }); }; return Groups; diff --git a/src/groups.js b/src/groups.js index 07266af711..87e6070757 100644 --- a/src/groups.js +++ b/src/groups.js @@ -898,4 +898,23 @@ var async = require('async'), db.setRemove('group:' + groupName + ':owners', toUid, callback); }; + Groups.search = function(query, options, callback) { + if (!query || !query.length) { + return callback(null, []); + } + + async.waterfall([ + async.apply(db.getObjectValues, 'groupslug:groupname'), + function(groupNames, next) { + groupNames = groupNames.filter(function(name) { + return name.match(new RegExp(query, 'i')); + }); + + async.mapLimit(groupNames, 5, function(groupName, next) { + Groups.get(groupName, options || {}, next); + }, next); + } + ], callback); + }; + }(module.exports)); diff --git a/src/socket.io/groups.js b/src/socket.io/groups.js index c4b4535e82..902041d6ec 100644 --- a/src/socket.io/groups.js +++ b/src/socket.io/groups.js @@ -126,6 +126,14 @@ SocketGroups.delete = function(socket, data, callback) { }); }; +SocketGroups.search = function(socket, data, callback) { + if (!data || !data.query) { + return callback(null, []); + } + + groups.search(data.query, data.options || {}, callback); +}; + SocketGroups.cover = {}; SocketGroups.cover.get = function(socket, data, callback) {