mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-10 16:05:49 +01:00
more topic searching work (working example, needs UX tie-in)
This commit is contained in:
@@ -445,16 +445,25 @@ var socket,
|
||||
return false;
|
||||
});
|
||||
|
||||
$('#search-form').on('submit', function (e) {
|
||||
e.preventDefault();
|
||||
var input = $(this).find('input'),
|
||||
term = input.val();
|
||||
require(['search'], function(search) {
|
||||
$('#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('');
|
||||
});
|
||||
});
|
||||
|
||||
$('.topic-search')
|
||||
.on('click', '.prev', function() {
|
||||
search.topicDOM.prev();
|
||||
})
|
||||
.on('click', '.next', function() {
|
||||
search.topicDOM.next();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,9 @@ define('search', ['navigator'], function(nav) {
|
||||
"use strict";
|
||||
/* globals socket, ajaxify */
|
||||
|
||||
var Search = {};
|
||||
var Search = {
|
||||
current: {}
|
||||
};
|
||||
|
||||
Search.query = function(term, callback) {
|
||||
// Detect if a tid was specified
|
||||
@@ -25,30 +27,62 @@ define('search', ['navigator'], function(nav) {
|
||||
tid: tid,
|
||||
term: term
|
||||
}, function(err, pids) {
|
||||
var args = arguments;
|
||||
callback(err);
|
||||
|
||||
// Sort pids numerically & store
|
||||
Search.results = pids.sort(function(a, b) {
|
||||
return a-b;
|
||||
});
|
||||
Search.current = {
|
||||
results: pids.sort(function(a, b) {
|
||||
return a-b;
|
||||
}),
|
||||
tid: tid,
|
||||
term: term
|
||||
};
|
||||
|
||||
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.topicDOM.update(0);
|
||||
});
|
||||
};
|
||||
|
||||
Search.highlightResult = function(index) {
|
||||
socket.emit('posts.getPidIndex', Search.results[index], function(err, postIndex) {
|
||||
nav.scrollToPost(postIndex-1, true); // why -1? Ask @barisusakli
|
||||
Search.checkPagePresence = function(tid, callback) {
|
||||
if (!ajaxify.currentPage.match(new RegExp('^topic/' + tid))) {
|
||||
ajaxify.go('topic/' + tid, callback);
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
};
|
||||
|
||||
Search.topicDOM = {};
|
||||
Search.topicDOM.start = function() {
|
||||
var topicSearchEl = $('.topic-search');
|
||||
|
||||
topicSearchEl.find('.count').html('1 / ' + Search.current.results.length);
|
||||
topicSearchEl.removeClass('hidden');
|
||||
Search.checkPagePresence(Search.current.tid, function() {
|
||||
Search.highlightResult(0);
|
||||
});
|
||||
};
|
||||
|
||||
Search.topicDOM.prev = function() {
|
||||
Search.topicDOM.update((Search.current.index === 0) ? Search.current.results.length-1 : Search.current.index-1);
|
||||
};
|
||||
|
||||
Search.topicDOM.next = function() {
|
||||
Search.topicDOM.update((Search.current.index === Search.current.results.length-1) ? 0 : Search.current.index+1);
|
||||
};
|
||||
|
||||
Search.topicDOM.update = function(index) {
|
||||
var topicSearchEl = $('.topic-search');
|
||||
|
||||
Search.current.index = index;
|
||||
|
||||
topicSearchEl.find('.count').html((index+1) + ' / ' + Search.current.results.length);
|
||||
topicSearchEl.removeClass('hidden');
|
||||
Search.checkPagePresence(Search.current.tid, function() {
|
||||
socket.emit('posts.getPidIndex', Search.current.results[index], function(err, postIndex) {
|
||||
nav.scrollToPost(postIndex-1, true); // why -1? Ask @barisusakli
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
return Search;
|
||||
});
|
||||
Reference in New Issue
Block a user