Button.purs 3.91 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 46 47 48 49 50 51 52 53
  where
    cpt {onClick, text} _ = do
      pure $
        H.span {}
          [
            H.button
              { className: "btn btn-primary", on: {click: onClick} }
              [ H.text text ]
          ]

54
centerButton :: R.Ref Sigmax.Sigma -> R.Element
55 56
centerButton sigmaRef = simpleButton {
    onClick: \_ -> do
57 58 59
      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}
60 61
  , text: "Center"
  }
62 63


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


cameraButton :: Record CameraButtonProps -> R.Element
cameraButton { id
75
             , hyperdataGraph: GET.HyperdataGraph { graph: GET.GraphData hyperdataGraph }
76
             , session
77 78
             , sigmaRef
             , treeReload } = simpleButton {
79 80 81 82
    onClick: \_ -> do
      let sigma = R.readRef sigmaRef
      Sigmax.dependOnSigma sigma "[cameraButton] sigma: Nothing" $ \s -> do
        screen <- Sigma.takeScreenshot s
83 84 85 86 87 88 89 90 91 92
        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 ]
93 94
        edges <- Sigmax.getEdges s
        nodes <- Sigmax.getNodes s
95 96 97 98 99 100 101 102 103 104 105 106
        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 }
107
        launchAff_ $ do
108
          clonedGraphId <- cloneGraph { id, hyperdataGraph, session }
109 110 111
          ret <- uploadArbitraryDataURL session clonedGraphId (Just $ nowStr <> "-" <> "screenshot.png") screen
          liftEffect $ treeReload unit
          pure ret
112 113
  , text: "Screenshot"
  }