Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. import Html exposing (..)
  2. import Html.Attributes exposing (..)
  3. import Html.Events exposing (..)
  4. import Http
  5. import Json.Decode as Decode
  6. import List exposing (..)
  7. import Random as Rand
  8. import Tuple as Tpl
  9.  
  10. main =
  11. Html.program
  12. {
  13. init = init
  14. , view = view
  15. , update = update
  16. , subscriptions = \_ -> Sub.none
  17. }
  18.  
  19. type BrickOrientation =
  20. Grid
  21. | Horizontal
  22. | Vertical
  23. | Mason
  24.  
  25. type alias Model =
  26. {
  27. scrolled: Int
  28. , layout: BrickOrientation
  29. , itemCount: Int
  30. , columns: Int
  31. , testSizes: List (Int, Int)
  32. }
  33.  
  34. init : (Model, Cmd Msg)
  35. init =
  36. let
  37. moe = Model 0 Vertical 20 5 []
  38. in
  39. update Roll moe
  40.  
  41. type Msg =
  42. NoOp
  43. | Roll
  44. | RollResult (List (Int, Int))
  45. | More (Result Http.Error Int)
  46.  
  47. update : Msg -> Model -> (Model, Cmd Msg)
  48. update msg model =
  49. case msg of
  50. NoOp ->
  51. (model, Cmd.none)
  52. Roll ->
  53. (model, Rand.generate RollResult
  54. (Rand.list model.itemCount <| Rand.pair (Rand.int 100 900) (Rand.int 100 900)))
  55. RollResult add ->
  56. ({model | testSizes = add}, Cmd.none)
  57. More (Ok filler) ->
  58. ({model | itemCount = model.itemCount + filler}, Cmd.none)
  59. More (Err _) ->
  60. (model, Cmd.none)
  61.  
  62. view : Model -> Html Msg
  63. view model =
  64. let
  65. cols = List.repeat model.columns (div [style
  66. [
  67. ("width", (toString (toFloat model.columns / 100)) ++ "%")
  68. ]]
  69. [])
  70. in
  71. case model.layout of
  72. Vertical ->
  73. List.repeat model.columns (div [style
  74. [
  75. ("width", (toString (toFloat model.columns / 100)) ++ "%")
  76. ]
  77. ]
  78. (List.foldl (\a b -> Tpl.second <|
  79. List.foldl (\current (newval, output) ->
  80. let
  81. addition = case (List.head newval) of
  82. Just x ->
  83. current ++ [x]
  84. Nothing ->
  85. current
  86. newsie = output ++ addition
  87. replacer = if isEmpty newval then
  88. []
  89. else
  90. drop 1 newval
  91. in
  92. (replacer, newsie)) (a, []) b)
  93. (List.repeat model.columns [])
  94. (splitUp (List.map generateGridBox model.testSizes)
  95. (ceiling <| (toFloat model.itemCount) / (toFloat model.columns)) [])))
  96. _ ->
  97. div [] []
  98.  
  99. splitUp : List a -> Int -> List (List a) -> List (List a)
  100. splitUp input splitAmount output =
  101. let
  102. outtie = if List.length input < splitAmount then
  103. input
  104. else
  105. take splitAmount input
  106. innie = if List.length input <= splitAmount then
  107. []
  108. else
  109. drop splitAmount input
  110. newie = if isEmpty output then
  111. singleton outtie
  112. else
  113. output ++ [outtie]
  114. in
  115. if isEmpty innie then
  116. newie
  117. else
  118. splitUp innie splitAmount newie
  119.  
  120. generateGridBox : (Int, Int) -> Html Msg
  121. generateGridBox (wid, hei) =
  122. div [style [
  123. ("background", "#eee")
  124. , ("display", "inline-block")
  125. , ("width", "100%")
  126. , ("height", "auto")
  127. , ("margin", "0")
  128. --, ("vertical-align", "top")
  129. , ("box-shadow", "inset 5px 5px blue")
  130. ]] [img [src <| "http://placehold.it/" ++ (toString wid)
  131. ++ "x" ++ (toString hei)
  132. ] []]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement