From 90e039ceb333bc6be5dc813180eee920fe10aa37 Mon Sep 17 00:00:00 2001 From: Eike Kettner Date: Sun, 24 May 2020 17:29:45 +0200 Subject: [PATCH] Change attachments position via drag-and-drop --- elm.json | 1 + modules/webapp/src/main/elm/Api.elm | 17 ++++++++ .../webapp/src/main/elm/Comp/ItemDetail.elm | 40 ++++++++++++++++--- 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/elm.json b/elm.json index 3d52f6ca..bbc41890 100644 --- a/elm.json +++ b/elm.json @@ -20,6 +20,7 @@ "elm/url": "1.0.0", "elm-explorations/markdown": "1.0.0", "justinmimbs/date": "3.1.2", + "norpan/elm-html5-drag-drop": "3.1.4", "ryannhg/date-format": "2.3.0", "truqu/elm-base64": "2.0.4" }, diff --git a/modules/webapp/src/main/elm/Api.elm b/modules/webapp/src/main/elm/Api.elm index 4a085d4f..f5fbd474 100644 --- a/modules/webapp/src/main/elm/Api.elm +++ b/modules/webapp/src/main/elm/Api.elm @@ -42,6 +42,7 @@ module Api exposing , login , loginSession , logout + , moveAttachmentBefore , newInvite , postEquipment , postNewUser @@ -100,6 +101,7 @@ import Api.Model.ItemProposals exposing (ItemProposals) import Api.Model.ItemSearch exposing (ItemSearch) import Api.Model.ItemUploadMeta exposing (ItemUploadMeta) import Api.Model.JobQueueState exposing (JobQueueState) +import Api.Model.MoveAttachment exposing (MoveAttachment) import Api.Model.NotificationSettings exposing (NotificationSettings) import Api.Model.OptionalDate exposing (OptionalDate) import Api.Model.OptionalId exposing (OptionalId) @@ -1009,6 +1011,21 @@ getJobQueueStateTask flags = -- Item +moveAttachmentBefore : + Flags + -> String + -> MoveAttachment + -> (Result Http.Error BasicResult -> msg) + -> Cmd msg +moveAttachmentBefore flags itemId data receive = + Http2.authPost + { url = flags.config.baseUrl ++ "/api/v1/sec/item/" ++ itemId ++ "/attachment/movebefore" + , account = getAccount flags + , body = Http.jsonBody (Api.Model.MoveAttachment.encode data) + , expect = Http.expectJson receive Api.Model.BasicResult.decoder + } + + itemSearch : Flags -> ItemSearch -> (Result Http.Error ItemLightList -> msg) -> Cmd msg itemSearch flags search receive = Http2.authPost diff --git a/modules/webapp/src/main/elm/Comp/ItemDetail.elm b/modules/webapp/src/main/elm/Comp/ItemDetail.elm index b4e32739..d14bc408 100644 --- a/modules/webapp/src/main/elm/Comp/ItemDetail.elm +++ b/modules/webapp/src/main/elm/Comp/ItemDetail.elm @@ -14,6 +14,7 @@ import Api.Model.EquipmentList exposing (EquipmentList) import Api.Model.IdName exposing (IdName) import Api.Model.ItemDetail exposing (ItemDetail) import Api.Model.ItemProposals exposing (ItemProposals) +import Api.Model.MoveAttachment exposing (MoveAttachment) import Api.Model.OptionalDate exposing (OptionalDate) import Api.Model.OptionalId exposing (OptionalId) import Api.Model.OptionalText exposing (OptionalText) @@ -39,6 +40,7 @@ import File exposing (File) import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (onCheck, onClick, onInput) +import Html5.DragDrop as DD import Http import Markdown import Page exposing (Page(..)) @@ -88,6 +90,7 @@ type alias Model = , completed : Set String , errored : Set String , loading : Set String + , attachDD : DD.Model String String } @@ -182,6 +185,7 @@ emptyModel = , completed = Set.empty , errored = Set.empty , loading = Set.empty + , attachDD = DD.init } @@ -244,6 +248,7 @@ type Msg | AddFilesUploadResp String (Result Http.Error BasicResult) | AddFilesProgress String Http.Progress | AddFilesReset + | AttachDDMsg (DD.Msg String String) @@ -1174,6 +1179,28 @@ update key flags next msg model = in noSub ( model, updateBars ) + AttachDDMsg lm -> + let + ( model_, result ) = + DD.update lm model.attachDD + + cmd = + case result of + Just ( src, trg, _ ) -> + if src /= trg then + Api.moveAttachmentBefore flags + model.item.id + (MoveAttachment src trg) + SaveResp + + else + Cmd.none + + Nothing -> + Cmd.none + in + noSub ( { model | attachDD = model_ }, cmd ) + -- view @@ -1425,14 +1452,17 @@ renderAttachmentsTabMenu model = (\pos -> \el -> a - [ classList + ([ classList [ ( "item", True ) , ( "active", attachmentVisible model pos ) ] - , title (Maybe.withDefault "No Name" el.name) - , href "" - , onClick (SetActiveAttachment pos) - ] + , title (Maybe.withDefault "No Name" el.name) + , href "" + , onClick (SetActiveAttachment pos) + ] + ++ DD.draggable AttachDDMsg el.id + ++ DD.droppable AttachDDMsg el.id + ) [ Maybe.map (Util.String.ellipsis 20) el.name |> Maybe.withDefault "No Name" |> text