From d40836e1057c66ec565e4d31d4963be38e7a1895 Mon Sep 17 00:00:00 2001
From: Edward <sabinusedward@yahoo.de>
Date: Sat, 11 Jul 2020 15:15:52 +0200
Subject: [PATCH] =?UTF-8?q?Sicherheit=20erh=C3=B6ht,=20etwas=20refactort,?=
 =?UTF-8?q?=20Benutzerschittstelle=20freundlciher=20gemacht?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/Manager/ViewManager.elm |  10 ++-
 src/Util/ViewParts.elm      | 121 ++++++++++++++++++------------------
 2 files changed, 67 insertions(+), 64 deletions(-)

diff --git a/src/Manager/ViewManager.elm b/src/Manager/ViewManager.elm
index 3df52ed..9344f86 100644
--- a/src/Manager/ViewManager.elm
+++ b/src/Manager/ViewManager.elm
@@ -26,13 +26,13 @@ viewEditor model =
   div [style "background-color" "#fff"]
       (List.concat
         [ standardViewPart
-        ,[ viewHeader_Buttons model.state
+        ,[ viewHeader_Buttons model.state model.picture
          , Html.section [class "section"] []
          ]
         , viewImage model "Editor"
         ,[ case model.httpLoadResult of
              Ok  s -> text ""
-             Err e -> text ("Could not load shapes: "++(httpErrorToString e))
+             Err e -> text ("Request failed: "++(httpErrorToString e))
          ]
         , case (getModalFromState model.state) of
             NoModal                -> []
@@ -54,7 +54,11 @@ viewEditor model =
                                                                              Nothing      -> Nothing
                                                                           ) w h
                                            ]
-            ShowLoadShapePicture url    -> [viewLoadShapePictureModal url         ]
+            ShowLoadShapePicture url    -> [viewLoadShapePictureModal url (case model.picture of
+                                                                             Just pic -> True
+                                                                             Nothing  -> False
+                                                                          )
+                                           ]
         ]
       )
 
diff --git a/src/Util/ViewParts.elm b/src/Util/ViewParts.elm
index f86cd95..5340614 100644
--- a/src/Util/ViewParts.elm
+++ b/src/Util/ViewParts.elm
@@ -30,55 +30,55 @@ standardViewPart = [ link [ rel "stylesheet" -- Bluma
                           , href "https://use.fontawesome.com/releases/v5.8.1/css/all.css"
                           ] []
                    ]
-viewHeader_Buttons : State -> Html Msg
-viewHeader_Buttons state =
+viewHeader_Buttons : State -> Maybe Picture -> Html Msg
+viewHeader_Buttons state mayPic =
+    let allowAddShape = case mayPic of
+                          Just pic -> pic.w > 0 && pic.h > 0
+                          Nothing  -> False
+        noPicMessage = "Bitte wählen Sie zuerst ein gültiges Bild aus!"
+    in
     div []
         [ div [class "section"]
               [ h1 [class "title"] [text "ShapePicture Editor"] ]
         , div [ style "margin-left" "25px"]
               [ viewDropDownMenue "Datei" True "angle-down" []
-                  [ dropdownItem "Ein ShapePicture laden"         (ManageModal (OpenLoadShapePicture "") "")
-                  , dropdownItem "Bild auswählen"                 (ManageModal (OpenManagePicture "" "") "")
-                  , dropdownItem "Json Datei anzeigen"            (ManageModal OpenShowJson "")
-                  , dropdownItem "ShapePicture Vorschau"          ShapePicturePreview
+                  [ dropDownItem "Ein ShapePicture laden" [ onClick (ManageModal (OpenLoadShapePicture "") "") ]
+                  , dropDownItem "Bild auswählen"         [ onClick (ManageModal (OpenManagePicture "" "") "") ]
+                  , dropDownItem "Json Datei anzeigen"    [ onClick (ManageModal OpenShowJson "")              ]
+                  , dropDownItem "ShapePicture Vorschau"  [ if allowAddShape then onClick ShapePicturePreview
+                                                                             else title noPicMessage           ]
                   ]
-              , viewDropDownMenue "Neue Form hinzüfügen"      True "" [onClick (ManageModal OpenShowInput "")] []
+              , viewDropDownMenue "Neue Form hinzüfügen" True ""
+                  ( if allowAddShape then [ onClick (ManageModal OpenShowInput "") ]
+                                     else [ Html.Attributes.disabled True, title noPicMessage ]
+                  ) []
               , if (eq_String_State "DrawShape" state)
                   then let iShape = getInputShapeFromState state
                        in if (eq_String_Shape "Polygon" iShape)
-                            then viewDropDownMenue "Polygon wird gezeichnet" True "angle-down" []
-                                   [ a [class "dropdown-item"
-                                       , onClick (ManageShapes (AddShape Empty) "")
-                                       , href ""
-                                       ] [text "Speichern"]
-                                   , a [class "dropdown-item"
-                                       , onClick (ManageModal CloseModal "")
-                                       , href ""
-                                       ] [text "Abbrechen"]
+                            then viewDropDownMenue "Vieleck wird gezeichnet" True "angle-down" []
+                                   [ dropDownItem "Speichern" [ if List.length (getPointListFromPolygon iShape) > 2
+                                                                  then onClick (ManageShapes (AddShape Empty) "")
+                                                                  else title "Bitte wählen Sie mindestens 3 Punkte für das Vieleck aus!"
+                                                              ]
+                                   , dropDownItem "Abbrechen" [ onClick (ManageModal CloseModal "") ]
                                    ]
                             else viewDropDownMenue
                                    (shapeNameStringToGerman
                                      (shapeNameToString
                                        (getInputShapeFromState state))++" zeichnen: abbrechen  ")
                                    False "times" [onClick (ManageModal CloseModal "")] []
-                  else viewDropDownMenue "Neue Form zeichnen" True "angle-down" []
-                         [ a [class "dropdown-item"
-                             , onClick (OpenDrawShape (Circle "" "" ""))
-                             , href ""
-                             ] [text "Kreis"]
-                         , a [class "dropdown-item"
-                             , onClick (OpenDrawShape (Rectangle "" "" "" ""))
-                             , href ""
-                             ] [text "Rechteck"]
-                         , a [class "dropdown-item"
-                             , onClick (OpenDrawShape (Ellipse "" "" "" ""))
-                             , href ""
-                             ] [text "Ellipse"]
-                         , a [class "dropdown-item"
-                             , onClick (OpenDrawShape (Polygon []))
-                             , href ""
-                             ] [text "Vieleck"]
-                         ]
+                  else viewDropDownMenue "Neue Form zeichnen" True "angle-down"
+                         (if allowAddShape then []
+                                           else [ Html.Attributes.disabled True, title noPicMessage ]
+                         )
+                         (if allowAddShape
+                            then [ dropDownItem "Kreis"    [onClick (OpenDrawShape (Circle "" "" ""))      ]
+                                 , dropDownItem "Rechteck" [onClick (OpenDrawShape (Rectangle "" "" "" ""))]
+                                 , dropDownItem "Ellipse"  [onClick (OpenDrawShape (Ellipse "" "" "" ""))  ]
+                                 , dropDownItem "Vieleck"  [onClick (OpenDrawShape (Polygon []))           ]
+                                 ]
+                            else []
+                         )
               , viewDropDownMenue "Tabelle mit Formen zeigen" True "" [onClick (ManageModal OpenShowTable "")] []
               ]
         ]
@@ -89,6 +89,7 @@ viewImage model mode =
         picH = String.fromInt picture.h
         loading =  Html.img [ Html.Events.on "load" (decodeImgLoad ImgLoaded)
                             , src (Url.toString picture.url)
+                            , Html.Attributes.hidden True
                             ] []
         isOnlyView = case mode of
                        "Editor" -> False
@@ -190,22 +191,10 @@ viewNewShapeInputModal iShape ix iy =
                                    then "Form auswählen"
                                    else (shapeNameStringToGerman(shapeNameToString iShape))
                                 ) False "angle-down" []
-                [ a [ class "dropdown-item"
-                    , onClick (ManageModal (Check "Circle") "")
-                    , href ""
-                    ] [text "Kreis"]
-                , a [ class "dropdown-item"
-                    , onClick (ManageModal (Check "Rectangle") "")
-                    , href ""
-                    ] [text "Rechteck"]
-                , a [ class "dropdown-item"
-                    , onClick (ManageModal (Check "Ellipse") "")
-                    , href ""
-                    ] [text "Ellipse"]
-                , a [ class "dropdown-item"
-                    , onClick (ManageModal (Check "Polygon" ) "")
-                    , href ""
-                    ] [text "Vieleck"]
+                [ dropDownItem "Kreis"    [onClick (ManageModal (Check "Circle") "")   ]
+                , dropDownItem "Rechteck" [onClick (ManageModal (Check "Rectangle") "")]
+                , dropDownItem "Ellipse"  [onClick (ManageModal (Check "Ellipse") "")  ]
+                , dropDownItem "Vieleck"  [onClick (ManageModal (Check "Polygon" ) "") ]
                 ]
             , if (shapeNameToString iShape)==""
                 then div [style "margin-top" "120px"] []
@@ -416,13 +405,14 @@ viewEditShapeModal id shapeStyle =
                                     , shapeDeleteButton id "6"
                                     ] 41.1
                       ]
-viewLoadShapePictureModal : String -> Html Msg
-viewLoadShapePictureModal urlString =
+viewLoadShapePictureModal : String -> Bool -> Html Msg
+viewLoadShapePictureModal urlString existsChanges =
     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))
+                              , if existsChanges then newLoadWarning "ShapePicture" else text ""
                               ]
               , modalFooter [ button [class "button is-success"
                                         , if (isUrl urlString) then onClick (LoadShapePicture urlString)
@@ -437,6 +427,9 @@ viewManagePictureModal s mayUrl currentUrl w h=
                         Nothing  -> "Ungültige Url."
         w1 = String.fromInt w
         h1 = String.fromInt h
+        existsChanges = case currentUrl of
+                          Just url -> True
+                          Nothing  -> False
     in modalCard [ modalHeader "Bild auswählen"
                  , modalCardBody [ case currentUrl of
                                      Just curUrl -> text ("Aktuelles Bild: "++(Url.toString curUrl))
@@ -445,6 +438,7 @@ viewManagePictureModal s mayUrl currentUrl w h=
                                  , label [] [ input [class "input", Html.Attributes.placeholder "Url zum Bild"
                                                     , value s, onInput (ManageModal (OpenManagePicture "i1" "")) ] [] ]
                                  , text ("Status: "++picUrlState)
+                                 , if existsChanges then newLoadWarning "Bild" else text ""
                                  ]
                  , modalFooter [ button [class "button is-success"
                                         , case mayUrl of
@@ -469,12 +463,13 @@ viewDropDownMenue header isGreen iconType attributes dropDownItems =
         [ class "dropdown is-hoverable"
         , style "margin-right" "15px"
         , style "margin-bottom" "15px"
-        ] attributes
+        ] []
       )
       [ div [ class "dropdown-trigger"]
-            [ button [ class ("button "++(if isGreen then "is-success" else "")
+            [ button ((class ("button "++(if isGreen then "is-success" else "")
                                        ++" aria-haspopup=true aria-controls=dropdown-menu")
-                     ]
+                      )::attributes
+                     )
                      [ Html.span [] [text header]
                      , if iconType == ""
                          then text ""
@@ -488,11 +483,11 @@ viewDropDownMenue header isGreen iconType attributes dropDownItems =
           else div [ class "dropdown-menu role=menu"]
                    [ div [ class "dropdown-content"] dropDownItems ]
       ]
-dropdownItem : String -> Msg -> Svg Msg
-dropdownItem title msg = a [class "dropdown-item"
-                           , onClick msg
-                           , href ""
-                           ] [text title]
+dropDownItem : String -> List (Attribute Msg) -> Svg Msg
+dropDownItem title attributes = a (List.concat [[class "dropdown-item is-disabled", href ""]
+                                               , attributes
+                                               ]
+                                  ) [text title]
 viewSvgShapes : List (Shape, ShapeStyle) -> Int -> Int -> List (Svg Msg)
 viewSvgShapes shapes activeShape id =
     case shapes of
@@ -582,4 +577,8 @@ modalStrongHeader attributes children =
                 , button [class "delete aria-label=close", onClick (ManageModal CloseModal "")] []
                 ]
 modalCardStrongBody : List (Attribute Msg) -> List (Html Msg) -> Html Msg
-modalCardStrongBody attributes children = Html.section ([class "modal-card-body"]++attributes) children
\ No newline at end of file
+modalCardStrongBody attributes children = Html.section ([class "modal-card-body"]++attributes) children
+newLoadWarning : String -> Html Msg
+newLoadWarning object = div [] [ label [class "label"][text "Warnung: "]
+                               , text ("Wenn Sie ein neues "++object++" laden, gehen Ihre aktuellen Änderungen verloren.")
+                               ]
\ No newline at end of file
-- 
GitLab