diff options
Diffstat (limited to 'assets/javascript')
-rw-r--r-- | assets/javascript/app.js | 10 | ||||
-rw-r--r-- | assets/javascript/core/sidebar.js | 41 | ||||
-rw-r--r-- | assets/javascript/utils/platform.js | 5 |
3 files changed, 50 insertions, 6 deletions
diff --git a/assets/javascript/app.js b/assets/javascript/app.js index fde6e20..55e3546 100644 --- a/assets/javascript/app.js +++ b/assets/javascript/app.js @@ -3,16 +3,14 @@ require([ "core/state", "core/exercise", "core/progress", -], function($, _state, exercise, progress){ + "core/sidebar" +], function($, _state, exercise, progress, sidebar){ $(document).ready(function() { var state = _state(); var $book = state.$book; - // Toggle summary - $book.find(".book-header .toggle-summary").click(function(e) { - e.preventDefault(); - $book.toggleClass("with-summary"); - }); + // Init sidebar + sidebar.init(); // Star and watch count $.getJSON("https://api.github.com/repos/"+state.githubId) diff --git a/assets/javascript/core/sidebar.js b/assets/javascript/core/sidebar.js new file mode 100644 index 0000000..648aadb --- /dev/null +++ b/assets/javascript/core/sidebar.js @@ -0,0 +1,41 @@ +define([ + "utils/storage", + "utils/platform", + "core/state" +], function(storage, platform, state) { + + // Toggle sidebar with or withour animation + var toggleSidebar = function(_state, animation) { + if (animation == null) animation = true; + + var $book = state().$book; + $book.toggleClass("without-animation", !animation); + $book.toggleClass("with-summary", _state); + + storage.set("sidebar", isOpen()); + }; + + // Return true if sidebar is open + var isOpen = function() { + return state().$book.hasClass("with-summary"); + }; + + // Prepare sidebar: state and toggle button + var init = function() { + var $book = state().$book; + + // Toggle summary + $book.find(".book-header .toggle-summary").click(function(e) { + e.preventDefault(); + toggleSidebar(); + }); + + // Init last state if not mobile and not homepage + if (!isOpen()) toggleSidebar(platform.isMobile ? false : storage.get("sidebar", false), false); + }; + + return { + init: init, + toggle: toggleSidebar + } +});
\ No newline at end of file diff --git a/assets/javascript/utils/platform.js b/assets/javascript/utils/platform.js new file mode 100644 index 0000000..ad5f3b4 --- /dev/null +++ b/assets/javascript/utils/platform.js @@ -0,0 +1,5 @@ +define([], function() { + return { + isMobile: /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) + }; +});
\ No newline at end of file |