diff --git a/main.js b/main.js index 9799e4f..49bb719 100644 --- a/main.js +++ b/main.js @@ -6452,7 +6452,7 @@ var $author$project$Main$init = F3( return _Utils_Tuple2( A5( $author$project$Main$Model, - A5($author$project$Main$State, key, route, '', false, _List_Nil), + A5($author$project$Main$State, key, route, '', false, $elm$core$Maybe$Nothing), $author$project$Main$blankPlayer, $elm$core$Maybe$Nothing, $elm$core$Maybe$Nothing, @@ -6464,6 +6464,11 @@ var $elm$core$Platform$Sub$none = $elm$core$Platform$Sub$batch(_List_Nil); var $author$project$Main$subscriptions = function (_v0) { return $elm$core$Platform$Sub$none; }; +var $elm$core$Set$Set_elm_builtin = function (a) { + return {$: 'Set_elm_builtin', a: a}; +}; +var $elm$core$Set$empty = $elm$core$Set$Set_elm_builtin($elm$core$Dict$empty); +var $elm$core$Debug$log = _Debug_log; var $elm$core$Platform$Cmd$none = $elm$core$Platform$Cmd$batch(_List_Nil); var $author$project$Main$printError = function (error) { if (error.$ === 'NetworkError') { @@ -6484,29 +6489,48 @@ var $author$project$Main$setError = F2( {error: error}) }); }); -var $elm$core$Basics$not = _Basics_not; -var $author$project$Main$switchBooleanAt = function (idx) { - return F2( - function (i, value) { - return _Utils_eq(i, idx) ? (!value) : value; - }); -}; +var $elm$core$Set$insert = F2( + function (key, _v0) { + var dict = _v0.a; + return $elm$core$Set$Set_elm_builtin( + A3($elm$core$Dict$insert, key, _Utils_Tuple0, dict)); + }); +var $elm$core$Dict$member = F2( + function (key, dict) { + var _v0 = A2($elm$core$Dict$get, key, dict); + if (_v0.$ === 'Just') { + return true; + } else { + return false; + } + }); +var $elm$core$Set$member = F2( + function (key, _v0) { + var dict = _v0.a; + return A2($elm$core$Dict$member, key, dict); + }); +var $elm$core$Set$remove = F2( + function (key, _v0) { + var dict = _v0.a; + return $elm$core$Set$Set_elm_builtin( + A2($elm$core$Dict$remove, key, dict)); + }); var $author$project$Main$switchSelectionState = F2( - function (idx, model) { - var state = model.state; - var selection = model.state.selectedItems; - return _Utils_update( - model, - { - state: _Utils_update( - state, - { - selectedItems: A2( - $elm$core$List$indexedMap, - $author$project$Main$switchBooleanAt(idx), - selection) - }) - }); + function (id, selection) { + if (selection.$ === 'Just') { + var s = selection.a; + return $elm$core$Maybe$Just( + function () { + var _v1 = A2($elm$core$Set$member, id, s); + if (_v1) { + return A2($elm$core$Set$remove, id, s); + } else { + return A2($elm$core$Set$insert, id, s); + } + }()); + } else { + return A2($elm$core$Debug$log, 'ignore switchSelectionState', $elm$core$Maybe$Nothing); + } }); var $elm$url$Url$addPort = F2( function (maybePort, starter) { @@ -6552,15 +6576,6 @@ var $elm$url$Url$toString = function (url) { _Utils_ap(http, url.host)), url.path))); }; -var $elm$core$Maybe$withDefault = F2( - function (_default, maybe) { - if (maybe.$ === 'Just') { - var value = maybe.a; - return value; - } else { - return _default; - } - }); var $author$project$Main$update = F2( function (msg, model) { switch (msg.$) { @@ -6595,33 +6610,7 @@ var $author$project$Main$update = F2( state, { route: page, - selectedItems: function () { - switch (page.$) { - case 'GroupLoot': - return A2( - $elm$core$List$map, - function (v) { - return false; - }, - A2($elm$core$Maybe$withDefault, _List_Nil, model.groupLoot)); - case 'PlayerChest': - return A2( - $elm$core$List$map, - function (v) { - return false; - }, - A2($elm$core$Maybe$withDefault, _List_Nil, model.loot)); - case 'Merchant': - return A2( - $elm$core$List$map, - function (v) { - return false; - }, - A2($elm$core$Maybe$withDefault, _List_Nil, model.merchantItems)); - default: - return _List_Nil; - } - }() + selectedItems: $elm$core$Maybe$Just($elm$core$Set$empty) }) }); }(), @@ -6702,9 +6691,21 @@ var $author$project$Main$update = F2( $elm$core$Platform$Cmd$none); } default: - var idx = msg.a; + var id = msg.a; + var state = model.state; return _Utils_Tuple2( - A2($author$project$Main$switchSelectionState, idx, model), + _Utils_update( + model, + { + state: _Utils_update( + state, + { + selectedItems: A2( + $elm$core$Debug$log, + 'new selection', + A2($author$project$Main$switchSelectionState, id, state.selectedItems)) + }) + }), $elm$core$Platform$Cmd$none); } }); @@ -6994,12 +6995,44 @@ var $elm$html$Html$Attributes$classList = function (classes) { A2($elm$core$List$filter, $elm$core$Tuple$second, classes)))); }; var $elm$html$Html$input = _VirtualDom_node('input'); +var $author$project$Main$isSelected = F2( + function (id, selection) { + return A2($elm$core$Set$member, id, selection); + }); var $elm$html$Html$label = _VirtualDom_node('label'); +var $elm$json$Json$Decode$at = F2( + function (fields, decoder) { + return A3($elm$core$List$foldr, $elm$json$Json$Decode$field, decoder, fields); + }); +var $elm$json$Json$Decode$bool = _Json_decodeBool; +var $elm$html$Html$Events$targetChecked = A2( + $elm$json$Json$Decode$at, + _List_fromArray( + ['target', 'checked']), + $elm$json$Json$Decode$bool); +var $elm$html$Html$Events$onCheck = function (tagger) { + return A2( + $elm$html$Html$Events$on, + 'change', + A2($elm$json$Json$Decode$map, tagger, $elm$html$Html$Events$targetChecked)); +}; var $elm$html$Html$td = _VirtualDom_node('td'); var $elm$html$Html$tr = _VirtualDom_node('tr'); var $elm$html$Html$Attributes$type_ = $elm$html$Html$Attributes$stringProperty('type'); -var $author$project$Main$viewItemTableRow = F3( - function (selected, idx, item) { +var $author$project$Main$viewItemTableRow = F2( + function (selection, item) { + var _v0 = function () { + if (selection.$ === 'Just') { + var s = selection.a; + return _Utils_Tuple2( + true, + A2($author$project$Main$isSelected, item.id, s)); + } else { + return _Utils_Tuple2(false, false); + } + }(); + var canSelect = _v0.a; + var selected = _v0.b; return A2( $elm$html$Html$tr, _List_fromArray( @@ -7021,9 +7054,7 @@ var $author$project$Main$viewItemTableRow = F3( $elm$html$Html$label, _List_fromArray( [ - $elm$html$Html$Attributes$class('level checkbox'), - $elm$html$Html$Events$onClick( - $author$project$Main$LootViewItemSwitched(idx)) + $elm$html$Html$Attributes$class('level checkbox') ]), _List_fromArray( [ @@ -7070,7 +7101,11 @@ var $author$project$Main$viewItemTableRow = F3( _List_fromArray( [ $elm$html$Html$Attributes$class('checkbox level-item'), - $elm$html$Html$Attributes$type_('checkbox') + $elm$html$Html$Attributes$type_('checkbox'), + $elm$html$Html$Events$onCheck( + function (v) { + return $author$project$Main$LootViewItemSwitched(item.id); + }) ]), _List_Nil) ])) @@ -7078,38 +7113,39 @@ var $author$project$Main$viewItemTableRow = F3( ])) ])); }); -var $author$project$Main$viewLoot = function (items) { - return A2( - $elm$html$Html$table, - _List_fromArray( - [ - $elm$html$Html$Attributes$class('table is-fullwidth is-striped') - ]), - _Utils_ap( +var $author$project$Main$viewLoot = F2( + function (items, selection) { + return A2( + $elm$html$Html$table, _List_fromArray( [ - A2( - $elm$html$Html$thead, - _List_fromArray( - [ - $elm$html$Html$Attributes$class('table-header') - ]), - _List_fromArray( - [ - A2( - $elm$html$Html$th, - _List_Nil, - _List_fromArray( - [ - $elm$html$Html$text('Nom') - ])) - ])) + $elm$html$Html$Attributes$class('table is-fullwidth is-striped') ]), - A2( - $elm$core$List$indexedMap, - $author$project$Main$viewItemTableRow(false), - items))); -}; + _Utils_ap( + _List_fromArray( + [ + A2( + $elm$html$Html$thead, + _List_fromArray( + [ + $elm$html$Html$Attributes$class('table-header') + ]), + _List_fromArray( + [ + A2( + $elm$html$Html$th, + _List_Nil, + _List_fromArray( + [ + $elm$html$Html$text('Nom') + ])) + ])) + ]), + A2( + $elm$core$List$map, + $author$project$Main$viewItemTableRow(selection), + items))); + }); var $elm$html$Html$i = _VirtualDom_node('i'); var $author$project$Main$showWealthField = F2( function (name, value) { @@ -7301,6 +7337,15 @@ var $author$project$Main$viewSearchBar = A2( $elm$html$Html$Attributes$class('input') ]), _List_Nil); +var $elm$core$Maybe$withDefault = F2( + function (_default, maybe) { + if (maybe.$ === 'Just') { + var value = maybe.a; + return value; + } else { + return _default; + } + }); var $author$project$Main$view = function (model) { return { body: _List_fromArray( @@ -7330,8 +7375,10 @@ var $author$project$Main$view = function (model) { $elm$html$Html$text('Mon Coffre') ])), $author$project$Main$viewSearchBar, - $author$project$Main$viewLoot( - A2($elm$core$Maybe$withDefault, _List_Nil, model.loot)) + A2( + $author$project$Main$viewLoot, + A2($elm$core$Maybe$withDefault, _List_Nil, model.loot), + model.state.selectedItems) ]); case 'GroupLoot': return _List_fromArray( @@ -7343,8 +7390,10 @@ var $author$project$Main$view = function (model) { [ $elm$html$Html$text('Coffre de groupe') ])), - $author$project$Main$viewLoot( - A2($elm$core$Maybe$withDefault, _List_Nil, model.groupLoot)) + A2( + $author$project$Main$viewLoot, + A2($elm$core$Maybe$withDefault, _List_Nil, model.groupLoot), + model.state.selectedItems) ]); case 'Merchant': return _List_fromArray( @@ -7356,8 +7405,10 @@ var $author$project$Main$view = function (model) { [ $elm$html$Html$text('Acheter des objets') ])), - $author$project$Main$viewLoot( - A2($elm$core$Maybe$withDefault, _List_Nil, model.merchantItems)) + A2( + $author$project$Main$viewLoot, + A2($elm$core$Maybe$withDefault, _List_Nil, model.merchantItems), + model.state.selectedItems) ]); default: return _List_fromArray( diff --git a/src/Main.elm b/src/Main.elm index ad472b3..8b6a422 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -10,6 +10,7 @@ import Html.Events exposing (..) import Http import Json.Decode exposing (Decoder, field, list, string, int) import Url.Parser as P exposing (Parser, (), oneOf, s) +import Set exposing (Set) -- Main main : Program () Model Msg @@ -25,12 +26,16 @@ main = -- Model +type alias Selection = Set Int + +emptySelection = [] + type alias State = { navKey : Nav.Key , route : Route , error : String , menuOpen : Bool - , selectedItems : List Bool + , selectedItems : Maybe Selection } type alias Model = @@ -48,7 +53,7 @@ init flags url key = Just r -> r Nothing -> PlayerChest in - ( Model (State key route "" False []) blankPlayer Nothing Nothing Nothing, fetchInitialData 0) + ( Model (State key route "" False Nothing) blankPlayer Nothing Nothing Nothing, fetchInitialData 0) fetchInitialData : Int -> Cmd Msg @@ -175,18 +180,11 @@ update msg model = in case route of Just page -> - ( let state = model.state in - { model - | state = { state - | route = page - -- Reinitialize selectionList with url change - , selectedItems = - case page of - GroupLoot -> List.map (\v -> False) (Maybe.withDefault [] model.groupLoot) - PlayerChest -> List.map (\v -> False) (Maybe.withDefault [] model.loot) - Merchant -> List.map (\v -> False) (Maybe.withDefault [] model.merchantItems) - NewLoot -> [] - } + ( let + state = model.state + in + { model | state = + { state | route = page , selectedItems = Just Set.empty} } , case page of GroupLoot -> Cmd.none @@ -224,8 +222,13 @@ update msg model = , Cmd.none ) - LootViewItemSwitched idx -> - ( switchSelectionState idx model, Cmd.none ) + LootViewItemSwitched id -> + let + state = model.state + in + ( { model | state = + { state | selectedItems = Debug.log "new selection" (switchSelectionState id state.selectedItems) }} + , Cmd.none ) -- ERRORS @@ -246,20 +249,14 @@ printError error = -- STATE Utils -switchBooleanAt idx = - (\i value -> - if i == idx then - not value - else - value - ) - -switchSelectionState idx model = - let - state = model.state - selection = model.state.selectedItems - in - { model | state = { state | selectedItems = List.indexedMap (switchBooleanAt idx) selection } } +switchSelectionState : Int -> Maybe Selection -> Maybe Selection +switchSelectionState id selection = + case selection of + Just s -> + Just (case Set.member id s of + True -> Set.remove id s + False -> Set.insert id s) + Nothing -> Debug.log "ignore switchSelectionState" Nothing -- SUBSCRIPTIONS -- @@ -283,17 +280,17 @@ view model = PlayerChest -> [ p [class "heading"] [text "Mon Coffre"] , viewSearchBar - , viewLoot (Maybe.withDefault [] model.loot) + , viewLoot (Maybe.withDefault [] model.loot) model.state.selectedItems ] GroupLoot -> [ p [] [text "Coffre de groupe"] - , viewLoot (Maybe.withDefault [] model.groupLoot) + , viewLoot (Maybe.withDefault [] model.groupLoot) model.state.selectedItems ] Merchant -> [ p [] [text "Acheter des objets"] - , viewLoot (Maybe.withDefault [] model.merchantItems) + , viewLoot (Maybe.withDefault [] model.merchantItems) model.state.selectedItems ] NewLoot -> @@ -306,26 +303,37 @@ view model = -- LOOT Views -viewLoot : Loot -> Html Msg -viewLoot items = +isSelected id selection = + Set.member id selection + +viewLoot : Loot -> Maybe Selection -> Html Msg +viewLoot items selection = table [ class "table is-fullwidth is-striped"] ([ thead [class "table-header"] [ th [] [text "Nom"] ] ] - ++ List.indexedMap (False |> viewItemTableRow) items + ++ List.map (viewItemTableRow selection) items ) -viewItemTableRow selected idx item = +viewItemTableRow selection item = + let + (canSelect, selected) = + case selection of + Just s -> + (True, isSelected item.id s) + Nothing -> + (False, False) + in tr [ classList [ ("is-selected", selected) ] ] [ td [] - [ label [ class "level checkbox", onClick (LootViewItemSwitched idx) ] + [ label [ class "level checkbox" ] [ div [ class "level-left" ] [ p [class "level-item"] [ text item.name ] ] , div [ class "level-right" ] [ p [class "level-item"] [ text (String.fromInt item.base_price ++ "po") ] - , input [class "checkbox level-item", type_ "checkbox"] [] + , input [class "checkbox level-item", type_ "checkbox", onCheck (\v -> LootViewItemSwitched item.id)] [] ] ] ]