-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.elm
151 lines (115 loc) · 3.2 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
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
-- MODEL
type alias Model =
{ showPicker : Bool
, view : String
, month : String
, year : Int
}
init : ( Model, Cmd msg )
init =
(Model False "none" "" 2017) ! []
-- UPDATE
type Msg
= UpdateMonth String
| TogglePicker
| ChangeView String
update : Msg -> Model -> ( Model, Cmd msg )
update msg model =
case msg of
UpdateMonth month ->
({ model | month = month, showPicker = not model.showPicker }) ! []
TogglePicker ->
({ model | showPicker = not model.showPicker }) ! []
ChangeView view ->
({ model | view = view }) ! []
-- VIEW
abbreviatedMonths : List ( String, String )
abbreviatedMonths =
[ ( "01", "Jan." )
, ( "02", "Feb." )
, ( "03", "Mar." )
, ( "04", "Apr." )
, ( "05", "May" )
, ( "06", "Jun." )
, ( "07", "Jul." )
, ( "08", "Aug." )
, ( "09", "Sep." )
, ( "10", "Oct." )
, ( "11", "Nov." )
, ( "12", "Dec." )
]
view : Model -> Html Msg
view model =
let
result =
if String.isEmpty model.month then
""
else
model.month ++ "/" ++ (toString model.year)
resultInput =
input
[ type_ "text"
, value result
, onFocus TogglePicker
]
[]
tableView =
if model.view == "year" then
yearPickerTable model
else
monthPickerTable model
in
if model.showPicker then
div [ class "month-picker-overlay" ]
[ resultInput
, monthPickerHeaderView model
, tableView
]
else
div [ class "month-picker-overlay" ]
[ resultInput ]
yearPickerTable : Model -> Html Msg
yearPickerTable model =
div [] []
monthPickerTable : Model -> Html Msg
monthPickerTable model =
div [ class "month-picker-month-table" ]
(List.map
(\m ->
button
[ class "btn btn-default"
, onClick <|
UpdateMonth <|
Tuple.first m
]
[ text (Tuple.second m) ]
)
abbreviatedMonths
)
monthPickerHeaderView : Model -> Html msg
monthPickerHeaderView model =
div [ class "month-picker-header" ]
[ div [ class "month-picker-year" ]
[ div []
[ a [ class "btn btn-default month-picker-previous" ]
[ span [ class "fa fa-caret-left" ] [] ]
, a [ class "btn btn-default month-picker-title" ]
[ text ("Year " ++ (toString model.year)) ]
, a [ class "month-picker-next" ]
[ span [ class "fa fa-caret-right" ] [] ]
]
]
]
-- MAIN
main : Program Never Model Msg
main =
program
{ init = init
, view = view
, update = update
, subscriptions = always Sub.none
}