summaryrefslogtreecommitdiffstats
path: root/lib/api
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
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')
-rw-r--r--lib/api/deprecate.js23
-rw-r--r--lib/api/encodeGlobal.js5
-rw-r--r--lib/api/encodeNavigation.js18
-rw-r--r--lib/api/encodePage.js8
-rw-r--r--lib/api/encodeProgress.js58
5 files changed, 92 insertions, 20 deletions
diff --git a/lib/api/deprecate.js b/lib/api/deprecate.js
index 73ca8be..fd9280f 100644
--- a/lib/api/deprecate.js
+++ b/lib/api/deprecate.js
@@ -1,3 +1,5 @@
+var is = require('is');
+
var logged = {};
var disabled = {};
@@ -40,19 +42,32 @@ function deprecateMethod(book, key, fn, msg) {
@param {Book|Output} book
@param {String} key: unique identitifer for the deprecated
@param {Object} instance
- @param {String} property
+ @param {String|Function} property
@param {String} msg: message to print when called
@return {Function}
*/
function deprecateField(book, key, instance, property, value, msg) {
+ var store = undefined;
+
+ var prepare = function() {
+ if (!is.undefined(store)) return;
+
+ if (is.fn(value)) store = value();
+ else store = value;
+ }
+
var getter = function(){
+ prepare();
+
logNotice(book, key, msg);
- return value;
+ return store;
};
var setter = function(v) {
+ prepare();
+
logNotice(book, key, msg);
- value = v;
- return value;
+ store = v;
+ return store;
};
Object.defineProperty(instance, property, {
diff --git a/lib/api/encodeGlobal.js b/lib/api/encodeGlobal.js
index 78919d3..0191c50 100644
--- a/lib/api/encodeGlobal.js
+++ b/lib/api/encodeGlobal.js
@@ -82,8 +82,9 @@ function encodeGlobal(output) {
// Deprecated properties
- deprecate.field(output, 'this.navigation', result, 'sections', encodeNavigation(output),
- '"navigation" property is deprecated');
+ deprecate.field(output, 'this.navigation', result, 'navigation', function() {
+ return encodeNavigation(output);
+ }, '"navigation" property is deprecated');
deprecate.field(output, 'this.book', result, 'book',
result, '"book" property is deprecated, use "this" directly instead');
diff --git a/lib/api/encodeNavigation.js b/lib/api/encodeNavigation.js
index c48ba50..8e329a1 100644
--- a/lib/api/encodeNavigation.js
+++ b/lib/api/encodeNavigation.js
@@ -1,3 +1,4 @@
+var Immutable = require('immutable');
/**
Encode an article for next/prev
@@ -26,13 +27,15 @@ function encodeArticle(pages, article) {
*/
function encodeNavigation(output) {
var book = output.getBook();
+ var pages = output.getPages();
var summary = book.getSummary();
var articles = summary.getArticlesAsList();
- return articles
+
+ var navigation = articles
.map(function(article, i) {
var ref = article.getRef();
- if (ref) {
+ if (!ref) {
return undefined;
}
@@ -45,14 +48,17 @@ function encodeNavigation(output) {
index: i,
title: article.getTitle(),
introduction: (i === 0),
- prev: prev? encodeArticle(prev) : undefined,
- next: next? encodeArticle(next) : undefined,
+ prev: prev? encodeArticle(pages, prev) : undefined,
+ next: next? encodeArticle(pages, next) : undefined,
level: article.getLevel()
}
];
})
- .toMap()
- .toJS();
+ .filter(function(e) {
+ return Boolean(e);
+ });
+
+ return Immutable.Map(navigation).toJS();
}
module.exports = encodeNavigation;
diff --git a/lib/api/encodePage.js b/lib/api/encodePage.js
index 1465137..8ad39c5 100644
--- a/lib/api/encodePage.js
+++ b/lib/api/encodePage.js
@@ -22,11 +22,9 @@ function encodePage(output, page) {
result.path = file.getPath();
result.rawPath = fs.resolve(result.path);
- // todo: (as deprecated)
- // page.progress
-
- deprecate.field(output, 'page.progress', result, 'sections', encodeProgress(output, page),
- '"page.progress" property is deprecated');
+ deprecate.field(output, 'page.progress', result, 'sections', function() {
+ return encodeProgress(output, page);
+ }, '"page.progress" property is deprecated');
deprecate.field(output, 'page.sections', result, 'sections', [
{
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;