-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeader.elm
144 lines (125 loc) · 3.71 KB
/
Header.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
module Header where
-- Module for the header
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Window
import Search exposing (Query)
import Signal
import CategoryBar
-- Model
-- Header model is a record type that contains an Search.Model type.
-- A record type was used here to future extensibility.
type alias Model =
{ search : Query
}
init : Model
init =
{ search = Search.init
}
-- Update
-- The header contains 2 actions:
-- SearchAction is an action that forwards the responsibility to the Search type
-- Reset will reset the search filter to an empty [] search.
type Action =
SearchAction Search.Action
| Reset
update : Action -> Model -> Model
update action model =
case action of
SearchAction search_action ->
{ model | search = Search.update search_action model.search }
Reset ->
{ search = Search.update Search.Reset model.search }
-- View
-- Fowarding address for Signals triggered by the divs in the header.
type alias Context =
{ headerAction : Signal.Address Action
, searchEnter : Signal.Address (List String)
, reset : Signal.Address ()
}
-- view renders the Html for the header.
-- The header contains a div_logo_name, which is the div that contains the logo
-- and a search bar.
view : (Int, Int) -> Context -> Model -> Html
view (col_limit, col_percent) context model =
let
logo_width = 77
name_width = 200
height = 100
logo_and_name_width = logo_width + name_width
search_context = Search.Context
(Signal.forwardTo context.headerAction SearchAction)
context.searchEnter
in
div [ style container_css ]
[ div_logo_name (logo_width, name_width, height) col_limit context.reset
, Search.view (logo_and_name_width, height) col_limit search_context model.search
]
-- div_logo_name is the Html for the div that contains the logo and site title.
-- Clicking this div will result in a reset Signal.
-- Input (logo_width, name_w, height)
div_logo_name : (Int, Int, Int) -> Int -> Signal.Address () -> Html
div_logo_name (logo_w, name_w, h) col_limit address =
let
position = if col_limit <= 2 then [ "margin" => "0 auto"]
else ["float" => "left"]
in
div [ style (logo_name_css ((logo_w + name_w), h) `List.append` position)
, onClick address () ]
[ div_logo logo_w
, div_name (name_w, h)
]
-- HTML div for logo with logo as background image
-- Input: logo_width
div_logo : Int -> Html
div_logo w =
div [ style (logo_css w) ]
[]
-- HTML div for text in logo
-- Input: (name_width, h)
div_name : (Int, Int) -> Html
div_name (w, h) =
div [ style (name_css (w, h)) ]
[h1 [ style name_text_css ]
[ text "UChicago Marketplace" ]
]
-- Util
toPixel : number -> String
toPixel x = (toString x) ++ "px"
-- CSS widgets
(=>) = (,)
container_css : List (String, String)
container_css =
[ "margin-bottom" => "10px"
, "background-color" => "#fff"
, "border-radius" => "0px 0px 7px 7px"
, "padding" => "0px 20px"
]
logo_name_css : (Int, Int) -> List (String, String)
logo_name_css (w, h) =
[ "height" => toPixel h
, "width" => toPixel w
, "cursor" => "pointer"
]
logo_css : Int -> List (String, String)
logo_css w =
[ "height" => "100%"
, "background-image" => "url(logo.jpg)"
, "width" => toPixel w
, "background-size" => "contain"
, "float" => "left"
]
name_css : (Int, Int) -> List (String, String)
name_css (w, h) =
["width" => toPixel w
, "margin" => "0px"
, "display" => "table-cell"
, "vertical-align" => "middle"
, "height" => toPixel h
]
name_text_css : List (String, String)
name_text_css =
[ "font-size" => "2em"
, "margin" => "0px"
]