-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
555 lines (432 loc) · 13.4 KB
/
app.js
File metadata and controls
555 lines (432 loc) · 13.4 KB
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
let loadPollMatch = document.location.href.match(/loadPoll=([a-zA-Z0-9_]+)/)
function storeLocal(path, value){
localStorage.setItem(path, JSON.stringify(value))
}
function getLocal(path, value){
let stored = localStorage.getItem(path)
if(!stored) return value
try{
parseValue = JSON.parse(stored)
return parseValue
}catch(err){
return value
}
}
function IS_DEV(){
return document.location.host.match(/localhost|goorm.io/)
}
function SORT_UNIQUE(){
return document.location.href.match(/sort=unique/)
}
function getPollsUrl(unique, loadPoll){
let pollUrls = []
if(loadPoll){
pollUrls.push(`loadPoll=${loadPoll}`)
}else{
if(loadPollMatch) pollUrls.push(`loadPoll=${loadPollMatch[1]}`)
}
if(unique) pollUrls.push(`sort=unique`)
if(pollUrls.length){
return `/?${pollUrls.join("&")}`
}
return `/`
}
function getUser(){
return IS_DEV() ? User({
id: "@nonymous",
username: "@nonymous"
}) : User(USER)
}
function genLink(href, display){
return `<a href="${href}" rel="noopener noreferrer" target="_blank">${display}</a>`
}
function userLink(username){
return genLink(`https://lichess.org/@/${username}`, username)
}
function api(topic, payload){
return new Promise(resolve => fetch('/api', {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
topic: topic,
payload: payload
})
}).then(
response => response.text().then(
text => {
try{
let response = JSON.parse(text)
resolve(response)
}catch(err){
console.log("api parse error", err, text)
}
}
),
err => {
console.log("api response error", err)
}
)
)
}
function addTransaction(transaction){
return new Promise(resolve => {
return this.api("addTransaction", {
transaction: transaction.serialize()
}).then(result => {
if(result.quotaExceeded){
window.alert("Transaction quota exceeded. Wait a little.")
}else if(result.error){
window.alert(result.status)
}else{
resolve(result)
}
})
})
}
class ControlButton_ extends button_{
constructor(callback){
super(callback)
this.bdrs("solid").bdrw(1).bdrc("#aaa").bdrr(10).pad(4)
.addStyle("paddingLeft", "10px").addStyle("paddingRight", "10px")
.addStyle("boxShadow", "3px 3px #000")
}
}
function ControlButton(callback){return new ControlButton_(callback)}
class UserWithVote_ extends SmartdomElement_{
constructor(props){
super({...props, ...{tagName: "div"}})
this.dib().fl().aic().mar(2).pad(2).bc("#eee").a(
div().mar(2).pad(3).addStyle("paddingLeft", "8px").addStyle("paddingRight", "8px").bc("#add").ffms().addStyle("fontStyle", "italic").html(userLink(this.props.username))
)
if(this.props.showCount) this.a(
div().mar(2).marl(4).marr(4).pad(2).bc("#ffa").fwb().c("#070").html(`${this.props.votes}`)
)
}
}
function UserWithVote(props){return new UserWithVote_(props)}
class SmartPoll_ extends SmartdomElement_{
constructor(props){
super({...props, ...{tagName: "div"}})
this.parentState = this.props.parentState
this.poll = this.props.poll || Poll()
this.pad(2).mar(2).bc(this.isMine() ? "#afa" : "#eee").bdrs("solid").bdrw(1).bdrc("#777").bdrr(10)
this.build()
}
isMine(){
return this.poll.author.equalTo(getUser())
}
delete(){
if(!this.poll.author.equalTo(getUser())){
window.alert("You can only delete your own poll.")
return
}
let transaction = DeletePoll({
author: this.poll.author,
pollId: this.poll.pollId
})
addTransaction(transaction).then(result => {
console.info(result)
})
}
addOption(){
if(!this.poll.author.equalTo(getUser())){
window.alert("You can only add an option to your own poll.")
return
}
let optionText = window.prompt("Option :")
if(optionText){
let option = PollOption({
author: getUser(),
option: optionText,
parentPollId: this.poll.pollId
})
let transaction = AddOption({
option: option
})
addTransaction(transaction).then(result => {
console.info(result)
})
}
}
loadPoll(){
document.location.href = getPollsUrl(SORT_UNIQUE(), this.poll.pollId)
}
collapse(){
storeLocal(`collapsePoll/${this.poll.pollId}`, true)
this.parentState.build()
}
expand(){
storeLocal(`collapsePoll/${this.poll.pollId}`, false)
this.parentState.build()
}
build(){
let collapsed = getLocal(`collapsePoll/${this.poll.pollId}`)
this.x().a(
div().fl().aic().jc("space-between").a(
div().c("#007").fwb().w(600).fs(22).mar(2).pad(2).bc("#ffe").html(this.poll.poll).curp().ae("click", _ => this.loadPoll()),
ControlButton(_ => this.addOption()).html("Add option").bc("#afa").marr(10).op(this.isMine() ? 1 : 0.5),
collapsed ?
ControlButton(_ => this.expand()).html("Expand").bc("#afa")
:
ControlButton(_ => this.collapse()).html("Collapse").bc("#dd8")
,
ControlButton(_ => this.delete()).html("Delete").bc("#faa").marr(10).op(this.isMine() ? 1 : 0.5)
),
collapsed ? div() : div().marl(10).pad(2).bc(this.isMine() ? "#9e9" : "#eee").html(`by <b style="color:#070">${this.poll.author.username}</b> <small><i><a href="https://lichess.org/@/${this.poll.author.username}" rel="noopener noreferrer" target="_blank">view profile</a> </i>created at ${new Date(this.poll.createdAt).toLocaleString()} ${this.poll.pollId} </small>`),
this.optionsDiv = collapsed ? div() : div().pad(2).marl(10).bc("#de9").a(
this.poll.options.sort((a,b) => SORT_UNIQUE() ? b.getNumVoters() - a.getNumVoters() : b.getNumVotes() - a.getNumVotes())
.map(option => SmartOption({option: option, parentPoll: this}))
)
)
this.optionsDiv.e.classList.add("unselectable")
return this
}
}
function SmartPoll(props){return new SmartPoll_(props)}
class SmartState_ extends SmartdomElement_{
constructor(props){
super({...props, ...{tagName: "div"}})
this.state = this.props.state || State()
this.pad(2).mar(2).bc("#aaf").bdrs("solid").bdrw(1).bdrc("#777").bdrr(10)
this.build()
}
build(){
this.x().a(
this.state.polls.filter(poll => loadPollMatch ? poll.pollId == loadPollMatch[1] : true).sort(this.cmpPolls.bind(this))
.map(poll => SmartPoll({parentState: this, poll: poll}))
)
return this
}
cmpPolls(a, b){
let me = getUser()
if( a.getNumMe(me) != b.getNumMe(me) ) return b.getNumMe(me) - a.getNumMe(me)
if( a.getNumMe(me) ) return b.createdAt - a.createdAt
let [aColl, bColl] = [a, b].map(poll => getLocal(`collapsePoll/${poll.pollId}`) ? 1 : 0)
console.log(aColl, bColl)
if(aColl != bColl) return aColl - bColl
return SORT_UNIQUE() ? b.getNumVoters() - a.getNumVoters() : b.getNumVotes() - a.getNumVotes()
}
}
function SmartState(props){return new SmartState_(props)}
class SmartOption_ extends SmartdomElement_{
constructor(props){
super({...props, ...{tagName: "div"}})
this.option = this.props.option || PollOption()
this.parentPoll = this.props.parentPoll
this.build()
this.fl().aic().jc("space-between").pad(2).mar(2).bc("#aff")
this.showVotesOpen = {}
}
isMine(){
return this.parentPoll.isMine()
}
vote(quantity){
let transaction = AddVote({
vote: Vote({
author: getUser(),
targetPollId: this.option.parentPollId,
targetOptionId: this.option.optionId,
quantity: quantity
})
})
addTransaction(transaction).then(result => {
console.info(result)
})
}
delete(){
if(!this.parentPoll.poll.author.equalTo(getUser())){
window.alert("You can only delete an option from your own poll.")
return
}
let transaction = DeleteOption({
parentPollId: this.option.parentPollId,
optionId: this.option.optionId
})
addTransaction(transaction).then(result => {
console.info(result)
})
}
edit(){
if(!this.parentPoll.poll.author.equalTo(getUser())){
window.alert("You can only edit an option from your own poll.")
return
}
let editedOption = window.prompt("Edit option:", this.option.option)
if(!editedOption){
return
}
let transaction = DeleteOption({
parentPollId: this.option.parentPollId,
optionId: this.option.optionId
})
addTransaction(transaction).then(result => {
console.info(result)
})
let option = PollOption({
author: getUser(),
option: editedOption,
parentPollId: this.option.parentPollId
})
transaction = AddOption({
option: option
})
addTransaction(transaction).then(result => {
console.info(result)
})
}
showVotes(kind, showCount){
const entries = Object.entries(this.option.getVoters())
if(!entries.length) return
let isOpen = this.showVotesOpen[kind]
for(let kind of ["total", "unique"]){
this.showVotesDivs[kind].x().disp("none")
this.showVotesOpen[kind] = false
}
if(isOpen){
this.showVotesOpen[kind] = false
return
}
this.showVotesOpen[kind] = true
this.showVotesDivs[kind].fl().a(entries
.filter(entry => entry[1].numVotes).sort((a,b) => b[1].numVotes - a[1].numVotes).map(entry =>
UserWithVote({username: entry[0], votes: entry[1].numVotes, showCount: showCount})
)
)
this.showVotesOpen[kind] = true
}
createShowVotesDiv(){
return div().disp("none").poa().pad(5).bc("#e8f")
.w(600).jcsa().addStyle("flexWrap", "wrap")
.t(30).l(-300).addStyle("zIndex", 100)
}
build(){
this.showVotesDivs = {
"total": this.createShowVotesDiv(),
"unique": this.createShowVotesDiv(),
}
let voteBlocks = ["total", "unique"].map(kind =>
[
div().html(`${kind}`),
div().por().w(50).tac().pad(2).mar(2).bc(kind == "total" ? "#ff0" : "#ccf").fs(18).c("#070").fwb()
.html(`${kind == "total" ? this.option.getNumVotes() : this.option.getNumVoters()}`).curp()
.ae("click", this.showVotes.bind(this, kind, kind == "total")).a(
this.showVotesDivs[kind]
)
]
)
this.x().a(
div().w(550).pad(2).mar(2).bc("#edf").fs(18).fwb().html(this.option.option),
ControlButton(_=>this.vote(1)).html(`<div style="display: flex; align-items: center;">Vote <img style="margin-left: 5px;" width="18px" src="/stuff/tick.png"></img></div>`).bc("#afa")
)
if(SORT_UNIQUE()) voteBlocks.reverse()
for(let block of voteBlocks){
this.a(block)
}
this.a(
ControlButton(_=>this.vote(-1)).html("Unvote").bc("#dd7"),
ControlButton(_=>this.edit()).html("Edit").bc("#aad").op(this.isMine() ? 1 : 0.5),
ControlButton(_=>this.delete()).html("Delete").bc("#faa").op(this.isMine() ? 1 : 0.5)
)
}
}
function SmartOption(props){return new SmartOption_(props)}
class App_ extends SmartdomElement_{
constructor(props){
super({...props, ...{tagName: "div"}})
this.state = this.props.state || State()
this.setState(this.state)
}
setState(state){
this.state = state
this.build()
}
createPoll(){
let poll = window.prompt("Poll question :")
if(poll){
let transaction = CreatePoll({
poll: poll,
author: getUser()
})
addTransaction(transaction).then(result => {
console.info(result)
})
}
}
changeSort(){
document.location.href = getPollsUrl(!SORT_UNIQUE())
}
build(){
this.controlPanel = div().mar(6).pad(6).bc("#8b8").por().h(35).bdrs("solid").bdrr(10).bdrc("#773")
this.x().bc("#ffa").a(
div().h(165),
div().por().a(
div().poa().t(-185).l(-20).addStyle("zIndex", "100").pad(3).fs(20).html(`<img src="stuff/listrawpoll.png"></img>`),
this.controlPanel.a(
div().poa().addStyle("zIndex", "200").a(
ControlButton(_=>this.createPoll()).bc("#afa").fs(22).pad(3)
.addStyle("paddingRight", "10px").addStyle("paddingLeft", "10px").html("Create poll"),
ControlButton(_=>this.changeSort()).bc(SORT_UNIQUE() ? "#ffa" : "#aaf" ).fs(14)
.html(SORT_UNIQUE() ? "Sort by total" : "Sort by unique").marl(10)
)
),
SmartState({state: this.state}).mart(5),
)
)
return this
}
}
function App(props){return new App_(props)}
let app
function isDocVerified(doc){
if(!doc.verifiedUser) return false
if(doc.verifiedUser.id == "@nonymous") return false
return true
}
function htmlLink(href, display){
return `<a href="${href}" rel="noopener noreferrer" target="_blank">${display}</a>`
}
function pollLink(pollId){
return htmlLink(getPollsUrl(false, pollId), pollId)
}
function docInfo(doc){
if(doc.topic == "addVote"){
return pollLink(doc.vote.targetPollId) + " / " + doc.vote.targetOptionId
}
if(doc.topic == "addOption"){
return pollLink(doc.option.parentPollId)
}
if(doc.topic == "deleteOption"){
return pollLink(doc.parentPollId)
}
if(doc.topic == "createPoll"){
return pollLink(doc.pollId)
}
if(doc.topic == "deletePoll"){
return doc.pollId
}
if(doc.topic == "oauthLogin"){
return ""
}
}
if(document.location.href.match(/latest=true/)){
api("getLatest", {limit: 200}).then(result => {
app = div().a(result.filter(doc => isDocVerified(doc)).map(doc => div().fl().aic().pad(2).mar(2).bc("#eee").a(
div().w(150).pad(2).mar(2).bc("#ffe").html(doc.topic),
div().w(150).pad(2).mar(2).bc("#eff").html(doc.verifiedUser.username),
div().w(200).pad(2).mar(2).bc("#fef").html(new Date(doc.createdAt).toLocaleString()).ffms(),
div().pad(2).mar(2).bc("#eee").html(docInfo(doc)),
)))
document.getElementById("root").appendChild(app.e)
})
}else{
app = App({state: State(STATE)})
document.getElementById("root").appendChild(app.e)
}
setupSource(blob => {
if(blob.kind != "tick") console.info(blob)
if(blob.topic == "setState"){
app.setState(State(blob.state))
}
}, TICK_INTERVAL)