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)
6
import Effect (Effect)
7
import Effect.Uncurried (EffectFn1, EffectFn2, mkEffectFn1, runEffectFn1)
Mael NICOLAS's avatar
Mael NICOLAS committed
8
import React (Children, ReactClass, ReactElement, ReactRef, SyntheticEventHandler, createElement, unsafeCreateElement)
9
import React.SyntheticEvent (SyntheticMouseEvent)
10
import Record.Unsafe (unsafeGet)
11
import Thermite (EventHandler)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
12
import Unsafe.Coerce (unsafeCoerce)
13
import Gargantext.Types (class Optional)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
14 15 16 17 18 19 20

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
21 22
foreign import nOverlapClass    :: ReactClass {children :: Children}
foreign import neoCypherClass   :: ReactClass {children :: Children}
Abinaya Sudhir's avatar
Abinaya Sudhir committed
23
foreign import neoGraphItemsProducersClass :: forall props. ReactClass props
Sudhir Kumar's avatar
Sudhir Kumar committed
24 25
foreign import nodeShapesClass             :: ReactClass {children :: Children}
foreign import randomizeNodePositionsClass :: ReactClass {children :: Children}
Abinaya Sudhir's avatar
Abinaya Sudhir committed
26
foreign import relativeSizeClass           :: forall props. ReactClass props
Sudhir Kumar's avatar
Sudhir Kumar committed
27
foreign import sigmaClass :: ReactClass {children :: Children}
Abinaya Sudhir's avatar
Abinaya Sudhir committed
28
foreign import sigmaEnableSVGClass :: forall props. ReactClass props
Sudhir Kumar's avatar
Sudhir Kumar committed
29
foreign import sigmaEnableWebGLClass :: ReactClass {children :: Children}
Abinaya Sudhir's avatar
Abinaya Sudhir committed
30

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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



87
type ForceLinkOptProps =
Abinaya Sudhir's avatar
Abinaya Sudhir committed
88 89 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
  ( 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"
  }

141
type ForceAtlas2OptProps =
Abinaya Sudhir's avatar
Abinaya Sudhir committed
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 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
  ( 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
            ,  clientY :: Number
            }
       }
  }

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
250

251 252 253 254 255 256 257 258 259 260 261 262 263
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
264
foreign import sigmaOnMouseMove :: {cursorSize :: Number} -> SyntheticMouseEvent -> Effect Unit
265 266 267 268 269
cameras :: SigmaInstance -> Array CameraInstance
cameras = unsafeGet "cameras"

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

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

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

276 277
foreign import pauseForceAtlas2 :: Effect Unit

278
type SigmaProps =
Abinaya Sudhir's avatar
Abinaya Sudhir committed
279 280 281 282
  ( renderer :: Renderer
  , settings :: SigmaSettings
  , style :: SigmaStyle
  , graph :: SigmaGraphData
283
  , ref :: SyntheticEventHandler (Nullable ReactRef)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
284 285
  , onClickNode :: SigmaNodeEvent -> Unit
  , onOverNode :: SigmaNodeEvent -> Unit
286 287 288 289
  , onOutNode :: SigmaNodeEvent -> Effect Unit
  , onClickEdge :: SigmaEdgeEvent -> Effect Unit
  , onOverEdge :: SigmaEdgeEvent -> Effect Unit
  , onOutEdge :: SigmaEdgeEvent -> Effect Unit
Abinaya Sudhir's avatar
Abinaya Sudhir committed
290 291 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
  )

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

Abinaya Sudhir's avatar
Abinaya Sudhir committed
345 346 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
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
  )