summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-02-18 16:34:11 +0100
committerSamy Pessé <samypesse@gmail.com>2016-02-18 16:34:11 +0100
commit8401a674c382cd1c7ca6c6a02e4def3d237c9734 (patch)
tree6b16eb46710e5e9510483d19ef5c256c5dd93938 /lib
parent3fcf1a7723a5a3b7a436974c445dbb2b79fad583 (diff)
downloadgitbook-8401a674c382cd1c7ca6c6a02e4def3d237c9734.zip
gitbook-8401a674c382cd1c7ca6c6a02e4def3d237c9734.tar.gz
gitbook-8401a674c382cd1c7ca6c6a02e4def3d237c9734.tar.bz2
Add test for navigation in summary
Diffstat (limited to 'lib')
-rw-r--r--lib/backbone/summary.js73
1 files changed, 52 insertions, 21 deletions
diff --git a/lib/backbone/summary.js b/lib/backbone/summary.js
index 0d1d0d7..8e1a66d 100644
--- a/lib/backbone/summary.js
+++ b/lib/backbone/summary.js
@@ -79,38 +79,62 @@ TOCArticle.prototype.hasParent = function() {
return (this.parent instanceof TOCArticle);
};
-// Return next article in the TOC
-TOCArticle.prototype.next = function() {
+// Return a sibling (next or prev) in the parent
+// Withotu taking in consideration children/parent
+TOCArticle.prototype.sibling = function(direction) {
var parentsArticles = this.parent.articles;
var pos = _.findIndex(parentsArticles, this);
- if ((pos + 1) >= parentsArticles.length) {
- if (this.hasParent()) {
- return this.parent.next();
- } else {
- return null;
- }
- } else {
- // next has the same parent
- return parentsArticles[pos + 1];
+ if (parentsArticles[pos + direction]) {
+ return parentsArticles[pos + direction];
}
+
+ return null;
};
-// Return previous article in the TOC
-TOCArticle.prototype.prev = function() {
+// Return a sibling (next or prev)
+// It takes parents.children in consideration
+TOCArticle.prototype._sibling = function(direction) {
+ // Next should go to the first children
+ if (direction > 0 && this.hasChildren()) {
+ return _.first(this.articles);
+ }
+
var parentsArticles = this.parent.articles;
var pos = _.findIndex(parentsArticles, this);
- if ((pos - 1) < 0) {
- if (this.hasParent()) {
- return this.parent.prev();
- } else {
- return null;
+ // First child and has parent
+ if (pos == 0 && direction < 0 && this.hasParent()) {
+ return this.parent;
+ }
+
+ // Last child and has parent
+ if(pos == (parentsArticles.length - 1) && direction > 0 && this.hasParent()) {
+ return this.parent.sibling(1);
+ }
+
+ if (parentsArticles[pos + direction]) {
+ var article = parentsArticles[pos + direction];
+
+ // If goign back, take last children from "brother"
+ if (direction < 0 && article.hasChildren()) {
+ article = _.last(article.articles);
}
- } else {
- // prev has the same parent
- return parentsArticles[pos - 1];
+
+ return article;
}
+
+ return null;
+};
+
+// Return next article in the TOC
+TOCArticle.prototype.next = function() {
+ return this._sibling(1);
+};
+
+// Return previous article in the TOC
+TOCArticle.prototype.prev = function() {
+ return this._sibling(-1);
};
// Map over all articles
@@ -228,6 +252,13 @@ Summary.prototype.getArticle = function(page) {
});
};
+// Return the first TOCArticle for a specific level
+Summary.prototype.getArticleByLevel = function(lvl) {
+ return this.find(function(article) {
+ return article.level == lvl;
+ });
+};
+
// Return the count of articles in the summary
Summary.prototype.count = function() {
return this._length;