summaryrefslogtreecommitdiffstats
path: root/lib/api/encodeProgress.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-04-29 10:17:16 +0200
committerSamy Pessé <samypesse@gmail.com>2016-04-29 10:17:16 +0200
commit93e701f4712ab9e476061f8f81fb03eda1c2af2a (patch)
tree8e07db9dcb7b44d07d29f2d0a4cd8eccb564302e /lib/api/encodeProgress.js
parent68b9bf7e38867eed8763854a731f15959aaf65b5 (diff)
downloadgitbook-93e701f4712ab9e476061f8f81fb03eda1c2af2a.zip
gitbook-93e701f4712ab9e476061f8f81fb03eda1c2af2a.tar.gz
gitbook-93e701f4712ab9e476061f8f81fb03eda1c2af2a.tar.bz2
Complete deprecated page.progress and this.navigation
Diffstat (limited to 'lib/api/encodeProgress.js')
-rw-r--r--lib/api/encodeProgress.js58
1 files changed, 55 insertions, 3 deletions
diff --git a/lib/api/encodeProgress.js b/lib/api/encodeProgress.js
index f32c8d8..2522eb5 100644
--- a/lib/api/encodeProgress.js
+++ b/lib/api/encodeProgress.js
@@ -1,4 +1,5 @@
-
+var Immutable = require('immutable');
+var encodeNavigation = require('./encodeNavigation');
/**
page.progress is a deprecated property from GitBook v2
@@ -8,8 +9,59 @@
@return {Object}
*/
function encodeProgress(output, page) {
- // todo
- return {};
+ var book = output.getBook();
+ var pages = output.getPages();
+ var summary = book.getSummary();
+ var articles = summary.getArticlesAsList();
+
+ var current = page.getPath();
+ var navigation = encodeNavigation(output);
+ navigation = Immutable.Map(navigation);
+
+ var n = navigation.size;
+ var percent = 0, prevPercent = 0, currentChapter = null;
+ var done = true;
+
+ var chapters = navigation
+ .map(function(nav, chapterPath) {
+ nav.path = chapterPath;
+ return nav;
+ })
+ .valueSeq()
+ .sortBy(function(nav) {
+ return nav.index;
+ })
+ .map(function(nav, i) {
+ // Calcul percent
+ nav.percent = (i * 100) / Math.max((n - 1), 1);
+
+ // Is it done
+ nav.done = done;
+ if (nav.path == current) {
+ currentChapter = nav;
+ percent = nav.percent;
+ done = false;
+ } else if (done) {
+ prevPercent = nav.percent;
+ }
+
+ return nav;
+ })
+ .toJS();
+
+ return {
+ // Previous percent
+ prevPercent: prevPercent,
+
+ // Current percent
+ percent: percent,
+
+ // List of chapter with progress
+ chapters: chapters,
+
+ // Current chapter
+ current: currentChapter
+ };
}
module.exports = encodeProgress;