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

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

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

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

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

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

81 82 83 84 85 86 87
type CameraProps =
  ( x :: Number
  , y :: Number
  , ratio :: Number
  , angle :: Number
  )

88 89
foreign import data SigmaInstance' :: Row Type
foreign import data CameraInstance' :: Row Type
90 91
type SigmaInstance = { | SigmaInstance' }
type CameraInstance = { | CameraInstance' }
James Laver's avatar
James Laver committed
92

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

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

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

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

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

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