Themes.purs 3.03 KB
Newer Older
1 2
module Gargantext.Components.Themes where

3 4
import Gargantext.Prelude

5
import Data.Array as A
6
import Data.Eq.Generic (genericEq)
7
import Data.Generic.Rep (class Generic)
8
import Data.Maybe (Maybe(..), maybe)
9
import Effect (Effect)
10 11
import FFI.Simple ((.=))
import Gargantext.Utils.Reactix (getElementById, (~~))
12
import Gargantext.Utils.Reactix as R2
13 14
import Reactix as R
import Reactix.DOM.HTML as H
15
import Toestand as T
16

17 18
here :: R2.Here
here = R2.here "Gargantext.Components.Themes"
19 20 21 22

stylesheetElId :: String
stylesheetElId = "bootstrap-css"

23 24
newtype Theme = Theme { location :: String
                      , name :: String }
25 26
derive instance Generic Theme _
instance Eq Theme where
27
  eq = genericEq
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

themeName :: Theme -> String
themeName (Theme { name }) = name

defaultTheme :: Theme
defaultTheme = Theme { name: "default"
                     , location: "styles/bootstrap-default.css" }

greysonTheme :: Theme
greysonTheme = Theme { name: "greyson"
                     , location: "styles/bootstrap-greyson.css" }

monotonyTheme :: Theme
monotonyTheme = Theme { name: "monotony"
                      , location: "styles/bootstrap-monotony.css" }

44 45 46 47 48
herbieTheme :: Theme
herbieTheme = Theme { name: "herbie"
                      , location: "styles/bootstrap-herbie.css" }

darksterTheme :: Theme
49
darksterTheme = Theme { name: "darkster"
50 51
                      , location: "styles/bootstrap-darkster.css" }

52
allThemes :: Array Theme
53
allThemes = [ defaultTheme, greysonTheme, monotonyTheme, herbieTheme, darksterTheme]
54 55 56 57 58 59 60 61 62 63

switchTheme :: Theme -> Effect Unit
switchTheme (Theme { location }) = do
  mEl <- R2.getElementById stylesheetElId
  case mEl of
    Nothing -> pure unit
    Just el -> do
      _ <- pure $ (el .= "href") location
      pure unit

64 65
markThemeToDOMTree :: Theme -> Effect Unit
markThemeToDOMTree (Theme { name }) = do
66 67 68
  let setTheme el = pure $ (el ~~ "setAttribute") [ "data-theme", name ]
  getElementById "app"    >>= maybe R.nothing (setTheme)
  getElementById "portal" >>= maybe R.nothing (setTheme)
69 70


71
type ThemeSwitcherProps = (
72
    theme  :: T.Box Theme
73 74 75 76 77 78 79
  , themes :: Array Theme
  )

themeSwitcher :: R2.Component ThemeSwitcherProps
themeSwitcher = R.createElement themeSwitcherCpt

themeSwitcherCpt :: R.Component ThemeSwitcherProps
80
themeSwitcherCpt = here.component "themeSwitcher" cpt
81 82
  where
    cpt { theme, themes } _ = do
83
      currentTheme <- T.useLive T.unequal theme
84 85 86 87

      let option (Theme { name }) = H.option { value: name } [ H.text name ]
      let options = map option themes

88
      R.useEffectOnce' $ markThemeToDOMTree currentTheme
89

90
      pure $ R2.select { className: "form-control"
91 92
                       , defaultValue: themeName currentTheme
                       , on: { change: onChange theme } } options
93
      where
94
        onChange box e = do
95 96 97 98 99 100 101
          let value = R.unsafeEventValue e
          let mTheme = A.head $ A.filter (\(Theme { name }) -> value == name) themes

          case mTheme of
            Nothing -> pure unit
            Just t  -> do
              switchTheme t
102
              markThemeToDOMTree t
103
              T.write_ t box