From 7b0fb162f2c62e36b26847faeebba792ecaead41 Mon Sep 17 00:00:00 2001
From: Edward <sabinusedward@yahoo.de>
Date: Fri, 10 Jul 2020 14:08:50 +0200
Subject: [PATCH] Added load ShapePicture Button

---
 src/Manager/ModalManager.elm  | 24 ++++++++++++++++--------
 src/Manager/UpdateManager.elm | 35 ++++++++++++++++++++---------------
 src/Manager/ViewManager.elm   |  6 ++++--
 src/ShapePicture_Editor.elm   |  6 ++----
 src/TypeHolder.elm            |  6 ++++--
 src/Util/Util.elm             | 11 +++++++++--
 src/Util/ViewParts.elm        | 28 +++++++++++++++++++++-------
 7 files changed, 76 insertions(+), 40 deletions(-)

diff --git a/src/Manager/ModalManager.elm b/src/Manager/ModalManager.elm
index 5bf45ad..6030847 100644
--- a/src/Manager/ModalManager.elm
+++ b/src/Manager/ModalManager.elm
@@ -13,6 +13,7 @@ type Modal
   | ShowJson
   | EditShape Int ShapeStyle
   | ShowManagePicture String (Maybe Url.Url) Int Int
+  | ShowLoadShapePicture String
 
 type ModalMsg
   = CloseModal
@@ -23,6 +24,7 @@ type ModalMsg
   | OpenShowJson
   | OpenEditShape Int ShapeStyle String String
   | OpenManagePicture String String
+  | OpenLoadShapePicture String
   -- for input / delete Shapes
   | ResetShowInput
   | Check String
@@ -66,12 +68,13 @@ updateModal msg modal =
                                                _     -> shapeStyle
                                             )
     OpenManagePicture iT value -> case modal of
-                                    NoModal              -> ShowManagePicture value Nothing 0 0
-                                    ShowTable            -> modal -- ERROR
-                                    ShowShape _ _        -> modal -- ERROR
-                                    ShowInput _ _ _      -> modal -- ERROR
-                                    EditShape _ _        -> modal -- ERROR
-                                    ShowJson             -> modal -- ERROR
+                                    NoModal                -> ShowManagePicture value Nothing 0 0
+                                    ShowTable              -> modal -- ERROR
+                                    ShowShape _ _          -> modal -- ERROR
+                                    ShowInput _ _ _        -> modal -- ERROR
+                                    EditShape _ _          -> modal -- ERROR
+                                    ShowJson               -> modal -- ERROR
+                                    ShowLoadShapePicture _ -> modal -- ERROR
                                     ShowManagePicture s url w h ->
                                       case iT of
                                         "i1" -> case Url.fromString value of
@@ -80,7 +83,7 @@ updateModal msg modal =
                                         "i2" -> ShowManagePicture s url (Maybe.withDefault 0 (String.toInt value)) h
                                         "i3" -> ShowManagePicture s url w (Maybe.withDefault 0 (String.toInt value))
                                         _    -> modal
-
+    OpenLoadShapePicture urlString -> ShowLoadShapePicture urlString
     ResetShowInput -> ShowInput
                         (deleteCoordsFromShape
                           (getInputShapeFromModal modal)
@@ -99,6 +102,7 @@ updateModal msg modal =
         ShowInput iShape ix iy   -> updateInput inputType value modal "ShowInput" iShape ix iy {x = 0, y = 0}
         EditShape _ _            -> modal -- ERROR
         ShowManagePicture _ _ _ _-> modal -- ERROR
+        ShowLoadShapePicture _   -> modal -- ERROR
     InputPoint -> case modal of
                     NoModal                   -> modal -- ERROR
                     ShowTable                 -> modal -- ERROR
@@ -107,6 +111,7 @@ updateModal msg modal =
                     ShowInput iShape ix iy    -> ShowInput (Polygon ((ix,iy)::(getPointListFromPolygon iShape))) "" ""
                     EditShape _ _             -> modal -- ERROR
                     ShowManagePicture _ _ _ _ -> modal -- ERROR
+                    ShowLoadShapePicture  _   -> modal -- ERROR
 updateInput : Char -> String -> Modal -> String -> Shape -> String -> String -> MousePosition -> Modal
 updateInput inputType value modal modalType iShape ix iy mouse =
     case inputType of
@@ -155,6 +160,7 @@ getUrlFromModal modal =
       ShowJson                    -> Nothing
       EditShape _ _               -> Nothing
       ShowManagePicture _ url _ _ -> url
+      ShowLoadShapePicture _      -> Nothing
 getInputShapeFromModal : Modal -> Shape
 getInputShapeFromModal modal =
     case modal of
@@ -165,6 +171,7 @@ getInputShapeFromModal modal =
       ShowJson                  -> Empty
       EditShape _ _             -> Empty
       ShowManagePicture _ _ _ _ -> Empty
+      ShowLoadShapePicture  _   -> Empty
 -- Transform Modal to other Types
 modalNameToString : Modal -> String
 modalNameToString modal =
@@ -175,4 +182,5 @@ modalNameToString modal =
       ShowShape _ _             -> "ShowShape"
       ShowJson                  -> "ShowJson"
       EditShape _ _             -> "EditShape"
-      ShowManagePicture _ _ _ _ -> "ShowManagePicture"
\ No newline at end of file
+      ShowManagePicture _ _ _ _ -> "ShowManagePicture"
+      ShowLoadShapePicture  _   -> "ShowLoadShapePicture"
\ No newline at end of file
diff --git a/src/Manager/UpdateManager.elm b/src/Manager/UpdateManager.elm
index 7cb4f1c..021f04f 100644
--- a/src/Manager/UpdateManager.elm
+++ b/src/Manager/UpdateManager.elm
@@ -35,6 +35,7 @@ updateModel msg model =
                                               OpenShowJson               -> updateModal modalMsg                       modal
                                               OpenEditShape id ss iT _   -> updateModal (OpenEditShape id ss iT value) modal
                                               OpenManagePicture iT _     -> updateModal (OpenManagePicture iT value)   modal
+                                              OpenLoadShapePicture _     -> updateModal (OpenLoadShapePicture value)   modal
                                               ResetShowInput             -> updateModal modalMsg                       modal
                                               Check          _           -> updateModal modalMsg                       modal
                                               Input          inputType _ -> updateModal (Input inputType value)        modal
@@ -112,14 +113,16 @@ updateModel msg model =
                                                       )
                                                       mPos
                                    }
-      InitialJsonStringLoaded result ->
+      ShapePictureJsonStringLoaded result ->
         case result of
-          Ok "" -> { model | httpLoadResult = Err (Http.BadBody "Empty result for LoadShapes") }
+          Ok "" -> { model | httpLoadResult = Err (Http.BadBody "Empty result for LoadShapePicture") }
           Ok s  -> updateModel (DecodeJson s) { model | httpLoadResult = result }
           Err e -> { model | httpLoadResult = result }
       LinkClicked urlRequest -> model
       UrlChanged  url        -> { model | url     = url }
-      SavePicture pic        -> { model | picture = Just pic }
+      SavePicture pic        -> { model | picture = Just pic
+                                        , saved   = []
+                                }
       ImgLoaded  imgDs       -> { model | picture = Just (Picture (maybePictureToPicture model.picture).url imgDs.width imgDs.height) }
       DecodeJson jsonText ->
         case resultDecode jsonText of
@@ -135,23 +138,25 @@ updateModel msg model =
                                         }
                         , picture     = pl.picture
                 }
-
+      LoadShapePicture url -> { model | url = let thisUrl = model.url in { thisUrl | query = Just ("url="++url) } }
 updateCmdMsg : Msg -> Model -> Cmd Msg
 updateCmdMsg msg model =
     case msg of
-      ManageModal             _ _ -> Cmd.none
-      ManageShapes            _ _ -> Cmd.none
-      SvgShapeHover           _   -> Cmd.none
-      UpdateMousePos          _   -> Cmd.none
-      InputDrawShape              -> Cmd.none
-      OpenDrawShape           _   -> Cmd.none
-      SavePicture             _   -> Cmd.none
-      ImgLoaded               _   -> Cmd.none
-      DecodeJson              _   -> Cmd.none
-      InitialJsonStringLoaded _   -> Cmd.none
+      ManageModal                  _ _ -> Cmd.none
+      ManageShapes                 _ _ -> Cmd.none
+      SvgShapeHover                _   -> Cmd.none
+      UpdateMousePos               _   -> Cmd.none
+      InputDrawShape                   -> Cmd.none
+      OpenDrawShape                _   -> Cmd.none
+      SavePicture                  _   -> Cmd.none
+      ImgLoaded                    _   -> Cmd.none
+      DecodeJson                   _   -> Cmd.none
+      ShapePictureJsonStringLoaded _   -> Cmd.none
+      LoadShapePicture             url -> Http.get { url = url
+                                                   , expect = Http.expectString ShapePictureJsonStringLoaded
+                                                   }
       LinkClicked urlReq ->
         case urlReq of
           Browser.Internal url  -> Nav.pushUrl model.key (Url.toString url)
-
           Browser.External href -> Nav.load href
       UrlChanged     url -> Cmd.none
diff --git a/src/Manager/ViewManager.elm b/src/Manager/ViewManager.elm
index 4405230..7ec6b23 100644
--- a/src/Manager/ViewManager.elm
+++ b/src/Manager/ViewManager.elm
@@ -46,13 +46,14 @@ viewEditor model =
                                               |> Tuple.second
                                           )
                                       ]
-            ShowJson                    -> [viewJsonModal           model                      ]
-            EditShape id shapeStyle     -> [viewEditShapeModal      id shapeStyle              ]
+            ShowJson                    -> [viewJsonModal           model         ]
+            EditShape id shapeStyle     -> [viewEditShapeModal      id shapeStyle ]
             ShowManagePicture s url w h -> [viewManagePictureModal  s url (case model.picture of
                                                                              Just picture -> Just picture.url
                                                                              Nothing      -> Nothing
                                                                           ) w h
                                            ]
+            ShowLoadShapePicture url    -> [viewLoadShapePictureModal url         ]
         ]
       )
 
@@ -78,5 +79,6 @@ viewViewer model =
             ShowJson                  -> []
             EditShape _ _             -> []
             ShowManagePicture _ _ _ _ -> []
+            ShowLoadShapePicture  _   -> []
         ]
       )
\ No newline at end of file
diff --git a/src/ShapePicture_Editor.elm b/src/ShapePicture_Editor.elm
index bd8a17d..e5d2221 100644
--- a/src/ShapePicture_Editor.elm
+++ b/src/ShapePicture_Editor.elm
@@ -32,15 +32,13 @@ init _ url key =
       , httpLoadResult = (Ok " Init")
       , picture = Nothing
       , queryInfo = queryInfo
-      , jsonFile = { text  = "{\"pictureUrl\":\"https://i2-prod.mirror.co.uk/incoming/article9690209.ece/ALTERNATES/s1200/Namibia.jpg\",\"shapes\":[{\"shape\":{\"circle\":{\"cx\":\"50\",\"cy\":\"50\",\"r\":\"40\"}},\"shapeprop\":{\"hFC\":\"white\",\"hFO\":\"0.5\",\"hSC\":\"red\",\"hSW\":\"3\",\"hSO\":\"1.0\",\"uFC\":\"white\",\"uFO\":\"0.0\",\"uSC\":\"red\",\"uSW\":\"3\",\"uSO\":\"1.0\",\"title\":\"default\",\"body\":\"default\"}},{\"shape\":{\"rect\":{\"x\":\"100\",\"y\":\"100\",\"width\":\"50\",\"height\":\"60\"}},\"shapeprop\":{\"hFC\":\"white\",\"hFO\":\"0.5\",\"hSC\":\"red\",\"hSW\":\"3\",\"hSO\":\"1.0\",\"uFC\":\"white\",\"uFO\":\"0.0\",\"uSC\":\"red\",\"uSW\":\"3\",\"uSO\":\"1.0\",\"title\":\"default\",\"body\":\"default\"}},{\"shape\":{\"polygon\":[{\"x\":\"200\",\"y\":\"150\"},{\"x\":\"250\",\"y\":\"400\"},{\"x\":\"120\",\"y\":\"340\"},{\"x\":\"160\",\"y\":\"250\"},{\"x\":\"180\",\"y\":\"170\"}]},\"shapeprop\":{\"hFC\":\"white\",\"hFO\":\"0.5\",\"hSC\":\"red\",\"hSW\":\"3\",\"hSO\":\"1.0\",\"uFC\":\"white\",\"uFO\":\"0.0\",\"uSC\":\"red\",\"uSW\":\"3\",\"uSO\":\"1.0\",\"title\":\"default\",\"body\":\"default\"}}]}"
-                   , error = ""
-                   }
+      , jsonFile = { text  = "", error = "" }
       , url  = url
       , key  = key
       }
      , case queryInfo.jsonUrl of
          Just jsonUrl -> Http.get { url = Url.toString jsonUrl
-                                  , expect = Http.expectString InitialJsonStringLoaded
+                                  , expect = Http.expectString ShapePictureJsonStringLoaded
                                   }
          Nothing -> Cmd.none
 
diff --git a/src/TypeHolder.elm b/src/TypeHolder.elm
index 1187c15..32c1b4e 100644
--- a/src/TypeHolder.elm
+++ b/src/TypeHolder.elm
@@ -35,7 +35,7 @@ type Msg
   | UpdateMousePos MousePosition
   | InputDrawShape
   -- for loading Strings with http
-  | InitialJsonStringLoaded (Result Http.Error String)
+  | ShapePictureJsonStringLoaded (Result Http.Error String)
   -- Url
   | LinkClicked Browser.UrlRequest
   | UrlChanged Url.Url
@@ -43,4 +43,6 @@ type Msg
   | DecodeJson String
   -- for saving pictureUrl
   | SavePicture Picture
-  | ImgLoaded ImgDimensions
\ No newline at end of file
+  | ImgLoaded ImgDimensions
+  -- for loading ShapePicture
+  | LoadShapePicture String
\ No newline at end of file
diff --git a/src/Util/Util.elm b/src/Util/Util.elm
index db269c3..494e9d5 100644
--- a/src/Util/Util.elm
+++ b/src/Util/Util.elm
@@ -94,7 +94,7 @@ type alias Picture = { url: Url.Url, w: Int, h: Int }
 newPicture : Url.Url -> Picture
 newPicture pictureUrl = { url = pictureUrl, w = 0, h = 0 }
 emptyPicture : Picture
-emptyPicture = { url = Url.Url Url.Http "" Nothing "" Nothing Nothing
+emptyPicture = { url = emptyUrl
                , w = -1, h = -1
                }
 maybePictureToPicture : Maybe Picture -> Picture
@@ -129,4 +129,11 @@ getQueryInfo url =
                      _       -> True
           , jsonUrl = Url.fromString jsonUrl
           }
-
+isUrl : String -> Bool
+isUrl str = case Url.fromString str of
+              Just url -> True
+              Nothing  -> False
+isUrlMessage : String -> String
+isUrlMessage str = if (isUrl str) then "Url ist gültig." else "Ungültige Url."
+emptyUrl : Url.Url
+emptyUrl = Url.Url Url.Http "" Nothing "" Nothing Nothing
\ No newline at end of file
diff --git a/src/Util/ViewParts.elm b/src/Util/ViewParts.elm
index 2a6ce99..63be61a 100644
--- a/src/Util/ViewParts.elm
+++ b/src/Util/ViewParts.elm
@@ -36,7 +36,12 @@ viewHeader_Buttons state =
         [ div [class "section"]
               [ h1 [class "title"] [text "ShapePicture Editor"] ]
         , div [ style "margin-left" "25px"]
-              [ button [ onClick (ManageModal (OpenManagePicture "" "") "")
+              [  button [ onClick (ManageModal (OpenLoadShapePicture "") "")
+                       , class "button is-success is-pulled-left is-size-6"
+                       , style "margin-bottom" "15px"
+                       , style "margin-right" "15px"
+                       ] [ text "Ein ShapePicture laden" ]
+              , button [ onClick (ManageModal (OpenManagePicture "" "") "")
                        , class "button is-success is-pulled-left is-size-6"
                        , style "margin-bottom" "15px"
                        , style "margin-right" "15px"
@@ -412,6 +417,20 @@ viewEditShapeModal id shapeStyle =
                                     , shapeDeleteButton id "6"
                                     ] 41.1
                       ]
+viewLoadShapePictureModal : String -> Html Msg
+viewLoadShapePictureModal urlString =
+    modalCard [ modalHeader "Ein ShapePicture laden"
+              , modalCardBody [ label [class "label"] [text "Url zum ShapePicture"]
+                              , label [] [ input [class "input", Html.Attributes.placeholder "Url zum ShapePicture"
+                                                 , value urlString, onInput (ManageModal (OpenLoadShapePicture "")) ] [] ]
+                              , text ("Status: "++(isUrlMessage urlString))
+                              ]
+              , modalFooter [ button [class "button is-success"
+                                        , if (isUrl urlString) then onClick (LoadShapePicture urlString)
+                                                               else Html.Attributes.disabled True
+                                        ] [text "Ãœbernehmen"]
+                               ] 22.0
+              ]
 viewManagePictureModal : String -> Maybe Url.Url -> Maybe Url.Url -> Int -> Int -> Html Msg
 viewManagePictureModal s mayUrl currentUrl w h=
     let picUrlState = case mayUrl of
@@ -439,12 +458,7 @@ viewJsonModal : Model -> Html Msg
 viewJsonModal model =
     let picture = maybePictureToPicture model.picture
     in modalCard [ modalHeader "Json Datei"
-                 , modalCardBody [ button [ onClick (DecodeJson model.jsonFile.text)
-                                          , class "button is-success is-pulled-left is-size-6"
-                                          , style "margin-right" "15px"
-                                          , style "margin-bottom" "15px"
-                                          ] [ text "string to shapes" ]
-                                 , div [] [ Html.pre [] [Html.text <| Json.Encode.encode 3 <| (writeAlltoJson model.saved (Url.toString picture.url))] ]
+                 , modalCardBody [ div [] [ Html.pre [] [Html.text <| Json.Encode.encode 3 <| (writeAlltoJson model.saved (Url.toString picture.url))] ]
                                  , Html.p [] [text <| model.jsonFile.error]
                                  ]
                  , modalFooter [] 0.0
-- 
GitLab