diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-06 16:46:29 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-06 16:46:29 -0700 |
commit | a42f287a9299d5e2e67198ae9c4488390da585e9 (patch) | |
tree | 7388a85ef40b5d986443e45ea46001b357e26279 /theme/javascript/core | |
parent | d2f62fd109e6a30592f1a9767dfc2a0e7293f0cc (diff) | |
download | gitbook-a42f287a9299d5e2e67198ae9c4488390da585e9.zip gitbook-a42f287a9299d5e2e67198ae9c4488390da585e9.tar.gz gitbook-a42f287a9299d5e2e67198ae9c4488390da585e9.tar.bz2 |
Filter summary using search bar
Diffstat (limited to 'theme/javascript/core')
-rw-r--r-- | theme/javascript/core/search.js | 14 | ||||
-rw-r--r-- | theme/javascript/core/sidebar.js | 18 |
2 files changed, 27 insertions, 5 deletions
diff --git a/theme/javascript/core/search.js b/theme/javascript/core/search.js index e674bb0..c00f662 100644 --- a/theme/javascript/core/search.js +++ b/theme/javascript/core/search.js @@ -2,8 +2,9 @@ define([ "jQuery", "lodash", "lunr", - "core/state" -], function($, _, lunr, state) { + "core/state", + "core/sidebar" +], function($, _, lunr, state, sidebar) { var index = null; var $searchBar = state.$book.find(".book-search"); var $searchInput = $searchBar.find("input"); @@ -71,7 +72,14 @@ define([ toggleSearch(false); return; } - console.log("search", q); + if (q.length == 0) { + sidebar.filter(null); + } else { + var results = search(q); + sidebar.filter( + _.pluck(results, "path") + ); + } }); }; diff --git a/theme/javascript/core/sidebar.js b/theme/javascript/core/sidebar.js index e324e19..ab5b19a 100644 --- a/theme/javascript/core/sidebar.js +++ b/theme/javascript/core/sidebar.js @@ -1,8 +1,9 @@ define([ + "lodash", "utils/storage", "utils/platform", "core/state" -], function(storage, platform, state) { +], function(_, storage, platform, state) { var $summary = state.$book.find(".book-summary"); // Toggle sidebar with or withour animation @@ -35,9 +36,22 @@ define([ } }; + // Filter summary with a list of path + var filterSummary = function(paths) { + console.log("filter with", paths); + $summary.find("li").each(function() { + var path = $(this).data("path"); + var st = paths == null || _.contains(paths, path); + + $(this).toggle(st); + if (st) $(this).parents("li").show(); + }); + }; + return { $el: $summary, init: init, - toggle: toggleSidebar + toggle: toggleSidebar, + filter: filterSummary } });
\ No newline at end of file |