makes selection work

This commit is contained in:
2019-11-02 21:55:55 +01:00
parent 68c8761c3d
commit eb2ee54add
2 changed files with 200 additions and 141 deletions

255
main.js
View File

@@ -6452,7 +6452,7 @@ var $author$project$Main$init = F3(
return _Utils_Tuple2( return _Utils_Tuple2(
A5( A5(
$author$project$Main$Model, $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, $author$project$Main$blankPlayer,
$elm$core$Maybe$Nothing, $elm$core$Maybe$Nothing,
$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) { var $author$project$Main$subscriptions = function (_v0) {
return $elm$core$Platform$Sub$none; 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 $elm$core$Platform$Cmd$none = $elm$core$Platform$Cmd$batch(_List_Nil);
var $author$project$Main$printError = function (error) { var $author$project$Main$printError = function (error) {
if (error.$ === 'NetworkError') { if (error.$ === 'NetworkError') {
@@ -6484,29 +6489,48 @@ var $author$project$Main$setError = F2(
{error: error}) {error: error})
}); });
}); });
var $elm$core$Basics$not = _Basics_not; var $elm$core$Set$insert = F2(
var $author$project$Main$switchBooleanAt = function (idx) { function (key, _v0) {
return F2( var dict = _v0.a;
function (i, value) { return $elm$core$Set$Set_elm_builtin(
return _Utils_eq(i, idx) ? (!value) : value; 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( var $author$project$Main$switchSelectionState = F2(
function (idx, model) { function (id, selection) {
var state = model.state; if (selection.$ === 'Just') {
var selection = model.state.selectedItems; var s = selection.a;
return _Utils_update( return $elm$core$Maybe$Just(
model, function () {
{ var _v1 = A2($elm$core$Set$member, id, s);
state: _Utils_update( if (_v1) {
state, return A2($elm$core$Set$remove, id, s);
{ } else {
selectedItems: A2( return A2($elm$core$Set$insert, id, s);
$elm$core$List$indexedMap, }
$author$project$Main$switchBooleanAt(idx), }());
selection) } else {
}) return A2($elm$core$Debug$log, 'ignore switchSelectionState', $elm$core$Maybe$Nothing);
}); }
}); });
var $elm$url$Url$addPort = F2( var $elm$url$Url$addPort = F2(
function (maybePort, starter) { function (maybePort, starter) {
@@ -6552,15 +6576,6 @@ var $elm$url$Url$toString = function (url) {
_Utils_ap(http, url.host)), _Utils_ap(http, url.host)),
url.path))); 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( var $author$project$Main$update = F2(
function (msg, model) { function (msg, model) {
switch (msg.$) { switch (msg.$) {
@@ -6595,33 +6610,7 @@ var $author$project$Main$update = F2(
state, state,
{ {
route: page, route: page,
selectedItems: function () { selectedItems: $elm$core$Maybe$Just($elm$core$Set$empty)
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;
}
}()
}) })
}); });
}(), }(),
@@ -6702,9 +6691,21 @@ var $author$project$Main$update = F2(
$elm$core$Platform$Cmd$none); $elm$core$Platform$Cmd$none);
} }
default: default:
var idx = msg.a; var id = msg.a;
var state = model.state;
return _Utils_Tuple2( 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); $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)))); A2($elm$core$List$filter, $elm$core$Tuple$second, classes))));
}; };
var $elm$html$Html$input = _VirtualDom_node('input'); 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$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$td = _VirtualDom_node('td');
var $elm$html$Html$tr = _VirtualDom_node('tr'); var $elm$html$Html$tr = _VirtualDom_node('tr');
var $elm$html$Html$Attributes$type_ = $elm$html$Html$Attributes$stringProperty('type'); var $elm$html$Html$Attributes$type_ = $elm$html$Html$Attributes$stringProperty('type');
var $author$project$Main$viewItemTableRow = F3( var $author$project$Main$viewItemTableRow = F2(
function (selected, idx, item) { 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( return A2(
$elm$html$Html$tr, $elm$html$Html$tr,
_List_fromArray( _List_fromArray(
@@ -7021,9 +7054,7 @@ var $author$project$Main$viewItemTableRow = F3(
$elm$html$Html$label, $elm$html$Html$label,
_List_fromArray( _List_fromArray(
[ [
$elm$html$Html$Attributes$class('level checkbox'), $elm$html$Html$Attributes$class('level checkbox')
$elm$html$Html$Events$onClick(
$author$project$Main$LootViewItemSwitched(idx))
]), ]),
_List_fromArray( _List_fromArray(
[ [
@@ -7070,7 +7101,11 @@ var $author$project$Main$viewItemTableRow = F3(
_List_fromArray( _List_fromArray(
[ [
$elm$html$Html$Attributes$class('checkbox level-item'), $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) _List_Nil)
])) ]))
@@ -7078,38 +7113,39 @@ var $author$project$Main$viewItemTableRow = F3(
])) ]))
])); ]));
}); });
var $author$project$Main$viewLoot = function (items) { var $author$project$Main$viewLoot = F2(
return A2( function (items, selection) {
$elm$html$Html$table, return A2(
_List_fromArray( $elm$html$Html$table,
[
$elm$html$Html$Attributes$class('table is-fullwidth is-striped')
]),
_Utils_ap(
_List_fromArray( _List_fromArray(
[ [
A2( $elm$html$Html$Attributes$class('table is-fullwidth is-striped')
$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( _Utils_ap(
$elm$core$List$indexedMap, _List_fromArray(
$author$project$Main$viewItemTableRow(false), [
items))); 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 $elm$html$Html$i = _VirtualDom_node('i');
var $author$project$Main$showWealthField = F2( var $author$project$Main$showWealthField = F2(
function (name, value) { function (name, value) {
@@ -7301,6 +7337,15 @@ var $author$project$Main$viewSearchBar = A2(
$elm$html$Html$Attributes$class('input') $elm$html$Html$Attributes$class('input')
]), ]),
_List_Nil); _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) { var $author$project$Main$view = function (model) {
return { return {
body: _List_fromArray( body: _List_fromArray(
@@ -7330,8 +7375,10 @@ var $author$project$Main$view = function (model) {
$elm$html$Html$text('Mon Coffre') $elm$html$Html$text('Mon Coffre')
])), ])),
$author$project$Main$viewSearchBar, $author$project$Main$viewSearchBar,
$author$project$Main$viewLoot( A2(
A2($elm$core$Maybe$withDefault, _List_Nil, model.loot)) $author$project$Main$viewLoot,
A2($elm$core$Maybe$withDefault, _List_Nil, model.loot),
model.state.selectedItems)
]); ]);
case 'GroupLoot': case 'GroupLoot':
return _List_fromArray( return _List_fromArray(
@@ -7343,8 +7390,10 @@ var $author$project$Main$view = function (model) {
[ [
$elm$html$Html$text('Coffre de groupe') $elm$html$Html$text('Coffre de groupe')
])), ])),
$author$project$Main$viewLoot( A2(
A2($elm$core$Maybe$withDefault, _List_Nil, model.groupLoot)) $author$project$Main$viewLoot,
A2($elm$core$Maybe$withDefault, _List_Nil, model.groupLoot),
model.state.selectedItems)
]); ]);
case 'Merchant': case 'Merchant':
return _List_fromArray( return _List_fromArray(
@@ -7356,8 +7405,10 @@ var $author$project$Main$view = function (model) {
[ [
$elm$html$Html$text('Acheter des objets') $elm$html$Html$text('Acheter des objets')
])), ])),
$author$project$Main$viewLoot( A2(
A2($elm$core$Maybe$withDefault, _List_Nil, model.merchantItems)) $author$project$Main$viewLoot,
A2($elm$core$Maybe$withDefault, _List_Nil, model.merchantItems),
model.state.selectedItems)
]); ]);
default: default:
return _List_fromArray( return _List_fromArray(

View File

@@ -10,6 +10,7 @@ import Html.Events exposing (..)
import Http import Http
import Json.Decode exposing (Decoder, field, list, string, int) import Json.Decode exposing (Decoder, field, list, string, int)
import Url.Parser as P exposing (Parser, (</>), oneOf, s) import Url.Parser as P exposing (Parser, (</>), oneOf, s)
import Set exposing (Set)
-- Main -- Main
main : Program () Model Msg main : Program () Model Msg
@@ -25,12 +26,16 @@ main =
-- Model -- Model
type alias Selection = Set Int
emptySelection = []
type alias State = type alias State =
{ navKey : Nav.Key { navKey : Nav.Key
, route : Route , route : Route
, error : String , error : String
, menuOpen : Bool , menuOpen : Bool
, selectedItems : List Bool , selectedItems : Maybe Selection
} }
type alias Model = type alias Model =
@@ -48,7 +53,7 @@ init flags url key =
Just r -> r Just r -> r
Nothing -> PlayerChest Nothing -> PlayerChest
in 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 fetchInitialData : Int -> Cmd Msg
@@ -175,18 +180,11 @@ update msg model =
in in
case route of case route of
Just page -> Just page ->
( let state = model.state in ( let
{ model state = model.state
| state = { state in
| route = page { model | state =
-- Reinitialize selectionList with url change { state | route = page , selectedItems = Just Set.empty}
, 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 -> []
}
} }
, case page of , case page of
GroupLoot -> Cmd.none GroupLoot -> Cmd.none
@@ -224,8 +222,13 @@ update msg model =
, Cmd.none , Cmd.none
) )
LootViewItemSwitched idx -> LootViewItemSwitched id ->
( switchSelectionState idx model, Cmd.none ) let
state = model.state
in
( { model | state =
{ state | selectedItems = Debug.log "new selection" (switchSelectionState id state.selectedItems) }}
, Cmd.none )
-- ERRORS -- ERRORS
@@ -246,20 +249,14 @@ printError error =
-- STATE Utils -- STATE Utils
switchBooleanAt idx = switchSelectionState : Int -> Maybe Selection -> Maybe Selection
(\i value -> switchSelectionState id selection =
if i == idx then case selection of
not value Just s ->
else Just (case Set.member id s of
value True -> Set.remove id s
) False -> Set.insert id s)
Nothing -> Debug.log "ignore switchSelectionState" Nothing
switchSelectionState idx model =
let
state = model.state
selection = model.state.selectedItems
in
{ model | state = { state | selectedItems = List.indexedMap (switchBooleanAt idx) selection } }
-- SUBSCRIPTIONS -- SUBSCRIPTIONS
-- --
@@ -283,17 +280,17 @@ view model =
PlayerChest -> PlayerChest ->
[ p [class "heading"] [text "Mon Coffre"] [ p [class "heading"] [text "Mon Coffre"]
, viewSearchBar , viewSearchBar
, viewLoot (Maybe.withDefault [] model.loot) , viewLoot (Maybe.withDefault [] model.loot) model.state.selectedItems
] ]
GroupLoot -> GroupLoot ->
[ p [] [text "Coffre de groupe"] [ p [] [text "Coffre de groupe"]
, viewLoot (Maybe.withDefault [] model.groupLoot) , viewLoot (Maybe.withDefault [] model.groupLoot) model.state.selectedItems
] ]
Merchant -> Merchant ->
[ p [] [text "Acheter des objets"] [ p [] [text "Acheter des objets"]
, viewLoot (Maybe.withDefault [] model.merchantItems) , viewLoot (Maybe.withDefault [] model.merchantItems) model.state.selectedItems
] ]
NewLoot -> NewLoot ->
@@ -306,26 +303,37 @@ view model =
-- LOOT Views -- LOOT Views
viewLoot : Loot -> Html Msg isSelected id selection =
viewLoot items = Set.member id selection
viewLoot : Loot -> Maybe Selection -> Html Msg
viewLoot items selection =
table [ class "table is-fullwidth is-striped"] table [ class "table is-fullwidth is-striped"]
([ thead [class "table-header"] ([ thead [class "table-header"]
[ th [] [text "Nom"] ] [ 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) ] ] tr [ classList [ ("is-selected", selected) ] ]
[ td [] [ td []
[ label [ class "level checkbox", onClick (LootViewItemSwitched idx) ] [ label [ class "level checkbox" ]
[ div [ class "level-left" ] [ div [ class "level-left" ]
[ p [class "level-item"] [ text item.name ] [ p [class "level-item"] [ text item.name ]
] ]
, div [ class "level-right" ] , div [ class "level-right" ]
[ p [class "level-item"] [ text (String.fromInt item.base_price ++ "po") ] [ 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)] []
] ]
] ]
] ]