Simplify .next and .prev for articles

This commit is contained in:
Samy Pessé
2016-02-21 12:05:31 +01:00
parent 884540e565
commit 51fd8ae08c
2 changed files with 45 additions and 95 deletions
+13 -79
View File
@@ -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;
});
};
+32 -16
View File
@@ -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,19 +213,6 @@ 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('hello7.md');
var prev = article.prev();
var next = article.next();
should(prev).be.ok();
should(next).be.ok();
prev.path.should.equal('hello6.md');
next.path.should.equal('hello8.md');
});
it('should return next/prev for a joint -> parts', function() {
var article = book.summary.getArticle('hello6.md');
@@ -236,16 +226,42 @@ describe('Summary / Table of contents', function() {
next.path.should.equal('hello7.md');
});
it('should return only prev for last', function() {
it('should return next/prev for a joint <- parts', function() {
var article = book.summary.getArticle('hello7.md');
var prev = article.prev();
var next = article.next();
should(prev).be.ok();
should(next).be.ok();
prev.path.should.equal('hello6.md');
next.path.should.equal('hello8.md');
});
it('should return next and prev', function() {
var article = book.summary.getArticle('hello8.md');
var prev = article.prev();
var next = article.next();
should(prev).be.ok();
should(next).be.ok();
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('hello10.md');
var prev = article.prev();
var next = article.next();
should(prev).be.ok();
should(next).be.not.ok();
prev.path.should.equal('hello7.md');
prev.path.should.equal('hello9.md');
});
});
});