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
|
module Dialog exposing
( Msg(..)
, Model
, Config
, init
, update
, view
)
import Platform.Cmd exposing (Cmd)
import Task exposing (Task)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
-- Model
type alias Model model msg =
{ config : Maybe (Config model msg)
, mapMsg : Msg model msg -> msg
}
type alias Config model msg =
{ title : String
, body : model -> Html msg
, confirm : String
, confirmMsg : model -> Result msg msg
, undo : String
}
init : (Msg model msg -> msg) -> Model model msg
init mapMsg =
{ config = Nothing
, mapMsg = mapMsg
}
-- Update
type Msg model msg =
NoOp
| ConfirmMsg (model -> Result msg msg)
| Open (Config model msg)
| Close
update : Msg model msg -> model -> Model model msg -> (Model model msg, Cmd msg)
update msg baseModel model =
case msg of
NoOp ->
( model
, Cmd.none
)
ConfirmMsg confirmMsg ->
case confirmMsg baseModel of
Ok msg ->
( { model | config = Nothing }
, Task.perform (always msg) (always msg) (Task.succeed NoOp)
)
Err msg ->
( model
, Task.perform (always msg) (always msg) (Task.succeed NoOp)
)
Open config ->
( { model | config = Just config }
, Cmd.none
)
Close ->
( { model | config = Nothing }
, Cmd.none
)
-- View
view : model -> Model model msg -> Html msg
view model { mapMsg, config } =
let isVisible =
case config of
Just _ -> True
Nothing -> False
in div
[ class "dialog" ]
[ curtain mapMsg isVisible
, case config of
Nothing ->
text ""
Just c ->
dialog model mapMsg c
]
curtain : (Msg model msg -> msg) -> Bool -> Html msg
curtain mapMsg isVisible =
div
[ class "curtain"
, style
[ ("position", "fixed")
, ("top", "0")
, ("left", "0")
, ("width", "100%")
, ("height", "100%")
, ("background-color", "rgba(0, 0, 0, 0.5)")
, ("z-index", if isVisible then "1000" else "-1")
, ("opacity", if isVisible then "1" else "0")
, ("transition", "all 0.2s ease")
]
, onClick (mapMsg Close)
]
[]
dialog : model -> (Msg model msg -> msg) -> Config model msg -> Html msg
dialog model mapMsg { title, body, confirm, confirmMsg, undo } =
div
[ class "content"
, style
[ ("position", "fixed")
, ("top", "25%")
, ("left", "50%")
, ("transform", "translate(-50%, -25%)")
, ("z-index", "1000")
, ("background-color", "white")
, ("padding", "20px")
, ("border-radius", "5px")
, ("box-shadow", "0px 0px 15px rgba(0, 0, 0, 0.5)")
]
]
[ h1 [] [ text title ]
, body model
, div
[ style
[ ("float", "right")
]
]
[ button
[ class "confirm"
, onClick (mapMsg <| ConfirmMsg confirmMsg)
, style
[ ("margin-right", "15px")
]
]
[ text confirm ]
, button
[ class "undo"
, onClick (mapMsg Close)
]
[ text undo ]
]
]
|