getto-systems/elm-sort/1.0.0

Getto.Sort

sort utilities for html table

import Html as H exposing ( Html )
import Html.Attributes as A
import Html.Events as E


type Msg
  = SortBy Sort.Value


sort = "id" |> Sort.by


sort |> Sort.stateOf "id"   |> link "id"
sort |> Sort.stateOf "name" |> link "name"


link : String -> Sort.State -> Html Msg
link text {current,next} =
  H.a
    ( List.append
      [ "#" |> A.href
      , next |> SortBy |> E.onClick
      ]
      ( case current of
        Nothing -> []
        Just _  -> [ "is-active" |> A.class ]
      )
    )
    [ text |> H.text
    , " "  |> H.text
    , case current of
        Nothing        -> ""     |> H.text
        Just Sort.Up   -> "up"   |> H.text
        Just Sort.Down -> "down" |> H.text
    ]

Definition

type Value

sort definition

= Up
| Down

sort direction

type alias State =
{ current : Maybe Direction
, next : Value
}

current sort direction and next sort definition

Construction

by : String -> Value

create Value that sort by "column"

State

stateOf : String -> Value -> State

get current sort state from value and "column"

Encode/Decode

-> { column : String
, direction : String
}

encode value to string

"id" |> Sort.by |> Sort.toString
-- { column = "id", direction = "down" }
{ column : Maybe String
, direction : Maybe String
}
-> Maybe Value

decode value from string

{ column    = Just "id"
, direction = Just "up"
}
|> Sort.fromString