Sigmajs.purs 3.88 KB
Newer Older
James Laver's avatar
James Laver committed
1
module Gargantext.Hooks.Sigmax.Sigmajs where
Sudhir Kumar's avatar
Sudhir Kumar committed
2 3

import Prelude
Abinaya Sudhir's avatar
Abinaya Sudhir committed
4

5
import Data.Nullable (Nullable)
6
import Effect (Effect)
7
import Effect.Uncurried (EffectFn1, runEffectFn1)
James Laver's avatar
James Laver committed
8
import React (ReactRef, SyntheticEventHandler)
9
import Record.Unsafe (unsafeGet)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
10
import Unsafe.Coerce (unsafeCoerce)
11
import Gargantext.Types (class Optional)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

foreign import data SigmaNode :: Type
foreign import data SigmaEdge :: Type
foreign import data SigmaSettings :: Type

type SigmaNodeEvent =
  { "data" ::
       { node :: {id :: Int, label :: String}
       , captor ::
            { clientX :: Number
            , clientY :: Number
            }
       }
  }

type SigmaEdgeEvent =
  { "data"::
       { node :: SigmaEdge
       , captor ::
            { clientX :: Number
32
            , clientY :: Number
Abinaya Sudhir's avatar
Abinaya Sudhir committed
33 34 35 36 37 38 39 40 41 42 43 44 45
            }
       }
  }

newtype SigmaGraphData = SigmaGraphData
  { nodes :: Array SigmaNode
  , edges :: Array SigmaEdge
  }

type SigmaNodeOptProps =
  ( x :: Number
  , y :: Number
  , size :: Number
James Laver's avatar
James Laver committed
46
  , color :: String
Abinaya Sudhir's avatar
Abinaya Sudhir committed
47 48 49 50 51 52 53 54 55
  , label :: String
  )

type SigmaNodeReqProps o =
  { id :: String
  | o
  }

type SigmaEdgeOptProps =
James Laver's avatar
James Laver committed
56
  ( color :: String
Abinaya Sudhir's avatar
Abinaya Sudhir committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  , label :: String
  , "type" :: String
  )

type SigmaEdgeReqProps o =
  { id :: String
  , source :: String
  , target :: String
  | o
  }

sigmaNode :: forall o. Optional o SigmaNodeOptProps => SigmaNodeReqProps o -> SigmaNode
sigmaNode = unsafeCoerce

sigmaEdge :: forall o. Optional o SigmaEdgeOptProps => SigmaEdgeReqProps o -> SigmaEdge
sigmaEdge = unsafeCoerce

-- se_ex01 :: SigmaEdge
-- se_ex01 = sigmaEdge { id : "", source : "", target : "", label : ""}

-- sn_ex01 :: SigmaNode
-- sn_ex01 = sigmaNode { id : "", label : ""}

foreign import data SigmaStyle :: Type
81

82 83 84 85 86 87 88 89 90 91 92
type CameraProps =
  ( x :: Number
  , y :: Number
  , ratio :: Number
  , angle :: Number
  )

foreign import data SigmaInstance' :: # Type
foreign import data CameraInstance' :: # Type
type SigmaInstance = { | SigmaInstance' }
type CameraInstance = { | CameraInstance' }
James Laver's avatar
James Laver committed
93

94 95 96 97 98
cameras :: SigmaInstance -> Array CameraInstance
cameras = unsafeGet "cameras"

getCameraProps :: CameraInstance -> { | CameraProps }
getCameraProps = unsafeCoerce
99

100
foreign import goToImpl :: forall o. CameraInstance -> EffectFn1 { | o } CameraInstance
Abinaya Sudhir's avatar
Abinaya Sudhir committed
101

102 103
goTo :: forall o. Optional o CameraProps => CameraInstance -> { | o } -> Effect CameraInstance
goTo cam = runEffectFn1 (goToImpl cam)
104

105 106
foreign import pauseForceAtlas2 :: Effect Unit

107
type SigmaProps =
James Laver's avatar
James Laver committed
108
  ( renderer :: String
Abinaya Sudhir's avatar
Abinaya Sudhir committed
109 110 111
  , settings :: SigmaSettings
  , style :: SigmaStyle
  , graph :: SigmaGraphData
112
  , ref :: SyntheticEventHandler (Nullable ReactRef)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
113 114
  , onClickNode :: SigmaNodeEvent -> Unit
  , onOverNode :: SigmaNodeEvent -> Unit
115 116 117 118
  , onOutNode :: SigmaNodeEvent -> Effect Unit
  , onClickEdge :: SigmaEdgeEvent -> Effect Unit
  , onOverEdge :: SigmaEdgeEvent -> Effect Unit
  , onOutEdge :: SigmaEdgeEvent -> Effect Unit
Abinaya Sudhir's avatar
Abinaya Sudhir committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  )

sStyle :: forall style. { | style } -> SigmaStyle
sStyle = unsafeCoerce

newtype EdgeShape = EdgeShape String

edgeShape :: { line :: EdgeShape
, arrow :: EdgeShape
, curve :: EdgeShape
, curvedArrow :: EdgeShape
, dashed :: EdgeShape
, dotted :: EdgeShape
, parallel :: EdgeShape
, tapered :: EdgeShape
}
edgeShape =
  { line : EdgeShape "line"
  , arrow : EdgeShape "arrow"
  , curve : EdgeShape "curve"
  , curvedArrow : EdgeShape "curvedArrow"
  , dashed : EdgeShape "dashed"
  , dotted : EdgeShape "dotted"
  , parallel : EdgeShape "parallel"
  , tapered : EdgeShape "tapered"
  }

newtype NodeShape = NodeShape String

nodeShape :: { def :: NodeShape
, pacman :: NodeShape
, star :: NodeShape
, equilateral :: NodeShape
, cross :: NodeShape
, diamond :: NodeShape
, circle :: NodeShape
, square :: NodeShape
}
nodeShape =
  { def : NodeShape "def"
  , pacman : NodeShape "pacman"
  , star : NodeShape "star"
  , equilateral : NodeShape "equilateral"
  , cross : NodeShape "cross"
  , diamond : NodeShape "diamond"
  , circle : NodeShape "circle"
  , square : NodeShape "square"
  }