Frame.purs 3.99 KB
Newer Older
1 2
module Gargantext.Components.Nodes.Frame where

Karen Konou's avatar
Karen Konou committed
3 4
import Gargantext.Prelude

James Laver's avatar
James Laver committed
5
import Data.Argonaut (decodeJson, (.:))
Karen Konou's avatar
Karen Konou committed
6
import Data.Argonaut as Argonaut
7
import Data.Eq.Generic (genericEq)
8
import Data.Generic.Rep (class Generic)
9
import Data.Maybe (Maybe(..))
10 11
import Data.Newtype (class Newtype)
import Data.Show.Generic (genericShow)
James Laver's avatar
James Laver committed
12
import Effect.Aff (Aff)
13 14 15 16 17
import Reactix as R
import Reactix.DOM.HTML as H
import Simple.JSON as JSON
import Toestand as T

18
import Gargantext.Components.FolderView as FV
Karen Konou's avatar
Karen Konou committed
19
import Gargantext.Components.Node (NodePoly(..))
20 21 22
import Gargantext.Hooks.Loader (useLoader)
import Gargantext.Routes (SessionRoute(NodeAPI))
import Gargantext.Sessions (Session, get, sessionId)
23
import Gargantext.Types (NodeType(..))
24
import Gargantext.Utils.Reactix as R2
25
import Gargantext.Utils.Toestand as T2
26

James Laver's avatar
James Laver committed
27 28
here :: R2.Here
here = R2.here "Gargantext.Components.Nodes.Frame"
29

30
newtype Hyperdata = Hyperdata { base :: String, frame_id :: String }
31
derive instance Generic Hyperdata _
32 33 34 35 36
derive instance Newtype Hyperdata _
instance Eq Hyperdata where eq = genericEq
instance Show Hyperdata where show = genericShow
derive newtype instance JSON.ReadForeign Hyperdata
derive newtype instance JSON.WriteForeign Hyperdata
37 38

type Props =
39 40
  ( nodeId   :: Int
  , nodeType :: NodeType
41
  , session  :: Session
42 43 44
  )

type KeyProps =
James Laver's avatar
James Laver committed
45
  ( key      :: String
46
  | Props
47 48
  )

James Laver's avatar
James Laver committed
49
frameLayout :: R2.Leaf Props
50 51
frameLayout props = R.createElement frameLayoutCpt props []

52
frameLayoutCpt :: R.Component Props
James Laver's avatar
James Laver committed
53
frameLayoutCpt = here.component "frameLayout" cpt where
54 55 56 57
  cpt { nodeId, nodeType, session } _ = do
    pure $ frameLayoutWithKey { key, nodeId, nodeType, session }
      where
        key = show (sessionId session) <> "-" <> show nodeId
58

James Laver's avatar
James Laver committed
59
frameLayoutWithKey :: R2.Leaf KeyProps
60 61 62
frameLayoutWithKey props = R.createElement frameLayoutWithKeyCpt props []

frameLayoutWithKeyCpt :: R.Component KeyProps
James Laver's avatar
James Laver committed
63 64
frameLayoutWithKeyCpt = here.component "frameLayoutWithKey" cpt where
  cpt { nodeId, session, nodeType} _ = do
65 66 67
    reload <- T.useBox T2.newReload
    reload' <- T.useLive T.unequal reload
    useLoader {nodeId, reload: reload', session} loadframeWithReload $
James Laver's avatar
James Laver committed
68
      \frame -> frameLayoutView {frame, nodeId, reload, session, nodeType}
69 70

type ViewProps =
71 72
  ( frame    :: NodePoly Hyperdata
  , reload   :: T2.ReloadS
James Laver's avatar
James Laver committed
73 74 75
  , nodeId   :: Int
  , session  :: Session
  , nodeType :: NodeType
76 77 78
  )

type Base = String
James Laver's avatar
James Laver committed
79

80 81
type FrameId = String

82
hframeUrl :: NodeType -> Base -> FrameId -> String
James Laver's avatar
James Laver committed
83
hframeUrl NodeFrameNotebook _ frame_id = frame_id  -- Temp fix : frame_id is currently the whole url created
84 85
hframeUrl NodeFrameCalc  base frame_id = base <> "/" <> frame_id
hframeUrl NodeFrameVisio base frame_id = base <> "/" <> frame_id
James Laver's avatar
James Laver committed
86
hframeUrl _ base frame_id = base <> "/" <> frame_id <> "?view" -- "?both"
87 88 89 90 91

frameLayoutView :: Record ViewProps -> R.Element
frameLayoutView props = R.createElement frameLayoutViewCpt props []

frameLayoutViewCpt :: R.Component ViewProps
James Laver's avatar
James Laver committed
92
frameLayoutViewCpt = here.component "frameLayoutView" cpt
93
  where
James Laver's avatar
James Laver committed
94
    cpt { frame: (NodePoly { hyperdata: Hyperdata { base, frame_id }})
95 96 97 98
        , nodeId
        , nodeType
        , reload
        , session } _ =
99 100
      pure $ H.div{} [
        FV.backButton
Karen Konou's avatar
Karen Konou committed
101
      , FV.homeButton
102 103
      , H.div { className : "frame"
              , rows: "100%,*" }
104 105 106 107 108
              [ -- H.script { src: "https://visio.gargantext.org/external_api.js"} [],
               H.iframe { src: hframeUrl nodeType base frame_id
                         , width: "100%"
                         , height: "100%"
                         } []
109 110
          ]
      ]
111

112 113
type LoadProps   = ( nodeId  :: Int
                   , session :: Session )
James Laver's avatar
James Laver committed
114

115 116 117
type ReloadProps = ( nodeId  :: Int
                   , reload :: T2.Reload
                   , session :: Session )
118 119

loadframe' :: Record LoadProps -> Aff (NodePoly Hyperdata)
James Laver's avatar
James Laver committed
120
loadframe' { nodeId, session } = get session $ NodeAPI Node (Just nodeId) ""
121 122

-- Just to make reloading effective
James Laver's avatar
James Laver committed
123 124
loadframeWithReload :: Record ReloadProps -> Aff (NodePoly Hyperdata)
loadframeWithReload { nodeId, session } = loadframe' { nodeId, session }