This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

Aui.Labels

Functions to present AUI labels.

Presentation

label : Config a -> List (Html a) -> Html a

Show a label with a given configuration and content .

label (baseConfig
        |> withNavigate "http://elm-lang.org/"
        |> withRemove RemovedLabel)
    [ text "Please remove or click me!" ]

Actions

type Action a = Navigate String | Trigger a

Actions that can be bound to a label

navigate : String -> Action a

Action to navigate to an url

trigger : a -> Action a

Action to trigger an action on click

Types

type Config a = Config (Maybe a) (Maybe (Action a))

Configuration to present a label.

Utility

baseConfig : Config a

Base configuration to show a label. No actions are bound by default.

onClick : Action a -> Config a -> Config a

Bind action to click on label

onRemove : a -> Config a -> Config a

Allow the label to be removed triggering the given message.

module Aui.Labels exposing (label, Action, navigate, trigger, Config, baseConfig, onClick, onRemove)

{-| Functions to present AUI labels.


# Presentation

@docs label


# Actions

@docs Action, navigate, trigger


# Types

@docs Config


# Utility

@docs baseConfig, onClick, onRemove

-}

import Html exposing (Html, span, a)
import Html.Attributes exposing (class, href, style)
import Html.Events


{-| Actions that can be bound to a label
-}
type Action a
    = Navigate String
    | Trigger a


{-| Action to navigate to an url
-}
navigate : String -> Action a
navigate url =
    Navigate url


{-| Action to trigger an action on click
-}
trigger : a -> Action a
trigger x =
    Trigger x


{-| Configuration to present a label.
-}
type Config a
    = Config (Maybe a) (Maybe (Action a))


{-| Base configuration to show a label. No actions are bound by default.
-}
baseConfig : Config a
baseConfig =
    Config Nothing Nothing


{-| Bind action to click on label
-}
onClick : Action a -> Config a -> Config a
onClick x (Config onRemove _) =
    Config onRemove (Just x)


{-| Allow the label to be removed triggering the given message.
-}
onRemove : a -> Config a -> Config a
onRemove x (Config _ onClick) =
    Config (Just x) onClick


{-| Show a label with a given configuration and content .

    label (baseConfig
            |> withNavigate "http://elm-lang.org/"
            |> withRemove RemovedLabel)
        [ text "Please remove or click me!" ]

-}
label : Config a -> List (Html a) -> Html a
label (Config onRemove onClick) inner =
    let
        classString =
            if onRemove == Nothing then
                "aui-label"
            else
                "aui-label aui-label-closeable aui-label-split"

        inner_ =
            case onRemove of
                Nothing ->
                    inner

                Just action ->
                    [ span [ class "aui-label-split-main" ] inner
                    , labelRemoveSpan action
                    ]

        actionAttrs_ =
            actionAttrs onClick

        elem =
            if onClick == Nothing then
                span
            else
                a
    in
        elem (class classString :: actionAttrs_)
            inner_


labelRemoveSpan : a -> Html a
labelRemoveSpan x =
    span [ class "aui-label-split-close", Html.Events.onClick x ]
        [ span [ class "aui-icon aui-icon-close" ] []
        ]


actionAttrs : Maybe (Action a) -> List (Html.Attribute a)
actionAttrs onClick =
    case onClick of
        Just action ->
            case action of
                Navigate x ->
                    [ href x, style [ ( "cursor", "pointer" ) ] ]

                Trigger a ->
                    [ Html.Events.onClick a, style [ ( "cursor", "pointer" ) ] ]

        Nothing ->
            []