Corpus.Document: wire the ngram/termlist changes

parent d4d44eaf
...@@ -25,16 +25,21 @@ import Reactix.SyntheticEvent as E ...@@ -25,16 +25,21 @@ import Reactix.SyntheticEvent as E
import Gargantext.Types ( TermList ) import Gargantext.Types ( TermList )
import Gargantext.Components.Annotation.Utils ( termClass ) import Gargantext.Components.Annotation.Utils ( termClass )
import Gargantext.Components.NgramsTable.Core ( NgramsTable(..), highlightNgrams ) import Gargantext.Components.NgramsTable.Core ( NgramsTerm, NgramsTable(..), _NgramsElement, _list, highlightNgrams )
import Gargantext.Components.Annotation.Menu ( AnnotationMenu, annotationMenu ) import Gargantext.Components.Annotation.Menu ( AnnotationMenu, annotationMenu )
import Gargantext.Utils.Selection as Sel import Gargantext.Utils.Selection as Sel
type Run = Tuple String (Maybe TermList) type Run = Tuple String (Maybe TermList)
type Props = ( ngrams :: NgramsTable, text :: Maybe String ) type Props =
( ngrams :: NgramsTable
, setTermList :: NgramsTerm -> Maybe TermList -> TermList -> Effect Unit
, text :: Maybe String
)
defaultProps :: Record Props -- UNUSED
defaultProps = { ngrams: NgramsTable Map.empty, text: Nothing } -- defaultProps :: Record Props
-- defaultProps = { ngrams: NgramsTable Map.empty, text: Nothing, setTermList: \_ _ _ -> pure unit }
annotatedField :: Record Props -> R.Element annotatedField :: Record Props -> R.Element
annotatedField p = R.createElement annotatedFieldComponent p [] annotatedField p = R.createElement annotatedFieldComponent p []
...@@ -42,14 +47,15 @@ annotatedField p = R.createElement annotatedFieldComponent p [] ...@@ -42,14 +47,15 @@ annotatedField p = R.createElement annotatedFieldComponent p []
annotatedFieldComponent :: R.Component Props annotatedFieldComponent :: R.Component Props
annotatedFieldComponent = R.hooksComponent "AnnotatedField" cpt annotatedFieldComponent = R.hooksComponent "AnnotatedField" cpt
where where
runs props = cpt {ngrams,setTermList,text} _ = do
HTML.div { className: "annotated-field-runs" } (map annotateRun $ compile props)
cpt props _ = do
menu /\ setMenu <- R.useState $ \_ -> pure Nothing menu /\ setMenu <- R.useState $ \_ -> pure Nothing
let wrapperProps = let wrapperProps =
{ className: "annotated-field-wrapper" { className: "annotated-field-wrapper"
, onContextMenu: mkEffectFn1 (maybeShowMenu setMenu props.ngrams) } , onContextMenu: mkEffectFn1 (maybeShowMenu setMenu setTermList ngrams)
pure $ HTML.div wrapperProps [ maybeAddMenu setMenu (runs props) menu] }
runs =
HTML.div { className: "annotated-field-runs" } $ map annotateRun $ compile ngrams text
pure $ HTML.div wrapperProps [maybeAddMenu setMenu runs menu]
maybeAddMenu maybeAddMenu
:: (Maybe AnnotationMenu -> Effect Unit) :: (Maybe AnnotationMenu -> Effect Unit)
...@@ -59,16 +65,16 @@ maybeAddMenu ...@@ -59,16 +65,16 @@ maybeAddMenu
maybeAddMenu setMenu e (Just props) = annotationMenu setMenu props <> e maybeAddMenu setMenu e (Just props) = annotationMenu setMenu props <> e
maybeAddMenu _ e _ = e maybeAddMenu _ e _ = e
compile :: Record Props -> Array Run compile :: NgramsTable -> Maybe String -> Array Run
compile props = runs props.text compile ngrams = maybe [] (highlightNgrams ngrams)
where runs = maybe [] (highlightNgrams props.ngrams)
maybeShowMenu maybeShowMenu
:: (Maybe AnnotationMenu -> Effect Unit) :: (Maybe AnnotationMenu -> Effect Unit)
-> (NgramsTerm -> Maybe TermList -> TermList -> Effect Unit)
-> NgramsTable -> NgramsTable
-> E.SyntheticEvent DE.MouseEvent -> E.SyntheticEvent DE.MouseEvent
-> Effect Unit -> Effect Unit
maybeShowMenu setMenu ngrams event = do maybeShowMenu setMenu setTermList ngrams event = do
s <- Sel.getSelection s <- Sel.getSelection
case s of case s of
Just sel -> do Just sel -> do
...@@ -76,9 +82,11 @@ maybeShowMenu setMenu ngrams event = do ...@@ -76,9 +82,11 @@ maybeShowMenu setMenu ngrams event = do
"" -> pure unit "" -> pure unit
sel' -> do sel' -> do
let x = E.clientX event let x = E.clientX event
let y = E.clientY event y = E.clientY event
list = findNgram ngrams sel'
setList = setTermList sel' list
E.preventDefault event E.preventDefault event
setMenu $ Just { x, y, list: findNgram ngrams sel' } setMenu $ Just { x, y, sel, list, setList }
Nothing -> pure unit Nothing -> pure unit
findNgram :: NgramsTable -> String -> Maybe TermList findNgram :: NgramsTable -> String -> Maybe TermList
......
...@@ -15,16 +15,23 @@ import Gargantext.Types ( TermList(..), termListName ) ...@@ -15,16 +15,23 @@ import Gargantext.Types ( TermList(..), termListName )
import Gargantext.Components.Annotation.Utils ( termClass ) import Gargantext.Components.Annotation.Utils ( termClass )
import Gargantext.Components.ContextMenu.ContextMenu as CM import Gargantext.Components.ContextMenu.ContextMenu as CM
import Gargantext.Utils.Selection (Selection, selectionToString)
type Props = ( list :: Maybe TermList ) type Props =
( sel :: Selection
, list :: Maybe TermList
, setList :: TermList -> Effect Unit
)
type AnnotationMenu = { x :: Number, y :: Number | Props } type AnnotationMenu = { x :: Number, y :: Number | Props }
-- | An Annotation Menu is parameterised by a Maybe Termlist of the -- | An Annotation Menu is parameterised by a Maybe Termlist of the
-- | TermList the currently selected text belongs to -- | TermList the currently selected text belongs to
annotationMenu :: (Maybe AnnotationMenu -> Effect Unit) -> AnnotationMenu -> R.Element annotationMenu :: (Maybe AnnotationMenu -> Effect Unit) -> AnnotationMenu -> R.Element
annotationMenu setMenu { x,y,list } = annotationMenu setMenu { x,y,sel,list,setList } =
CM.contextMenu { x,y,setMenu } [ R.createElement annotationMenuCpt {list} [] ] CM.contextMenu { x,y,setMenu } [
R.createElement annotationMenuCpt {sel,list,setList} []
]
annotationMenuCpt :: R.Component Props annotationMenuCpt :: R.Component Props
annotationMenuCpt = R.hooksComponent "Annotation.Menu" cpt annotationMenuCpt = R.hooksComponent "Annotation.Menu" cpt
...@@ -36,13 +43,8 @@ annotationMenuCpt = R.hooksComponent "Annotation.Menu" cpt ...@@ -36,13 +43,8 @@ annotationMenuCpt = R.hooksComponent "Annotation.Menu" cpt
addToList :: Record Props -> TermList -> Maybe R.Element addToList :: Record Props -> TermList -> Maybe R.Element
addToList {list: Just t'} t addToList {list: Just t'} t
| t == t' = Nothing | t == t' = Nothing
addToList props t = Just $ CM.contextMenuItem [ link ] addToList {setList} t = Just $ CM.contextMenuItem [ link ]
where link = HTML.a { onClick: click, className: className } [ HTML.text label ] where link = HTML.a { onClick: click, className: className } [ HTML.text label ]
label = "Add to " <> termListName t label = "Add to " <> termListName t
className = termClass t className = termClass t
click = mkEffectFn1 $ \_ -> addToTermList props t click = mkEffectFn1 $ \_ -> setList t
-- TODO: what happens when we add to a term list?
addToTermList :: Record Props -> TermList -> Effect Unit
addToTermList _ _ = pure unit
...@@ -233,7 +233,7 @@ ngramsTableSpec = simpleSpec performAction render ...@@ -233,7 +233,7 @@ ngramsTableSpec = simpleSpec performAction render
-- TODO ROOT-UPDATE -- TODO ROOT-UPDATE
-- patch the root of the child to be equal to the root of the parent. -- patch the root of the child to be equal to the root of the parent.
performAction (AddNewNgram ngram) {path: params} _ = performAction (AddNewNgram ngram) {path: params} _ =
lift $ addNewNgram ngram params lift $ addNewNgram ngram Nothing params
render :: Render State LoadedNgramsTableProps Action render :: Render State LoadedNgramsTableProps Action
render dispatch { path: pageParams render dispatch { path: pageParams
......
...@@ -496,7 +496,7 @@ type CoreState s = ...@@ -496,7 +496,7 @@ type CoreState s =
putTable :: {nodeId :: Int, listIds :: Array Int, tabType :: TabType} -> Versioned NgramsTablePatch -> Aff (Versioned NgramsTablePatch) putTable :: {nodeId :: Int, listIds :: Array Int, tabType :: TabType} -> Versioned NgramsTablePatch -> Aff (Versioned NgramsTablePatch)
putTable {nodeId, listIds, tabType} = putTable {nodeId, listIds, tabType} =
put (toUrl Back (PutNgrams tabType (head listIds)) $ Just nodeId) put (toUrl Back (PutNgrams tabType (head listIds) Nothing) $ Just nodeId)
commitPatch :: forall s. {nodeId :: Int, listIds :: Array Int, tabType :: TabType} commitPatch :: forall s. {nodeId :: Int, listIds :: Array Int, tabType :: TabType}
-> Versioned NgramsTablePatch -> StateCoTransformer (CoreState s) Unit -> Versioned NgramsTablePatch -> StateCoTransformer (CoreState s) Unit
...@@ -524,9 +524,9 @@ convOrderBy (T.DESC (T.ColumnName "Score (Occurrences)")) = ScoreDesc ...@@ -524,9 +524,9 @@ convOrderBy (T.DESC (T.ColumnName "Score (Occurrences)")) = ScoreDesc
convOrderBy (T.ASC _) = TermAsc convOrderBy (T.ASC _) = TermAsc
convOrderBy (T.DESC _) = TermDesc convOrderBy (T.DESC _) = TermDesc
addNewNgram :: forall s. NgramsTerm -> CoreParams s -> Aff Unit addNewNgram :: forall s. NgramsTerm -> Maybe TermList -> CoreParams s -> Aff Unit
addNewNgram ngram {nodeId, listIds, tabType} = addNewNgram ngram mayList {nodeId, listIds, tabType} =
post (toUrl Back (PutNgrams tabType (head listIds)) $ Just nodeId) [ngram] post (toUrl Back (PutNgrams tabType (head listIds) mayList) $ Just nodeId) [ngram]
ngramsLoaderClass :: Loader.LoaderClass PageParams VersionedNgramsTable ngramsLoaderClass :: Loader.LoaderClass PageParams VersionedNgramsTable
ngramsLoaderClass = Loader.createLoaderClass "NgramsTableLoader" loadNgramsTable ngramsLoaderClass = Loader.createLoaderClass "NgramsTableLoader" loadNgramsTable
......
...@@ -166,10 +166,11 @@ pathUrl c (GetNgrams ...@@ -166,10 +166,11 @@ pathUrl c (GetNgrams
_ -> pathUrl c (NodeAPI Url_Document) i _ -> pathUrl c (NodeAPI Url_Document) i
pathUrl c (PutNgrams t listid) i = pathUrl c (PutNgrams t listid termList) i =
pathUrl c (NodeAPI Node) i <> "/ngrams?ngramsType=" <> showTabType' t <> listid' pathUrl c (NodeAPI Node) i <> "/ngrams?ngramsType="
where <> showTabType' t
listid' = maybe "" (\x -> "&list=" <> show x) listid <> maybe "" (\x -> "&list=" <> show x) listid
<> foldMap (\x -> "&listType=" <> show x) termList
pathUrl c Auth Nothing = c.prePath <> "auth" pathUrl c Auth Nothing = c.prePath <> "auth"
pathUrl c Auth (Just _) = "impossible" -- TODO better types pathUrl c Auth (Just _) = "impossible" -- TODO better types
pathUrl c (NodeAPI nt) i = c.prePath <> nodeTypeUrl nt <> (maybe "" (\i' -> "/" <> show i') i) pathUrl c (NodeAPI nt) i = c.prePath <> nodeTypeUrl nt <> (maybe "" (\i' -> "/" <> show i') i)
...@@ -274,7 +275,7 @@ data Path ...@@ -274,7 +275,7 @@ data Path
, termSizeFilter :: Maybe TermSize , termSizeFilter :: Maybe TermSize
, searchQuery :: String , searchQuery :: String
} }
| PutNgrams TabType (Maybe ListId) | PutNgrams TabType (Maybe ListId) (Maybe TermList)
-- ^ The name is not good. In particular this URL is used both in PUT and POST. -- ^ The name is not good. In particular this URL is used both in PUT and POST.
| NodeAPI NodeType | NodeAPI NodeType
| Search { {-id :: Int | Search { {-id :: Int
......
...@@ -48,7 +48,7 @@ initialState {loaded: {ngramsTable: Versioned {version}}} = ...@@ -48,7 +48,7 @@ initialState {loaded: {ngramsTable: Versioned {version}}} =
-- This is a subset of NgramsTable.Action. -- This is a subset of NgramsTable.Action.
data Action data Action
= SetTermListItem NgramsTerm (Replace TermList) = SetTermListItem NgramsTerm (Replace TermList)
| AddNewNgram NgramsTerm | AddNewNgram NgramsTerm TermList
| Refresh | Refresh
newtype Status = Status { failed :: Int newtype Status = Status { failed :: Int
...@@ -284,8 +284,8 @@ docViewSpec = simpleSpec performAction render ...@@ -284,8 +284,8 @@ docViewSpec = simpleSpec performAction render
where where
pe = NgramsPatch { patch_list: pl, patch_children: mempty } pe = NgramsPatch { patch_list: pl, patch_children: mempty }
pt = PatchMap $ Map.singleton n pe pt = PatchMap $ Map.singleton n pe
performAction (AddNewNgram ngram) {path: params} _ = performAction (AddNewNgram ngram termList) {path: params} _ =
lift $ addNewNgram ngram params lift $ addNewNgram ngram (Just termList) params
render :: Render State LoadedDataProps Action render :: Render State LoadedDataProps Action
render dispatch { path: pageParams render dispatch { path: pageParams
...@@ -325,7 +325,9 @@ docViewSpec = simpleSpec performAction render ...@@ -325,7 +325,9 @@ docViewSpec = simpleSpec performAction render
] ]
where where
ngramsTable = applyNgramsTablePatch ngramsTablePatch initTable ngramsTable = applyNgramsTablePatch ngramsTablePatch initTable
annotate text = scuff $ AnnotatedField.annotatedField { ngrams: ngramsTable, text } setTermList ngram Nothing newList = dispatch $ AddNewNgram ngram newList
setTermList ngram (Just oldList) newList = dispatch $ SetTermListItem ngram (replace oldList newList)
annotate text = scuff $ AnnotatedField.annotatedField { ngrams: ngramsTable, setTermList, text }
li' = li [className "list-group-item justify-content-between"] li' = li [className "list-group-item justify-content-between"]
text' x = text $ maybe "Nothing" identity x text' x = text $ maybe "Nothing" identity x
badge s = span [className "badge badge-default badge-pill"] [text s] badge s = span [className "badge badge-default badge-pill"] [text s]
......
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