Guest User

Untitled

a guest
Mar 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. port module Header exposing ( .. )
  2.  
  3.  
  4. -- Library imports
  5. --------------------------------------------------------------------------------
  6. import Html exposing (Html, button, div, text, h4, p, hr, nav, text, ul, li, a)
  7. import Html.Attributes exposing (class, id, attribute, href)
  8. import Html.Events exposing (onClick)
  9. import List
  10. import Platform.Sub
  11.  
  12.  
  13. -- Model
  14. --------------------------------------------------------------------------------
  15. type alias NavElement = (String, String)
  16.  
  17. type alias Model = { nav_elements : List NavElement
  18. , title : String
  19. , is_fixed : Bool
  20. }
  21.  
  22.  
  23. model : List NavElement -> String -> Model
  24. model nav_elements title = { nav_elements = nav_elements
  25. , title = title
  26. , is_fixed = False
  27. }
  28.  
  29.  
  30. type alias Flags = { brand : String
  31. , pages : List (String, String)
  32. }
  33.  
  34.  
  35. init : Flags -> (Model, Cmd Msg)
  36. init flags =
  37. ( (model flags.pages flags.brand)
  38. , Cmd.none
  39. )
  40.  
  41.  
  42. -- Update
  43. --------------------------------------------------------------------------------
  44. type Msg = UpdateScrollPos Float
  45.  
  46.  
  47. update : Msg -> Model -> (Model, Cmd Msg)
  48. update msg model =
  49. case msg of
  50. UpdateScrollPos x ->
  51. ( { model | is_fixed = x >= 81.3 }
  52. , Cmd.none
  53. )
  54.  
  55.  
  56. -- Subscriptions
  57. --------------------------------------------------------------------------------
  58. port onScroll : (Float -> msg) -> Sub msg
  59.  
  60.  
  61. subscriptions : Model -> Sub Msg
  62. subscriptions model = onScroll UpdateScrollPos
  63.  
  64.  
  65. -- View
  66. --------------------------------------------------------------------------------
  67. viewNavElement : NavElement -> Html Msg
  68. viewNavElement (txt, url) =
  69. li []
  70. [ a [ href url ]
  71. [ text txt ]
  72. ]
  73.  
  74.  
  75. viewNavElements : List NavElement -> Bool -> Html Msg
  76. viewNavElements nav_elements is_fixed =
  77. let
  78. att = [ class "nav"
  79. , class "navbar-nav"
  80. , class "banner-background"
  81. , class "banner-background-bottom"
  82. ]
  83. in
  84. ul ( if is_fixed then ( class "sticky" ) :: att else att )
  85. ( List.map viewNavElement nav_elements )
  86.  
  87.  
  88. view : Model -> Html Msg
  89. view model =
  90. let
  91. brand = div ( if model.is_fixed then
  92. [ class "navbar-brand"
  93. , class "banner-background"
  94. , class "fixed-padding"
  95. ]
  96. else
  97. [ class "navbar-brand"
  98. , class "banner-background"
  99. ]
  100. )
  101. [ text model.title ]
  102. navbar = viewNavElements model.nav_elements model.is_fixed
  103.  
  104. in
  105. div [ id "banner"
  106. ]
  107. [ nav [ id "site-navigation"
  108. , class "navbar"
  109. , attribute "role" "navigation"
  110. ]
  111. [ brand
  112. , navbar
  113. ]
  114. ]
  115.  
  116.  
  117. -- Main statement
  118. --------------------------------------------------------------------------------
  119. main = Html.programWithFlags { init = init
  120. , update = update
  121. , subscriptions = subscriptions
  122. , view = view
  123. }
Add Comment
Please, Sign In to add comment