module View.Timer ( timerView ) where import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) import Time exposing (Time) import Signal import Maybe import List import String import Model.Model exposing (..) import Model.Timer exposing (..) import Model.Edition.Edition exposing (..) import Model.Edition.NameEdition exposing (..) import Model.Edition.TimeEdition exposing (..) import Model.TimerState exposing (..) import Model.Id exposing (..) import Update.Update exposing (..) import Update.UpdateTimer exposing (..) import View.ActivatedClasses exposing (..) import Utils.Maybe exposing (..) timerView : Model -> (Id, Timer) -> Html timerView model (id, timer) = div [ class "timer" ] [ renderMaybeEdition model id Name (nameBlockReadOnly id timer) (nameBlockEdition id timer) , renderMaybeEdition model id Time (timeBlockReadOnly id timer) (timeBlockEdition timer) , playPauseBlock (id, timer) , stopBlock (id, timer) , removeBlock (id, timer) ] nameBlockReadOnly : Id -> Timer -> Html nameBlockReadOnly id timer = div [ class "name block" , onClick actions.address (Edit id Name) ] [ text (timerName id timer) ] nameBlockEdition : Id -> Timer -> Edition -> Html nameBlockEdition id timer edition = div [ [ (True, "name block edition") , (isEmpty edition, "empty") ] |> activatedClasses , onClick actions.address NoOp ] [ if isEmpty edition then text (timerName id timer) else edition.chars |> renderNameEdition |> flip String.append "_" |> text ] timerName : Id -> Timer -> String timerName id = Maybe.withDefault ("Timer " ++ toString id) << .name timeBlockReadOnly : Id -> Timer -> Html timeBlockReadOnly id timer = div [ [ (True, "time block") , (timer.state == Ringing, "isRinging") , (timer.state == Running, "isRunning") ] |> activatedClasses , if timer.state == Ringing then onClick actions.address (UpdateTimer id Stop) else onClick actions.address (Edit id Time) ] [ timeWithProgressBar timer ] timeBlockEdition : Timer -> Edition -> Html timeBlockEdition timer edition = div [ [ (True, "time block edition") , (isEmpty edition, "empty") ] |> activatedClasses , onClick actions.address NoOp ] [ if isEmpty edition then timeWithProgressBar timer else text (editionView edition.chars) ] editionView : List Char -> String editionView numbers = let (minutes, seconds) = toMinutesAndSeconds numbers in minutes ++ " : " ++ seconds timeView : Time -> String timeView time = let totalSeconds = truncate (time / 1000) totalMinutes = totalSeconds // 60 restSeconds = totalSeconds `rem` 60 in (String.padLeft 2 '0' (toString totalMinutes)) ++ " : " ++ (String.padLeft 2 '0' (toString restSeconds)) timeWithProgressBar : Timer -> Html timeWithProgressBar timer = div [] [ span [ class "progressBar" , let width = 1 - timer.currentTime / (initTime timer.initialTime) |> (*) 198 |> round |> toString |> flip String.append "px" in style [ ("width", width) ] ] [] , span [ class "text" ] [ text (timeView timer.currentTime) ] ] playPauseBlock : (Id, Timer) -> Html playPauseBlock (id, timer) = button [ class <| "playPause block" , onClick actions.address (UpdateTimer id ToggleRunning) ] [ let icon = if timer.state == Running then "fa-pause" else "fa-play" in i [ class <| "fa fa-fw " ++ icon ] [] ] stopBlock : (Id, Timer) -> Html stopBlock (id, timer) = button [ class <| "stop block" , onClick actions.address (UpdateTimer id Stop) ] [ i [ class "fa fa-fw fa-stop" ] [] ] removeBlock : (Id, Timer) -> Html removeBlock (id, timer) = button [ class <| "remove block" , onClick actions.address (RemoveTimer id) ] [ i [ class "fa fa-fw fa-remove" ] [] ] renderMaybeEdition : Model -> Id -> Kind -> Html -> (Edition -> Html) -> Html renderMaybeEdition model id kind readOnlyView editionView = let maybeEdition = filterMaybe (\edition -> edition.id == id) model.edition in case maybeEdition of Just edition -> if edition.kind == kind then editionView edition else readOnlyView Nothing -> readOnlyView