-
Notifications
You must be signed in to change notification settings - Fork 37
/
interactive_variables.js
261 lines (231 loc) · 7.43 KB
/
interactive_variables.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* Get the existing variable names
* @returns the set of all different variable names
*/
function getAllVariables() {
allVariables = document.getElementsByClassName("var")
s = new Set()
for (variable of allVariables) {
classList = variable.classList
for (className of classList) {
if (className.startsWith('var_')) {
s.add(className)
}
}
}
return s
}
/**
* Get the variable name of an object
* @param {Object} obj the object
* @returns {String} the variable name of the object
*/
function getVariableName(obj) {
classList = obj.classList
for (className of classList) {
if (className.startsWith('var_')) {
return className
}
}
return ""
}
/**
* Selects the contents of an element
* @param {*} el the element to select the contents of
*/
function selectElementContents(el) {
var range = document.createRange();
range.selectNodeContents(el.children[0]);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
/**
* Handler for clicking an editable variable.
* Underlines all objects and selects the content of the current on.
*
* @param {*} listObjs list of objects to underline
* @param {*} obj current object to select
*/
function onClickAction(listObjs, obj) {
text = obj.innerText.trim()
for (elem of listObjs) {
if (!elem.innerHTML.includes("<span")) {
continue
}
if (text.length != 0) {
elem.setAttribute("style", "border-bottom: 2px solid #aaa;")
}
}
selectElementContents(obj);
}
/**
* Handler for when you unfocus the editing of a variable.
* Removes the style of all related objects.
*
* @param {*} listObjs list of objects to remove the style
*/
function onBlurAction(listObjs) {
for (elem of listObjs) {
// ignore divs without span
if (!elem.innerHTML.includes('<span')) {
continue
}
// its empty so we dont want to remove the style
if (!elem.innerHTML.includes("><")) {
elem.removeAttribute("style")
}
}
}
/**
* Input handler -- underlines and changes the text of all relate objects to the current one.
* Also updates the local storage.
*
* @param {*} elemsWithClassName iterable with the objects of a certain class name
* @param {*} cur currently edited text
* @param {*} name variable name
*/
function onInputAction(elemsWithClassName, cur, name) {
text = cur.innerText.trim()
toBox = text.length == 0
for (elem of elemsWithClassName) {
if (!elem.innerHTML.includes('<span')) {
continue
}
if (toBox) {
elem.setAttribute("style", "border: 2px solid #aaa;")
} else {
elem.setAttribute("style", "border-bottom: 2px solid #aaa;")
}
if (elem != cur) {
var innerSpan = elem.children[0];
innerSpan.textContent = text;
}
}
// if (cur.innerText.trim().length == 0) {
// cur.setAttribute("style", "border: 2px solid #aaa;")
// }
updateLocalStorage(name, cur.textContent.trim())
}
/**
* Update this page's local storage dictionary
* @param {string} variable_name variable name to be changed
* @param {string} new_name new value of this variable
*/
function updateLocalStorage(variable_name, new_name) {
URL_s = window.location.pathname
data = window.localStorage.getItem(URL_s)
if (data == null) {
data = {}
} else {
data = JSON.parse(data);
}
data[variable_name] = new_name
window.localStorage.setItem(URL_s, JSON.stringify(data));
}
/**
* Rename the current page variables according to the local storage
*/
function renameWithLocalStorage() {
URL_s = window.location.pathname
data = window.localStorage.getItem(URL_s)
if (data == null) {
return
}
data = JSON.parse(data);
for (var key in data) {
elemsWithClassName = document.body.getElementsByClassName(key)
for (elem of elemsWithClassName) {
if (!elem.innerHTML.includes('<span')) {
continue
}
var innerSpan = elem.children[0];
innerSpan.textContent = data[key];
}
}
}
/**
* Resets the names of the current page to their original value
*/
function resetVariableNames() {
url = window.location.pathname
data = window.localStorage.getItem(url)
if (data == null) {
return
}
data = JSON.parse(data);
for (var key in data) {
elemsWithClassName = document.body.getElementsByClassName(key)
for (elem of elemsWithClassName) {
if (!elem.innerHTML.includes('<span')) {
continue
}
var innerSpan = elem.children[0];
if (key.startsWith("var_vec_")) {
innerSpan.textContent = key.split("_")[2];
} else if (key.startsWith("var_")) {
innerSpan.textContent = key.split("_")[1];
}
}
}
window.localStorage.removeItem(url)
}
/**
* Set variable's elements as editable and configures all handlers, onclick, onblur, input and keydown.
*/
function setEditableAndConfigureHandlers() {
allVarNames = getAllVariables()
for (varName of allVarNames) {
elemsWithClassName = document.body.getElementsByClassName(varName)
for (o of elemsWithClassName) {
// not interested in objects with more than 1 child
if (o.children.length != 1) {
continue
}
// set as editable
o.setAttribute("contenteditable", "true")
// set handlers
o.onclick = (function (elemsWithClassName, obj) {
return function () {
onClickAction(elemsWithClassName, obj);
}
})(elemsWithClassName, o);
o.onblur = (function (elemsWithClassName) {
return function () {
onBlurAction(elemsWithClassName);
}
})(elemsWithClassName);
o.addEventListener('input', (function (elemsWithClassName, obj, name) {
return function (event) {
onInputAction(elemsWithClassName, obj, name)
}
})(elemsWithClassName, o, varName))
o.addEventListener('keydown', (function (o) {
return function (evt) {
// pressed enter
if (evt.keyCode === 13 || evt.keyCode === 27) {
// console.log("pressed enter or escape. Gonna blur the element", evt.keyCode)
o.blur()
// unselect text
window.getSelection().removeAllRanges();
evt.preventDefault()
}
// press delete or backspace on "empty" label
else if (o.innerText.trim().length == 0 && (evt.keyCode === 8 || evt.keyCode === 46)) {
// console.log("blocking event", evt.keyCode)
evt.preventDefault();
}
}
})(o))
}
}
}
/**
* Runs after mathjax has finished rendering. It:
* - renames variables using local storage
* - Sets the variable divs as editable, and configures handlers for click, blur and input
*/
function setupInteractiveVariables() {
renameWithLocalStorage()
setEditableAndConfigureHandlers()
}