From 5d22806ded9f3c6b5874fc2ee2206999522b4e6f Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 29 Aug 2014 11:18:02 -0400 Subject: [PATCH] more topic searching work (working example, needs UX tie-in) --- public/src/app.js | 19 +++++++--- public/src/modules/search.js | 68 +++++++++++++++++++++++++++--------- 2 files changed, 65 insertions(+), 22 deletions(-) diff --git a/public/src/app.js b/public/src/app.js index 4a3e3b6ca2..f7965a518d 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -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(); + }); }); } diff --git a/public/src/modules/search.js b/public/src/modules/search.js index a110bf4155..7de1f33395 100644 --- a/public/src/modules/search.js +++ b/public/src/modules/search.js @@ -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,29 +27,61 @@ 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 + }; + + Search.topicDOM.update(0); + }); + }; + + Search.checkPagePresence = function(tid, callback) { + if (!ajaxify.currentPage.match(new RegExp('^topic/' + tid))) { + ajaxify.go('topic/' + tid, callback); + } else { + callback(); + } + }; - 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 = {}; + 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.highlightResult = function(index) { - socket.emit('posts.getPidIndex', Search.results[index], function(err, postIndex) { - nav.scrollToPost(postIndex-1, true); // why -1? Ask @barisusakli + 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;