-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp_live.html.heex
649 lines (635 loc) · 24.8 KB
/
app_live.html.heex
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
<main class="font-sans container mx-auto">
<.form
:let={_f}
for={%{}}
as={:item}
phx-submit="create"
class="w-full lg:w-1/2 m-auto"
>
<textarea
class="w-full py-1 px-1 text-slate-800 text-3xl
bg-white bg-clip-padding
resize-none
max-h-100
transition ease-in-out
border border-b border-slate-200
focus:border-none focus:outline-none
my-2"
name="text"
id="textval"
phx-change="validate"
phx-debounce="2000"
placeholder="Capture what is on your mind ..."
required="required"
x-data="{resize() {
$el.style.height = '100px';
$el.style.height = $el.scrollHeight + 'px';
}
}"
x-on:focus=""
x-init="$el.selectionStart= $el.value.length;
$el.selectionEnd= $el.value.length;
$el.focus();
resize();"
x-on:input="resize"
><%= @text_value %></textarea>
<!-- Display the selected tags -->
<div class="">
<%= for selected_tag <- @selected_tags do %>
<span
class="text-white font-bold py-1 px-2 rounded-full ml-2"
style={"background-color:#{selected_tag.color}"}
>
<%= selected_tag.text %>
</span>
<% end %>
</div>
<!-- Select Input -->
<div
class="lg:w-1/2 "
x-data="{open: false}"
x-on:click.away="open = false"
x-on:keydown.escape="open = false"
>
<input
type="text"
name="tag"
class="w-full my-2"
x-on:focus="open = true"
x-on:keydown.enter.prevent="
if (event.key === 'Enter') {
event.target.value = '';
}
"
phx-keyup="filter-tags"
phx-keyup-debounce="250"
phx-keydown="add-first-tag"
autocomplete="off"
placeholder="tags"
/>
<div
class="relative z-10 drop-shadow-lg w-auto"
x-show="open"
x-transition
x-cloak
>
<ul class="absolute border bg-white max-h-64 overflow-auto w-full">
<%= for tag <- @tags do %>
<li
class="border-b p-2 cursor-pointer hover:bg-slate-200"
phx-click="toggle_tag"
phx-value-tag_id={tag.id}
>
<div class="relative h-10">
<div class="inline-flex items-center w-3/4">
<span
class="w-5 h-5 bg-red-500 rounded-full mx-2 shrink-0"
style={"background-color:#{tag.color}"}
>
</span>
<span class="overflow-hidden text-ellipsis whitespace-nowrap">
<%= tag.text %>
</span>
</div>
<%= if Enum.member?(@selected_tags, tag) do %>
<svg
class="absolute font-bold w-4 top-0 bottom-0 m-auto right-0 text-green-500"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
>
<path
fill-rule="evenodd"
d="M19.916 4.626a.75.75 0 01.208 1.04l-9 13.5a.75.75 0 01-1.154.114l-6-6a.75.75 0 011.06-1.06l5.353 5.353 8.493-12.739a.75.75 0 011.04-.208z"
clip-rule="evenodd"
/>
</svg>
<% end %>
</div>
</li>
<% end %>
<li class="border-b p-2 cursor-pointer hover:bg-slate-200">
<%= link("edit tags",
to: "/tags",
class: "block w-full text-center"
) %>
</li>
</ul>
</div>
</div>
<!-- End Select Input -->
<!-- Want to help "DRY" this? see: https://github.com/dwyl/app-mvp-phoenix/issues/105 -->
<!-- https://tailwindcss.com/docs/justify-content#end -->
<div class="flex justify-end mr-1">
<button class="inline-flex items-center px-2 py-1 mt-1 h-9
bg-green-700 hover:bg-green-800 text-white rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-5 w-5 mr-2"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M15 13l-3 3m0 0l-3-3m3 3V8m0 13a9 9 0 110-18 9 9 0 010 18z"
/>
</svg>
Save
</button>
</div>
</.form>
<!-- List of items with inline buttons and controls -->
<ul id="items" phx-hook="Items" x-data="{selected_item: null}" class="w-full">
<%= for item <- filter_items(@items, @filter, @filter_tag) do %>
<li
id={"item-#{item.id}"}
data-id={item.id}
class={"mt-2 flex flex-col w-full border-t border-slate-200 py-2 item #{if item.id == @editing do 'cursor-default' else 'cursor-grab' end}"}
draggable={"#{if item.id == @editing do 'false' else 'true' end}"}
x-data="{selected: false}"
x-on:dragstart="selected = true; $dispatch('highlight', {id: $el.id}); selected_item = $el"
x-on:dragend="selected = false; $dispatch('remove-highlight', {id: $el.id}); selected_item = null; $dispatch('update_indexes', {fromItemId: $el.dataset.id})"
x-bind:class="selected ?? 'cursor-grabbing'"
x-on:dragover.throttle="$dispatch('dragover_item', {selected_item_id: selected_item.id, current_item: $el})"
data-highlight={JS.add_class("bg-teal-300")}
data-remove-highlight={JS.remove_class("bg-teal-300")}
>
<div class="flex flex-row">
<svg
class="max-w-[15px] dragDrop-icon"
data-darkreader-inline-stroke=""
fill="none"
stroke="currentColor"
stroke-width="1.5"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
aria-hidden="true"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M12 6.75a.75.75 0 110-1.5.75.75 0 010 1.5zM12 12.75a.75.75 0 110-1.5.75.75 0 010 1.5zM12 18.75a.75.75 0 110-1.5.75.75 0 010 1.5z"
>
</path>
</svg>
<%= if archived?(item) do %>
<label>
<div class="hidden">
Toggle the checkbox to set an item's status to done.
</div>
<input
type="checkbox"
checked
phx-value-id={item.id}
phx-value-cid={item.cid}
phx-click="toggle"
class="flex-none p-4 m-2 form-checkbox text-slate-400 cursor-not-allowed"
/>
</label>
<label class="w-full text-slate-400 m-1 line-through">
<%= item.text %>
</label>
<div class="flex flex-col">
<div class="flex flex-col justify-end mr-1">
<button
disabled
class="cursor-not-allowed inline-flex items-center px-2 py-1 h-9
bg-gray-200 text-gray-800 rounded-md"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-5 w-5 mr-2"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M5 8h14M5 8a2 2 0 110-4h14a2 2 0 110 4M5 8v10a2 2 0 002 2h10a2 2 0 002-2V8m-9 4h4"
/>
</svg>
Archived
</button>
</div>
</div>
<% else %>
<!-- if item is "done" (status: 4) strike-through and show "Archive" button -->
<%= if done?(item) do %>
<label>
<div class="hidden">
Toggle the checkbox to set an item's status to done.
</div>
<input
type="checkbox"
phx-value-id={item.id}
phx-value-cid={item.cid}
phx-click="toggle"
class="flex-none p-4 m-2 form-checkbox text-slate-400"
checked
/>
</label>
<label class="w-full text-slate-400 m-2 line-through">
<%= item.text %>
</label>
<div class="flex flex-col">
<div class="flex flex-col justify-end mr-1">
<!-- "Archive" button with icon see: https://github.com/dwyl/app-mvp-phoenix/issues/101 -->
<button
class="inline-flex items-center px-2 py-1 mr-2 h-9
bg-gray-200 hover:bg-gray-300 text-gray-800 rounded-md"
phx-click="delete"
phx-value-id={item.id}
phx-value-cid={item.cid}
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-5 w-5 mr-2"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M5 8h14M5 8a2 2 0 110-4h14a2 2 0 110 4M5 8v10a2 2 0 002 2h10a2 2 0 002-2V8m-9 4h4"
/>
</svg>
Archive
</button>
<p>
<span class="text-sm font-mono font-semibold flex flex-col justify-end text-right mr-2 mt-1">
<%= timer_text(item) %>
</span>
</p>
</div>
</div>
<!-- else render the buttons for start|stop timer -->
<% else %>
<!-- Show checkbox so the item can be marked as "done" -->
<label>
<div class="hidden">
Toggle the checkbox to set an item's status to done.
</div>
<input
type="checkbox"
phx-value-id={item.id}
phx-value-cid={item.cid}
phx-click="toggle"
class="flex-none p-4 m-2 form-checkbox hover:text-slate-600"
/>
</label>
<!-- Editing renders the textarea and "save" button - near identical (duplicate) code from above
Help wanted DRY-ing this ... see: https://github.com/dwyl/app-mvp-phoenix/issues/105 -->
<%= if item.id == @editing do %>
<div class="flex flex-col grow">
<form
phx-submit="update-item"
id={"form-update-item-#{item.id}"}
class="w-full pr-2"
>
<textarea
id={"textarea-editing-of-item-#{item.id}"}
class="w-full flex-auto text-slate-800
bg-white bg-clip-padding
transition ease-in-out
border border-b border-slate-200
focus:border-none focus:outline-none"
name="text"
placeholder="What is on your mind?"
autofocus
required="required"
value={item.text}
><%= item.text %></textarea>
<!--<input
id={"tag-of-item-#{item.id}"}
type="text"
name="tags"
value={tags_to_string(item.tags)}
placeholder="tag1, tag2..."
/>-->
<input type="hidden" name="id" value={item.id} />
<div
class="flex justify-end mr-1"
id={"save-button-item-#{item.id}"}
>
<button class="inline-flex items-center px-2 py-1 mt-1 h-9
bg-green-700 hover:bg-green-800 text-white rounded-md">
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-5 w-5 mr-2"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M15 13l-3 3m0 0l-3-3m3 3V8m0 13a9 9 0 110-18 9 9 0 010 18z"
/>
</svg>
Save
</button>
</div>
</form>
<div>
<%= if (length @editing_timers) > 0 do %>
<h1 class="text-4xl font-bold">Timers</h1>
<% else %>
<h1 class="text-2xl text-center font-semibold text-slate-400">
No timers associated with this item.
</h1>
<% end %>
<div class="flex flex-col w-full mt-2">
<%= @editing_timers |> Enum.with_index |> Enum.map(fn({changeset, index}) -> %>
<.form
:let={f}
for={changeset}
phx-submit="update-item-timer"
id={"form-update-timer-#{changeset.data.id}"}
class="w-full pr-2"
>
<div class="flex flex-row w-full justify-between">
<div class="flex flex-row items-center">
<h3 class="mr-3">Start:</h3>
<input
type="text"
required="required"
name="timer_start"
id={"#{changeset.data.id}_start"}
value={
NaiveDateTime.add(
changeset.data.start,
@hours_offset_fromUTC,
:hour
)
}
/>
</div>
<div class="flex flex-row items-center">
<h3 class="mr-3">Stop:</h3>
<input
type="text"
name="timer_stop"
id={"#{changeset.data.id}_stop"}
value={
if is_nil(changeset.data.stop) do
changeset.data.stop
else
NaiveDateTime.add(
changeset.data.stop,
@hours_offset_fromUTC,
:hour
)
end
}
/>
</div>
<input
type="hidden"
name="timer_id"
value={changeset.data.id}
/>
<input type="hidden" name="index" value={index} />
<button
type="submit"
id={"button_timer-update-#{changeset.data.id}"}
class="text-white bg-blue-700
hover:bg-blue-800 focus:outline-none focus:ring-4 focus:ring-blue-300
font-medium rounded-full text-sm px-5 py-2.5 text-center
mr-2 mb-2
dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
>
Update
</button>
</div>
<span class="text-red-700">
<%= error_tag(f, :id) %>
</span>
</.form>
<% end) %>
</div>
</div>
</div>
<% else %>
<!-- Render item.text as click-able label -->
<label
class="w-full flex-auto text-slate-800 m-2"
phx-click="edit-item"
phx-value-id={item.id}
phx-value-cid={item.cid}
>
<%= item.text %>
</label>
<% end %>
<%= if timers_any?(item) do %>
<!-- always display the time elapsed in the UI https://github.com/dwyl/app-mvp-phoenix/issues/106 -->
<%= if timer_stopped?(item) do %>
<div class="flex flex-col">
<div class="flex flex-col justify-end mr-1">
<!-- render "continue" button -->
<button
phx-click="start"
phx-value-id={item.id}
phx-value-cid={item.cid}
class="inline-flex items-center px-2 py-2 h-9
bg-teal-600 hover:bg-teal-800 text-white rounded-md"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-5 w-5 mr-1"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
Resume
</button>
<p>
<span class="text-sm font-mono font-semibold flex flex-col justify-end text-right mr-2 mt-1">
<%= timer_text(item) %>
</span>
</p>
</div>
</div>
<% else %>
<%= if started?(item) do %>
<!-- render the counting timer with Apline.js! see: github.com/dwyl/learn-alpine.js -->
<div
class="flex flex-col"
x-data="{
start: $refs.timer_start ? $refs.timer_start.innerHTML : null,
current: null,
stop: null,
interval: null
}"
x-init="
start = $refs.timer_start.innerHTML;
current = start;
interval = setInterval(() => { current = Date.now(), start = $refs.timer_start ? $refs.timer_start.innerHTML : null }, 500)
"
>
<!-- this is how we pass the start|stop time from Elixir (server) to Alpine (client) -->
<span x-ref="timer_start" class="hidden">
<%= timestamp(item.start) %>
</span>
<div class="flex flex-col justify-end mr-1">
<button
phx-click="stop"
phx-value-id={item.id}
phx-value-cid={item.cid}
phx-value-timerid={item.timer_id}
class="inline-flex items-center px-2 py-2 h-9 mr-1
bg-red-500 hover:bg-red-700 text-white rounded-md"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-5 w-5 mr-1"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
Stop
</button>
<p>
<span
x-text="timer_text(start, current || stop)"
class="text-sm font-mono font-semibold text-right mr-1"
>
00:00:00
</span>
</p>
</div>
</div>
<% end %>
<!-- end if started?(item) -->
<% end %>
<% else %>
<!-- render start button -->
<button
phx-click="start"
phx-value-id={item.id}
phx-value-cid={item.cid}
class="inline-flex items-center px-2 py-2 h-9 mr-1
bg-teal-700 hover:bg-teal-800 text-white rounded-md"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-5 w-5 mr-1"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
Start
</button>
<% end %>
<!-- end timers_any?(item) -->
<% end %>
<% end %>
</div>
<%= if item.id != @editing do %>
<div class="ml-2">
<%= for tag <- item.tags do %>
<%= live_patch(tag.text,
to:
Routes.live_path(@socket, AppWeb.AppLive, %{
filter_by: @filter,
filter_by_tag: tag.text
}),
style: "background-color:#{tag.color}",
class: " text-white font-bold py-1 px-2 rounded-full"
) %>
<% end %>
</div>
<% end %>
</li>
<% end %>
<!-- end for item <- @items -->
</ul>
</main>
<footer class="mt-5 container mx-auto">
<%= if has_items?(@items) do %>
<div class="flex flex-row justify-center p-1">
<div class="px-2 py-2">
<%= live_patch("All",
to: Routes.live_path(@socket, AppWeb.AppLive, %{filter_by: "all"}),
class: class_footer_link("all", @filter)
) %>
</div>
<div class="px-2 py-2">
<%= live_patch("Active",
to: Routes.live_path(@socket, AppWeb.AppLive, %{filter_by: "active"}),
class: class_footer_link("active", @filter)
) %>
</div>
<div class="px-2 py-2">
<%= live_patch("Done",
to: Routes.live_path(@socket, AppWeb.AppLive, %{filter_by: "done"}),
class: class_footer_link("done", @filter)
) %>
</div>
<div class="px-2 py-2">
<%= live_patch("Archived",
to:
Routes.live_path(@socket, AppWeb.AppLive, %{filter_by: "archived"}),
class: class_footer_link("archived", @filter)
) %>
</div>
</div>
<% end %>
</footer>
<script>
function leftPad(val) {
return val < 10 ? '0' + String(val) : val;
}
function timer_text(start, current) {
current = parseInt(current, 10)
start = parseInt(start, 10)
let h="00", m="00", s="00";
const diff = current - start;
// seconds
if(diff > 1000) {
s = Math.floor(diff / 1000);
s = s > 60 ? s % 60 : s;
s = leftPad(s);
}
// minutes
if(diff > 60000) {
m = Math.floor(diff/60000);
m = m > 60 ? m % 60 : m;
m = leftPad(m)
}
// hours
if(diff > 3600000) {
h = Math.floor(diff/3600000);
h = leftPad(h)
}
return h + ':' + m + ':' + s;
}
</script>