Menu.purs 2.11 KB
Newer Older
James Laver's avatar
James Laver committed
1 2 3 4
-- | A ContextMenU that allows you to add terms to a list
module Gargantext.Components.Annotation.Menu where


James Laver's avatar
James Laver committed
5
import Prelude ( Unit, (==), ($), (<>), unit, pure, otherwise, const )
James Laver's avatar
James Laver committed
6
import Data.Array as A
7
import Data.Maybe ( Maybe(..), maybe' )
James Laver's avatar
James Laver committed
8 9 10
import Effect ( Effect )
import Effect.Uncurried ( mkEffectFn1 )
import Reactix as R
11 12
import Reactix.DOM.HTML as HTML
import Reactix.SyntheticEvent as E
James Laver's avatar
James Laver committed
13 14

import Gargantext.Types ( TermList(..), termListName )
15
import Gargantext.Components.Annotation.Utils ( termBootstrapClass )
James Laver's avatar
James Laver committed
16 17

import Gargantext.Components.ContextMenu.ContextMenu as CM
18
import Gargantext.Utils.Selection (Selection, selectionToString)
James Laver's avatar
James Laver committed
19

20 21
data MenuType = NewNgram | SetTermListItem

22
type Props =
23
  ( list :: Maybe TermList
24
  , menuType :: MenuType
James Laver's avatar
James Laver committed
25
  , setList :: TermList -> Effect Unit -- not a state hook setter
26
  )
27

28
type AnnotationMenu = { x :: Number, y :: Number | Props }
James Laver's avatar
James Laver committed
29 30 31

-- | An Annotation Menu is parameterised by a Maybe Termlist of the
-- | TermList the currently selected text belongs to
James Laver's avatar
James Laver committed
32
annotationMenu :: ((Maybe AnnotationMenu -> Maybe AnnotationMenu) -> Effect Unit) -> AnnotationMenu -> R.Element
33
annotationMenu setMenu { x,y,list,menuType,setList } =
34
  CM.contextMenu { x,y,setMenu } [
35
    R.createElement annotationMenuCpt {list,menuType,setList} []
36
  ]
James Laver's avatar
James Laver committed
37 38

annotationMenuCpt :: R.Component Props
39
annotationMenuCpt = R.hooksComponent "Annotation.Menu" cpt
James Laver's avatar
James Laver committed
40
  where
41 42
    cpt props _ = pure $ R.fragment $ children props
    children props = A.mapMaybe (addToList props) [ GraphTerm, CandidateTerm, StopTerm ]
James Laver's avatar
James Laver committed
43

44
-- | Given the TermList to render the item for zand the Maybe TermList the item may belong to, possibly render the menuItem
45 46 47
addToList :: Record Props -> TermList -> Maybe R.Element
addToList {list: Just t'} t
  | t == t'   = Nothing
48 49 50 51
addToList {menuType, setList} t = Just $ CM.contextMenuItem [ link ]
  where
    link = HTML.a { onClick: click, className: className } [ HTML.text (label menuType) ]
    label NewNgram = "Add to " <> termListName t
Nicolas Pouillard's avatar
Nicolas Pouillard committed
52
    label SetTermListItem = "Change to " <> termListName t
53
    className = "list-group-item list-group-item-" <> (termBootstrapClass t)
54
    click = mkEffectFn1 $ \_ -> setList t