-
Notifications
You must be signed in to change notification settings - Fork 0
/
CategoryBar.elm
269 lines (240 loc) · 7.84 KB
/
CategoryBar.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
module CategoryBar where
-- Module for category bar.
-- Category bar will be horizontal allowed.
-- Else it will collapse into a vertical sidebar.
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.Animation as UI
import Html.Animation.Properties exposing (..)
import Html.Events exposing (..)
import Effects exposing (..)
-- Model
type Category =
Apartments
| Subleases
| Appliances
| Bikes
| Books
| Cars
| Electronics
| Employment
| Furniture
| Miscellaneous
| Services
| Wanted
| Free
| None
-- CategoryBar model contains 3 objects:
-- on is the category that has been clicked.
-- hover contains the category indicates a category that the mouse hovers over.
-- style is a style for the vertical category bar that slides out when the browser screen is small.
type alias Model =
{ on : Category
, hover : Category
, style : UI.Animation
}
init : Model
init =
{ on = None
, hover = None
, style = UI.init [ Left -100 Px
, Opacity 0.0
]
}
-- List of all categories that will be rendered in the view
allCategories : List Category
allCategories =
[Apartments
, Subleases
, Appliances
, Bikes
, Books
, Cars
, Electronics
, Employment
, Furniture
, Miscellaneous
, Services
, Wanted
, Free
]
-- Update
-- Union type of actions for the category bar:
-- ToggleCategory that the action that occurs when a category is clicked.
-- Depending on context, ToggleCategory either turns on or off a category.
-- MouseEneter/MouseLeave are actions for mouse entering or leaving a category
-- for the hover record in the model.
-- Reset will reset the categories to the inital state with no category triggered.
-- If the mouse is hovering over a cateogry when reset is triggered, the hover
-- highlight will not be turned off.
-- Show, Hide, Animate are Actions for the the slideout sidebar animation.
type Action =
ToggleCategory Category
| MouseEnter Category
| MouseLeave Category
| Reset
| Show
| Hide
| Animate UI.Action
update : Action -> Model -> (Model , Effects Action)
update action model =
case action of
ToggleCategory category ->
let
model' = if category == model.on then {model | on = None}
else {model | on = category }
in
(model', Effects.none)
MouseEnter category -> ({model | hover = category }, Effects.none)
MouseLeave category -> ({model | hover = None}, Effects.none)
Reset -> ({model | on = None }, Effects.none)
Show ->
UI.animate
|> UI.props
[ Left (UI.to 0) Px
, Opacity (UI.to 1)
]
|> onMenu model
Hide ->
UI.animate
|> UI.props
[ Left (UI.to -100) Px
, Opacity (UI.to 0)
]
|> onMenu model
Animate action ->
onMenu model action
-- Helper function for animation.
onMenu =
UI.forwardTo
Animate
.style
(\w style -> {w | style = style })
--view
-- Set of addresses to trigger actions from the view.
-- There are 3 types of UI effects:
-- categoryInput is the Signal when the mouse hovers over a category
-- cateogryEnter is the Signal for when a category is clicked.
-- This also changes the rendered listings.
-- Animation is the Signal for triggering the animation sidebar which is only
-- activated when the width of the browser viewport is small.
type alias Context =
{ categoryInput : Signal.Address Action
, categoryEnter : Signal.Address Category
, animation : Signal.Address Action
}
-- view generates the HTML for the category bar.
-- If viewport width is small, it will render a hidden vertical category bar
-- that slides out.
-- else a regular horizontal category bar is rendered.
view : Int -> Context -> Model -> Html
view col_limit context model =
let debug = Debug.log "Col Limit in CategoryBar View" col_limit in
if col_limit < 4 then verticalTrigger context model
else horizontalView context model
-- Set of views to render the horizontal category bar
horizontalView : Context -> Model -> Html
horizontalView context model =
div [ style border_bar_css ]
(List.map (horizontalCategory context model) allCategories)
horizontalCategory : Context -> Model -> Category -> Html
horizontalCategory context model category =
let
css = if (model.on == category) || (model.hover == category) then on_css else off_css
special_modifier_css = if category == Apartments then left_tab_css
else if category == Free then right_tab_css
else []
in
div [ style (css ++ horizontal_category_css ++ special_modifier_css)
, onClick context.categoryEnter category
, onMouseEnter context.categoryInput (MouseEnter category)
, onMouseLeave context.categoryInput (MouseLeave category) ]
[text <| toString category]
-- Set of views to render vertical category bar.
verticalTrigger : Context -> Model -> Html
verticalTrigger context model =
let
on_category = if model.on == None then "Category" else toString model.on
in
div [ onMouseEnter context.animation Show
, onMouseLeave context.animation Hide
, style trigger_css
, id "vertical-trigger"
]
[ h4 [style vertical_text_css ] [text on_category]
, verticalView context model]
verticalView : Context -> Model -> Html
verticalView context model =
div [ style (vertical_view_css ++ (UI.render model.style))
, id "vertical-view"]
(List.map (verticalCategory context model) allCategories)
verticalCategory : Context -> Model -> Category -> Html
verticalCategory context model category =
let
css = if (model.on == category) || (model.hover == category) then on_css else off_css
in
div [style (css ++ vertical_category_css)
, onClick context.categoryEnter category
, onMouseEnter context.categoryInput (MouseEnter category)
, onMouseLeave context.categoryInput (MouseLeave category) ]
[text <| toString category]
-- CSS
(=>) = (,)
on_css : List (String, String)
on_css =
[ "background-color" => "#800000"
, "color" => "#fff"]
off_css : List (String, String)
off_css =
["background-color" => "#fff"]
horizontal_category_css : List (String, String)
horizontal_category_css =
[ "display" => "inline-block"
, "padding" => "5px"
, "min-width" => "calc(7.5% - 10px)"
, "cursor" => "pointer"
]
left_tab_css : List (String, String)
left_tab_css = ["border-top-left-radius" => "5px"]
right_tab_css : List (String, String)
right_tab_css = ["border-top-right-radius" => "5px"]
border_bar_css : List (String, String)
border_bar_css =
["text-align" => "center"]
trigger_css : List (String, String)
trigger_css = [ "position" => "absolute"
, "left" => "0px"
, "top"=> "0px"
, "width" => "25px"
, "height" => "100%"
, "border" => "1px solid #f5f5f5"
, "background-color" => "white"
]
vertical_view_css : List (String, String)
vertical_view_css = [ "position"=> "absolute"
, "top" => "-2px"
, "margin-left" => "-2px"
, "padding" => "5px 20px"
, "background-color" => "#fff"
, "border" => "1px solid #f5f5f5"
, "text-align" => "center"
, "z-index" => "9"
]
vertical_category_css : List (String, String)
vertical_category_css =
[ "display" => "block"
, "padding" => "5px 5px 5px 0px"
, "cursor" => "pointer"
, "float" => "left"
, "width" => "100%"
, "font-size" => "large"
]
vertical_text_css : List (String, String)
vertical_text_css =
[ "width" => "1px"
, "font-size" => "1em"
, "padding" => "0px 4px"
, "word-wrap" => "break-word"
, "margin-top" => "10px"
]