Sigmajs.purs 3.83 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)
8 9
import React (SyntheticEventHandler)
import React.Ref as RR
10
import Record.Unsafe (unsafeGet)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
11
import Unsafe.Coerce (unsafeCoerce)
12
import Gargantext.Types (class Optional)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

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
33
            , clientY :: Number
Abinaya Sudhir's avatar
Abinaya Sudhir committed
34 35 36 37 38 39 40 41 42 43 44 45 46
            }
       }
  }

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

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

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

type SigmaEdgeOptProps =
James Laver's avatar
James Laver committed
57
  ( color :: String
Abinaya Sudhir's avatar
Abinaya Sudhir committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  , 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
82

83 84 85 86 87 88 89 90 91 92 93
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
94

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

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

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

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

106
type SigmaProps =
James Laver's avatar
James Laver committed
107
  ( renderer :: String
Abinaya Sudhir's avatar
Abinaya Sudhir committed
108 109 110
  , settings :: SigmaSettings
  , style :: SigmaStyle
  , graph :: SigmaGraphData
111
  , ref :: RR.RefHandler RR.ReactInstance
Abinaya Sudhir's avatar
Abinaya Sudhir committed
112 113
  , onClickNode :: SigmaNodeEvent -> Unit
  , onOverNode :: SigmaNodeEvent -> Unit
114 115 116 117
  , onOutNode :: SigmaNodeEvent -> Effect Unit
  , onClickEdge :: SigmaEdgeEvent -> Effect Unit
  , onOverEdge :: SigmaEdgeEvent -> Effect Unit
  , onOutEdge :: SigmaEdgeEvent -> Effect Unit
Abinaya Sudhir's avatar
Abinaya Sudhir committed
118 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
  )

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"
  }