Sigmajs.purs 12.4 KB
Newer Older
Sudhir Kumar's avatar
Sudhir Kumar committed
1 2 3
module Gargantext.Components.GraphExplorer.Sigmajs where

import Prelude
Abinaya Sudhir's avatar
Abinaya Sudhir committed
4

5
import Data.Nullable (Nullable)
James Laver's avatar
James Laver committed
6 7
import Data.Sequence (Seq)
import Data.Sequence as Seq
8
import Effect (Effect)
9
import Effect.Uncurried (EffectFn1, EffectFn2, mkEffectFn1, runEffectFn1)
Mael NICOLAS's avatar
Mael NICOLAS committed
10
import React (Children, ReactClass, ReactElement, ReactRef, SyntheticEventHandler, createElement, unsafeCreateElement)
11
import React.SyntheticEvent (SyntheticMouseEvent)
12
import Record.Unsafe (unsafeGet)
13
import Thermite (EventHandler)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
14
import Unsafe.Coerce (unsafeCoerce)
15
import Gargantext.Types (class Optional)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
16 17 18 19 20 21 22

foreign import edgeShapesClass  :: forall props. ReactClass props
foreign import filterClass      :: forall props. ReactClass props
foreign import forceAtlas2Class :: forall props. ReactClass props
foreign import forceLinkClass   :: forall props. ReactClass props
foreign import loadGEXFClass    :: forall props. ReactClass props
foreign import loadJSONClass    :: forall props. ReactClass props
Sudhir Kumar's avatar
Sudhir Kumar committed
23 24
foreign import nOverlapClass    :: ReactClass {children :: Children}
foreign import neoCypherClass   :: ReactClass {children :: Children}
Abinaya Sudhir's avatar
Abinaya Sudhir committed
25
foreign import neoGraphItemsProducersClass :: forall props. ReactClass props
Sudhir Kumar's avatar
Sudhir Kumar committed
26 27
foreign import nodeShapesClass             :: ReactClass {children :: Children}
foreign import randomizeNodePositionsClass :: ReactClass {children :: Children}
Abinaya Sudhir's avatar
Abinaya Sudhir committed
28
foreign import relativeSizeClass           :: forall props. ReactClass props
Sudhir Kumar's avatar
Sudhir Kumar committed
29
foreign import sigmaClass :: ReactClass {children :: Children}
Abinaya Sudhir's avatar
Abinaya Sudhir committed
30
foreign import sigmaEnableSVGClass :: forall props. ReactClass props
Sudhir Kumar's avatar
Sudhir Kumar committed
31
foreign import sigmaEnableWebGLClass :: ReactClass {children :: Children}
Abinaya Sudhir's avatar
Abinaya Sudhir committed
32

33
neoCypher :: forall o. Optional o NeoCypherOptProps  => NeoCypherReqProps o -> ReactElement
Sudhir Kumar's avatar
Sudhir Kumar committed
34
neoCypher props = unsafeCreateElement neoCypherClass (unsafeCoerce props) []
Abinaya Sudhir's avatar
Abinaya Sudhir committed
35

36
loadJSON :: forall o. Optional o (onGraphLoaded :: Effect Unit) => { "path" :: String | o } -> ReactElement
Sudhir Kumar's avatar
Sudhir Kumar committed
37
loadJSON props = unsafeCreateElement loadJSONClass props []
Abinaya Sudhir's avatar
Abinaya Sudhir committed
38

39
loadGEXF :: forall o. Optional o (onGraphLoaded :: Effect Unit) => { "path" :: String | o } -> ReactElement
Sudhir Kumar's avatar
Sudhir Kumar committed
40
loadGEXF props = unsafeCreateElement loadGEXFClass props []
Abinaya Sudhir's avatar
Abinaya Sudhir committed
41

42
forceLink :: forall o. Optional o ForceLinkOptProps  => { | o} -> ReactElement
Sudhir Kumar's avatar
Sudhir Kumar committed
43
forceLink props = unsafeCreateElement forceLinkClass props []
Abinaya Sudhir's avatar
Abinaya Sudhir committed
44 45

nOverlap :: forall o. Optional o NOverlapOptProps  => { | o } -> ReactElement
Sudhir Kumar's avatar
Sudhir Kumar committed
46
nOverlap props = unsafeCreateElement nOverlapClass (unsafeCoerce props) []
Abinaya Sudhir's avatar
Abinaya Sudhir committed
47 48 49 50 51

randomizeNodePositions :: ReactElement
randomizeNodePositions  = createElement randomizeNodePositionsClass {} []

relativeSize :: {initialSize :: Number } -> ReactElement
Sudhir Kumar's avatar
Sudhir Kumar committed
52
relativeSize props = unsafeCreateElement randomizeNodePositionsClass (unsafeCoerce props) []
Abinaya Sudhir's avatar
Abinaya Sudhir committed
53

54
forceAtlas2 :: forall o. Optional o ForceAtlas2OptProps  => { | o } -> ReactElement
Sudhir Kumar's avatar
Sudhir Kumar committed
55
forceAtlas2 props = unsafeCreateElement forceAtlas2Class props []
Abinaya Sudhir's avatar
Abinaya Sudhir committed
56

57
sigma :: forall props. Optional props SigmaProps =>  { | props} -> Array ReactElement -> ReactElement
Sudhir Kumar's avatar
Sudhir Kumar committed
58
sigma props children = unsafeCreateElement sigmaClass (unsafeCoerce props) children
Abinaya Sudhir's avatar
Abinaya Sudhir committed
59 60 61 62 63

sigmaEnableWebGL :: ReactElement
sigmaEnableWebGL = createElement sigmaEnableWebGLClass {} []

edgeShapes :: { "default" :: EdgeShape } -> ReactElement
Sudhir Kumar's avatar
Sudhir Kumar committed
64
edgeShapes props = unsafeCreateElement edgeShapesClass props []
Abinaya Sudhir's avatar
Abinaya Sudhir committed
65 66

nodeShapes :: { "default" :: NodeShape } -> ReactElement
Sudhir Kumar's avatar
Sudhir Kumar committed
67
nodeShapes props = unsafeCreateElement nodeShapesClass (unsafeCoerce props) []
Abinaya Sudhir's avatar
Abinaya Sudhir committed
68 69 70 71 72 73


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

74
type NeoCypherOptProps =
Abinaya Sudhir's avatar
Abinaya Sudhir committed
75
  ( producers :: String
76
  , onGraphLoaded :: Effect Unit
Abinaya Sudhir's avatar
Abinaya Sudhir committed
77 78 79 80 81 82 83 84 85 86 87 88
  )

type NeoCypherReqProps o =
  { url :: String
  , user :: String
  , password :: String
  , query :: String
  | o
  }



89
type ForceLinkOptProps =
Abinaya Sudhir's avatar
Abinaya Sudhir committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 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
  ( barnesHutOptimize :: Boolean
  , barnesHutTheta :: Number
  , adjustSizes :: Boolean
  , iterationsPerRender :: Number
  , linLogMode :: Boolean
  , outboundAttractionDistribution :: Boolean
  , edgeWeightInfluence :: Number
  , scalingRatio :: Number
  , strongGravityMode :: Boolean
  , gravity :: Number
  , alignNodeSiblings :: Boolean
  , nodeSiblingsScale :: Number
  , nodeSiblingsAngleMin :: Number
  , worker :: Boolean
  , background :: Boolean
  , easing :: SigmaEasing
  , randomize :: Randomize
  , slowDown :: Number
  , timeout :: Number
  )

newtype Randomize = Randomize String

randomize :: { globally :: Randomize
, locally :: Randomize
, no :: Randomize
}
randomize =
  { globally : Randomize "globally"
  , locally : Randomize "locally"
  , no : Randomize "no"
  }

newtype SigmaEasing = SigmaEasing String

sigmaEasing :: { linear :: SigmaEasing
, quadraticIn :: SigmaEasing
, quadraticOut :: SigmaEasing
, quadraticInOut :: SigmaEasing
, cubicIn :: SigmaEasing
, cubicOut :: SigmaEasing
, cubicInOut :: SigmaEasing
}
sigmaEasing =
  { linear : SigmaEasing "linear"
  , quadraticIn : SigmaEasing "quadraticIn"
  , quadraticOut : SigmaEasing "quadraticOut"
  , quadraticInOut : SigmaEasing "quadraticInOut"
  , cubicIn : SigmaEasing "cubicIn"
  , cubicOut : SigmaEasing "cubicOut"
  , cubicInOut : SigmaEasing "cubicInOut"
  }

143
type ForceAtlas2OptProps =
Abinaya Sudhir's avatar
Abinaya Sudhir committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  ( worker :: Boolean
  , barnesHutOptimize :: Boolean
  , barnesHutTheta :: Number
  , adjustSizes :: Boolean
  , iterationsPerRender :: Number
  , linLogMode :: Boolean
  , outboundAttractionDistribution :: Boolean
  , edgeWeightInfluence :: Number
  , scalingRatio :: Number
  , strongGravityMode :: Boolean
  , slowDown :: Number
  , gravity :: Number
  , timeout :: Number
  , fixedY  :: Boolean
  , startingIterations :: Number
  , skipHidden :: Boolean
  )


type NOverlapOptProps =
  ( nodes :: Array SigmaNode
  , nodeMargin :: Number
  , scaleNodes :: Number
  , gridSize :: Number
  , permittedExpansion :: Number
  , speed :: Number
  , maxIterations :: Number
  , easing :: SigmaEasing
  , duration :: Number
  )

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

type SigmaEdgeEvent =
  { "data"::
       { node :: SigmaEdge
       , captor ::
            { clientX :: Number
190
            , clientY :: Number
Abinaya Sudhir's avatar
Abinaya Sudhir committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
            }
       }
  }

newtype Renderer = Renderer String

webgl :: Renderer
webgl = Renderer "webgl"

canvas :: Renderer
canvas = Renderer "canvas"

newtype Color = Color String

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

type SigmaNodeOptProps =
  ( x :: Number
  , y :: Number
  , size :: Number
  , color :: Color
  , label :: String
  )

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

type SigmaEdgeOptProps =
  ( color :: Color
  , 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 : ""}

sigmaSettings :: forall o. Optional o SigmaSettingProps => { | o } -> SigmaSettings
sigmaSettings = unsafeCoerce

foreign import data SigmaStyle :: Type
252

253 254 255 256 257 258 259 260 261 262 263 264 265
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' }
foreign import setSigmaRef :: EffectFn1 (Nullable ReactRef) Unit
foreign import getSigmaRef :: Effect SigmaInstance
266
foreign import sigmaOnMouseMove :: {cursorSize :: Number} -> SyntheticMouseEvent -> Effect Unit
267 268 269 270 271
cameras :: SigmaInstance -> Array CameraInstance
cameras = unsafeGet "cameras"

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

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

275 276
goTo :: forall o. Optional o CameraProps => CameraInstance -> { | o } -> Effect CameraInstance
goTo cam = runEffectFn1 (goToImpl cam)
277

278 279
foreign import pauseForceAtlas2 :: Effect Unit

280
type SigmaProps =
Abinaya Sudhir's avatar
Abinaya Sudhir committed
281 282 283 284
  ( renderer :: Renderer
  , settings :: SigmaSettings
  , style :: SigmaStyle
  , graph :: SigmaGraphData
285
  , ref :: SyntheticEventHandler (Nullable ReactRef)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
286 287
  , onClickNode :: SigmaNodeEvent -> Unit
  , onOverNode :: SigmaNodeEvent -> Unit
288 289 290 291
  , onOutNode :: SigmaNodeEvent -> Effect Unit
  , onClickEdge :: SigmaEdgeEvent -> Effect Unit
  , onOverEdge :: SigmaEdgeEvent -> Effect Unit
  , onOutEdge :: SigmaEdgeEvent -> Effect Unit
Abinaya Sudhir's avatar
Abinaya Sudhir committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
  )

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

newtype ScalingMode = ScalingMode String

scalingMode :: { inside :: ScalingMode
, outside :: ScalingMode
}
346

Abinaya Sudhir's avatar
Abinaya Sudhir committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
scalingMode =
  { inside : ScalingMode "inside"
  , outside : ScalingMode "outside"
  }

type SigmaSettingProps =
  ( clone :: Boolean
  , immutable :: Boolean
  , verbose :: Boolean
  , defaultNodeType :: String
  , defaultEdgeType :: String
  , defaultLabelColor :: String
  , defaultEdgeCOlor :: String
  , defaultNodeColor :: String
  , defaultLabelSize :: String
  , edgeColor :: String
  , minArrowSize :: Number
  , font :: String
  , fontStyle :: String
  , labelColor :: String
  , labelSize :: String
  , labelSizeRatio :: Number
  , labelThreshold :: Number
  , labelMaxSize :: Number
  , webglOversamplingRatio :: Number
  , borderSize :: Number
  , nodeBorderColor :: String
  , defaultNodeBorderColor :: String
  , hoverFont :: String
  , hoverFontStyle :: String
  , labelHoverShadow :: String
  , labelHoverShadowColor :: String
  , nodeHoverColor :: String
  , defaultNodeHoverColor :: String
  , labelHoverBGColor :: String
  , defaultHoverLabelBGColor :: String
  , defaultHoverLabelColor :: String
  , labelHoverColor :: String
  , defaultLabelHoverColor :: String
  , singleHover :: Boolean
  , edgeHoverColor :: String
  , defaultEdgeHoverColor :: String
  , edgeHoverSizeRatio :: Number
  , edgeHoverExtremities :: Boolean
  , drawLabels :: Boolean
  , drawEdgeLabels :: Boolean
  , drawEdges :: Boolean
  , drawNodes :: Boolean
  , batchEdgesDrawing :: Boolean
  , canvasEdgesBatchSize :: Number
  , webglEdgesBatchSize :: Number
  , hideEdgesOnMove :: Boolean
  , scalingMode :: ScalingMode
  , sideMargin :: Number
  , minEdgeSize :: Number
  , maxEdgeSize :: Number
  , minNodeSize :: Number
  , maxNodeSize :: Number
  , touchEnabled :: Boolean
  , mouseEnabled :: Boolean
  , mouseWheelEnabled :: Boolean
  , doubleClickEnabled :: Boolean
  , eventsEnabled :: Boolean
  , zoomingRatio :: Number
  , doubleClickZoomingRatio :: Number
  , zoomMin :: Number
  , zoomMax :: Number
  , mouseZoomDuration :: Number
  , doubleClickZoomDuration :: Number
  , mouseInertiaDuration :: Number
  , mouseInertiaRatio :: Number
  , touchInertiaDuration :: Number
  , touchInertiaRatio :: Number
  , doubleClickTimeout :: Number
  , doubleTapTimeout :: Number
  , dragTimeout :: Number
  , autoResize :: Boolean
  , autoRescale :: Boolean
  , enableCamera :: Boolean
  , enableHovering :: Boolean
  , enableEdgeHovering :: Boolean
  , edgeHoverPrecision :: Number
  , rescaleIgnoreSize :: Boolean
  , skipErrors :: Boolean
  , nodesPowRatio :: Number
  , edgesPowRatio :: Number
  , animationsTime :: Number
  , twNodeRendBorderSize :: Number
  , twNodeRendBorderColor :: String
  , twEdgeDefaultOpacity  :: Number
  , twSelectedColor       :: String
  , twNodesGreyOpacity    :: Number
  , twBorderGreyColor     :: String
  , twEdgeGreyColor       :: String
  )