forked from dglittle/PIMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
653 lines (582 loc) · 18.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
<html>
<head>
<title>PIMA</title>
<style>
.fill {
width: 100%;
height: 100%;
}
table {
border-collapse: collapse;
}
th, td {
padding: 0;
}
.header {
margin-top:15px;
margin-bottom:5px;
margin-left:5px;
font-size:small;
font-weight:bold
}
</style>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://dglittle.github.io/gl519/index.js"></script>
<script src="http://dglittle.github.io/cdn/tab.js"></script>
<script src="AES.js"></script>
<script src="u_new.js"></script>
<script>
_.getStartOfDay = function (time) {
var t = new Date(time)
t.setHours(0)
t.setMinutes(0)
t.setSeconds(0)
t.setMilliseconds(0)
return t.getTime()
}
_.englishTimeSpan = function(t) {
var second = 1000
var minute = second * 60
var hour = minute * 60
var day = hour * 24
var week = day * 7
var month = day * 30
var year = day * 365
// years
if (t >= Number.MAX_VALUE)
return "never"
if (t / year >= 1)
return (t / year).toFixed(1) + " years"
if (t / month >= 1)
return (t / month).toFixed(1) + " months"
if (t / week >= 1)
return (t / week).toFixed(1) + " weeks"
if (t / day >= 1)
return (t / day).toFixed(1) + " days"
if (t / hour >= 1)
return (t / hour).toFixed(1) + " hours"
if (t / minute >= 1)
return (t / minute).toFixed(1) + " minutes"
if (t / second >= 1)
return (t / second).toFixed(1) + " seconds"
return "now"
}
function openCard(card) {
card.touchTime = _.time()
g_current_card = card
save()
refresh()
}
function newCard(now) {
now = now || _.time()
return {
text : "",
createTime : now,
showTime : now,
touchTime : now,
lastPushTime : 0
}
}
function getTitle(card) {
return card.text.match(/\s*(.*)/)[1]
}
function addNewCard() {
var card = newCard()
g_db.cards.push(card)
openCard(card)
$('.mainCardEditor textarea').focus()
}
function drawSmallCard(card) {
var div = $('<div style="width:180px"/>')
div.text(getTitle(card))
div.height('1.2em')
div.css('overflow', 'hidden')
div.css('background', card == g_current_card ? 'gold' : 'lightgrey')
div.click(function() {
openCard(card)
})
return div
}
function drawSmallCards(cards) {
var div = $('<div class="fill"/>')
div.css('overflow', 'auto')
_.each(cards, function (e) {
div.append(drawSmallCard(e).css('margin-bottom', '1px'))
})
return div
}
function searchCards(cards, q) {
if (q.match(/^::size$/)) {
return _.sortDesc(cards, function (c) { return c.text.length }).slice(0, 20)
}
var searchAll = false
if (q.match(/^:/)) {
searchAll = true
q = q.slice(1)
}
var REs = _.map(q.split(/\s+|\b/), function (q) { return new RegExp(_.escapeRegExp(q), "i") })
function scoreCard(card) {
if (!searchAll && card.showTime >= Number.MAX_VALUE) {
return -1
}
var score = -1
var title = getTitle(card)
_.each(REs, function (re) {
if (title.match(re))
score += 3
else if (card.text.match(re))
score += 1
})
return score + (1.0 / (title.length + 2))
}
return _.map(_.sortBy(_.filter(_.map(cards, function (e) {
return { card : e, score : scoreCard(e) }
}), function (e) {
return e.score >= 0
}), function (e) {
return -e.score
}), function (e) {
return e.card
})
}
function goToCard() {
var textarea = $('textarea:focus').get()[0]
if (textarea) {
var s = ""
if (textarea.selectionStart != textarea.selectionEnd) {
s = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd)
} else {
for (var start = textarea.selectionStart; start >= 0; start--) {
if (!textarea.value[start].match(/./)) {
start++
break
}
}
for (var end = textarea.selectionStart; end < textarea.value.length; end++) {
if (!textarea.value[end].match(/./)) {
break
}
}
s = textarea.value.substring(start, end)
}
if (s) {
openCard(searchCards(g_db.cards, s)[0])
}
}
}
function drawSmallCardsWithDates(num) {
var div = $('<div class="fill"/>')
div.css('overflow', 'auto')
var cards = _.sortBy(g_db.cards, function (e) { return e.showTime }).slice(0, num)
var startOfToday = _.getStartOfDay(_.time())
function getDisplayDate(time) {
return ("" + new Date(time)).replace(/0+([1-9])/g, '$1').match(/\S+ \S+ \S+/)[0]
}
function drawHeader(time) {
var text = null
if (time < startOfToday)
text = "backlog"
else if (time >= startOfToday && time < startOfToday + 24 * 60 * 60 * 1000)
text = getDisplayDate(time) + " (today)"
else
text = getDisplayDate(time)
return $('<div class="header">').text(text)
}
var nextHeaderTime = startOfToday
_.each(cards, function (c, i) {
if (i == 0 && c.showTime < startOfToday) {
div.append(drawHeader(c.showTime))
}
while (nextHeaderTime <= c.showTime) {
div.append(drawHeader(nextHeaderTime))
nextHeaderTime += 24 * 60 * 60 * 1000
}
div.append(drawSmallCard(c).css('margin-bottom', '1px'))
})
return div
}
function drawCardEditor(card) {
var div = $('<div class="fill"/>')
var t = $('<textarea class="fill"/>')
// this setTimeout works around a bug in WebKit,
// and may not be necessary after that bug is fixed.
// see: http://realgl.blogspot.com/2013/01/webkit-bug.html
setTimeout(function () {
t.val(card.text)
function f() {
card.text = t.val()
save()
}
t.keyup(f)
t.change(f)
}, 1)
div.append(t)
return div
}
function drawCardPasswordEditor(card) {
var now = _.time()
if ((card.showPasswordsUntil && now < card.showPasswordsUntil) || (!card.showPasswordsUntil && now < card.createTime + 1000 * 60 * 10)) {
return splitVert(1, $('<button>hide passwords</button>').click(function () {
card.showPasswordsUntil = _.time()
refresh()
}), drawCardEditor(card))
}
var top = $('<div/>')
top.append($('<button>show passwords</button>').click(function () {
card.showPasswordsUntil = _.time() + 1000 * 60 * 10
refresh()
}))
var copypad = $('<input type="text" style="width:7px"></input>')
var copypadMessage = $('<span></span>')
top.append(copypad).append(copypadMessage)
var s = card.text
var token = "XXX"
while (s.match(token)) {
token += "X"
}
var a = []
s = s.replace(/(http:|https:)|(:\s*)(.*)/g, function (m, _, colon, value) {
if (_) return m
a.push(value)
return colon + token
})
s = _.escapeXml(s)
s = s.replace(/\r?\n/g, '<br>')
var i = 0
s = s.replace(new RegExp(token, 'g'), function () {
var r = '<span class="copyable" style="background:lightgrey">XXX</span>'
i++
return r
})
var bottom = $('<div class="fill"/>').html(s + "<br>")
$('.copyable', bottom).each(function (i, span) {
$(span).click(function () {
copypad.val(a[i]).select().focus()
copypadMessage.html('<b>← ctrl-c</b>')
})
})
copypad.keyup(function () {
window.setTimeout(function () {
copypad.val('nothing').select().focus()
copypadMessage.text('')
}, 0)
})
return splitVert(1, top, bottom)
}
g_searchQuery = ""
function drawCardSearchAndRecent() {
var div = $('<div class="fill"/>')
div.css('overflow', 'auto')
div.append($('<div class="header">recent</div>'))
div.append(drawSmallCards(_.sortBy(g_db.cards, function (e) { return -e.touchTime }).slice(0, 4)).removeClass('fill').css('width', '100%'))
div.append($('<div class="header">search</div>'))
div.append($('<input class="searchBox" type="text" style="width:100%"/>').val(g_searchQuery).keydown(function (e) {
if (e.keyCode == 13) {
g_searchQuery = $(e.currentTarget).val()
openCard(searchCards(g_db.cards, g_searchQuery)[0])
}
}))
div.append(drawSmallCards(g_searchQuery ? searchCards(g_db.cards, g_searchQuery) : []).removeClass('fill').css('width', '100%'))
return div
}
function pushback(card, amount, fuzzy, cb) {
if (amount < 0) amount = card.lastPushTime
if (fuzzy) amount *= _.lerp(0, 1, 1, 0.5, Math.pow(Math.random(), 2))
card.lastPushTime = amount
var openNext = true
if (amount > 0) {
card.showTime = _.time() + card.lastPushTime
} else {
var today = _.getStartOfDay(_.time())
var tomorrow = today + 24 * 60 * 60 * 1000
if (card.showTime > tomorrow) openNext = false
card.showTime = today
_.each(g_db.cards, function (c) {
if (c.showTime < tomorrow && c.showTime >= card.showTime)
card.showTime = c.showTime + 1
})
}
if (!cb) {
if (openNext) {
openCard(_.min(g_db.cards, function (e) { return e.showTime }))
} else {
save()
refresh()
}
} else {
cb()
}
}
function drawCardInfo(card) {
var div = $('<div/>')
div.text("showtime: " + _.englishTimeSpan(card.showTime - _.time()))
return div
}
function drawPushbackButtons(card, cb) {
var div = $('<div/>')
function createButton(label, time, fuzzy) {
var b
div.append(b = $('<button/>').text(label).click(function () {
if (card)
pushback(card, time, fuzzy, cb)
}))
return b
}
createButton('.', 0)
createButton('1', 24 * 60 * 60 * 1000)
createButton('2', 2 * 24 * 60 * 60 * 1000)
createButton('4', 4 * 24 * 60 * 60 * 1000, true)
createButton('w', 7 * 24 * 60 * 60 * 1000, true)
createButton('2w', 14 * 24 * 60 * 60 * 1000, true)
createButton('m', 30 * 24 * 60 * 60 * 1000, true)
createButton('2m', 60 * 24 * 60 * 60 * 1000, true)
createButton('4m', 120 * 24 * 60 * 60 * 1000, true)
createButton('y', 365 * 24 * 60 * 60 * 1000, true)
createButton('\u221E', Number.MAX_VALUE, false).css({
color : 'red',
'font-weight' : 'bold'
})
return div
}
function grabFromGMail(username, password, cb) {
$.post('/api/gmail/' + g_secret_id, username + "," + password, function (data) {
var gmailIds = {}
_.each(g_db.cards, function (card) {
gmailIds[card.gmailId] = true
})
var incomingIds = []
var ms = eval('(' + data + ')')
var now = _.time()
_.each(ms, function (m) {
incomingIds.push(m.gmailId)
if (!gmailIds[m.gmailId]) {
gmailIds[m.gmailId] = true
var card = newCard(now++)
card.gmailId = m.gmailId
var from = m.from
from = _.trim(from.replace(/ <[^>]+>/, ''))
if (from.indexOf("Greg Little") >= 0) {
from = ""
} else {
from = '[' + from + '] '
}
card.text = from + m.subject + "\n: " + ("https://mail.google.com/mail/#inbox/" + m.gmailId) + "\n\n" + m.body.slice(0, 1024) + "\n\nfrom gmail " + username
g_db.cards.push(card)
}
})
refresh()
if (ms.length > 0) {
$.post('/api/gmail-removeStars/' + g_secret_id, [username, password].concat(incomingIds).join(','), function () {
grabFromGMail(username, password, cb)
})
} else {
cb()
}
})
}
function drawGrabFromGMailThing() {
var container = $('<div/>')
while (_.ensure(g_db, "gmailAccounts", []).length < 2) {
g_db.gmailAccounts.push({
username : "",
password : ""
})
}
_.each(g_db.gmailAccounts, function (a) {
var div = $('<div/>')
var username = $('<input type="text"></input>').attr("value", a.username)
var password = $('<input type="password" style="width:40px"></input>').attr("value", a.password)
div.append($('<button>get stars</button>').click(function () {
var me = $(this)
a.username = username.val()
a.password = password.val()
me.attr('disabled', 'disabled')
grabFromGMail(a.username, a.password, function () {
me.removeAttr('disabled')
})
})).append(username).append(password)
container.append(div)
})
return container
}
function splitHorz(percent, a, b) {
var t = $('<table class="fill"><tr><td class="a" width="' + percent + '%"></td><td class="b"></td></tr></table>')
var _a = t.find('.a')
var _b = t.find('.b')
_a.append(a)
_b.append(b)
return t
}
function splitVert(percent, a, b) {
var t = $('<table class="fill"><tr><td class="a" height="' + percent + '%"></td></tr><tr><td class="b"></td></tr></table>')
var _a = t.find('.a')
var _b = t.find('.b')
_a.append(a)
_b.append(b)
return t
}
function encrypt(plaintext, password) {
return Aes.Ctr.encrypt(plaintext, password, 256);
}
function decrypt(ciphertext, password) {
return Aes.Ctr.decrypt(ciphertext, password, 256);
}
function getPassword(cb) {
var div = $('<div/>')
div.text("password:")
function onOk() {
_.closeDialog()
cb(pass.val())
}
var pass = $('<input type="password"/>').keydown(function (e) {
if (e.keyCode == 13) {
e.preventDefault()
onOk()
}
})
div.append(pass)
div.append($('<button/>').text('ok').click(function () {
onOk()
}))
_.dialog(div)
pass.focus()
}
g_saver = null
function drawSavePanel() {
var d = $('<div/>')
var b = $('<button class="saveButton"/>').text('save').click(function () {
save(0)
})
if (!g_saver)
b.attr('disabled', 'disabled')
d.append(b)
d.append($('<span class="saveMessage" style="font-size:x-small"/>'))
return d
}
function save(delay) {
$('.saveButton').removeAttr('disabled')
if (g_saver)
clearTimeout(g_saver)
g_saver = setTimeout(function () {
var startTime = _.time()
var msg = []
var s = JSON.stringify(g_db)
msg.push('size: ' + size_format(s.length))
var deadSize = size_format(JSON.stringify(_.filter(g_db.cards, function (c) { return c.showTime >= Number.MAX_VALUE })).length - 2)
if (g_password) {
s = encrypt(s, g_password)
}
$.post('/api/save/' + XXX_SECRET_ID_XXX, s, function () {
$('.saveButton').attr('disabled', 'disabled')
if (delay == 0) {
refresh()
var endTime = _.time()
msg.push('time: ' + ((endTime - startTime) / 1000).toFixed(2) + "s")
$('.saveMessage').append($('<span style="margin-left:7px;margin-right:7px"/>').text(msg.join(', '))).append($('<button style="font-size:x-small"/>').text('purge: ' + (deadSize)).click(function () {
g_db.cards = _.filter(g_db.cards, function (c) { return c.showTime < Number.MAX_VALUE })
save(0)
}))
}
})
g_saver = null
}, delay == null ? 5 * 1000 : delay)
}
function refresh() {
var div = $('<div class="fill"/>')
var savePanel = drawSavePanel()
var workingMemory = drawCardEditor(g_db.workingCard)
if (!g_current_card) g_current_card = _.max(g_db.cards, function (e) { return e.touchTime })
var pushbackButtons = drawPushbackButtons(g_current_card)
var cardInfo = drawCardInfo(g_current_card)
if (g_current_card.text.match(/@password|pass:|password:/) && !g_current_card.text.match(/@nopass(word)?/)) {
var mainCard = drawCardPasswordEditor(g_current_card)
} else {
var mainCard = drawCardEditor(g_current_card)
}
mainCard.addClass('mainCardEditor')
var upcomingCards = drawSmallCardsWithDates(100)
var searchAndRecent = drawCardSearchAndRecent()
var gmail = drawGrabFromGMailThing()
div.append(
splitHorz(99,
splitHorz(50,
splitVert(1, savePanel,
splitVert(99, workingMemory, gmail)),
splitVert(1, cardInfo,
splitVert(1, pushbackButtons, mainCard))),
splitHorz(50, upcomingCards, searchAndRecent)
)
)
$('body').empty().append(div)
}
g_hotkeys = []
function setHotkeys() {
$('body').keydown(function (e) {
if (e.metaKey && (e.keyCode == 83)) {
e.preventDefault()
save(0)
// this prevents the keyup event soon to follow
// from marking us as dirty
var temp = save
save = function () {}
setTimeout(function () {
save = temp
}, 500)
}
if (e.metaKey && (e.keyCode == 13)) {
e.preventDefault()
addNewCard()
}
if (e.metaKey && (e.keyCode == 70)) {
e.preventDefault()
$('.searchBox').focus().select()
}
if (e.metaKey && (e.keyCode == 71)) {
e.preventDefault()
goToCard()
}
for (var i = g_hotkeys.length - 1; i >= 0; i--) {
g_hotkeys[i](e)
}
})
}
$(function () {
$('body').ajaxError(function (_, s) {
alert("error: " + s.responseText)
})
g_db = null
g_password = null
g_secret_id = XXX_SECRET_ID_XXX
g_current_card = null
$.get('/data.txt', function (data) {
if (!data) {
data = JSON.stringify({
cards : [newCard()],
workingCard : newCard()
})
}
var originalData = data
function tryToOpen() {
try {
g_db = eval('(' + data + ')')
} catch(e) {
}
if (g_db) {
refresh()
setHotkeys()
} else {
getPassword(function (pass) {
g_password = pass
data = decrypt(originalData, g_password)
tryToOpen()
})
}
}
tryToOpen()
})
})
</script>
</body>
</html>