-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
451 lines (400 loc) · 19.5 KB
/
index.html
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<!---------------------------------+
keys: |
+ fact: a fact is a pro/con. |
+ item: set of facts. |
--------------------------------->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>mass comparison helper</title>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
<link rel="stylesheet" href="./css/index.min.css">
</head>
<body>
<script>
// functions ----------------------------------------------------------------------------
var createElement = document.createElement.bind(document)
function appendFactToOl(parent, html) {
let listItem = createElement("li")
listItem.insertAdjacentHTML('beforeend', html)
parent.appendChild(listItem)
}
function deleteSecondFact(fact) {
let previousSibling = fact.previousSibling
while(previousSibling) {
if(previousSibling.nodeType != 1)
previousSibling.remove()
else if(previousSibling.tagName != "DIV")
previousSibling.remove()
else
break
previousSibling = fact.previousSibling
}
fact.previousElementSibling.focus();
( contentEditableElement => { //focus to EOL
range = document.createRange()
range.selectNodeContents(contentEditableElement)
range.collapse(false)
selection = window.getSelection()
selection.removeAllRanges()
selection.addRange(range)
} ) (fact.previousElementSibling)
fact.remove()
}
function doReordering() {
let mainOl = document.getElementsByTagName("ol")[0]
let sortedList = getUnsortedElementList()
sortedList.sort( (a, b) => {
if(a.count == b.count)
return (a.factCount < b.factCount) ? 1 : -1
return (parseInt(a.count) < parseInt(b.count)) ? 1 : -1
} )
mainOl.textContent = ''
sortedList.forEach( c => {
mainOl.appendChild(c.element)
} )
reorderButtonHide()
}
function factHtml(thing = 'pro', text = '') {
return `
<span class="sign" onclick="signChange(this)">` + ( (thing == 'pro') ? '+' : '-' ) + `</span>:
<div class="fact" type="text" placeholder="` + thing + `" onkeyup="factOnKeyUp(event)" onkeydown="return factOnKeyDown(event)" contenteditable>` + text + `</div>
`
}
let suggestionActive = false
function factOnKeyUp(e) {
// updates counter
// removes/reinstates suggestion
function removeActiveSuggestion() {
Array.from( document.getElementsByClassName("suggestion") ).forEach( c => c.remove() )
}
let fact = e.target
updateCounter(fact)
let whatsWritten = fact.textContent
if( whatsWritten.trim() ) {
removeActiveSuggestion()
let suggestion = ""
for(let i = 0; i < window.d_factsList.length; i++)
if( window.d_factsList[i].startsWith(whatsWritten) ) {
suggestion = window.d_factsList[i]
break
}
if(suggestion) {
fact.insertAdjacentHTML("afterend", `<span class="suggestion">` + suggestion.replace(whatsWritten, '') + "</span>")
suggestionActive = true
}
} else
if(suggestionActive) {
removeActiveSuggestion()
suggestionActive = false
}
}
function factOnKeyDown(e) {
// handles backspace
// etc
let fact = e.target
let li = fact.parentElement
function addNewLi() {
let ol = li.parentElement
if(fact.textContent) {
appendFactToOl( ol, factHtml() )
ol.lastElementChild.lastElementChild.focus()
} else {
fact.parentElement.remove()
let outerLi = ol.parentElement
outerLi.insertAdjacentHTML("afterend", `
<li>
<div class="item" onkeydown="return rejectEnters(event)" role="textbox" placeholder="Item" contenteditable></div> [<span class="counter">±8</span>]
<ol type="a">
<li>`
+ factHtml() +
`</li>
</ol>
</li>
`)
outerLi.nextElementSibling.firstElementChild.focus()
}
}
function placeCaretAtEnd(el) {
el.focus();
if (typeof window.getSelection != "undefined"
&& typeof document.createRange != "undefined") {
var range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(false);
textRange.select();
}
}
if(e.keyCode == 8) {
// backspace-handler
let ol = li.parentElement
if(!fact.textContent) {
if(li.getElementsByClassName("fact").length > 1)
// if there is a fact before this one
deleteSecondFact(fact)
else if(ol.getElementsByClassName("fact").length > 1) {
// if there are more fact sets
let previousLi = li.previousElementSibling
li.remove()
placeCaretAtEnd(previousLi.lastElementChild)
} else
//if we are deleting a lonely fact
placeCaretAtEnd(ol.parentElement.firstElementChild)
return false
}
}
if(e.keyCode == 9) { //tab key
if(fact.textContent) {
if(fact.getAttribute("placeholder") == 'pro') {
if(!fact.nextElementSibling)
fact.insertAdjacentHTML( 'afterend', ` | ` + factHtml('con') )
fact.nextElementSibling.nextElementSibling.focus()
} else {
if(li.getElementsByClassName('fact').length == 1) {
li.insertAdjacentHTML( 'afterbegin', factHtml() + ' | ')
li.firstElementChild.nextElementSibling.focus()
}
else addNewLi()
}
} else signChange(fact.previousElementSibling)
return false
}
if(e.keyCode == 13) { //enter key
//implement, if there's only one "-" in the thing add a plus before it
addNewLi()
return false
}
}
function getUnsortedElementList() { // what ??
let mainOl = document.getElementsByTagName("ol")[0]
let sortedList = []
for( let counter of mainOl.getElementsByClassName('counter') ) {
let outerLi = counter.parentElement
sortedList.push({
count : counter.textContent,
element : outerLi,
factCount: outerLi.getElementsByClassName("fact").length
})
}
return sortedList
}
function itemOnKeyDown(event) {
rejectEnters(event)
if(event.keyCode == 8) {
// backspace-handler
var item = event.target
if(!item.textContent) {
var itemLi = item.parentElement
var facts = itemLi.getElementsByClassName("fact")
console.log(facts.length)
if(facts.length == 1 && !facts[0].textContent)
item.parentElement.remove()
return false
}
}
}
function rejectEnters(e) {
if(e.keyCode == 13)
return false
}
function reorderButtonHide() {
document.getElementsByTagName("button")[0].removeAttribute('style')
}
function signChange(e) {
var factDiv = e.nextElementSibling
if(e.textContent == "-") {
e.textContent = "+"
factDiv.setAttribute("placeholder", "pro")
}
else {
e.textContent = "-"
factDiv.setAttribute("placeholder", "con")
}
let li = factDiv.parentElement
if(li.getElementsByClassName('fact').length > 1) {
let previous = factDiv.previousSibling
while(previous) {
if(previous.nodeType == 1)
if(previous.tagName == 'DIV')
break
previous = previous.previousSibling
}
let placeholder = previous.getAttribute("placeholder")
if( (placeholder == "pro" && e.textContent == "+") || (placeholder == "con" && e.textContent == "-") ) {
let factContents = factDiv.textContent
deleteSecondFact(factDiv)
let ol = li.parentElement
appendFactToOl( ol, factHtml(e.textContent == "+" ? "pro" : "con", factContents) )
factDiv = ol.lastElementChild.lastElementChild
factDiv.focus()
}
}
updateCounter(factDiv)
}
function titleChanger(title) {
// changes the window title
if(title)
document.title = title + ' - mass comparison helper'
else
document.title = "mass comparison helper"
}
function updateCounter(e) {
var itemLi = e.parentElement.parentElement.parentElement
var counterCount = 0, factsLength = 0
for( let fact of itemLi.getElementsByClassName("fact") ) {
factsLength++
if(fact.textContent) {
if(fact.getAttribute("placeholder") == 'pro')
counterCount++
else
counterCount--
}
}
var counter = itemLi.getElementsByClassName("counter")[0]
let previousCount = counter.textContent
if(factsLength == 1 && !counterCount)
counter.textContent = "±8"
else
counter.textContent = counterCount
if(previousCount != counter.textContent) {
sortedList = getUnsortedElementList()
if(sortedList.length > 1) {
if( !sortedList.every( (c, i, array) => {
if(i == 0)
return true
prevCount = parseInt(array[i-1].count)
currCount = parseInt(c.count)
if(prevCount == currCount)
return array[i-1].factCount >= c.factCount
return prevCount >= currCount
} ) )
document.getElementsByTagName("button")[0].style.display = "initial"
else
reorderButtonHide()
} else
reorderButtonHide()
}
}
</script>
<!-- MAIN -->
<div id="title" onkeyup="titleChanger(this.textContent)" onkeydown="return rejectEnters(event)" role="textbox"
placeholder="TITLE" contenteditable
></div>
<button type="button" onclick="doReordering()">reorder</button>
<ol>
<li>
<div class="item" onkeydown="return itemOnKeyDown(event)" role="textbox" placeholder="Item"
contenteditable
></div> [<span class="counter">±8</span>]
<!-- Note that we are rejecting enters on .item because every new item by default has a fact -->
<ol type="a">
<li>
<script>
document.write( factHtml() )
</script>
</li>
</ol>
</li>
</ol>
<div id="empty"></div>
<div id="notescontainer">
<div id="addnotes" placeholder="add notes..." contenteditable></div>
</div>
<script>
const FileSync = require('lowdb/adapters/FileSync')
const ipcRenderer = require('electron').ipcRenderer
const lowDB = require('lowdb')
const titleElement = document.getElementById("title")
function fileOpen(location) {
const db = lowDB( new FileSync(location) )
titleElement.textContent = db.get('meta.title').value()
if( db.get('meta.notes').value() ) {
let addnotesDiv = document.getElementById("addnotes")
addnotesDiv.innerHTML = db.get('meta.notes').value()
document.getElementById("empty").style.height = addnotesDiv.clientHeight + "px"
console.log(addnotesDiv.offsetHeight)
}
const itemList = titleElement.nextElementSibling.nextElementSibling
itemList.textContent = ''
db.get('items').value().forEach(item => {
let listItem = createElement('li')
listItem.insertAdjacentHTML('beforeend', `
<div class="item" onkeydown="return rejectEnters(event)" role="textbox" placeholder="Item" contenteditable>` + item.item + `</div> [<span class="counter">±8</span>]
`)
let factsList = createElement('ol')
factsList.setAttribute('type', 'a')
item.facts.forEach(fact => {
let html = factHtml(fact.thing, fact.text)
if(factsList.childElementCount > 0) {
let lastLi = factsList.lastElementChild
if(lastLi.getElementsByClassName("fact").length == 1 && (c => { return ( c == 'pro' ? 'con' : 'pro') })( lastLi.lastElementChild.getAttribute("placeholder") ) == fact.thing)
lastLi.insertAdjacentHTML('beforeend', ' | ' + html)
else
appendFactToOl(factsList, html)
} else
appendFactToOl(factsList, html)
})
listItem.appendChild(factsList)
itemList.appendChild(listItem)
})
for (let item of itemList.children)
updateCounter(item.lastElementChild.lastElementChild.lastElementChild)
//todo: make a list of all the facts and put them in the memory
window.d_factsList = []
for ( fact of document.getElementsByClassName("fact") )
d_factsList.push(fact.innerHTML)
window.d_factsList = [...new Set(d_factsList)]
return false
}
//if the app was dragged onto the dock or used with open-with:
let openWith = ipcRenderer.sendSync('query-open-with', null)
if(openWith) {
fileOpen(openWith)
}
let fileLocation = false
window.addEventListener('keydown', (event) => {
if(event.metaKey && event.keyCode == 83) { //filesave
const title = titleElement.textContent
const fs = require('fs')
if(!fileLocation) {
fileLocation = ipcRenderer.sendSync('query-save-location', title)
if( fs.existsSync(fileLocation) )
fs.unlinkSync(fileLocation)
} else {
fs.unlinkSync(fileLocation)
}
const db = lowDB( new FileSync(fileLocation) )
db.defaults({meta: {}, items: []}).write()
db.set('meta.title', title).write()
if(document.getElementById("addnotes").textContent)
db.set('meta.notes', document.getElementById("addnotes").innerHTML).write()
for( let item of document.getElementsByClassName("item") ) {
var facts = []
for( let fact of item.parentElement.getElementsByClassName("fact") ) {
facts.push({
thing: fact.getAttribute('placeholder'),
text: fact.textContent
})
}
db.get('items').push({
item: item.textContent,
facts: facts
}).write()
}
return false
} else if(event.metaKey && event.keyCode == 79) { //file open
fileLocation = ipcRenderer.sendSync('query-open-location', null)[0]
fileOpen(fileLocation)
}
}, true)
</script>
</body>
</html>