[GraphExplorer] Workaround the state to access the camera

parent 43e812aa
......@@ -40,13 +40,18 @@ exports.forceLinkClass = FL.default;
}
const applyOnCamera = function(props, f){
if (props != null) {
const camera = props.sigma.cameras[0];
// console.log(camera);
// console.log(f);
f(camera)();
}
exports.setSigmaRef = function(props) {
if (props.sigma) {
window.sigmaGargInstance = props.sigma;
}
};
exports.getSigmaRef = function() {
return window.sigmaGargInstance;
};
exports.goToImpl = function(cam) {
return function(props) {
console.log("goTo", cam, props);
return cam.goTo(props);
};
};
exports.applyOnCameraImpl = applyOnCamera;
......@@ -4,8 +4,9 @@ import Prelude
import Data.Nullable (Nullable)
import Effect (Effect)
import Effect.Uncurried (EffectFn1, EffectFn2, mkEffectFn1, runEffectFn2)
import Effect.Uncurried (EffectFn1, EffectFn2, mkEffectFn1, runEffectFn1)
import React (Children, ReactClass, ReactElement, ReactRef, SyntheticEventHandler, createElement, unsafeCreateElement)
import Record.Unsafe (unsafeGet)
import Thermite (EventHandler)
import Unsafe.Coerce (unsafeCoerce)
import Gargantext.Types (class Optional)
......@@ -246,20 +247,29 @@ sigmaSettings = unsafeCoerce
foreign import data SigmaStyle :: Type
type Camera =
{
x :: Number,
y :: Number,
ratio :: Number,
angle :: Number
}
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
cameras :: SigmaInstance -> Array CameraInstance
cameras = unsafeGet "cameras"
getCameraProps :: CameraInstance -> { | CameraProps }
getCameraProps = unsafeCoerce
foreign import applyOnCameraImpl :: forall a. EffectFn2 (Nullable ReactRef) (a -> EventHandler) Unit
foreign import goToImpl :: forall o. CameraInstance -> EffectFn1 { | o } CameraInstance
applyOnCamera :: forall a. (a -> EventHandler) -> EffectFn1 (Nullable ReactRef) Unit
applyOnCamera a = mkEffectFn1 h
where h :: Nullable ReactRef -> Effect Unit
h r = runEffectFn2 applyOnCameraImpl r a
goTo :: forall o. Optional o CameraProps => CameraInstance -> { | o } -> Effect CameraInstance
goTo cam = runEffectFn1 (goToImpl cam)
type SigmaProps =
( renderer :: Renderer
......
......@@ -62,11 +62,6 @@ backLocal v = { baseUrl: "http://localhost:8008"
, prePath: "/api/" <> show v <> "/"
}
backDemo :: ApiVersion -> Config
backDemo v = { baseUrl: "https://demo.gargantext.org"
, prePath: "/api/" <> show v <> "/"
}
backDev :: ApiVersion -> Config
backDev v = { baseUrl: "https://dev.gargantext.org"
, prePath: "/api/" <> show v <> "/"
......
......@@ -16,7 +16,7 @@ import Data.Int (fromString, toNumber)
import Data.Int as Int
import Data.Lens (Lens, Lens', over, (%~), (+~), (.~), (^.))
import Data.Lens.Record (prop)
import Data.Maybe (Maybe(..), fromJust, fromMaybe)
import Data.Maybe (Maybe(..), fromJust, fromMaybe, isNothing)
import Data.Newtype (class Newtype)
import Data.String (joinWith)
import Data.Symbol (SProxy(..))
......@@ -26,7 +26,7 @@ import Effect.Aff.Class (liftAff)
import Effect.Class (liftEffect)
import Effect.Console (log)
import Effect.Uncurried (runEffectFn1, runEffectFn2)
import Gargantext.Components.GraphExplorer.Sigmajs (Camera, Color(Color), SigmaEasing, SigmaGraphData(SigmaGraphData), SigmaNode, SigmaSettings, canvas, edgeShape, edgeShapes, forceAtlas2, applyOnCamera, sStyle, sigma, sigmaEasing, sigmaEdge, sigmaEnableWebGL, sigmaNode, sigmaSettings)
import Gargantext.Components.GraphExplorer.Sigmajs (Color(Color), SigmaEasing, SigmaGraphData(SigmaGraphData), SigmaNode, SigmaSettings, canvas, edgeShape, edgeShapes, forceAtlas2, setSigmaRef, getSigmaRef, cameras, getCameraProps, goTo, sStyle, sigma, sigmaEasing, sigmaEdge, sigmaEnableWebGL, sigmaNode, sigmaSettings)
import Gargantext.Components.GraphExplorer.Types (Cluster(..), MetaData(..), Edge(..), GraphData(..), Legend(..), Node(..), getLegendData)
import Gargantext.Components.Login.Types (AuthData(..), TreeId)
import Gargantext.Components.RandomText (words)
......@@ -57,7 +57,6 @@ data Action
| ChangeLabelSize Number
| ChangeNodeSize Number
| DisplayEdges
| SaveCamera Camera
-- | Zoom Boolean
newtype SelectedNode = SelectedNode {id :: String, label :: String}
......@@ -93,10 +92,6 @@ _drawEdges' = prop (SProxy :: SProxy "drawEdges")
_drawEdges :: Lens' SigmaSettings Boolean
_drawEdges f = unsafeCoerce $ _drawEdges' f
_camera :: forall s a. Lens' { camera :: a | s } a
_camera = prop (SProxy :: SProxy "camera")
-- TODO remove newtype here
newtype State = State
{ graphData :: GraphData
......@@ -110,7 +105,6 @@ newtype State = State
, corpusId :: Int
, treeId :: Maybe TreeId
, settings :: SigmaSettings
, camera :: Maybe Camera
}
initialState :: State
......@@ -126,7 +120,6 @@ initialState = State
, corpusId : 0
, treeId : Nothing
, settings : mySettings
, camera : Nothing
}
-- This one is not used: specOld is the one being used.
......@@ -177,11 +170,6 @@ performAction DisplayEdges _ _ =
modifyState_ $ \(State s) -> do
State $ ((_settings <<< _drawEdges) %~ not) s
performAction (SaveCamera c) _ _ =
modifyState_ $ \(State s) -> do
State $ ((_camera) .~ cam) s
where cam = Just {x: 0.0, y: 0.0, ratio: 0.0, angle: 0.0}
--performAction (Zoom True) _ _ =
-- modifyState_ $ \() -> do
-- State $
......@@ -213,8 +201,7 @@ render d p (State {sigmaGraphData, settings, legendData}) c =
, settings
, renderer : canvas
, style : sStyle { height : "96%"}
-- , ref: applyOnCamera
, ref: applyOnCamera $ d <<< SaveCamera
, ref: saveSigmaRef
, onClickNode : \e -> unsafePerformEffect $ do
_ <- log "this should be deleted"
-- _ <- logs $ unsafeCoerce e
......@@ -522,7 +509,6 @@ specOld = fold [treespec treeSpec, graphspec $ simpleSpec performAction render']
pure unit
] [text "Save"] -- TODO: Implement Save!
]
]
]
]
......@@ -540,11 +526,17 @@ specOld = fold [treespec treeSpec, graphspec $ simpleSpec performAction render']
[ sigma { graph, settings
, renderer : canvas
, style : sStyle { height : "95%"}
, ref: applyOnCamera $ d <<< SaveCamera
, ref: setSigmaRef
, onClickNode : \e ->
unsafePerformEffect $ do
_ <- log " hello 2"
_ <- log $ show st.camera
s <- getSigmaRef
case (cameras s !! 0) of
Just cam -> do
let camP = getCameraProps cam
_ <- log $ show camP
void $ goTo cam {ratio: camP.ratio / 2.0}
Nothing -> pure unit
_ <- d $ ShowSidePanel true
_ <- d $ SelectNode $ SelectedNode {id : (unsafeCoerce e).data.node.id, label : (unsafeCoerce e).data.node.label}
pure unit
......
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