summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/backbone/summary.js92
1 files changed, 13 insertions, 79 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;
});
};