/
Main.elm
1248 lines (1087 loc) · 43.1 KB
/
Main.elm
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
port module Main exposing (Effect(..), Model, Msg(..), init, main, update, view)
import Accessibility.Styled as Html exposing (Attribute, Html)
import Accessibility.Styled.Style exposing (invisible)
import Browser
import Browser.Dom as Dom
import Browser.Navigation as Navigation
import Content exposing (Content)
import Css
import Css.Global
import Css.Reset
import Database exposing (Database)
import Database.ID as ID exposing (ID)
import Database.Sync as Sync exposing (Sync)
import Html.Styled as Inaccessible
import Html.Styled.Attributes as Attrs exposing (css)
import Html.Styled.Events as Events
import Html.Styled.Events.Extra exposing (onClickPreventDefaultForLinkWithHref)
import Json.Decode as Decode exposing (Decoder, Value)
import Maybe.Extra
import Node exposing (Node)
import Random
import Route exposing (Route)
import Selection exposing (Selection)
import Settings exposing (Settings)
import Task
import Time exposing (Posix)
import Url exposing (Url)
import Widgets.Button as Button
import Widgets.Colors as Colors
import Widgets.Icons as Icons
import Widgets.Text as Text
import Widgets.TimeDifference as TimeDifference
type alias Model key =
{ database : Database
, url : Url
, key : key
, route : Route
, currentTime : Posix
, settings : Settings
-- view state
, editing : Maybe Editing
, selection : Maybe Selection
, draftSync : Maybe Sync
}
type alias Editing =
{ id : ID
, input : Result ( String, List String ) Content
, selection : { start : Int, end : Int }
}
init : Value -> Url -> key -> ( Model key, Effect )
init flags url key =
let
{ seed, documents, now, settings } =
case Decode.decodeValue flagsDecoder flags of
Ok stuff ->
stuff
Err err ->
Debug.todo (Debug.toString err)
( model, routingEffects ) =
update
(UrlChanged url)
{ database = Database.load seed documents
, url = url
, key = key
, route = Route.Root
, currentTime = now
, settings = settings
-- view state
, editing = Nothing
, selection = Nothing
, draftSync = Nothing
}
in
( model
, Batch
[ routingEffects
, Batch (List.map SyncOnce settings.syncs)
]
)
flagsDecoder :
Decoder
{ now : Posix
, seed : Random.Seed
, documents : List Database.Document
, settings : Settings
}
flagsDecoder =
Decode.map3
(\millis documents settings ->
{ now = Time.millisToPosix millis
, seed = Random.initialSeed millis
, documents = documents
, settings = settings
}
)
(Decode.field "now" Decode.int)
(Decode.field "documents" (Decode.list (Decode.field "doc" Database.decoder)))
(Decode.field "settings"
(Decode.nullable Settings.decoder
|> Decode.map (Maybe.withDefault Settings.init)
)
)
type Msg
= ClickedLink Browser.UrlRequest
| UrlChanged Url
| GotCurrentTime Posix
| PouchDBPutSuccessfully Value
| TimerTriggeredSave
| TimerTriggeredSyncAll
| TimerTriggeredCompaction
| FocusedOnEditor
| UserClickedNewNote
| UserEditedNode String
| UserFinishedEditing
| UserHitEnterOnNode
| UserSelectedNoteInList ID
| UserWantsToEditNode ID
| UserWantsToIndentNode
| UserWantsToDedentNode
| UserHitBackspaceAtBeginningOfNode
| UserWantsToMoveNodeUp
| UserWantsToMoveNodeDown
| UserChangedSelection { start : Int, end : Int }
| UserWantsToCreateNewSync
-- workarounds for elm-program-test issues
| UserWantsToOpenNoteWithTitle Content
| UserWantsToNavigate Route
-- draft sync form
| UserTypedInDraftSyncDatabaseField String
| UserTypedInDraftSyncHostField String
| UserTypedInDraftSyncPasswordField String
| UserTypedInDraftSyncUsernameField String
| UserSubmittedSyncForm
| UserWantsToDeleteSync Sync
type Effect
= NoEffect
| Batch (List Effect)
| LoadUrl String
| PushUrl Route
| ReplaceUrl Route
| Put Value
| FocusOnEditor
| SyncOnce Sync
| Compact
type UpdatedRevision
= ForSettings
| ForRow ID
update : Msg -> Model key -> ( Model key, Effect )
update msg model =
case msg of
ClickedLink (Browser.Internal url) ->
( model, PushUrl (Route.parse url) )
ClickedLink (Browser.External url) ->
( model, LoadUrl url )
UrlChanged url ->
case Route.parse url of
Route.NodeByTitle content ->
let
( id, database ) =
findOrCreateNoteWithTitle model.currentTime content model.database
in
( { model | database = database }
, ReplaceUrl (Route.NodeById id)
)
route ->
( { model | route = route }
, NoEffect
)
UserWantsToNavigate route ->
( model
, PushUrl route
)
GotCurrentTime now ->
( { model | currentTime = now }
, NoEffect
)
PouchDBPutSuccessfully value ->
let
decoder =
Decode.map2 Tuple.pair
(Decode.field "id"
(Decode.oneOf
[ Decode.map ForRow ID.decoder
, Decode.map (\_ -> ForSettings) Settings.idDecoder
]
)
)
(Decode.field "rev" Decode.string)
in
case Decode.decodeValue decoder value of
Ok ( ForRow id, rev ) ->
( { model | database = Database.updateRevision id rev model.database }
, NoEffect
)
Ok ( ForSettings, rev ) ->
( { model | settings = Settings.updateRevision rev model.settings }
, NoEffect
)
Err err ->
Debug.todo (Debug.toString err)
TimerTriggeredSave ->
let
( rows, database ) =
Database.toPersist model.database
in
( { model | database = database }
, Batch (List.map (Put << Database.encode) rows)
)
TimerTriggeredSyncAll ->
( model
, Batch (List.map SyncOnce model.settings.syncs)
)
TimerTriggeredCompaction ->
( model, Compact )
FocusedOnEditor ->
( model, NoEffect )
UserClickedNewNote ->
let
( row, database ) =
Database.insert model.currentTime (Node.title Content.empty) model.database
in
( { model
| database = database
, editing =
Just
{ id = row.id
, input = Ok Content.empty
, selection = { start = 0, end = 0 }
}
}
, Batch
[ PushUrl (Route.NodeById row.id)
, FocusOnEditor
]
)
UserEditedNode input ->
case model.editing of
Nothing ->
( model
, NoEffect
)
Just editing ->
case Content.fromString input of
Ok content ->
( { model
| editing = Just { editing | input = Ok content }
, database = Database.update model.currentTime editing.id (Node.setContent content) model.database
}
, NoEffect
)
Err problems ->
( { model | editing = Just { editing | input = Err ( input, problems ) } }
, NoEffect
)
UserFinishedEditing ->
case Maybe.map .input model.editing of
Just (Ok _) ->
( { model | editing = Nothing }
, NoEffect
)
_ ->
( model
, NoEffect
)
UserHitEnterOnNode ->
case
Maybe.map2 Tuple.pair
model.editing
(Maybe.andThen (\editing -> Database.get editing.id model.database) model.editing)
of
Just ( editing, row ) ->
let
( leftContent, rightContent ) =
-- demeter chain?
Content.splitAt editing.selection.start (Node.content row.node)
( newNode, inserted ) =
model.database
|> Database.update model.currentTime row.id (Node.setContent leftContent)
|> Database.insert model.currentTime (Node.node rightContent)
database =
if Node.isTitle row.node then
Database.moveInto row.id newNode.id inserted
else
Database.moveAfter row.id newNode.id inserted
in
( { model
| editing =
Just
{ id = newNode.id
, input = Ok (Node.content newNode.node)
, selection = { start = 0, end = 0 }
}
, database = database
}
, FocusOnEditor
)
Nothing ->
( model
, NoEffect
)
UserSelectedNoteInList id ->
( model
, PushUrl (Route.NodeById id)
)
UserWantsToEditNode id ->
case Database.get id model.database of
Just row ->
( { model
| editing =
Just
{ id = id
, input = Ok (Node.content row.node)
, selection = { start = 0, end = 0 }
}
}
, FocusOnEditor
)
Nothing ->
( model
, NoEffect
)
UserWantsToIndentNode ->
let
current =
Maybe.map .id model.editing
newParent =
current
|> Maybe.andThen (\id -> Database.previousSibling id model.database)
|> Maybe.andThen (\id -> Database.get id model.database)
in
case Maybe.map2 Tuple.pair current newParent of
Just ( id, parent ) ->
( { model
| database =
case List.reverse parent.children of
lastChild :: _ ->
Database.moveAfter lastChild id model.database
[] ->
Database.moveInto parent.id id model.database
}
, FocusOnEditor
)
Nothing ->
( model
, NoEffect
)
UserWantsToDedentNode ->
let
current =
Maybe.map .id model.editing
newParent =
current
|> Maybe.andThen (\id -> Database.get id model.database)
|> Maybe.andThen .parent
|> Maybe.andThen (\id -> Database.get id model.database)
|> Maybe.andThen
(\parent ->
if Node.isTitle parent.node then
Nothing
else
Just parent
)
in
case Maybe.map2 Tuple.pair current newParent of
Just ( id, parent ) ->
( { model | database = Database.moveAfter parent.id id model.database }
, FocusOnEditor
)
Nothing ->
( model, NoEffect )
UserHitBackspaceAtBeginningOfNode ->
let
maybeRow =
Maybe.andThen (\{ id } -> Database.get id model.database) model.editing
maybeTarget =
Maybe.Extra.orListLazy
[ \_ -> Maybe.andThen (\{ id } -> Database.previousSibling id model.database) maybeRow
, \_ -> Maybe.andThen .parent maybeRow
]
|> Maybe.andThen (\id -> Database.get id model.database)
in
case Maybe.map2 Tuple.pair maybeRow maybeTarget of
Just ( row, target ) ->
let
updatedContent =
Content.append (Node.content target.node) (Node.content row.node)
in
( { model
| database =
model.database
|> Database.update model.currentTime target.id (Node.setContent updatedContent)
|> Database.delete row.id
, editing =
Just
{ id = target.id
, input = Ok updatedContent
, selection = { start = 0, end = 0 }
}
}
, FocusOnEditor
)
Nothing ->
( model, NoEffect )
UserWantsToMoveNodeUp ->
let
maybeTarget =
Maybe.Extra.orListLazy
[ \_ ->
model.editing
|> Maybe.andThen (\{ id } -> Database.previousSibling id model.database)
, \_ ->
model.editing
|> Maybe.andThen (\{ id } -> Database.get id model.database)
|> Maybe.andThen .parent
]
in
case Maybe.map2 Tuple.pair model.editing maybeTarget of
Just ( { id }, target ) ->
( { model | database = Database.moveBefore target id model.database }
, FocusOnEditor
)
Nothing ->
( model, NoEffect )
UserWantsToMoveNodeDown ->
let
maybeTarget =
Maybe.Extra.orListLazy
[ \_ ->
model.editing
|> Maybe.andThen (\{ id } -> Database.nextSibling id model.database)
, \_ ->
model.editing
|> Maybe.andThen (\{ id } -> Database.get id model.database)
|> Maybe.andThen .parent
|> Maybe.andThen (\id -> Database.nextSibling id model.database)
]
in
case Maybe.map2 Tuple.pair model.editing maybeTarget of
Just ( { id }, target ) ->
( { model | database = Database.moveAfter target id model.database }
, FocusOnEditor
)
Nothing ->
( model, NoEffect )
UserChangedSelection selection ->
( { model
| editing =
Maybe.map
(\editing -> { editing | selection = selection })
model.editing
}
, NoEffect
)
UserWantsToOpenNoteWithTitle content ->
let
( id, database ) =
findOrCreateNoteWithTitle model.currentTime content model.database
in
( { model | database = database }
, PushUrl (Route.NodeById id)
)
UserWantsToCreateNewSync ->
( { model | draftSync = Just (Sync "" "" "" "") }
, NoEffect
)
UserTypedInDraftSyncDatabaseField database ->
( { model | draftSync = Maybe.map (\draftSync -> { draftSync | database = database }) model.draftSync }
, NoEffect
)
UserTypedInDraftSyncHostField host ->
( { model | draftSync = Maybe.map (\draftSync -> { draftSync | host = host }) model.draftSync }
, NoEffect
)
UserTypedInDraftSyncPasswordField password ->
( { model | draftSync = Maybe.map (\draftSync -> { draftSync | password = password }) model.draftSync }
, NoEffect
)
UserTypedInDraftSyncUsernameField username ->
( { model | draftSync = Maybe.map (\draftSync -> { draftSync | username = username }) model.draftSync }
, NoEffect
)
UserSubmittedSyncForm ->
case model.draftSync of
Just draftSync ->
if Sync.isValid draftSync then
let
newSettings =
Settings.insertSync draftSync model.settings
in
( { model | draftSync = Nothing, settings = newSettings }
, Batch
[ Put (Settings.encode newSettings)
, SyncOnce draftSync
]
)
else
( model, NoEffect )
Nothing ->
( model, NoEffect )
UserWantsToDeleteSync sync ->
let
newSettings =
Settings.removeSync sync model.settings
in
( { model | settings = newSettings }
, Put (Settings.encode newSettings)
)
{-| TODO: should this go in Database?
-}
findOrCreateNoteWithTitle : Posix -> Content -> Database -> ( ID, Database )
findOrCreateNoteWithTitle now content database =
case Database.filter (\node -> Node.content node == content) database of
title :: _ ->
( title.id
, database
)
[] ->
Database.insert now (Node.title content) database
|> Tuple.mapFirst .id
perform : Model Navigation.Key -> Effect -> Cmd Msg
perform model effect =
case effect of
NoEffect ->
Cmd.none
Batch effects ->
Cmd.batch (List.map (perform model) effects)
LoadUrl url ->
Navigation.load url
PushUrl route ->
if model.route == route then
Cmd.none
else
Navigation.pushUrl model.key (Route.toString route)
ReplaceUrl route ->
if model.route == route then
Cmd.none
else
Navigation.replaceUrl model.key (Route.toString route)
Put value ->
put value
FocusOnEditor ->
Task.attempt
(\_ -> FocusedOnEditor)
(Dom.focus "editor")
SyncOnce sync ->
case Sync.toUrl sync of
Just url ->
syncOnce url
Nothing ->
Cmd.none
Compact ->
compact ()
port put : Value -> Cmd msg
port syncOnce : String -> Cmd msg
port compact : () -> Cmd msg
port putSuccessfully : (Value -> msg) -> Sub msg
subscriptions : Model key -> Sub Msg
subscriptions model =
Sub.batch
[ putSuccessfully PouchDBPutSuccessfully
, Time.every 1000 (\_ -> TimerTriggeredSave)
, Time.every 60000 (\_ -> TimerTriggeredSyncAll)
, Time.every 120000 (\_ -> TimerTriggeredCompaction)
-- Using a 1-second resolution is more than enough for most UI
-- concerns. If it needs to be more precise, crank this down! But
-- remember aware that it'll be a lot of updates and not precise ticks.
, Time.every 1000 GotCurrentTime
]
view : Model key -> Browser.Document Msg
view model =
{ title = "Notes"
, body =
List.map Html.toUnstyled
[ Css.Reset.meyerV2
, Css.Reset.borderBoxV201408
, Css.Global.global
[ Css.Global.body
[ Css.backgroundColor (Colors.toCss Colors.whiteLightest)
, Css.color (Colors.toCss Colors.blackDark)
]
]
, viewApplication model
]
}
viewApplication : Model key -> Html Msg
viewApplication model =
Html.main_
[ css
[ Css.property "display" "grid"
, Css.property "grid-template-columns" "350px 1fr"
, Css.property "grid-template-rows" "auto 1fr auto"
, Css.property "grid-template-areas" "\"header .\" \"list note\" \"sync note\""
, Css.property "grid-column-gap" "10px"
, Css.height (Css.vh 100)
, Css.width (Css.pct 100)
]
]
[ viewHeader [ css [ Css.property "grid-area" "header" ] ]
, viewNav [ css [ Css.property "grid-area" "list" ] ]
(case model.route of
Route.NodeById id ->
Just id
_ ->
Nothing
)
model.currentTime
(Database.filter Node.isTitle model.database)
, viewSync [ css [ Css.property "grid-area" "sync" ] ]
, Html.div [ css [ Css.property "grid-area" "note" ] ]
[ case model.route of
Route.NotFound ->
Html.text "Not found!"
Route.Root ->
Html.text "Select or create a note!"
Route.NodeById id ->
viewRow id model
Route.NodeByTitle _ ->
Html.text "You shouldn't ever see this page!"
Route.SyncSettings ->
viewSyncSettingsPage model
]
]
viewHeader : List (Attribute Never) -> Html Msg
viewHeader attrs =
Html.header
(Attrs.css
[ Css.borderBottom3 (Css.px 1) Css.solid (Colors.toCss Colors.greyLight)
, Css.borderRight3 (Css.px 1) Css.solid (Colors.toCss Colors.greyLight)
]
:: attrs
)
[ Button.button (Button.OnClick UserClickedNewNote)
[ Button.transparent
, Button.css
[ Css.padding (Css.px 12)
, Css.cursor Css.pointer
]
]
[ Icons.chick
{ height = 30
, shell = Colors.greyDark
, chick = Colors.yellowDark
}
, Html.span invisible [ Html.text "New Note" ]
]
]
viewNav : List (Attribute Never) -> Maybe ID -> Posix -> List Database.Row -> Html Msg
viewNav attrs activeId now rows =
Html.nav
(Attrs.css
[ Css.height (Css.pct 100)
, Css.borderRight3 (Css.px 1) Css.solid (Colors.toCss Colors.greyLight)
, Css.overflowY Css.scroll
, Css.overflowX Css.hidden
]
:: attrs
)
[ rows
|> List.sortBy (\row -> -(Time.posixToMillis row.updated))
|> List.map (\row -> Html.li [] [ viewNavLink activeId now row ])
|> Html.ul []
]
viewSync : List (Attribute Never) -> Html Msg
viewSync attrs =
Html.div attrs
[ Inaccessible.a
[ Attrs.href (Route.toString Route.SyncSettings)
, onClickPreventDefaultForLinkWithHref (UserWantsToNavigate Route.SyncSettings)
]
[ Html.text "Sync" ]
]
viewSyncSettingsPage : Model key -> Html Msg
viewSyncSettingsPage model =
-- TODO: pull this out to a widget and remove the duplication
Html.section
[ Attrs.css
[ Css.maxWidth (Css.em 50)
, Css.padding2 Css.zero (Css.px 27)
, Css.margin2 Css.zero Css.auto
]
]
[ Html.h1 [ Attrs.css [ Text.h1 ] ] [ Html.text "Sync Settings" ]
, case model.draftSync of
Just draftSync ->
-- TODO: extract this all to a widget
let
field onInput label value =
Html.labelBefore
[ Attrs.css
[ Text.text
, Css.display Css.tableRow
]
]
(Html.span [ Attrs.css [ Css.display Css.tableCell ] ] [ Html.text label ])
(Html.inputText label
[ Attrs.value value
, Events.onInput onInput
, Attrs.css [ Css.display Css.tableCell ]
]
)
in
-- using onSubmit as a hook here since hitting enter needs to work
Inaccessible.form
[ Events.onSubmit UserSubmittedSyncForm
, Attrs.css
[ Css.display Css.table
, Css.margin2 (Css.px 27) Css.zero
]
]
[ field UserTypedInDraftSyncHostField "Host" draftSync.host
, field UserTypedInDraftSyncDatabaseField "Database" draftSync.database
, field UserTypedInDraftSyncUsernameField "Username" draftSync.username
, field UserTypedInDraftSyncPasswordField "Password" draftSync.password
, Button.button
Button.Submit
[ Button.enabled (Sync.isValid draftSync) ]
[ Html.text "Start Syncing" ]
]
Nothing ->
Button.button (Button.OnClick UserWantsToCreateNewSync)
[ Button.css [ Css.margin2 (Css.px 27) Css.zero ] ]
[ Html.text "Sync with a new CouchDB Server" ]
, let
th =
Attrs.css
[ Text.text
, Css.fontWeight Css.bold
, Css.textAlign Css.left
]
cell =
Attrs.css
[ Text.text
, Css.padding (Css.px 5)
]
in
Html.table
[ Attrs.css [ Css.width (Css.pct 100) ] ]
[ Html.thead []
[ Html.tr []
[ Html.th [ th ] [ Html.text "Host" ]
, Html.th [ th ] [ Html.text "Database" ]
, Html.th [ th ] [ Html.text "Username" ]
, Html.th [ th ] [ Html.text "Password" ]
, Html.th [] []
]
]
, Html.tbody []
(List.map
(\sync ->
Html.tr []
[ Html.td [ cell ] [ Html.text sync.host ]
, Html.td [ cell ] [ Html.text sync.database ]
, Html.td [ cell ] [ Html.text sync.username ]
, Html.td [ cell ]
[ if sync.password /= "" then
Html.text "*****"
else
Html.text ""
]
, Html.td []
[ Button.button
(Button.OnClick (UserWantsToDeleteSync sync))
[ Button.delete ]
[ Html.text "Delete" ]
]
]
)
model.settings.syncs
)
]
]
viewNavLink : Maybe ID -> Posix -> Database.Row -> Html Msg
viewNavLink activeId now { id, node, updated } =
Button.button
(Button.OnClick (UserSelectedNoteInList id))
[ Button.transparent
, Button.css [ Css.width (Css.pct 100) ]
]
[ -- Safari doesn't let you make a button a grid container. Boo.
Html.div
[ Attrs.css
[ Css.padding (Css.px 12)
, Css.width (Css.pct 100)
-- it's always text!
, Text.text
-- content layout
, Css.property "display" "grid"
, Css.property "grid-template-columns" "15% 1fr"
, Css.property "grid-template-rows" "auto"
, Css.property "grid-template-areas" "\"date title\""
, Css.property "grid-column-gap" "10px"
-- surround by borders
, Css.borderBottom3 (Css.px 1) Css.solid (Colors.toCss Colors.greyLight)
, Css.borderLeft3 (Css.px 8) Css.solid Css.transparent
, Css.property "transition" "all 0.25s"
-- you can click on this!
, Css.cursor Css.pointer
-- highlight the active node
, -- TODO: make an isActive helper that checks if a child is active too
if activeId == Just id then
Css.batch
[ Css.borderLeftColor (Colors.toCss Colors.greenLight)
, Css.backgroundColor (Colors.toCss Colors.whiteLight)
]
else
Css.batch []
]
]
[ Html.div
[ css [ Css.property "grid-area" "date" ] ]
[ TimeDifference.compact updated now ]
, Content.toHtml
{ activate = Nothing
, navigate = UserWantsToOpenNoteWithTitle
, navigateUrl = Route.toString << Route.NodeByTitle
}
[ css
[ Css.property "grid-area" "title"
, Css.textAlign Css.left
, Css.height (Css.calc Text.textLineHeight Css.plus Text.textLineHeight)
, Css.property "display" "-webkit-box"
, Css.property "-webkit-line-clamp" "2"
, Css.property "-webkit-box-orient" "vertical"
, Css.overflow Css.hidden
]
]
(Node.content node)
]
]
viewRow : ID -> Model key -> Html Msg
viewRow id model =
case Database.get id model.database of
Nothing ->
Html.text "Node not found!"
Just row ->
let
tag =
if Node.isTitle row.node then
-- TODO: pull this out to a widget and remove the duplication
Html.section
[ Attrs.css
[ Css.maxWidth (Css.em 50)
, Css.padding2 Css.zero (Css.px 27)
, Css.margin2 Css.zero Css.auto
]
]
else
-- TODO: extract this list item to a widget or something
Html.li
[ Attrs.css
[ Css.pseudoElement "marker" [ Css.color (Colors.toCss Colors.greenDark) ]
, Css.marginBottom (Css.px 5.333)
, Css.position Css.relative
]
]
in
tag
[ case model.editing of
Just editing ->
if id == editing.id then
Html.form []
[ Html.textarea
[ Attrs.css
[ if Node.isTitle row.node then
Text.h1
else
Text.text