1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
-- Column selection dropdown for tables. Assumes that the currently selected
-- columns are in the query string as the 'c' parameter, e.g.:
--
-- ?c=column_id&c=modified&...
--
-- Accepts a list of columns from Perl, e.g.:
--
-- [
-- [ 'column_id', 'Column Label' ],
-- [ 'modified', 'Date modified' ],
-- ...
-- ]
module ColSelect exposing (main)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Browser
import Browser.Navigation exposing (load)
import Set
import Erl -- elm/url can't extract a full list of query parameters and hence can't be used to modify a parameter without removing all others.
import Lib.DropDown as DD
import Lib.Api as Api
import Lib.Html exposing (..)
main : Program (String, Columns) Model Msg
main = Browser.element
{ init = \e -> (init e, Cmd.none)
, view = view
, update = update
, subscriptions = \model -> DD.sub model.dd
}
type alias Columns = List (String, String)
type alias Model =
{ cols : Columns
, url : Erl.Url -- Without the "c" parameter
, sel : Set.Set String
, dd : DD.Config Msg
}
init : (String, Columns) -> Model
init (u, c) =
{ cols = c
, url = Erl.removeQuery "c" <| Erl.parse u
, sel = Set.fromList <| Erl.getQueryValuesForKey "c" <| Erl.parse u
, dd = DD.init "colselect" Open
}
type Msg
= Open Bool
| Toggle String Bool
| Update
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Open b -> ({ model | dd = DD.toggle model.dd b }, Cmd.none)
Toggle s b -> ({ model | sel = if b then Set.insert s model.sel else Set.remove s model.sel }, Cmd.none)
Update -> (model, load <| Erl.toString <| List.foldl (\s u -> Erl.addQuery "c" s u) model.url <| Set.toList model.sel)
view : Model -> Html Msg
view model =
let item (cid, cname) = li [ ] [ linkRadio (Set.member cid model.sel) (Toggle cid) [ text cname ] ]
in
DD.view model.dd Api.Normal
(text "Select columns")
(\_ -> [ ul []
<| List.map item model.cols
++ [ li [ ] [ input [ type_ "button", class "submit", value "update", onClick Update ] [] ] ]
])
|