Advertisement
Guest User

Grim Dawn Devotions

a guest
Jun 3rd, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.66 KB | None | 0 0
  1. module Elmish.SimpleInput
  2.  
  3. (**
  4. Minimal application showing how to use Elmish
  5. You can find more info about Emish architecture and samples at https://elmish.github.io/
  6. *)
  7.  
  8. open Fable.Core.JsInterop
  9. open Fable.React
  10. open Fable.React.Props
  11. open Elmish
  12. open Elmish.React
  13.  
  14. type Ident = string
  15. type Devotion = {minText:string;maxText:string;id:Ident}
  16. // MODEL
  17. type DevotionAffect =
  18.     | Modifier of id:Ident*text:string
  19.     | ADevotion of Devotion
  20. let getDevotionId =
  21.     function
  22.     | Modifier(ident,_) -> ident
  23.     | ADevotion x -> x.id
  24. type Model =
  25.     { MaxAll:bool;Values: DevotionAffect list;Checked:Ident Set}
  26.  
  27. type Msg =
  28.     | DevotionCheckChange of Ident
  29.  
  30. let init () =
  31.     {   Values = [
  32.         Modifier ("max","Max Devotions")
  33.         ADevotion {id="crBlue";minText="CrossRoads Blue +18 def";maxText="CrossRoads Blue +24 def"}
  34.         ]
  35.         MaxAll=false
  36.         Checked=Set.empty
  37.     }, Cmd.none
  38.  
  39. // UPDATE
  40.  
  41. let update (msg:Msg) (model:Model) =
  42.     match msg with
  43.     | DevotionCheckChange ident ->
  44.         printfn "Got a check from %s" ident
  45.         let chk =
  46.             if Set.contains ident model.Checked then
  47.                 Set.remove ident model.Checked
  48.             else Set.add ident model.Checked
  49.         {model with
  50.             Checked = chk
  51.                
  52.             MaxAll= chk.Contains "max"
  53.         }, Cmd.none
  54.  
  55.  
  56. let inline getTgtId (e:Browser.Types.Event) = e.target?id
  57. // VIEW (rendered with React)
  58. let labeledChk ident textStyle text isChecked onChange =
  59.     div [] [
  60.         input[Type "checkbox";Id ident;Checked isChecked;OnChange (getTgtId >> onChange)]
  61.         span [Style textStyle][str text]
  62.     ]
  63.  
  64. let devChk dispatch isMax =
  65.     function
  66.     | ADevotion x,isChecked ->
  67.         labeledChk x.id
  68.             (if isChecked then [FontStyle "italic"] else List.empty)
  69.             (if isMax then x.maxText else x.minText)
  70.             isChecked
  71.             (fun _ -> Msg.DevotionCheckChange x.id |> dispatch)
  72.     | Modifier (id,text),isChecked ->
  73.         labeledChk id
  74.             List.empty
  75.             text
  76.             isChecked
  77.             (fun _ -> Msg.DevotionCheckChange id |> dispatch)
  78.  
  79. let view (model:Model) dispatch =
  80.     div [ Class "main-container" ][
  81.         yield span [][ str "test"]
  82.         yield! model.Values |> Seq.map (fun x ->
  83.             devChk dispatch model.MaxAll (x,getDevotionId x |> model.Checked.Contains ))
  84.     ]
  85.  
  86. printfn "starting up"
  87. // App
  88. Program.mkProgram init update view
  89. |> Program.withReact "elmish-app"
  90. |> Program.run
  91.  
  92. // App
  93. Program.mkProgram init update view
  94. |> Program.withConsoleTrace
  95. |> Program.withReactSynchronous "elmish-app"
  96. |> Program.run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement