<commit_before>module Todo where
{-| TodoMVC implemented in Elm, using plain HTML and CSS for rendering.

This application is broken up into four distinct parts:

  1. Model  - a full definition of the application's state
  2. Update - a way to step the application state forward
  3. View   - a way to visualize our application state with HTML
  4. Inputs - the signals necessary to manage events

This clean division of concerns is a core part of Elm. You can read more about
this in the Pong tutorial: http://elm-lang.org/blog/Pong.elm

This program is not particularly large, so definitely see the following
document for notes on structuring more complex GUIs with Elm:
https://gist.github.com/evancz/2b2ba366cae1887fe621
-}

import String
import Html
import Html (..)
import Html.Events (..)
import Html.Optimize.RefEq as Ref
import Maybe
import Window

import Graphics.Input (..)
import Graphics.Input as Input
import Debug


---- MODEL ----

-- The full application state of our todo app.
type State =
    { tasks      : [Task]
    , field      : String
    , uid        : Int
    , visibility : String
    }

type Task =
    { description : String
    , completed   : Bool
    , editing     : Bool
    , id          : Int
    }

newTask : String -> Int -> Task
newTask desc id =
    { description = desc
    , completed = False 
    , editing = False
    , id = id
    }

emptyState : State
emptyState =
    { tasks = []
    , visibility = "all"
    , field = ""
    , uid = 0
    }


---- UPDATE ----

-- A description of the kinds of actions that can be performed on the state of
-- the application. See the following post for more info on this pattern and
-- some alternatives: https://gist.github.com/evancz/2b2ba366cae1887fe621
data Action
    = NoOp
    | UpdateField String
    | EditingTask Int Bool
    | UpdateTask Int String
    | Add
    | Delete Int
    | DeleteComplete
    | Check Int Bool
    | CheckAll Bool
    | ChangeVisibility String

-- How we step the state forward for any given action
step : Action -> State -> State
step action state =
    case action of
      NoOp -> state

      Add ->
          { state | uid <- state.uid + 1
                  , field <- ""
                  , tasks <- if String.isEmpty state.field
                               then state.tasks
                               else state.tasks ++ [newTask state.field state.uid]
          }

      UpdateField str ->
          { state | field <- str }

      EditingTask id isEditing ->
          let update t = if t.id == id then { t | editing <- isEditing } else t
          in  { state | tasks <- map update state.tasks }

      UpdateTask id task ->
          let update t = if t.id == id then { t | description <- task } else t
          in  { state | tasks <- map update state.tasks }

      Delete id ->
          { state | tasks <- filter (\t -> t.id /= id) state.tasks }

      DeleteComplete ->
          { state | tasks <- filter (not . .completed) state.tasks }

      Check id isCompleted ->
          let update t = if t.id == id then { t | completed <- isCompleted } else t
          in  { state | tasks <- map update state.tasks }

      CheckAll isCompleted ->
          let update t = { t | completed <- isCompleted } in
          { state | tasks <- map update state.tasks }

      ChangeVisibility visibility ->
          { state | visibility <- visibility }


---- VIEW ----

view : State -> Html
view state =
    node "div"
      [ "className" := "todomvc-wrapper" ]
      [ "visibility" := "hidden" ]
      [ node "section"
          [ "id" := "todoapp" ]
          []
          [ Ref.lazy taskEntry state.field
          , Ref.lazy2 taskList state.visibility state.tasks
          , Ref.lazy2 controls state.visibility state.tasks
          ]
      , infoFooter
      ]

onEnter : Handle a -> a -> EventListener
onEnter handle value =
    on "keydown" (when (\k -> k.keyCode == 13) getKeyboardEvent) handle (always value)

taskEntry : String -> Html
taskEntry value =
    node "header" 
      [ "id" := "header" ]
      []
      [ node "h1" [] [] [ text "todos" ]
      , eventNode "input"
          [ "id"          := "new-todo"
          , "placeholder" := "What needs to be done?"
          , "autofocus"   := "true"
          , "value"       := value
          , "name"        := "newTodo"
          ]
          []
          [ on "input" getValue actions.handle UpdateField
          , onEnter actions.handle Add
          ]
          []
      ]

taskList : String -> [Task] -> Html
taskList visibility tasks =
    let isVisible todo =
            case Debug.log "visibility" visibility of
              "completed" -> todo.completed
              "active" -> not todo.completed
              "all" -> True

        allCompleted = all .completed tasks
    in
    node "section"
      [ "id" := "main" ]
      [ "visibility" := if isEmpty tasks then "hidden" else "visible" ]<commit_msg>Get rid of debugging stuff<commit_after>module Todo where
{-| TodoMVC implemented in Elm, using plain HTML and CSS for rendering.

This application is broken up into four distinct parts:

  1. Model  - a full definition of the application's state
  2. Update - a way to step the application state forward
  3. View   - a way to visualize our application state with HTML
  4. Inputs - the signals necessary to manage events

This clean division of concerns is a core part of Elm. You can read more about
this in the Pong tutorial: http://elm-lang.org/blog/Pong.elm

This program is not particularly large, so definitely see the following
document for notes on structuring more complex GUIs with Elm:
https://gist.github.com/evancz/2b2ba366cae1887fe621
-}

import String
import Html
import Html (..)
import Html.Events (..)
import Html.Optimize.RefEq as Ref
import Maybe
import Window

import Graphics.Input (..)
import Graphics.Input as Input


---- MODEL ----

-- The full application state of our todo app.
type State =
    { tasks      : [Task]
    , field      : String
    , uid        : Int
    , visibility : String
    }

type Task =
    { description : String
    , completed   : Bool
    , editing     : Bool
    , id          : Int
    }

newTask : String -> Int -> Task
newTask desc id =
    { description = desc
    , completed = False 
    , editing = False
    , id = id
    }

emptyState : State
emptyState =
    { tasks = []
    , visibility = "all"
    , field = ""
    , uid = 0
    }


---- UPDATE ----

-- A description of the kinds of actions that can be performed on the state of
-- the application. See the following post for more info on this pattern and
-- some alternatives: https://gist.github.com/evancz/2b2ba366cae1887fe621
data Action
    = NoOp
    | UpdateField String
    | EditingTask Int Bool
    | UpdateTask Int String
    | Add
    | Delete Int
    | DeleteComplete
    | Check Int Bool
    | CheckAll Bool
    | ChangeVisibility String

-- How we step the state forward for any given action
step : Action -> State -> State
step action state =
    case action of
      NoOp -> state

      Add ->
          { state | uid <- state.uid + 1
                  , field <- ""
                  , tasks <- if String.isEmpty state.field
                               then state.tasks
                               else state.tasks ++ [newTask state.field state.uid]
          }

      UpdateField str ->
          { state | field <- str }

      EditingTask id isEditing ->
          let update t = if t.id == id then { t | editing <- isEditing } else t
          in  { state | tasks <- map update state.tasks }

      UpdateTask id task ->
          let update t = if t.id == id then { t | description <- task } else t
          in  { state | tasks <- map update state.tasks }

      Delete id ->
          { state | tasks <- filter (\t -> t.id /= id) state.tasks }

      DeleteComplete ->
          { state | tasks <- filter (not . .completed) state.tasks }

      Check id isCompleted ->
          let update t = if t.id == id then { t | completed <- isCompleted } else t
          in  { state | tasks <- map update state.tasks }

      CheckAll isCompleted ->
          let update t = { t | completed <- isCompleted } in
          { state | tasks <- map update state.tasks }

      ChangeVisibility visibility ->
          { state | visibility <- visibility }


---- VIEW ----

view : State -> Html
view state =
    node "div"
      [ "className" := "todomvc-wrapper" ]
      [ "visibility" := "hidden" ]
      [ node "section"
          [ "id" := "todoapp" ]
          []
          [ Ref.lazy taskEntry state.field
          , Ref.lazy2 taskList state.visibility state.tasks
          , Ref.lazy2 controls state.visibility state.tasks
          ]
      , infoFooter
      ]

onEnter : Handle a -> a -> EventListener
onEnter handle value =
    on "keydown" (when (\k -> k.keyCode == 13) getKeyboardEvent) handle (always value)

taskEntry : String -> Html
taskEntry value =
    node "header" 
      [ "id" := "header" ]
      []
      [ node "h1" [] [] [ text "todos" ]
      , eventNode "input"
          [ "id"          := "new-todo"
          , "placeholder" := "What needs to be done?"
          , "autofocus"   := "true"
          , "value"       := value
          , "name"        := "newTodo"
          ]
          []
          [ on "input" getValue actions.handle UpdateField
          , onEnter actions.handle Add
          ]
          []
      ]

taskList : String -> [Task] -> Html
taskList visibility tasks =
    let isVisible todo =
            case visibility of
              "completed" -> todo.completed
              "active" -> not todo.completed
              "all" -> True

        allCompleted = all .completed tasks
    in
    node "section"
      [ "id" := "main" ]
      [ "visibility" := if isEmpty tasks then "hidden" else "visible" ]
{'fraction_non_alphanumeric': 0.10196597738616685, 'fraction_numerical': 0.0071304879291025774, 'mean_word_length': 1.821264367816092, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 53, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4449257', 'n_tokens_mistral': 2872, 'n_tokens_neox': 2723, 'n_words': 1000}