Skip to content

Commit 8e2d267

Browse files
committed
Update clause numbers through the ToC
to allow new clauses to be added dynamically in the middle of the document.
1 parent feb7269 commit 8e2d267

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

clause.html

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616
<polymer-element name="cxx-clause" extends="cxx-section">
1717
<script>
1818
(function() {
19-
var clause_num = 1;
19+
function updateTocClauses() {
20+
this.super();
21+
var toc = document.querySelector('cxx-toc');
22+
if (toc) {
23+
toc.clauses = document.querySelectorAll('cxx-clause');
24+
}
25+
}
2026
Polymer('cxx-clause', {
21-
attached: function() {
22-
this.super();
23-
var my_clause_num = clause_num++;
24-
25-
this.async(function() {
26-
// async() lets the descendant elements upgrade; after which we
27-
// need to traverse them to collect section numbers.
28-
this.update_sec_nums(my_clause_num);
29-
});
27+
// Convenience function at the clause level, which gets called from <cxx-toc>.
28+
set_clause_num: function(clause_num) {
29+
this.update_sec_nums(clause_num);
3030
},
31+
32+
attached: updateTocClauses,
33+
detached: updateTocClauses,
3134
});
3235
})();
3336
</script>

toc.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ limitations under the License.
1515

1616
(function() {
1717
Polymer('cxx-toc', {
18-
// Hierarchy :: [{ elem: Element, sections: Hierarchy }]
18+
// Hierarchy :: [{ elem: Element, title: H1, sections: Hierarchy }]
1919
sections: [],
2020

21-
applyAuthorStyles: true,
21+
// Updated with the list of <cxx-clause> elements in the document each
22+
// time such an element is attached or detached.
23+
clauses: [],
2224

2325
collectSections: function(root) {
2426
var sections = [];
@@ -33,9 +35,15 @@ limitations under the License.
3335
sections: sections};
3436
},
3537

36-
created: function() {
37-
var clauses = window.document.querySelectorAll('cxx-clause');
38-
this.sections = clauses.array().map(this.collectSections, this);
38+
clausesChanged: function() {
39+
this.sections = this.clauses.array().map(function(clause, index) {
40+
clause.set_clause_num(index + 1);
41+
return this.collectSections(clause);
42+
}, this);
43+
},
44+
45+
attached: function() {
46+
this.clauses = document.querySelectorAll('cxx-clause');
3947
}
4048
})
4149
})();

0 commit comments

Comments
 (0)