Elm modal dialog box

alpha_cod

How do I integrate this sweet dialog into my Elm code. I've included the JS and CSS in my index.html. How do I call this JavaScript function in my update function?

update : Action -> Model -> (Model, Effects Action)
update action model =
  case action of
    Submit ->
      let valid = length model.name > 0
      in
        if valid 
          then (model, swal({"title": "Invalid name"}))
          else (model, swal({"title": "Valid name"}))
Adam Marshall

It's tricky to rig up a full example without all the view code to check with, but I'm hoping this simpler version helps! Cribbed to some extend from this repo...

index.html

<!DOCTYPE html>
<html>
  <head>
    <script src="Main.js" type="text/javascript"></script>
        <script type="text/javascript"
            src="bower_components/sweetalert/dist/sweetalert.min.js"
        ></script>
        <link rel="stylesheet"
            href="bower_components/sweetalert/dist/sweetalert.css"
        />
  </head>
  <body>
        <div id="main"></div>   
        <script type="text/javascript">

            var so = Elm.embed(Elm.Main, document.getElementById('main'));
            so.ports.callSwal.subscribe(doAlert); 
            function doAlert(space) {
                    if (space) swal("Hey, a spacebar!");
            }

        </script>
  </body>
</html>

modal.elm

import Graphics.Element
import Keyboard


port callSwal : Signal Bool
port callSwal =
    Keyboard.space


main = Graphics.Element.show "How about pressing a spacebar?"

stuff I did to make it work

$ bower install sweetalert
$ elm-make modal.elm --output=Main.js

Note

  • Embed the Elm application, to give js an object to access ("so" here)
  • In js, subscribe to a named port and give it a callback function.
  • create the port in elm. This one takes a simple Bool, but I guess yours will want at least a String.

A better answer

The trick turns out to be noticing that startApp has a mailbox baked into it, which you can access through app.model.

The alert message becomes part of your model. If it's an empty string, we interpret that as meaning "don't trigger any alerts".

NB. I've no idea why update needs to return a tuple with an Events Action in it. That's not been used here..

Here's an example of it all put together:

var so = Elm.embed(Elm.Main, document.getElementById('main'));
so.ports.alert.subscribe(function(text) {
    if (text.length > 0) swal(text);
});
import StartApp
import Task exposing (Task)
import Effects exposing (Effects, Never)
import Html exposing (Html, div, input, button, text)
import Html.Events exposing (on, onClick, targetValue)
import String exposing (length)


app :
  { html : Signal Html
  , model : Signal Model
  , tasks : Signal (Task Never ())
  }
app =
  StartApp.start
    { init = init
    , update = update
    , view = view
    , inputs = []
    }


port alert : Signal String
port alert =
    Signal.map (\n -> n.alert) app.model


main : Signal Html
main =
  app.html


-- MODEL

type alias Model =
  { name : String
  , alert : String
  }


init : (Model, Effects Action)
init =
  ( { name = ""
    , alert = ""
    }
  , Effects.none
  )


-- UPDATE

type Action
    = Submit
    | TextEntry String


update : Action -> Model -> (Model, Effects Action)
update action model =
  case action of

    Submit ->
      if length model.name > 0 then
        ({ model | alert = "Valid name" }, Effects.none)
      else
        ({ model | alert = "Invalid name" }, Effects.none)

    TextEntry txt ->
      ({ model | name = txt, alert = "" }, Effects.none)


-- VIEW

view : Signal.Address Action -> Model -> Html
view address model =
  let f = (\str -> Signal.message address (TextEntry str)) in
  div
    []
    [ input
      [ on "input" targetValue f ]
      []
    , button
      [ onClick address Submit ]
      [ text "Submit" ]
    ]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

TeamCity with showing a modal dialog box

From Dev

Modal Dialog Box using JQuery & AJAX

From Dev

How to handle modal-dialog box in Protractor?

From Dev

How to handle modal dialog box in Selenium?

From Dev

How to display a modal dialog box in ReactJS?

From Dev

Positioning a Modal-Dialog box within a Canvas

From Dev

Do any of the latest browsers offer a modal confirm Yes No Dialog box?

From Dev

Global application menu active in modal dialog box (on Linux)

From Dev

Open a modal dialog box, containing dynamic data on table cell click

From Dev

how to create a non-modal bootstrap dialog box

From Dev

Windows Internet Explorer Modal dialog box preventing selenium test run

From Dev

Modal dialog box does not communicate with main HTA window

From Dev

Global application menu active in modal dialog box (on Linux)

From Dev

HTML modal-dialog box does not load properly

From Dev

maxWidth uncenters dialog box (modal) for material-ui

From Dev

Modal Dialog Box not showing in ASP.NET web

From Dev

dlg.DoModal() is making the dialog box modal to the application and not to the previous dialogue box

From Dev

How do you prevent Modal dialog from closing when users clicks outside of the modal dialog box In JQuery UI?

From Dev

Modal RCP Dialog not modal

From Dev

Making Facebook Dialog Feed Box appear in the same window as a modal instead of popup of a page

From Dev

Show Modal Dialog/Confirmation Box Based on User Input in Code Behind/ASP.net

From Dev

How do I make a Modal Dialog Box Auto Open after 2 seconds?

From Dev

How to send user to home page if escape key clicked on JQuery modal dialog box

From Dev

Chrome's modal dialog box stuck off screen. How to move it back permanently?

From Dev

Selenium/Java/EdgeDriver: Modal window dialog box stops execution of test script

From Dev

href in modal dialog in bootstrap

From Dev

Passing parameters to modal dialog

From Dev

Validation in bootstrap modal dialog

From Java

Angular 2.0 and Modal Dialog

Related Related

  1. 1

    TeamCity with showing a modal dialog box

  2. 2

    Modal Dialog Box using JQuery & AJAX

  3. 3

    How to handle modal-dialog box in Protractor?

  4. 4

    How to handle modal dialog box in Selenium?

  5. 5

    How to display a modal dialog box in ReactJS?

  6. 6

    Positioning a Modal-Dialog box within a Canvas

  7. 7

    Do any of the latest browsers offer a modal confirm Yes No Dialog box?

  8. 8

    Global application menu active in modal dialog box (on Linux)

  9. 9

    Open a modal dialog box, containing dynamic data on table cell click

  10. 10

    how to create a non-modal bootstrap dialog box

  11. 11

    Windows Internet Explorer Modal dialog box preventing selenium test run

  12. 12

    Modal dialog box does not communicate with main HTA window

  13. 13

    Global application menu active in modal dialog box (on Linux)

  14. 14

    HTML modal-dialog box does not load properly

  15. 15

    maxWidth uncenters dialog box (modal) for material-ui

  16. 16

    Modal Dialog Box not showing in ASP.NET web

  17. 17

    dlg.DoModal() is making the dialog box modal to the application and not to the previous dialogue box

  18. 18

    How do you prevent Modal dialog from closing when users clicks outside of the modal dialog box In JQuery UI?

  19. 19

    Modal RCP Dialog not modal

  20. 20

    Making Facebook Dialog Feed Box appear in the same window as a modal instead of popup of a page

  21. 21

    Show Modal Dialog/Confirmation Box Based on User Input in Code Behind/ASP.net

  22. 22

    How do I make a Modal Dialog Box Auto Open after 2 seconds?

  23. 23

    How to send user to home page if escape key clicked on JQuery modal dialog box

  24. 24

    Chrome's modal dialog box stuck off screen. How to move it back permanently?

  25. 25

    Selenium/Java/EdgeDriver: Modal window dialog box stops execution of test script

  26. 26

    href in modal dialog in bootstrap

  27. 27

    Passing parameters to modal dialog

  28. 28

    Validation in bootstrap modal dialog

  29. 29

    Angular 2.0 and Modal Dialog

HotTag

Archive