Permalink
Browse files

separate FileLoader from FloorProperty

  • Loading branch information...
1 parent e7eb8f4 commit 10fcf55991748b66aba01174067adfc247a58177 @torii-y torii-y committed Dec 1, 2016
@@ -0,0 +1,39 @@
+module Component.FileLoader exposing (..)
+
+import Task
+import Html exposing (Html)
+
+import Util.File as File exposing (..)
+import Util.HtmlUtil as HtmlUtil
+
+import Model.I18n as I18n exposing (Language)
+
+import View.Styles as S
+
+
+type Msg
+ = LoadFile FileList
+
+
+type alias Config msg =
+ { onFileWithDataURL : File -> String -> msg
+ , onFileLoadFailed : File.Error -> msg
+ }
+
+
+update : Config msg -> Msg -> Cmd msg
+update config message =
+ case message of
+ LoadFile fileList ->
+ File.getAt 0 fileList
+ |> Maybe.map (\file ->
+ readAsDataURL file
+ |> Task.map (config.onFileWithDataURL file)
+ |> Task.onError (Task.succeed << config.onFileLoadFailed)
+ |> Task.perform identity
+ )
+ |> Maybe.withDefault Cmd.none
+
+view : Language -> Html Msg
+view lang =
+ HtmlUtil.fileLoadButton LoadFile S.imageLoadButton (I18n.loadImage lang)
@@ -8,7 +8,6 @@ import Html exposing (..)
import Html.Attributes exposing (..)
import Task
-import Util.File as File exposing (..)
import Util.HtmlUtil exposing (..)
import View.Styles as Styles
@@ -28,21 +27,16 @@ type Msg
| InputFloorOrd String
| InputFloorRealWidth String
| InputFloorRealHeight String
- | LoadFile FileList
- | GotDataURL File String
| SelectDeleteFloor
| DeleteDialogMsg (Dialog.Msg Msg)
| DeleteFloor
- | FileError File.Error
type Event
= OnNameChange String
| OnOrdChange Int
| OnRealSizeChange (Int, Int)
- | OnFileWithDataURL File String
| OnDeleteFloor
- | OnFileLoadFailed File.Error
| None
@@ -104,24 +98,6 @@ update message model =
in
(newModel, Cmd.none, sizeEvent newModel)
- LoadFile fileList ->
- case File.getAt 0 fileList of
- Just file ->
- let
- cmd =
- readAsDataURL file
- |> Task.map (GotDataURL file)
- |> Task.onError (\e -> Task.succeed (FileError e))
- |> Task.perform identity
- in
- (model, cmd, None)
-
- Nothing ->
- (model, Cmd.none, None)
-
- GotDataURL file url ->
- (model, Cmd.none, OnFileWithDataURL file url)
-
SelectDeleteFloor ->
(model, Dialog.open DeleteDialogMsg, None)
@@ -133,10 +109,7 @@ update message model =
({ model | deleteFloorDialog = deleteFloorDialog }, newMsg, None)
DeleteFloor ->
- (model, Cmd.none, OnDeleteFloor)
-
- FileError err ->
- (model, Cmd.none, OnFileLoadFailed err)
+ (model, Cmd.none, OnDeleteFloor)
ordEvent : String -> Event
@@ -289,12 +262,9 @@ deleteButtonView lang user floor =
text ""
-view : (Msg -> msg) -> Language -> Date -> User -> Floor -> FloorProperty -> Html msg -> List (Html msg)
-view transform lang visitDate user floor model publishButtonView =
- [ if User.isAdmin user then
- fileLoadButton LoadFile Styles.imageLoadButton (I18n.loadImage lang) |> Html.map transform
- else
- text ""
+view : (Msg -> msg) -> Language -> Date -> User -> Floor -> FloorProperty -> Html msg -> Html msg -> List (Html msg)
+view transform lang visitDate user floor model floorLoadButton publishButtonView =
+ [ floorLoadButton
, floorNameInputView lang user model |> Html.map transform
, floorOrdInputView lang user model |> Html.map transform
, floorRealSizeInputView lang user model |> Html.map transform
@@ -20,11 +20,14 @@ import Model.SearchResult as SearchResult exposing (SearchResult)
import Model.SaveRequest as SaveRequest exposing (SaveRequest(..))
import Model.ColorPalette as ColorPalette exposing (ColorPalette)
+import Util.File exposing (File)
+
import API.Cache as Cache exposing (Cache, UserState)
import Component.FloorProperty as FloorProperty
import Component.Header as Header exposing (..)
import Component.ObjectNameInput as ObjectNameInput
+import Component.FileLoader as FileLoader
import Page.Map.URL exposing (URL)
import Page.Map.ContextMenuContext exposing (ContextMenuContext)
@@ -118,4 +121,6 @@ type Msg
| SyncFloor
| MouseMove Position
| MouseUp
+ | FileLoaderMsg FileLoader.Msg
+ | GotFileWithDataURL File String
| Error GlobalError
@@ -17,7 +17,7 @@ import ContextMenu
import Util.ShortCut as ShortCut
import Util.IdGenerator as IdGenerator exposing (Seed)
import Util.DictUtil as DictUtil
-import Util.File exposing (..)
+import Util.File as File exposing (..)
import Util.HttpUtil as HttpUtil
import Model.Direction as Direction exposing (..)
@@ -45,6 +45,7 @@ import API.Cache as Cache exposing (Cache, UserState)
import Component.FloorProperty as FloorProperty
import Component.Header as Header
import Component.ObjectNameInput as ObjectNameInput
+import Component.FileLoader as FileLoader
import Page.Map.Model as Model exposing (Model, DraggingContext(..))
import Page.Map.Msg exposing (Msg(..))
@@ -1537,6 +1538,33 @@ update removeToken setSelectionStart msg model =
_ ->
model ! []
+
+ FileLoaderMsg msg ->
+ model !
+ [ FileLoader.update
+ { onFileWithDataURL = GotFileWithDataURL
+ , onFileLoadFailed = Error << FileError
+ }
+ msg
+ ]
+
+ GotFileWithDataURL file dataURL ->
+ let
+ (id, newSeed) =
+ IdGenerator.new model.seed
+
+ url = id
+
+ (width, height) =
+ File.getSizeOfImage dataURL
+
+ saveImageCmd =
+ API.saveEditingImage model.apiConfig url file
+ |> performAPI (always <| ImageSaved url width height)
+ in
+ { model | seed = newSeed }
+ ! [ saveImageCmd ]
+
Error e ->
let
newModel =
@@ -1961,23 +1989,6 @@ updateFloorByFloorPropertyEvent apiConfig event seed efloor =
in
(newFloor, seed) ! [ saveCmd ]
- FloorProperty.OnFileWithDataURL file dataURL ->
- let
- (id, newSeed) =
- IdGenerator.new seed
-
- url = id
-
- (width, height) =
- getSizeOfImage dataURL
-
- saveImageCmd =
- performAPI
- (always <| ImageSaved url width height)
- (API.saveEditingImage apiConfig url file)
- in
- (efloor, newSeed) ! [ saveImageCmd ]
-
FloorProperty.OnDeleteFloor ->
let
floor =
@@ -1989,14 +2000,6 @@ updateFloorByFloorPropertyEvent apiConfig event seed efloor =
in
(efloor, seed) ! [ cmd ]
- FloorProperty.OnFileLoadFailed err ->
- let
- cmd =
- Task.perform (Error << FileError) (Task.succeed err)
- in
- (efloor, seed) ! [ cmd ]
-
-
regesterPersonOfObject : API.Config -> Object -> Cmd Msg
regesterPersonOfObject apiConfig e =
case Object.relatedPerson e of
@@ -17,6 +17,7 @@ import View.SearchInputView as SearchInputView
import Component.FloorProperty as FloorProperty
import Component.Header as Header
+import Component.FileLoader as FileLoader
import Util.HtmlUtil exposing (..)
@@ -106,7 +107,8 @@ subViewForEdit model editingMode =
model.user
(EditingFloor.present editingFloor)
model.floorProperty
- (publishButtonView model.lang model.user)
+ (fileLoadButton model.lang model.user)
+ (publishButton model.lang model.user)
_ ->
[]
@@ -117,8 +119,16 @@ subViewForEdit model editingMode =
]
-publishButtonView : Language -> User -> Html Msg
-publishButtonView lang user =
+fileLoadButton : Language -> User -> Html Msg
+fileLoadButton lang user =
+ if User.isAdmin user then
+ FileLoader.view lang |> Html.map FileLoaderMsg
+ else
+ text ""
+
+
+publishButton : Language -> User -> Html Msg
+publishButton lang user =
if User.isAdmin user then
button
[ onClick_ PreparePublish

0 comments on commit 10fcf55

Please sign in to comment.