<commit_before>module Dreamwriter.View.LeftSidebar (view) where

import Dreamwriter.Action (..)
import Dreamwriter.Model (..)
import Dreamwriter.Doc (..)
import Dreamwriter.View.Outline as Outline
import Dreamwriter.View.OpenMenu as OpenMenu

import Regex (..)
import Html (..)
import Html.Attributes (..)
import Html.Events (..)
import Html.Tags (..)
import Maybe

-- TODO remove this once it's in elm-html
key = attr "key"

-- Replace illegal filename characters with underscores
illegalFilenameCharMatcher = regex "[/\\<>?|\":*]"

legalizeFilename : String -> String
legalizeFilename = replace All illegalFilenameCharMatcher (\_ -> "_")

downloadContentType = "text/plain;charset=UTF-8"

view : AppState -> Html
view state =
  case state.currentDoc of
    Nothing ->
      span [] []
    Just currentDoc ->
      let {headerNodes, bodyNodes} = case state.showOpenMenu of
        True  ->
          { headerNodes = [
              span [key "#cancel-open", class "sidebar-header-control",
                onclick actions.handle (\_ -> ToggleOpenMenu False)] [text "cancel"]
            ]

          , bodyNodes = [OpenMenu.view state.docs currentDoc]
          }
        False ->
          { headerNodes = [
              span [key "#new-doc-button", class "sidebar-header-control",
                onclick newDocInput.handle (always ())] [text "new"],
              span [key "#open-doc-button", class "sidebar-header-control",
                onclick actions.handle (\_ -> ToggleOpenMenu True)] [text "open"]
            ]

          , bodyNodes = viewCurrentDoc currentDoc
          }
      in
        div [key "#left-sidebar-container", id "left-sidebar-container", class "sidebar"] [
          div [key "#left-sidebar-header", id "left-sidebar-header", class "sidebar-header"] headerNodes,
          div [key "#left-sidebar-body", id "left-sidebar-body"] bodyNodes
        ]

viewCurrentDoc : Doc -> [Html]
viewCurrentDoc currentDoc =
  let downloadOptions = { filename    = (legalizeFilename currentDoc.title) ++ ".html"
                        , contentType = downloadContentType
                        }
  in
    [
      div [key "#title", id "title"] [text currentDoc.title],
      div [key "#file-buttons", id "file-buttons"] [
        span [key "#download-button", class "file-button",
          onclick downloadInput.handle (always downloadOptions)] [text "download"],
        span [key "#stats-button", class "file-button"] [text "stats"]
      ],

      Outline.view currentDoc.chapters
    ]<commit_msg>Another way to repro the vdom bug.<commit_after>module Dreamwriter.View.LeftSidebar (view) where

import Dreamwriter.Action (..)
import Dreamwriter.Model (..)
import Dreamwriter.Doc (..)
import Dreamwriter.View.Outline as Outline
import Dreamwriter.View.OpenMenu as OpenMenu

import Regex (..)
import Html (..)
import Html.Attributes (..)
import Html.Events (..)
import Html.Tags (..)
import Maybe

-- TODO remove this once it's in elm-html
key = attr "key"

-- Replace illegal filename characters with underscores
illegalFilenameCharMatcher = regex "[/\\<>?|\":*]"

legalizeFilename : String -> String
legalizeFilename = replace All illegalFilenameCharMatcher (\_ -> "_")

downloadContentType = "text/plain;charset=UTF-8"

view : AppState -> Html
view state =
  case state.currentDoc of
    Nothing ->
      span [] []
    Just currentDoc ->
      let {headerNodes, bodyNodes} = case state.showOpenMenu of
        True  ->
          { headerNodes = [
              span [key "#cancel-open", class "sidebar-header-control",
                onclick actions.handle (\_ -> ToggleOpenMenu False)] [text "cancel"]
            ]

          , bodyNodes = OpenMenu.view state.docs currentDoc
          }
        False ->
          { headerNodes = [
              span [key "#new-doc-button", class "sidebar-header-control",
                onclick newDocInput.handle (always ())] [text "new"],
              span [key "#open-doc-button", class "sidebar-header-control",
                onclick actions.handle (\_ -> ToggleOpenMenu True)] [text "open"]
            ]

          , bodyNodes = viewCurrentDoc currentDoc
          }
      in
        div [key "#left-sidebar-container", id "left-sidebar-container", class "sidebar"] [
          div [key "#left-sidebar-header", id "left-sidebar-header", class "sidebar-header"] headerNodes,
          div [key "#left-sidebar-body", id "left-sidebar-body"] [bodyNodes]
        ]

viewCurrentDoc : Doc -> Html
viewCurrentDoc currentDoc =
  let downloadOptions = { filename    = (legalizeFilename currentDoc.title) ++ ".html"
                        , contentType = downloadContentType
                        }
  in
    div [key "#current-doc-view"] [
      div [key "#title", id "title"] [text currentDoc.title],
      div [key "#file-buttons", id "file-buttons"] [
        span [key "#download-button", class "file-button",
          onclick downloadInput.handle (always downloadOptions)] [text "download"],
        span [key "#stats-button", class "file-button"] [text "stats"]
      ],

      Outline.view currentDoc.chapters
    ]
{'fraction_non_alphanumeric': 0.13003339226085248, 'fraction_numerical': 0.0003928501276762915, 'mean_word_length': 2.485284052019165, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4309987', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1401, 'n_words': 381}