Button.purs 3.9 KB
Newer Older
1
module Gargantext.Components.GraphExplorer.Button
2
  ( centerButton
3 4
  , Props
  , simpleButton
5
  , cameraButton
6 7 8
  ) where

import Prelude
9

10
import Data.Enum (fromEnum)
11
import Data.Maybe (Maybe(..))
12 13 14
import Data.DateTime as DDT
import Data.DateTime.Instant as DDI
import Data.String as DS
15
import DOM.Simple.Console (log2)
16
import Effect (Effect)
17
import Effect.Aff (launchAff_)
18
import Effect.Class (liftEffect)
19
import Effect.Now as EN
20 21 22
import Reactix as R
import Reactix.DOM.HTML as H

23
import Gargantext.Components.Forest.Tree.Node.Action.Upload (uploadArbitraryDataURL)
24 25 26
import Gargantext.Components.GraphExplorer.API (cloneGraph)
import Gargantext.Components.GraphExplorer.Types as GET
import Gargantext.Components.GraphExplorer.Utils as GEU
27 28
import Gargantext.Hooks.Sigmax as Sigmax
import Gargantext.Hooks.Sigmax.Sigma as Sigma
29
import Gargantext.Sessions (Session)
30 31 32
import Gargantext.Utils.Reactix as R2

thisModule = "Gargantext.Components.GraphExplorer.Button"
33 34 35 36 37 38 39 40 41 42

type Props = (
    onClick :: forall e. e -> Effect Unit
  , text :: String
  )

simpleButton :: Record Props -> R.Element
simpleButton props = R.createElement simpleButtonCpt props []

simpleButtonCpt :: R.Component Props
43
simpleButtonCpt = R.hooksComponentWithModule thisModule "simpleButton" cpt
44 45
  where
    cpt {onClick, text} _ = do
46 47 48
      pure $ H.button { className: "btn btn-outline-primary"
                      , on: {click: onClick}
                      } [ R2.small {} [ H.text text ] ]
49

50
centerButton :: R.Ref Sigmax.Sigma -> R.Element
51 52
centerButton sigmaRef = simpleButton {
    onClick: \_ -> do
53 54 55
      let sigma = R.readRef sigmaRef
      Sigmax.dependOnSigma sigma "[centerButton] sigma: Nothing" $ \s ->
        Sigma.goToAllCameras s {x: 0.0, y: 0.0, ratio: 1.0, angle: 0.0}
56 57
  , text: "Center"
  }
58 59


60 61
type CameraButtonProps = (
    id :: Int
62
  , hyperdataGraph :: GET.HyperdataGraph
63 64
  , session :: Session
  , sigmaRef :: R.Ref Sigmax.Sigma
65
  , treeReload :: Unit -> Effect Unit
66 67 68 69 70
  )


cameraButton :: Record CameraButtonProps -> R.Element
cameraButton { id
71
             , hyperdataGraph: GET.HyperdataGraph { graph: GET.GraphData hyperdataGraph }
72
             , session
73 74
             , sigmaRef
             , treeReload } = simpleButton {
75 76 77 78
    onClick: \_ -> do
      let sigma = R.readRef sigmaRef
      Sigmax.dependOnSigma sigma "[cameraButton] sigma: Nothing" $ \s -> do
        screen <- Sigma.takeScreenshot s
79 80 81 82 83 84 85 86 87 88
        now <- EN.now
        let nowdt = DDI.toDateTime now
            nowd = DDT.date nowdt
            nowt = DDT.time nowdt
            nowStr = DS.joinWith "-" [ show $ fromEnum $ DDT.year nowd
                                     , show $ fromEnum $ DDT.month nowd
                                     , show $ fromEnum $ DDT.day nowd
                                     , show $ fromEnum $ DDT.hour nowt
                                     , show $ fromEnum $ DDT.minute nowt
                                     , show $ fromEnum $ DDT.second nowt ]
89 90
        edges <- Sigmax.getEdges s
        nodes <- Sigmax.getNodes s
91 92 93 94 95 96 97 98 99 100 101 102
        let graphData = GET.GraphData $ hyperdataGraph { edges = map GEU.stEdgeToGET edges
                                                       , nodes = map GEU.stNodeToGET nodes }
        let cameras = map Sigma.toCamera $ Sigma.cameras s
        let camera = case cameras of
              [c] -> GET.Camera { ratio: c.ratio
                                , x: c.x
                                , y: c.y }
              _   -> GET.Camera { ratio: 1.0
                               , x: 0.0
                               , y: 0.0 }
        let hyperdataGraph = GET.HyperdataGraph { graph: graphData
                                                , mCamera: Just camera }
103
        launchAff_ $ do
104
          clonedGraphId <- cloneGraph { id, hyperdataGraph, session }
105 106 107
          ret <- uploadArbitraryDataURL session clonedGraphId (Just $ nowStr <> "-" <> "screenshot.png") screen
          liftEffect $ treeReload unit
          pure ret
108 109
  , text: "Screenshot"
  }