mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-07 22:45:46 +01:00
basic topic searching via search bar working
This commit is contained in:
@@ -445,11 +445,16 @@ var socket,
|
||||
return false;
|
||||
});
|
||||
|
||||
$('#search-form').on('submit', function () {
|
||||
var input = $(this).find('input');
|
||||
ajaxify.go('search/' + input.val().replace(/^[ ?#]*/, ''));
|
||||
$('#search-form').on('submit', function (e) {
|
||||
e.preventDefault();
|
||||
var input = $(this).find('input'),
|
||||
term = input.val();
|
||||
|
||||
require(['search'], function(search) {
|
||||
search.query(term, function() {
|
||||
input.val('');
|
||||
return false;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
define('forum/search', function() {
|
||||
define('forum/search', ['search'], function(searchModule) {
|
||||
var Search = {};
|
||||
|
||||
Search.init = function() {
|
||||
@@ -21,11 +21,13 @@ define('forum/search', function() {
|
||||
|
||||
$('#search-form input').val(searchQuery);
|
||||
|
||||
$('#mobile-search-form').off('submit').on('submit', function() {
|
||||
$('#mobile-search-form').off('submit').on('submit', function(e) {
|
||||
e.preventDefault();
|
||||
var input = $(this).find('input');
|
||||
ajaxify.go('search/' + input.val(), null, 'search');
|
||||
|
||||
searchModule.query(input.val(), function() {
|
||||
input.val('');
|
||||
return false;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
54
public/src/modules/search.js
Normal file
54
public/src/modules/search.js
Normal file
@@ -0,0 +1,54 @@
|
||||
define('search', ['navigator'], function(nav) {
|
||||
"use strict";
|
||||
/* globals socket, ajaxify */
|
||||
|
||||
var Search = {};
|
||||
|
||||
Search.query = function(term, callback) {
|
||||
// Detect if a tid was specified
|
||||
var topicSearch = term.match(/in:topic-([\d]+)/);
|
||||
|
||||
if (!topicSearch) {
|
||||
term = term.replace(/^[ ?#]*/, '');
|
||||
ajaxify.go('search/' + term);
|
||||
callback();
|
||||
} else {
|
||||
var cleanedTerm = term.replace(topicSearch[0], ''),
|
||||
tid = topicSearch[1];
|
||||
|
||||
Search.queryTopic(tid, cleanedTerm, callback);
|
||||
}
|
||||
};
|
||||
|
||||
Search.queryTopic = function(tid, term, callback) {
|
||||
socket.emit('topics.search', {
|
||||
tid: tid,
|
||||
term: term
|
||||
}, function(err, pids) {
|
||||
var args = arguments;
|
||||
|
||||
// Sort pids numerically & store
|
||||
Search.results = pids.sort(function(a, b) {
|
||||
return a-b;
|
||||
});
|
||||
|
||||
if (!err && !ajaxify.currentPage.match(new RegExp('^topic/' + tid))) {
|
||||
ajaxify.go('topic/' + tid, function() {
|
||||
if (callback) callback.apply(null, args);
|
||||
Search.highlightResult(0);
|
||||
});
|
||||
} else {
|
||||
if (callback) callback.apply(null, args);
|
||||
Search.highlightResult(0);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Search.highlightResult = function(index) {
|
||||
socket.emit('posts.getPidIndex', Search.results[index], function(err, postIndex) {
|
||||
nav.scrollToPost(postIndex-1, true); // why -1? Ask @barisusakli
|
||||
});
|
||||
};
|
||||
|
||||
return Search;
|
||||
});
|
||||
@@ -508,7 +508,7 @@ var async = require('async'),
|
||||
term: term
|
||||
}, callback);
|
||||
} else {
|
||||
callback(undefined, []);
|
||||
callback(new Error('no-plugins-available'), []);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user