-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfolding.js
239 lines (211 loc) · 6.68 KB
/
folding.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
(function () {
"use strict";
var headlineExp = /^H(\d)$/;
var todoExp = /^(.+)\.todo$/;
var tagExp = /@([\w]+)/;
function foldIt(elements) {
elements = elements || document.getElementsByClassName('folding');
if (elements.length) {
for (var i = 0; i < elements.length; i++) {
foldingIt(elements.item(i));
}
}
else if (elements.tagName) {
foldingIt(elements);
}
}
window.foldIt = foldIt;
function foldingIt(element) {
element.classList.add('folding');
element.addEventListener('click', function detectInteractiveElements(e) {
var match;
var topElement = e.target;
while (!topElement.parentElement.classList.contains('folding')) {
topElement = topElement.parentElement;
}
// Tags
if (e.target.classList.contains('folding-tag')) {
var tag = e.target.getAttribute('data-tag');
if (!element.classList.contains('tag-filter-' + tag)) {
var tagClass = 'folding-tagged-' + tag;
var tagged = element.getElementsByClassName('folding-tagged'), candidate;
for (var i=0; i<tagged.length; i++) {
candidate = tagged.item(i);
if (candidate.classList.contains(tagClass)) {
candidate.classList.add('active-tag');
}
else {
candidate.classList.remove('active-tag');
}
}
element.classList.add('tag-filter');
element.classList.add('tag-filter-' + tag);
}
else {
element.classList.remove('tag-filter');
element.classList.remove('tag-filter-' + tag);
}
}
// Headlines (folding)
else if (match = headlineExp.exec(topElement.nodeName)) {
e.preventDefault();
toggleHeadlineFolding(topElement);
}
});
var current = element.firstElementChild;
var match = null;
var tagList = null;
while (current) {
if (headlineExp.exec(current.nodeName) && (match = todoExp.exec(current.textContent))) {
handleTodo(current, match[1]);
}
tagList = [];
detectTags(current, tagList);
if (tagList.length) {
moveUpHeadlines(current, function tagParentHeadlines(h) {
tagList.forEach(tagElement.bind(h));
});
}
current = current.nextElementSibling;
}
// Find items that should be collapsed by default
var collapse = element.getElementsByClassName('folding-collapsed');
for (var i = 0; i < collapse.length; i++) {
if (headlineExp.exec(collapse.item(i).nodeName)) {
toggleHeadlineFolding(collapse.item(i));
}
}
}
function detectTags(element, tagList) {
tagList = tagList || [];
var appendTags = [];
var node = element.firstChild, next;
while (node) {
next = node.nextSibling;
if (node.nodeName == '#text') {
tagChopper(node, tagList);
}
else {
var childTags = tagList.slice();
detectTags(node, childTags);
if (childTags.length > tagList.length) {
appendTags = appendTags.concat(childTags.slice(tagList.length));
}
}
node = next;
}
appendTags.forEach(function appendTag(tag) {
tagList.push(tag);
});
if (tagList.length) {
tagList.forEach(tagElement.bind(element));
}
else {
element.classList.add('no-tag');
}
}
function tagChopper(node, tagList) {
var match, tag;
var text = node.textContent;
while (match = tagExp.exec(text)) {
var tagName = match[1].toLowerCase();
node.textContent = match.index ? text.substr(0, match.index) : '';
tagList.push(tagName);
tag = document.createElement('span');
tag.textContent = match[0];
tag.classList.add('folding-tag');
tag.setAttribute('data-tag', tagName);
node.parentElement.insertBefore(tag, node.nextSibling);
text = text.substr(match.index + match[0].length);
if (text.length) {
tag.insertAdjacentText('afterend', text);
node = tag.nextSibling;
}
else {
node = null;
}
}
}
function tagElement(tag) {
this.classList.add('folding-tagged');
this.classList.add('folding-tagged-' + tag);
}
function moveUpHeadlines(element, each) {
var match = headlineExp.exec(element.nodeName);
var candidateMatch;
while (element && (element = element.previousElementSibling)) {
if (candidateMatch = headlineExp.exec(element.nodeName)) {
if (!match || candidateMatch[1] < match[1]) {
each(element);
match = candidateMatch;
}
}
}
}
function moveAcrossGap(element, each) {
var match = headlineExp.exec(element.nodeName);
var current = element.nextElementSibling;
while (current) {
var siblingMatch = headlineExp.exec(current.nodeName);
// If we hit a headline element at the same or higher level.
if (siblingMatch && siblingMatch[1] <= match[1]) {
return current;
}
if (each) current = each(current);
current = current.nextElementSibling;
}
}
function toggleHeadlineFolding(element) {
element.classList.toggle('folded')
var fold = element.classList.contains('folded');
var siblingMatch;
if (!fold) {
element.removeChild(element.lastElementChild);
}
moveAcrossGap(element, function toggleFolding(current) {
if (fold) {
current.classList.add('hidden-by-fold');
}
else {
current.classList.remove('hidden-by-fold');
if (current.classList.contains('folded')) {
current = moveAcrossGap(current);
current = current ? current.previousElementSibling : null;
}
}
return current;
});
if (fold) {
var ellipsis = document.createElement('div');
ellipsis.textContent = '…';
ellipsis.classList.add('folding-ellipsis');
element.appendChild(ellipsis);
}
}
function handleTodo(header, title) {
while (header.childNodes.length) {
header.removeChild(header.childNodes.item(0));
}
header.appendChild(document.createTextNode(title));
var decorator = document.createElement('span');
decorator.classList.add('decorator');
decorator.appendChild(document.createTextNode('.todo'));
header.appendChild(decorator);
var list = header.nextElementSibling;
if (list.nodeName == 'UL') {
handleTodoList(list);
}
}
function handleTodoList(list) {
list.classList.add('todo');
var exp = /@done$/m;
var items = list.getElementsByTagName('li');
var i, item;
for (i = 0; i < items.length; i++) {
item = items.item(i);
if (exp.exec(item.childNodes.item(0).textContent)) {
item.classList.add('done');
}
}
}
})();