diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-21 12:05:31 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-21 12:05:31 +0100 |
commit | 51fd8ae08c1616b6826e72dd86e658f4e921bed9 (patch) | |
tree | 2482d497f2873db429ba8f6bdc3ab0e2b8d234ac | |
parent | 884540e56530b8e1c68c94604c0b45f0489d5020 (diff) | |
download | gitbook-51fd8ae08c1616b6826e72dd86e658f4e921bed9.zip gitbook-51fd8ae08c1616b6826e72dd86e658f4e921bed9.tar.gz gitbook-51fd8ae08c1616b6826e72dd86e658f4e921bed9.tar.bz2 |
Simplify .next and .prev for articles
-rw-r--r-- | lib/backbone/summary.js | 92 | ||||
-rw-r--r-- | test/summary.js | 30 |
2 files changed, 36 insertions, 86 deletions
diff --git a/lib/backbone/summary.js b/lib/backbone/summary.js index 5cc1418..4cfff28 100644 --- a/lib/backbone/summary.js +++ b/lib/backbone/summary.js @@ -22,6 +22,8 @@ function TOCArticle(def, parent) { // As string indicating the overall position // ex: '1.0.0' this.level; + this._next; + this._prev; // When README has been automatically added this.isAutoIntro = def.isAutoIntro; @@ -103,91 +105,14 @@ TOCArticle.prototype.hasParent = function() { return !(this.parent instanceof TOCPart); }; -// Return true if has a part as parent -TOCArticle.prototype.hasParentPart = function() { - return (this.parent instanceof TOCPart); -}; - -// Return first article -TOCArticle.prototype.first = function() { - return _.first(this.articles); -}; - -// Return last article -TOCArticle.prototype.last = function() { - var last = _.last(this.articles); - if (!last) return null; - - return last.last() || last; -}; - -// Return a sibling (next or prev) in the parent -// Without taking in consideration children/parent -TOCArticle.prototype.sibling = function(direction) { - var parentsArticles = this.parent.articles; - var pos = _.findIndex(parentsArticles, this); - - if (parentsArticles[pos + direction]) { - return parentsArticles[pos + direction]; - } - - return null; -}; - -// 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 part; - var parentsArticles = this.parent.articles; - var pos = _.findIndex(parentsArticles, this); - - // First child and has parent - if (pos == 0 && direction < 0) { - if (this.hasParent()) { - return this.parent; - } else if (this.hasParentPart()) { - part = this.parent.sibling(-1); - return part? part.last() : null; - } - } - - // Last child and has parent - if(pos == (parentsArticles.length - 1) && direction > 0) { - if (this.hasParent()) { - return this.parent.sibling(1); - } else if (this.hasParentPart()) { - part = this.parent.sibling(1); - return part? part.first() : null; - } - } - - 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); - } - - return article; - } - - return null; -}; - // Return next article in the TOC TOCArticle.prototype.next = function() { - return this._sibling(1); + return this._next; }; // Return previous article in the TOC TOCArticle.prototype.prev = function() { - return this._sibling(-1); + return this._prev; }; // Map over all articles @@ -375,10 +300,19 @@ Summary.prototype.count = function() { // Update the count and indexing of "level" Summary.prototype.update = function() { var that = this; + var prev = undefined; that._length = 0; that.walk(function(article, level) { + // Index level article.level = level; + + // Chain articles + article._prev = prev; + if (prev) prev._next = article; + + prev = article; + that._length += 1; }); }; diff --git a/test/summary.js b/test/summary.js index f7a975f..68c23b4 100644 --- a/test/summary.js +++ b/test/summary.js @@ -126,7 +126,10 @@ describe('Summary / Table of contents', function() { '* [Hello 6](hello6.md)\n\n\n' + '### Part 2\n\n' + '* [Hello 7](hello7.md)\n' + - '* [Hello 8](hello8.md)\n\n' + ' * [Hello 8](hello8.md)\n\n' + + '### Part 3\n\n' + + '* [Hello 9](hello9.md)\n' + + '* [Hello 10](hello10.md)\n\n' }) .then(function(_book) { book = _book; @@ -210,6 +213,19 @@ describe('Summary / Table of contents', function() { next.path.should.equal('hello6.md'); }); + it('should return next/prev for a joint -> parts', function() { + var article = book.summary.getArticle('hello6.md'); + + var prev = article.prev(); + var next = article.next(); + + should(prev).be.ok(); + should(next).be.ok(); + + prev.path.should.equal('hello5.md'); + next.path.should.equal('hello7.md'); + }); + it('should return next/prev for a joint <- parts', function() { var article = book.summary.getArticle('hello7.md'); @@ -223,8 +239,8 @@ describe('Summary / Table of contents', function() { next.path.should.equal('hello8.md'); }); - it('should return next/prev for a joint -> parts', function() { - var article = book.summary.getArticle('hello6.md'); + it('should return next and prev', function() { + var article = book.summary.getArticle('hello8.md'); var prev = article.prev(); var next = article.next(); @@ -232,12 +248,12 @@ describe('Summary / Table of contents', function() { should(prev).be.ok(); should(next).be.ok(); - prev.path.should.equal('hello5.md'); - next.path.should.equal('hello7.md'); + prev.path.should.equal('hello7.md'); + next.path.should.equal('hello9.md'); }); it('should return only prev for last', function() { - var article = book.summary.getArticle('hello8.md'); + var article = book.summary.getArticle('hello10.md'); var prev = article.prev(); var next = article.next(); @@ -245,7 +261,7 @@ describe('Summary / Table of contents', function() { should(prev).be.ok(); should(next).be.not.ok(); - prev.path.should.equal('hello7.md'); + prev.path.should.equal('hello9.md'); }); }); }); |