mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Add base for handling and storing progress
This commit is contained in:
@@ -31,11 +31,15 @@ module.exports = function (grunt) {
|
||||
include: ["requireLib"],
|
||||
paths: {
|
||||
"jQuery": 'vendors/jquery',
|
||||
"lodash": 'vendors/lodash',
|
||||
"requireLib": 'vendors/require',
|
||||
},
|
||||
shim: {
|
||||
'jQuery': {
|
||||
exports: '$'
|
||||
},
|
||||
'lodash': {
|
||||
exports: '_'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
require([
|
||||
"jQuery"
|
||||
], function($){
|
||||
"jQuery",
|
||||
"progress"
|
||||
], function($, progress){
|
||||
$(document).ready(function() {
|
||||
var $book = $(".book");
|
||||
var githubId = $book.data("github");
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
define([
|
||||
"lodash",
|
||||
"jQuery",
|
||||
"utils/storage"
|
||||
], function(_, $, storage) {
|
||||
// Get current level
|
||||
var getCurrentLevel = function() {
|
||||
return $(".book").data("level");
|
||||
};
|
||||
|
||||
// Return all levels
|
||||
var getLevels = function() {
|
||||
var levels = $(".book-summary li[data-level]");
|
||||
return _.map(levels, function(level) {
|
||||
return $(level).data("level");
|
||||
});
|
||||
};
|
||||
|
||||
// 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;
|
||||
};
|
||||
|
||||
// Show progress
|
||||
var showProgress = function() {
|
||||
var progress = getProgress();
|
||||
var $summary = $(".book-summary");
|
||||
|
||||
_.each(progress, function(value, level) {
|
||||
$summary.find("li[data-level='"+level+"']").toggleClass("done", value > 0);
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
'current': getCurrentLevel,
|
||||
'levels': getLevels,
|
||||
'get': getProgress,
|
||||
'mark': markProgress,
|
||||
'show': showProgress
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
define(function(){
|
||||
/*
|
||||
* Simple module for storing data in the browser's local storage
|
||||
*/
|
||||
return {
|
||||
set: function(key, value) {
|
||||
localStorage[key] = JSON.stringify(value);
|
||||
},
|
||||
get: function(key, def) {
|
||||
try {
|
||||
return JSON.parse(localStorage[key]) || def;
|
||||
} catch(err) {
|
||||
return localStorage[key] || def;
|
||||
}
|
||||
},
|
||||
remove: function(key) {
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
};
|
||||
});
|
||||
Vendored
+6785
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user