Commit 66dc5e27 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[RangeSlider] some types refactoring

parent 821d1728
...@@ -23,6 +23,15 @@ import Data.Tuple.Nested ((/\)) ...@@ -23,6 +23,15 @@ import Data.Tuple.Nested ((/\))
import Effect (Effect) import Effect (Effect)
import Effect.Aff (Aff) import Effect.Aff (Aff)
import Effect.Class (liftEffect) import Effect.Class (liftEffect)
import Partial.Unsafe (unsafePartial)
import Thermite (Render, Spec, simpleSpec, defaultPerformAction)
import Unsafe.Coerce (unsafeCoerce)
import Web.HTML (window)
import Web.HTML.Window (localStorage)
import Web.Storage.Storage (getItem)
import Reactix as R
import Reactix.DOM.HTML as RH
import Gargantext.Hooks.Sigmax.Types as Sigmax import Gargantext.Hooks.Sigmax.Types as Sigmax
import Gargantext.Hooks.Sigmax.Sigmajs (CameraProps, SigmaNode, cameras, getCameraProps, goTo, pauseForceAtlas2, sigmaOnMouseMove) import Gargantext.Hooks.Sigmax.Sigmajs (CameraProps, SigmaNode, cameras, getCameraProps, goTo, pauseForceAtlas2, sigmaOnMouseMove)
import Gargantext.Components.GraphExplorer.Controls as Controls import Gargantext.Components.GraphExplorer.Controls as Controls
...@@ -41,15 +50,8 @@ import Gargantext.Pages.Corpus.Graph.Tabs as GT ...@@ -41,15 +50,8 @@ import Gargantext.Pages.Corpus.Graph.Tabs as GT
import Gargantext.Router (Routes(..)) import Gargantext.Router (Routes(..))
import Gargantext.Types (class Optional) import Gargantext.Types (class Optional)
import Gargantext.Utils (toggleSet) import Gargantext.Utils (toggleSet)
import Gargantext.Utils.Range as Range
import Gargantext.Utils.Reactix as R2 import Gargantext.Utils.Reactix as R2
import Partial.Unsafe (unsafePartial)
import Thermite (Render, Spec, simpleSpec, defaultPerformAction)
import Unsafe.Coerce (unsafeCoerce)
import Web.HTML (window)
import Web.HTML.Window (localStorage)
import Web.Storage.Storage (getItem)
import Reactix as R
import Reactix.DOM.HTML as RH
type GraphId = Int type GraphId = Int
......
...@@ -14,6 +14,7 @@ import Thermite (PerformAction, Spec) ...@@ -14,6 +14,7 @@ import Thermite (PerformAction, Spec)
import Gargantext.Components.Login.Types (TreeId) import Gargantext.Components.Login.Types (TreeId)
import Gargantext.Components.Graph as Graph import Gargantext.Components.Graph as Graph
import Gargantext.Utils.Range as Range
newtype Node = Node newtype Node = Node
{ id_ :: String { id_ :: String
......
...@@ -28,19 +28,18 @@ import Gargantext.Utils.Range as Range ...@@ -28,19 +28,18 @@ import Gargantext.Utils.Range as Range
import Gargantext.Utils.Reactix as R2 import Gargantext.Utils.Reactix as R2
-- data Axis = X | Y -- data Axis = X | Y
type NumberRange = Range.Closed Number
-- To avoid overloading the terms 'min' and 'max' here, we treat 'min' -- To avoid overloading the terms 'min' and 'max' here, we treat 'min'
-- and 'max' as being the bounds of the scale and 'low' and 'high' as -- and 'max' as being the bounds of the scale and 'low' and 'high' as
-- being the selected values -- being the selected values
type Props = type Props =
( bounds :: NumberRange -- The minimum and maximum values it is possible to select ( bounds :: Range.NumberRange -- The minimum and maximum values it is possible to select
, initialValue :: NumberRange -- The user's selection of minimum and maximum values , initialValue :: Range.NumberRange -- The user's selection of minimum and maximum values
, epsilon :: Number -- The smallest possible change (for mouse) , epsilon :: Number -- The smallest possible change (for mouse)
, step :: Number -- The 'standard' change (for keyboard) , step :: Number -- The 'standard' change (for keyboard)
-- , axis :: Axis -- Which direction to move in -- , axis :: Axis -- Which direction to move in
, width :: Number , width :: Number
, height :: Number , height :: Number
, onChange :: NumberRange -> Effect Unit ) , onChange :: Range.NumberRange -> Effect Unit )
rangeSlider :: Record Props -> R.Element rangeSlider :: Record Props -> R.Element
rangeSlider props = R.createElement rangeSliderCpt props [] rangeSlider props = R.createElement rangeSliderCpt props []
...@@ -82,7 +81,7 @@ rangeSliderCpt = R.hooksComponent "RangeSlider" cpt ...@@ -82,7 +81,7 @@ rangeSliderCpt = R.hooksComponent "RangeSlider" cpt
R2.useLayoutEffect1' dragKnob $ \_ -> do R2.useLayoutEffect1' dragKnob $ \_ -> do
case dragKnob of case dragKnob of
Just knob -> do Just knob -> do
let drag = (getDragScale knob scalePos lowPos highPos) :: Maybe NumberRange let drag = (getDragScale knob scalePos lowPos highPos) :: Maybe Range.NumberRange
R.setRef dragScale drag R.setRef dragScale drag
let onMouseMove = EL.callback $ \(event :: Event.MouseEvent) -> let onMouseMove = EL.callback $ \(event :: Event.MouseEvent) ->
case reproject drag scalePos value (R2.domMousePosition event) of case reproject drag scalePos value (R2.domMousePosition event) of
...@@ -111,13 +110,13 @@ destroyEventHandler name ref = traverse_ destroy $ R.readRef ref ...@@ -111,13 +110,13 @@ destroyEventHandler name ref = traverse_ destroy $ R.readRef ref
EL.removeEventListener document name handler EL.removeEventListener document name handler
R.setRef ref Nothing R.setRef ref Nothing
setKnob :: Knob -> ((NumberRange -> NumberRange) -> Effect Unit) -> NumberRange -> Number -> Effect Unit setKnob :: Knob -> ((Range.NumberRange -> Range.NumberRange) -> Effect Unit) -> Range.NumberRange -> Number -> Effect Unit
setKnob knob setValue r val = setValue $ const $ setter knob r val setKnob knob setValue r val = setValue $ const $ setter knob r val
where where
setter MinKnob = Range.withMin setter MinKnob = Range.withMin
setter MaxKnob = Range.withMax setter MaxKnob = Range.withMax
getDragScale :: Knob -> R.Ref (Maybe DOMRect) -> R.Ref (Maybe DOMRect) -> R.Ref (Maybe DOMRect) -> Maybe NumberRange getDragScale :: Knob -> R.Ref (Maybe DOMRect) -> R.Ref (Maybe DOMRect) -> R.Ref (Maybe DOMRect) -> Maybe Range.NumberRange
getDragScale knob scalePos lowPos highPos = do getDragScale knob scalePos lowPos highPos = do
scale <- R.readRef scalePos scale <- R.readRef scalePos
low <- R.readRef lowPos low <- R.readRef lowPos
...@@ -144,25 +143,25 @@ renderKnob ref val label knob set = ...@@ -144,25 +143,25 @@ renderKnob ref val label knob set =
onMouseDown = mkEffectFn1 $ \_ -> set $ const $ Just knob onMouseDown = mkEffectFn1 $ \_ -> set $ const $ Just knob
-- todo round to nearest epsilon -- todo round to nearest epsilon
reproject :: Maybe NumberRange -> R.Ref (Maybe DOMRect) -> NumberRange -> R2.Point -> Maybe Number reproject :: Maybe Range.NumberRange -> R.Ref (Maybe DOMRect) -> Range.NumberRange -> R2.Point -> Maybe Number
reproject drag scale value (R2.Point mousePos) = do reproject drag scale value (R2.Point mousePos) = do
drag_ <- drag drag_ <- drag
scale_ <- rectRange <$> R.readRef scale scale_ <- rectRange <$> R.readRef scale
let normal = Range.normalise scale_ (Range.clamp drag_ mousePos.x) let normal = Range.normalise scale_ (Range.clamp drag_ mousePos.x)
pure $ Range.projectNormal value normal pure $ Range.projectNormal value normal
rectRange :: DOMRect -> NumberRange rectRange :: DOMRect -> Range.NumberRange
rectRange rect = Range.Closed { min, max } rectRange rect = Range.Closed { min, max }
where min = rect.left where min = rect.left
max = rect.right max = rect.right
initialValue :: Record Props -> NumberRange initialValue :: Record Props -> Range.NumberRange
initialValue props = roundRange props.epsilon props.bounds props.initialValue initialValue props = roundRange props.epsilon props.bounds props.initialValue
round :: Number -> NumberRange -> Number -> Number round :: Number -> Range.NumberRange -> Number -> Number
round epsilon bounds = roundToMultiple epsilon <<< Range.clamp bounds round epsilon bounds = roundToMultiple epsilon <<< Range.clamp bounds
roundRange :: Number -> NumberRange -> NumberRange -> NumberRange roundRange :: Number -> Range.NumberRange -> Range.NumberRange -> Range.NumberRange
roundRange epsilon bounds (Range.Closed initial) = Range.Closed { min, max } roundRange epsilon bounds (Range.Closed initial) = Range.Closed { min, max }
where min = round epsilon bounds initial.min where min = round epsilon bounds initial.min
max = round epsilon bounds initial.max max = round epsilon bounds initial.max
......
...@@ -17,6 +17,8 @@ instance closedRange :: Ord t => Range (Closed t) t where ...@@ -17,6 +17,8 @@ instance closedRange :: Ord t => Range (Closed t) t where
clamp (Closed r) = max r.min <<< min r.max clamp (Closed r) = max r.min <<< min r.max
within (Closed r) v = (v <= r.max) && (v >= r.min) within (Closed r) v = (v <= r.max) && (v >= r.min)
type NumberRange = Closed Number
range :: Closed Number -> Number range :: Closed Number -> Number
range (Closed r) = r.max - r.min range (Closed r) = r.max - r.min
......
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