diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-06 15:53:53 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-06 15:53:53 -0700 |
commit | ed41429d58733664d4d8870cda42d86a91f87081 (patch) | |
tree | da4963b02ae4fabfc7a23eb68006ec02ccc8db91 /theme/javascript/core | |
parent | 74edc4f279c8748f072efe3b090a2414b351d697 (diff) | |
download | gitbook-ed41429d58733664d4d8870cda42d86a91f87081.zip gitbook-ed41429d58733664d4d8870cda42d86a91f87081.tar.gz gitbook-ed41429d58733664d4d8870cda42d86a91f87081.tar.bz2 |
Add base search bar in sidebar
Diffstat (limited to 'theme/javascript/core')
-rw-r--r-- | theme/javascript/core/search.js | 41 | ||||
-rw-r--r-- | theme/javascript/core/sidebar.js | 26 |
2 files changed, 64 insertions, 3 deletions
diff --git a/theme/javascript/core/search.js b/theme/javascript/core/search.js new file mode 100644 index 0000000..f38e86f --- /dev/null +++ b/theme/javascript/core/search.js @@ -0,0 +1,41 @@ +define([ + "jQuery", + "lodash", + "lunr", +], function($, _, lunr) { + var index = null; + + // Load complete index + var loadIndex = function() { + return $.getJSON("search_index.json") + .then(function(data) { + index = lunr.Index.load(data); + }); + }; + + // Search for a term + var search = function(q) { + if (!index) return; + var results = _.chain(index.search(q)) + .map(function(result) { + var parts = result.ref.split("#") + return { + path: parts[0], + hash: parts[1] + } + }) + .value(); + + return results; + }; + + + var init = function() { + loadIndex(); + }; + + return { + init: init, + search: search + }; +});
\ No newline at end of file diff --git a/theme/javascript/core/sidebar.js b/theme/javascript/core/sidebar.js index c36f4fe..89d0d24 100644 --- a/theme/javascript/core/sidebar.js +++ b/theme/javascript/core/sidebar.js @@ -1,12 +1,13 @@ define([ "utils/storage", "utils/platform", - "core/state" -], function(storage, platform, state) { + "core/state", + "core/search" +], function(storage, platform, state, search) { // Toggle sidebar with or withour animation var toggleSidebar = function(_state, animation) { - if (-state != null && isOpen() == _state) return; + if (state != null && isOpen() == _state) return; if (animation == null) animation = true; var $book = state().$book; @@ -16,11 +17,24 @@ define([ storage.set("sidebar", isOpen()); }; + // Toggle search + var toggleSearch = function(_state) { + if (state != null && isSearchOpen() == _state) return; + + var $summary = state().$book.find(".book-summary"); + $summary.toggleClass("with-search", _state); + }; + // Return true if sidebar is open var isOpen = function() { return state().$book.hasClass("with-summary"); }; + // Return true if search bar is open + var isSearchOpen = function() { + return state().$book.find(".book-summary").hasClass("with-search"); + }; + // Prepare sidebar: state and toggle button var init = function() { var $book = state().$book; @@ -31,6 +45,12 @@ define([ toggleSidebar(); }); + // Toggle search + $book.find(".book-header .toggle-search").click(function(e) { + e.preventDefault(); + toggleSearch(); + }); + // Init last state if not mobile if (!platform.isMobile) { toggleSidebar(storage.get("sidebar", true), false); |