-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsection.js
97 lines (84 loc) · 3.09 KB
/
section.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
(function() {
"use strict";
Polymer('spec-section', {
// String section number, like "17.2.3". 1-based.
sec_num: "?",
applyAuthorStyles: true,
checkInvariants: function() {
if (!this.id) {
console.error(this, 'is missing an id.');
}
},
update_sec_nums: function(sec_num) {
this.sec_num = sec_num + '';
var child_index = 1;
// Assume there aren't any elements between spec-section levels.
for (var child = this.firstElementChild;
child;
child = child.nextElementSibling) {
if (child.tagName.toUpperCase() == "SPEC-SECTION") {
child.update_sec_nums(this.sec_num + '.' + (child_index++));
}
}
},
sec_numChanged: function() {
if (this.title_element) {
this.title_element.setAttribute(
'data-bookmark-label',
this.sec_num + ' ' + this.title_element.textContent);
}
},
attached: function() {
var title_element = this.querySelector('h1');
if (title_element && title_element.parentElement == this) {
this.title_element = title_element;
}
// async() lets the descendant elements upgrade; after which we
// need to traverse them to assign paragraph numbers.
this.async(this.numberParagraphChildren);
},
numberParagraphChildren: function(rootElement, para_num_start) {
var para_num = para_num_start || 1;
for (var child = (rootElement || this).firstElementChild; child;
child = child.nextElementSibling) {
if (child instanceof SpecSectionElement) {
return para_num;
} else if (child instanceof HTMLParagraphElement &&
!child.classList.contains('cont')) {
this.numberParagraph(para_num++, child);
/*
} else if (child instanceof SpecFunctionElement) {
this.numberParagraph(para_num++, child);
para_num = this.numberParagraphChildren(child, para_num);
} else if (child instanceof CxxFunctionAttributeElement) {
this.numberParagraph(para_num++, child);
*/
}
}
return para_num;
},
numberParagraph: function(number, element) {
var id = this.id + '.' + number;
if (element.id) {
console.warn('Paragraph already has id:', element);
var anchor = document.createElement('a');
anchor.id = id;
element.insertBefore(anchor, element.firstChild);
} else {
element.id = id;
}
element.setAttribute('para_num', number);
}
})
})();