diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-04 14:10:56 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-04 14:10:56 -0700 |
commit | 446d486a0fb725f3df76d9b6e8d2bca02042805b (patch) | |
tree | cfd5405f607fcca196ef5bad6b0b88662c30ccb9 /theme/javascript/core/progress.js | |
parent | 07101deaefb8848d40231eea12a3bf0710e6a78e (diff) | |
download | gitbook-446d486a0fb725f3df76d9b6e8d2bca02042805b.zip gitbook-446d486a0fb725f3df76d9b6e8d2bca02042805b.tar.gz gitbook-446d486a0fb725f3df76d9b6e8d2bca02042805b.tar.bz2 |
Add base for theming
Diffstat (limited to 'theme/javascript/core/progress.js')
-rw-r--r-- | theme/javascript/core/progress.js | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/theme/javascript/core/progress.js b/theme/javascript/core/progress.js new file mode 100644 index 0000000..755f111 --- /dev/null +++ b/theme/javascript/core/progress.js @@ -0,0 +1,66 @@ +define([ + "lodash", + "jQuery", + "utils/storage", + "core/state" +], function(_, $, storage, state) { + // Get current level + var getCurrentLevel = function() { + return state().level; + }; + + // Return all levels + var getLevels = function() { + var levels = $(".book-summary li[data-level]"); + return _.map(levels, function(level) { + return $(level).data("level").toString(); + }); + }; + + // Return a map chapter -> number (timestamp) + var getProgress = function() { + // Current level + var progress = storage.get("progress", {}); + + // Levels + var levels = getLevels(); + + _.each(levels, function(level) { + progress[level] = progress[level] || 0; + }); + + return progress; + }; + + // Change value of progress for a level + var markProgress = function(level, state) { + if (state == null) state = true; + + var progress = getProgress(); + progress[level] = state? Date.now() : 0; + + storage.set("progress", progress); + }; + + // Show progress + var showProgress = function() { + // Update progress + var progress = getProgress(); + var $summary = $(".book-summary"); + + _.each(progress, function(value, level) { + $summary.find("li[data-level='"+level+"']").toggleClass("done", value > 0); + }); + + // Mark current progress + markProgress(getCurrentLevel(), true); + }; + + return { + 'current': getCurrentLevel, + 'levels': getLevels, + 'get': getProgress, + 'mark': markProgress, + 'show': showProgress + }; +});
\ No newline at end of file |