-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathView.elm
242 lines (217 loc) · 7.45 KB
/
View.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
module View exposing (view)
import Html
import Html.Attributes
import Html.Events
import Markdown
import Model exposing (..)
view : Model -> Html.Html Msg
view model =
Html.div
[ Html.Attributes.class "container" ]
[ Html.h1
[]
[ Html.text "Who's holding the torch?" ]
, Html.div
[]
(List.indexedMap (viewStep model) model.workflow)
, Html.h2
[]
[ Html.text "Add a new step" ]
, Html.form
[ Html.Attributes.class "well"
, Html.Events.onSubmit AddStep
]
[ Html.div
[]
(torchForm model)
, Html.input
[ Html.Attributes.class "btn btn-primary"
, Html.Attributes.type_ "submit"
, Html.Attributes.value "Add this new step"
]
[]
]
]
torchForm : Model -> List (Html.Html Msg)
torchForm model =
[ formGroup
"Title"
(input model.title "Frobnicate the bizbaz" NewTitle)
, formGroup
"Content"
(Html.textarea
[ Html.Attributes.class "form-control"
, Html.Attributes.placeholder "Lengthy description"
, Html.Attributes.value model.content
, Html.Events.onInput NewContent
]
[]
)
, formGroup
"Bearer"
(input model.bearer "person@in.charge.com" NewBearer)
]
formGroup : String -> Html.Html Msg -> Html.Html Msg
formGroup label input =
Html.div
[ Html.Attributes.class "form-group" ]
[ Html.label [] [ Html.text label ]
, input
]
input : String -> String -> (String -> Msg) -> Html.Html Msg
input value placeholder onInput =
Html.input
[ Html.Attributes.class "form-control"
, Html.Attributes.placeholder placeholder
, Html.Attributes.value value
, Html.Events.onInput onInput
]
[]
viewTorch : Bool -> Torch -> Html.Html Msg
viewTorch stepEnabled torch =
let
done =
isTorchDone torch
in
Html.div [ Html.Attributes.class <| panelClass done ]
[ Html.div
[ Html.Attributes.class "panel-heading" ]
[ Html.text torch.title ]
, Html.div
[ Html.Attributes.class "panel-body" ]
[ Markdown.toHtml [] torch.content ]
, Html.div
[ Html.Attributes.class "panel-footer" ]
[ Html.text <| "In charge: " ++ torch.bearer
, Html.div
[ Html.Attributes.class "pull-right" ]
[ Html.button
[ Html.Attributes.type_ "button"
, Html.Attributes.class "btn btn-primary btn-xs"
, Html.Attributes.disabled (done || not stepEnabled)
, Html.Events.onClick (UpdateStatus torch.id)
]
[ Html.text <|
if done then
"Already done"
else if stepEnabled then
"I'm Done"
else
"Complete previous steps first"
]
]
]
]
viewStep : Model -> Int -> Step -> Html.Html Msg
viewStep model index step =
Html.div
[ Html.Attributes.class <| panelClass (isStepDone step) ]
[ Html.div
[ Html.Attributes.class "panel-heading" ]
[ Html.text <| "Step: " ++ (toString index)
, Html.label
[ Html.Attributes.style [ ( "margin-left", "20px" ) ] ]
[ changeAllMandatoryForm index step ]
, Html.a
[ Html.Attributes.class "pull-right"
, Html.Attributes.href "#"
]
[ Html.i
[ Html.Attributes.class "glyphicon glyphicon-trash"
, Html.Events.onClick (DeleteStep index)
]
[]
]
]
, Html.div
[ Html.Attributes.class "panel-body" ]
[ let
conjonction =
if step.allMandatory then
"and"
else
"or"
in
Html.ul []
(step.torchList
|> List.map (viewTorch (isEnabled model.workflow index))
|> List.intersperse (Html.h4 [] [ Html.text conjonction ])
)
]
, Html.div
[ Html.Attributes.class "panel-footer" ]
[ let
addTorchBtn =
Html.button
[ Html.Attributes.class "btn btn-primary btn-xs"
, Html.Events.onClick (NewTorch index)
]
[ Html.text "Add a new torch" ]
cancelTorchBtn =
Html.a
[ Html.Attributes.class "btn btn-link btn-xs"
, Html.Attributes.href "#"
, Html.Events.onClick CancelNewTorch
]
[ Html.text "Cancel" ]
in
case model.newTorch of
Nothing ->
addTorchBtn
Just stepIndex ->
if stepIndex == index then
Html.form
[ Html.Attributes.class "well"
, Html.Events.onSubmit (AddTorch index)
]
[ Html.div
[]
(torchForm model)
, Html.input
[ Html.Attributes.class "btn btn-primary btn-xs"
, Html.Attributes.type_ "submit"
, Html.Attributes.value "Add this new torch"
]
[]
, cancelTorchBtn
]
else
addTorchBtn
]
]
changeAllMandatoryForm : Int -> Step -> Html.Html Msg
changeAllMandatoryForm index step =
Html.label
[]
[ Html.input
[ Html.Attributes.checked step.allMandatory
, Html.Attributes.name <| "step-type" ++ (toString index)
, Html.Attributes.style [ ( "margin-left", "10px" ) ]
, Html.Attributes.type_ "checkbox"
, Html.Events.onCheck (ChangeAllMandatory index)
]
[]
, Html.text " All mandatory"
]
panelClass : Bool -> String
panelClass isDone =
if isDone then
"panel panel-success"
else
"panel panel-default"
isTorchDone : Torch -> Bool
isTorchDone torch =
torch.status == Done
isStepDone : Step -> Bool
isStepDone { torchList, allMandatory } =
if allMandatory then
List.all isTorchDone torchList
else
List.any isTorchDone torchList
isEnabled : Workflow -> Int -> Bool
isEnabled workflow index =
let
previousSteps =
List.take index workflow
in
List.all isStepDone previousSteps