ToggleButton.purs 3 KB
Newer Older
James Laver's avatar
James Laver committed
1 2 3 4
module Gargantext.Components.GraphExplorer.ToggleButton
  ( Props, toggleButton, toggleButtonCpt
  , controlsToggleButton
  , edgesToggleButton
5
  , sidebarToggleButton
6
  , pauseForceAtlasButton
7
  , treeToggleButton
James Laver's avatar
James Laver committed
8 9
  ) where

10
import Prelude
11 12

import Data.Maybe (Maybe(..))
13
import Data.Tuple (snd)
14
import Data.Tuple.Nested ((/\))
15
import Effect (Effect)
James Laver's avatar
James Laver committed
16
import Reactix as R
17
import Reactix.DOM.HTML as H
James Laver's avatar
James Laver committed
18

19
import Gargantext.Hooks.Sigmax as Sigmax
20
import Gargantext.Hooks.Sigmax.Sigma as Sigma
21 22 23 24 25

type Props = (
    state :: R.State Boolean
  , onMessage :: String
  , offMessage :: String
26
  , onClick :: forall e. e -> Effect Unit
27
  )
James Laver's avatar
James Laver committed
28 29 30

toggleButton :: Record Props -> R.Element
toggleButton props = R.createElement toggleButtonCpt props []
31

James Laver's avatar
James Laver committed
32 33 34
toggleButtonCpt :: R.Component Props
toggleButtonCpt = R.hooksComponent "ToggleButton" cpt
  where
35 36
    cpt {state, onMessage, offMessage, onClick} _ = do
      let (toggled /\ _) = state
James Laver's avatar
James Laver committed
37
      pure $
38 39 40
        H.span {}
          [
            H.button
41
              { className: "btn btn-primary", on: {click: onClick} }
42 43
              [ H.text (text onMessage offMessage toggled) ]
          ]
James Laver's avatar
James Laver committed
44 45 46 47 48
    text on _off true = on
    text _on off false = off

controlsToggleButton :: R.State Boolean -> R.Element
controlsToggleButton state =
49 50 51 52
  toggleButton {
      state: state
    , onMessage: "Hide Controls"
    , offMessage: "Show Controls"
53
    , onClick: \_ -> snd state not
54
    }
James Laver's avatar
James Laver committed
55

56 57
edgesToggleButton :: R.Ref (Maybe Sigmax.Sigma) -> R.State Boolean -> R.Element
edgesToggleButton sigmaRef state =
58 59 60 61
  toggleButton {
      state: state
    , onMessage: "Hide Edges"
    , offMessage: "Show Edges"
62 63 64 65 66 67
    , onClick: \_ -> do
      let mSigma = Sigmax.readSigma <$> R.readRef sigmaRef
      let (toggled /\ setToggled) = state
      case mSigma of
        Just (Just s) -> do
          let settings = {
68 69 70
                drawEdges: not toggled
              , drawEdgeLabels: not toggled
              , hideEdgesOnMove: toggled
71 72 73 74
            }
          Sigma.setSettings s settings
        _             -> pure unit
      setToggled not
75 76 77
    }

pauseForceAtlasButton :: R.Ref (Maybe Sigmax.Sigma) -> R.State Boolean -> R.Element
78 79 80 81 82 83 84
pauseForceAtlasButton sigmaRef state =
  toggleButton {
      state: state
    , onMessage: "Pause Force Atlas"
    , offMessage: "Start Force Atlas"
    , onClick: \_ -> do
      let mSigma = Sigmax.readSigma <$> R.readRef sigmaRef
85
      let (toggled /\ setToggled) = state
86 87 88 89 90 91 92 93
      case mSigma of
        Just (Just s) -> if toggled then
            Sigma.stopForceAtlas2 s
          else
            Sigma.restartForceAtlas2 s
        _             -> pure unit
      setToggled not
    }
James Laver's avatar
James Laver committed
94

95 96
treeToggleButton :: R.State Boolean -> R.Element
treeToggleButton state =
97 98 99 100
  toggleButton {
      state: state
    , onMessage: "Hide Tree"
    , offMessage: "Show Tree"
101
    , onClick: \_ -> snd state not
102
    }
103 104 105

sidebarToggleButton :: R.State Boolean -> R.Element
sidebarToggleButton state =
106 107 108 109
  toggleButton {
      state: state
    , onMessage: "Hide Sidebar"
    , offMessage: "Show Sidebar"
110
    , onClick: \_ -> snd state not
111
    }