-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.js
185 lines (162 loc) · 6.72 KB
/
main.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
(function () {
'use strict';
const classes = {
hidden: 'gcf-hidden-line',
sideways: 'gcf-sideways',
collapser: 'gcf-collapser',
ellipsis: 'gcf-ellipsis',
blockStart: 'gcf-block-start',
previouslyCollapsed: 'gcf-nested-hidden',
};
// Clear old classes and attributes from previous page loads
document.querySelectorAll('.' + classes.collapser).forEach((arrow) => {
arrow.parentElement.removeChild(arrow);
});
document.querySelectorAll('.' + classes.ellipsis).forEach((el) => {
el.parentElement.removeChild(el);
});
document.querySelectorAll('.' + classes.hidden).forEach((el) => {
el.classList.remove(classes.hidden);
});
document.querySelectorAll(`[${classes.previouslyCollapsed}]`).forEach((el) => {
el.removeAttribute(classes.previouslyCollapsed);
});
const codeLines = [...document.querySelectorAll('table.js-file-line-container tr .blob-code-inner')];
const codeLinesText = codeLines.map((l) => l.textContent);
const _arrow =
'<svg version="1.1" width="7px" fill="#969896" xmlns="http://www.w3.org/2000/svg" ' +
'xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1000 1000" enable-background="new 0 0 1000 1000" xml:space="preserve">' +
'<metadata> Svg Vector Icons : http://www.onlinewebfonts.com/icon </metadata>' +
'<g><path d="M579.5,879.8c-43.7,75.7-115.3,75.7-159,0L28.7,201.1c-43.7-75.7-8-137.7,79.5-137.7h783.7c87.5,0,123.2,62,79.5,137.7L579.5,879.8z"></path></g>' +
'</svg>';
class Element {
constructor(name) {
this.element = document.createElement(name);
}
addClass(className) {
this.element.classList.add(className);
return this;
}
setId(id) {
this.element.id = id;
return this;
}
setHTML(str) {
this.element.innerHTML = str;
return this;
}
}
const arrowFactory = (id) => {
return new Element('span').addClass(classes.collapser).setId(id).setHTML(_arrow).element;
};
const ellipsisFactory = (id) => {
return new Element('span').addClass(classes.ellipsis).setId(id).setHTML('...').element;
};
const spaceMap = new Map();
const pairs = new Map();
const stack = [];
const blockStarts = [];
const countLeadingWhitespace = (arr) => {
const getWhitespaceIndex = (i) => {
if (arr[i] !== ' ' && arr[i] !== '\t') {
return i;
}
i++;
return getWhitespaceIndex(i);
};
return getWhitespaceIndex(0);
};
const last = (arr) => arr[arr.length - 1];
const getPreviousSpaces = (map, lineNum) => {
let prev = map.get(lineNum - 1);
return prev === -1 ? getPreviousSpaces(map, lineNum - 1) : { lineNum: lineNum - 1, count: prev };
};
for (let lineNum = 0; lineNum < codeLinesText.length; lineNum++) {
let line = codeLinesText[lineNum];
let count = line.trim().length ? countLeadingWhitespace(line.split('')) : -1;
spaceMap.set(lineNum, count);
function tryPair() {
let top = last(stack);
if (count !== -1 && count <= spaceMap.get(top)) {
pairs.set(top, lineNum);
// codeLines[top].setAttribute(classes.blockStart, true);
const arrow = arrowFactory(`gcf-${top + 1}`);
codeLines[top].prepend(arrow);
blockStarts.push(codeLines[top]);
stack.pop();
return tryPair();
}
}
tryPair();
let prevSpaces = getPreviousSpaces(spaceMap, lineNum);
if (count > prevSpaces.count) {
stack.push(prevSpaces.lineNum);
}
}
const toggleCode = (action, start, end) => {
if (action === 'hide') {
const sliced = codeLines.slice(start, end);
sliced.forEach((elem) => {
const tr = elem.parentElement;
// If a line was already hidden, there was an inner block that
// was previously collapsed. Setting this attribute will
// protect the inner block from being expanded
// when this current outer block is expanded
if (tr.classList.contains(classes.hidden)) {
const prev = parseInt(tr.getAttribute(classes.previouslyCollapsed));
const count = prev ? prev + 1 : 1;
tr.setAttribute(classes.previouslyCollapsed, count);
}
tr.classList.add(classes.hidden);
});
codeLines[start - 1].appendChild(ellipsisFactory(`ellipsis-${start - 1}`));
} else if (action === 'show') {
const sliced = codeLines.slice(start, end);
const topLine = codeLines[start - 1];
sliced.forEach((elem) => {
const tr = elem.parentElement;
const nestedCount = parseInt(tr.getAttribute(classes.previouslyCollapsed));
if (!nestedCount) {
tr.classList.remove(classes.hidden);
} else if (nestedCount === 1) {
tr.removeAttribute(classes.previouslyCollapsed);
} else {
tr.setAttribute(classes.previouslyCollapsed, nestedCount - 1);
}
});
topLine.removeChild(topLine.lastChild);
}
};
const arrows = document.querySelectorAll('.' + classes.collapser);
function arrowListener(e) {
e.preventDefault();
let svg = e.currentTarget;
let td = e.currentTarget.parentElement;
let id = td.getAttribute('id');
let index = parseInt(id.slice(2)) - 1;
if (svg.classList.contains(classes.sideways)) {
svg.classList.remove(classes.sideways);
toggleCode('show', index + 1, pairs.get(index));
} else {
svg.classList.add(classes.sideways);
toggleCode('hide', index + 1, pairs.get(index));
}
}
arrows.forEach((c) => {
c.addEventListener('click', arrowListener);
});
function ellipsisListener(e) {
if (!e.target.parentElement) return;
if (e.target.classList.contains(classes.ellipsis)) {
let td = e.target.parentElement;
let svg = td.querySelector('.' + classes.sideways);
let id = e.target.parentElement.getAttribute('id');
let index = parseInt(id.slice(2)) - 1;
svg.classList.remove(classes.sideways);
toggleCode('show', index + 1, pairs.get(index));
}
}
blockStarts.forEach((line) => {
line.addEventListener('click', ellipsisListener);
});
})();