Commit 399e3b00 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[sigma.js] force atlas2 works, first draft

parent 597e9a73
Pipeline #3193 failed with stage
in 0 seconds
......@@ -20,6 +20,7 @@ import Gargantext.Components.Themes (darksterTheme)
import Gargantext.Components.Themes as Themes
import Gargantext.Hooks.Sigmax as Sigmax
import Gargantext.Hooks.Sigmax.Graphology as Graphology
import Gargantext.Hooks.Sigmax.ForceAtlas2 as ForceAtlas2
import Gargantext.Hooks.Sigmax.Sigma as Sigma
import Gargantext.Hooks.Sigmax.Types as SigmaxTypes
import Gargantext.Utils (getter)
......@@ -43,6 +44,8 @@ drawGraph :: forall s fa2. R2.Leaf (Props s fa2)
drawGraph = R2.leaf drawGraphCpt
drawGraphCpt :: forall s fa2. R.Memo (Props s fa2)
drawGraphCpt = R.memo' $ here.component "graph" cpt where
-- drawGraphCpt :: forall s fa2. R.Component (Props s fa2)
-- drawGraphCpt = here.component "graph" cpt where
-- | Component
-- |
cpt { elRef
......@@ -53,6 +56,8 @@ drawGraphCpt = R.memo' $ here.component "graph" cpt where
boxes <- AppStore.use
fa2Ref <- R.useRef (Nothing :: Maybe ForceAtlas2.FA2Layout)
{ showEdges
, graphStage
, graph
......@@ -75,8 +80,14 @@ drawGraphCpt = R.memo' $ here.component "graph" cpt where
R.useEffectOnce $ do
pure $ do
here.log "[graphCpt (Cleanup)]"
case R.readRef fa2Ref of
Nothing -> pure unit
Just fa2 -> do
ForceAtlas2.stop fa2
ForceAtlas2.kill fa2
R.setRef fa2Ref Nothing
Sigmax.dependOnSigma (R.readRef sigmaRef) "[graphCpt (Cleanup)] no sigma" $ \sigma -> do
Sigma.stopForceAtlas2 sigma
--Sigma.stopForceAtlas2 sigma
here.log2 "[graphCpt (Cleanup)] forceAtlas stopped for" sigma
Sigma.kill sigma
here.log "[graphCpt (Cleanup)] sigma killed"
......@@ -122,9 +133,18 @@ drawGraphCpt = R.memo' $ here.component "graph" cpt where
-- here.log2 "[graph] startForceAtlas" startForceAtlas
if startForceAtlas' then
Sigma.startForceAtlas2 sig fa2
case R.readRef fa2Ref of
Nothing -> do
fa2 <- ForceAtlas2.init (Sigma.graph sig)
ForceAtlas2.start fa2
R.setRef fa2Ref (Just fa2)
Just fa2 -> do
-- TODO Kill and restart? Maybe check fa2.graph first? Should be equal to sigma.graph
pure unit
else
Sigma.stopForceAtlas2 sig
case R.readRef fa2Ref of
Nothing -> pure unit
Just fa2 -> ForceAtlas2.stop fa2
case mCamera of
Just (GET.Camera { ratio, x, y }) -> do
......
......@@ -237,8 +237,9 @@ resetForceAtlasButtonCpt = here.component "resetForceAtlasToggleButton" cpt
-- TODO Sigma.killForceAtlas2 sigma
-- startForceAtlas2 sigma
Sigmax.dependOnSigma (R.readRef sigmaRef) "[resetForceAtlasButton] no sigma" $ \sigma -> do
Sigma.killForceAtlas2 sigma
Sigma.refreshForceAtlas sigma Graph.forceAtlas2Settings
-- TODO Use fa2Ref instead of sigmaRef
--Sigma.killForceAtlas2 sigma
--Sigma.refreshForceAtlas sigma Graph.forceAtlas2Settings
T.write_ SigmaxTypes.Killed forceAtlasState
------------------------------------------------------------------
......
......@@ -120,17 +120,22 @@ handleForceAtlas2Pause sigmaRef forceAtlasState mFAPauseRef settings = do
let sigma = R.readRef sigmaRef
toggled <- T.read forceAtlasState
dependOnSigma sigma "[handleForceAtlas2Pause] sigma: Nothing" $ \s -> do
let isFARunning = Sigma.isForceAtlas2Running s
-- TODO Rewrite using R.Ref FA2Layout instead of a Sigma ref
--let isFARunning = Sigma.isForceAtlas2Running s
let isFARunning = false
case Tuple toggled isFARunning of
Tuple ST.InitialRunning false -> do
Sigma.restartForceAtlas2 s settings
--Sigma.restartForceAtlas2 s settings
pure unit
Tuple ST.Running false -> do
Sigma.restartForceAtlas2 s settings
--Sigma.restartForceAtlas2 s settings
pure unit
case R.readRef mFAPauseRef of
Nothing -> pure unit
Just timeoutId -> clearTimeout timeoutId
Tuple ST.Paused true -> do
Sigma.stopForceAtlas2 s
--Sigma.stopForceAtlas2 s
pure unit
_ -> pure unit
setEdges :: Sigma.Sigma -> Boolean -> Effect Unit
......@@ -225,7 +230,8 @@ performDiff sigma g = do
traverse_ (Graphology.removeEdge sigmaGraph) removeEdges
traverse_ (Graphology.removeNode sigmaGraph) removeNodes
Sigma.refresh sigma
Sigma.killForceAtlas2 sigma
-- TODO Use FA2Layout here
--Sigma.killForceAtlas2 sigma
where
sigmaGraph = Sigma.graph sigma
sigmaEdgeIds = Graphology.edgeIds sigmaGraph
......
'use strict';
// https://graphology.github.io/standard-library/layout-forceatlas2.html
import FA2Layout from 'graphology-layout-forceatlas2/worker';
export function _init(graph) {
return new FA2Layout(graph, {
settings: {gravity: 1},
getEdgeWeight: 'weight'
})
}
export function _start(layout) {
return layout.start();
}
export function _stop(layout) {
return layout.stop();
}
export function _kill(layout) {
return layout.kill();
}
export function _isRunning(layout) {
return layout.isRunning();
}
module Gargantext.Hooks.Sigmax.ForceAtlas2 where
-- FFI for force atlas2: https://graphology.github.io/standard-library/layout-forceatlas2.html
import Prelude
import Data.Array as A
import Data.Sequence as Seq
import Data.Set as Set
import Data.Traversable (traverse)
import Effect (Effect)
import Effect.Timer (setTimeout)
import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn4)
import FFI.Simple ((..), (...), (.=))
import Gargantext.Hooks.Sigmax.Graphology as Graphology
import Gargantext.Hooks.Sigmax.Types as Types
import Record as Record
-- | Type representing the web worker.
foreign import data FA2Layout :: Type
-- TODO inferSettings
-- TODO init with settings
foreign import _init :: EffectFn1 Graphology.Graph FA2Layout
foreign import _start :: EffectFn1 FA2Layout Unit
foreign import _stop :: EffectFn1 FA2Layout Unit
foreign import _kill :: EffectFn1 FA2Layout Unit
foreign import _isRunning :: EffectFn1 FA2Layout Boolean
init :: Graphology.Graph -> Effect FA2Layout
init = runEffectFn1 _init
start :: FA2Layout -> Effect Unit
start = runEffectFn1 _start
stop :: FA2Layout -> Effect Unit
stop = runEffectFn1 _stop
kill :: FA2Layout -> Effect Unit
kill = runEffectFn1 _kill
isRunning :: FA2Layout -> Effect Boolean
isRunning = runEffectFn1 _isRunning
-- TODO?
restart :: FA2Layout -> Effect Unit
restart = start
refresh :: FA2Layout -> Effect Unit
refresh f = do
isRunning' <- isRunning f
if isRunning' then
pure unit
else do
_ <- setTimeout 100 $ do
restart f
_ <- setTimeout 100 $ stop f
pure unit
pure unit
......@@ -161,48 +161,6 @@ proxySetSettings :: forall settings.
Window -> Sigma -> settings -> Effect Unit
proxySetSettings = runEffectFn3 _proxySetSettings
-- TODO
-- | Start forceAtlas2 on a sigmajs instance.
startForceAtlas2 :: forall settings. Sigma -> settings -> Effect Unit
startForceAtlas2 _ _ = pure unit
--startForceAtlas2 s settings = pure $ s ... "startForceAtlas2" $ [ settings ]
-- | Restart forceAtlas2 on a sigmajs instance.
restartForceAtlas2 :: forall settings. Sigma -> settings -> Effect Unit
restartForceAtlas2 s settings = startForceAtlas2 s settings
-- TODO
-- | Stop forceAtlas2 on a sigmajs instance.
stopForceAtlas2 :: Sigma -> Effect Unit
stopForceAtlas2 _ = pure unit
--stopForceAtlas2 s = pure $ s ... "stopForceAtlas2" $ []
-- TODO
-- | Kill forceAtlas2 on a sigmajs instance.
killForceAtlas2 :: Sigma -> Effect Unit
killForceAtlas2 _ = pure unit
--killForceAtlas2 s = pure $ s ... "killForceAtlas2" $ []
-- | Return whether forceAtlas2 is running on a sigmajs instance.
isForceAtlas2Running :: Sigma -> Boolean
isForceAtlas2Running _ = false
--isForceAtlas2Running s = s ... "isForceAtlas2Running" $ [] :: Boolean
-- | Refresh forceAtlas2 (with a `setTimeout` hack as it seems it doesn't work
-- | otherwise).
refreshForceAtlas :: forall settings. Sigma -> settings -> Effect Unit
refreshForceAtlas s settings = do
let isRunning = isForceAtlas2Running s
if isRunning then
pure unit
else do
_ <- setTimeout 100 $ do
restartForceAtlas2 s settings
_ <- setTimeout 100 $
stopForceAtlas2 s
pure unit
pure unit
newtype SigmaEasing = SigmaEasing String
sigmaEasing ::
......
......@@ -611,7 +611,7 @@ triggerEvent = runEffectFn2 _triggerEvent
getInputValue :: R.Ref (Nullable DOM.Element) -> String
getInputValue elNullableRef = case toMaybe (R.readRef elNullableRef) of
Nothing -> ""
Just el ->
Just el ->
el .. "value"
setInputValue :: R.Ref (Nullable DOM.Element) -> String -> Effect Unit
......
......@@ -5388,7 +5388,14 @@ graceful-fs@^4.1.2:
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.6.tgz#ff040b2b0853b23c3d31027523706f1885d76bee"
integrity sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==
graphology-utils@^2.5.0:
graphology-layout-forceatlas2@^0.9.2:
version "0.9.2"
resolved "https://registry.yarnpkg.com/graphology-layout-forceatlas2/-/graphology-layout-forceatlas2-0.9.2.tgz#0f5986f55ef0d72162f23edd1837aa8dac69f20e"
integrity sha512-VUF1xGnoq5qT0Svo03Bn4a31bA1ofPjakwpILD72J2N5t6g66hLQq/r8TKIx041DN7DATQZ0v6LnrA6Iyqs/Ow==
dependencies:
graphology-utils "^2.1.0"
graphology-utils@^2.1.0, graphology-utils@^2.5.0:
version "2.5.2"
resolved "https://registry.yarnpkg.com/graphology-utils/-/graphology-utils-2.5.2.tgz#4d30d6e567d27c01f105e1494af816742e8d2440"
integrity sha512-ckHg8MXrXJkOARk56ZaSCM1g1Wihe2d6iTmz1enGOz4W/l831MBCKSayeFQfowgF8wd+PQ4rlch/56Vs/VZLDQ==
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment