Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Html exposing (..)
- import Html.Attributes exposing (..)
- import Html.Events exposing (..)
- import Http
- import Json.Decode as Decode
- import List exposing (..)
- import Random as Rand
- import Tuple as Tpl
- main =
- Html.program
- {
- init = init
- , view = view
- , update = update
- , subscriptions = \_ -> Sub.none
- }
- type BrickOrientation =
- Grid
- | Horizontal
- | Vertical
- | Mason
- type alias Model =
- {
- scrolled: Int
- , layout: BrickOrientation
- , itemCount: Int
- , columns: Int
- , testSizes: List (Int, Int)
- }
- init : (Model, Cmd Msg)
- init =
- let
- moe = Model 0 Vertical 20 5 []
- in
- update Roll moe
- type Msg =
- NoOp
- | Roll
- | RollResult (List (Int, Int))
- | More (Result Http.Error Int)
- update : Msg -> Model -> (Model, Cmd Msg)
- update msg model =
- case msg of
- NoOp ->
- (model, Cmd.none)
- Roll ->
- (model, Rand.generate RollResult
- (Rand.list model.itemCount <| Rand.pair (Rand.int 100 900) (Rand.int 100 900)))
- RollResult add ->
- ({model | testSizes = add}, Cmd.none)
- More (Ok filler) ->
- ({model | itemCount = model.itemCount + filler}, Cmd.none)
- More (Err _) ->
- (model, Cmd.none)
- view : Model -> Html Msg
- view model =
- let
- cols = List.repeat model.columns (div [style
- [
- ("width", (toString (toFloat model.columns / 100)) ++ "%")
- ]]
- [])
- in
- case model.layout of
- Vertical ->
- List.repeat model.columns (div [style
- [
- ("width", (toString (toFloat model.columns / 100)) ++ "%")
- ]
- ]
- (List.foldl (\a b -> Tpl.second <|
- List.foldl (\current (newval, output) ->
- let
- addition = case (List.head newval) of
- Just x ->
- current ++ [x]
- Nothing ->
- current
- newsie = output ++ addition
- replacer = if isEmpty newval then
- []
- else
- drop 1 newval
- in
- (replacer, newsie)) (a, []) b)
- (List.repeat model.columns [])
- (splitUp (List.map generateGridBox model.testSizes)
- (ceiling <| (toFloat model.itemCount) / (toFloat model.columns)) [])))
- _ ->
- div [] []
- splitUp : List a -> Int -> List (List a) -> List (List a)
- splitUp input splitAmount output =
- let
- outtie = if List.length input < splitAmount then
- input
- else
- take splitAmount input
- innie = if List.length input <= splitAmount then
- []
- else
- drop splitAmount input
- newie = if isEmpty output then
- singleton outtie
- else
- output ++ [outtie]
- in
- if isEmpty innie then
- newie
- else
- splitUp innie splitAmount newie
- generateGridBox : (Int, Int) -> Html Msg
- generateGridBox (wid, hei) =
- div [style [
- ("background", "#eee")
- , ("display", "inline-block")
- , ("width", "100%")
- , ("height", "auto")
- , ("margin", "0")
- --, ("vertical-align", "top")
- , ("box-shadow", "inset 5px 5px blue")
- ]] [img [src <| "http://placehold.it/" ++ (toString wid)
- ++ "x" ++ (toString hei)
- ] []]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement