-
Notifications
You must be signed in to change notification settings - Fork 0
/
ref.js
188 lines (150 loc) · 4.93 KB
/
ref.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
// throw out any extra characters and punctuation out of the note reference url and
// replace single spaces with hyphens
function clean(n) {
n = n
.replace(/\={1,}/g,'-')
.replace(/[^a-zA-Z0-9 -]/g,'')
.replace(/\s{1,}/g,'-')
.replace(/\-{2,}/g,'-');
return n;
}
// NEED TO disable scroll for original content layer
// how to NOT TO REDRAW the object each time?
// ALSO, why the a href - need to make it open in bg with cmd
// close the overlay with esc key
// navigate through article references with keyboard arrows
textArray = [], anchors = [],
urls = [], values = [];
var ids = [];
// store all H2 headings
notesArray = [];
notesArray = document.getElementsByClassName("page-body")[0].getElementsByTagName('h2');
// extract text without stylistic tags and
// generate urls out of every H2 note
function refs() {
for (var i = 0, l = notesArray.length; i < l; i++) {
textArray[i] = notesArray[i].textContent.toLowerCase();
textArray[i] = clean(textArray[i]).substring(0,30);
}
for (var i = 0, l = textArray.length; i < l; i++) {
urls[i] = window.location.href.split(/\?|#/)[0] + '-ref' + '/' + textArray[i];
}
}
// delete overlay content (when closing)
function closeLayer(event) {
var element = document.getElementById("box");
element.className = "hidden";
}
// store all spans inside H2 headings
function val() {
values = document
.getElementsByClassName("page-body")[0]
.querySelectorAll('h2 > span');
return values;
}
var
myObject,
box,
backBtn;
// draw a hidden overlay
function createOverlay() {
var div = document.createElement('div');
var div2 = document.createElement('div');
var btn = document.createElement('a');
box = div;
var innerBox = div2;
box.id = "box";
box.className = "hidden";
box.style.cssText = 'position:fixed;top:0;left:0;width:100%;height:100%;opacity:1;z-index:999;background:#f2f2f2;paddding:0;';
innerBox.style.cssText = 'position:relative;width:calc(100%);height:calc(100%);margin:0;';
btn.textContent = "close and go back";
btn.className = "action-btn";
btn.style.cssText = 'position:fixed;top:40px;right:30px;width:auto;padding:0 18px;height:40px;line-height:40px;cursor:pointer;';
box.appendChild(innerBox);
box.appendChild(btn);
innerBox.id = "overlay-content";
innerBox.innerHTML=`<object type="text/html" id="ref-obj" data="" style="width: 100%;height:100%;"></object>`;
document.body.appendChild(box);
btn.onclick = closeLayer;
}
createOverlay();
// open overlay
function openOverlay(event) {
event.preventDefault();
// console.log('shown');
box.className = "";
}
function getObject() {
// get the object
myObject = document.getElementById("overlay-content").getElementsByTagName('object')[0];
// if object.innerHTML exists
// ?? document.getElementById('ref-obj').contentDocument.readyState === 'complete'
// assign an onclik event to parent card
// backBtn = document.getElementById('ref-obj').contentDocument.getElementsByClassName("parent-card")[0].getElementsByTagName('a')[0];
// console.log(backBtn);
}
function all() {
refs(); // get the urls
( some = function () {
function insertBlock() {
val(); // gets all spans
// gets out the seqence, 0 1 2 0
// values[i] contains the whole span tag
function generate() {
var
val,
anchor,
valueBox;
for (var i = 0, l = values.length; i < l; i++) {
valueBox = values[i].innerHTML;
val = parseInt(valueBox);
values[i].className += "hidden";
if (val > 0) {
if (val === 1) {
values[i].insertAdjacentHTML(
'beforebegin',
` <a href="${urls[i]}" id="a${[i]}" onclick="return openOverlay(event)" class="counter-wrapper"><span class="counter counter-valid">${valueBox}
reference</span></a>`
);
} else {
values[i].insertAdjacentHTML(
'beforebegin',
` <a href="${urls[i]}" id="a${[i]}" onclick="return openOverlay(event)" class="counter-wrapper"><span class="counter counter-valid">${valueBox}
references</span></a>`
);
valueBox = val + ' references';
}
}
}
}
generate();
}
insertBlock();
})();
// prototype
function again() {
function geta() {
anchors = document.getElementsByClassName('counter-wrapper');
}
geta();
// get the object
getObject();
// gets each anchor's id
for (var i = 0, l = anchors.length; i < l; i++) {
var molly = function(index) {
anchors[index].addEventListener("click", function() {
ids[index] = anchors[index].id;
myObject.data = anchors[index].href;
});
}(i);
}
}
again();
}
if (document.readyState === 'complete') {
all();
} else {
document.addEventListener("DOMContentLoaded", function(event) {
all();
});
}