Menu.purs 2.05 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


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

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

import Gargantext.Components.ContextMenu.ContextMenu as CM
17
import Gargantext.Utils.Reactix as R2
James Laver's avatar
James Laver committed
18

19
thisModule :: String
20 21
thisModule = "Gargantext.Components.Annotation.Menu"

22 23
data MenuType = NewNgram | SetTermListItem

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

30 31 32
type AnnotationMenu = {
    x :: Number
  , y :: Number
33
  , onClose :: Effect Unit
34 35
  | Props
  }
James Laver's avatar
James Laver committed
36 37 38

-- | An Annotation Menu is parameterised by a Maybe Termlist of the
-- | TermList the currently selected text belongs to
39 40 41 42
annotationMenu :: AnnotationMenu -> R.Element
annotationMenu {x, y, list, menuType, onClose, setList} =
  CM.contextMenu {x, y, onClose} [
    R.createElement annotationMenuCpt {list, menuType, setList} []
43
  ]
James Laver's avatar
James Laver committed
44 45

annotationMenuCpt :: R.Component Props
46
annotationMenuCpt = R.hooksComponentWithModule thisModule "annotationMenu" cpt
James Laver's avatar
James Laver committed
47
  where
48
    cpt props _ = pure $ R.fragment $ children props
49
    children props = A.mapMaybe (addToList props) [ MapTerm, CandidateTerm, StopTerm ]
James Laver's avatar
James Laver committed
50

51
-- | Given the TermList to render the item for zand the Maybe TermList the item may belong to, possibly render the menuItem
52 53 54
addToList :: Record Props -> TermList -> Maybe R.Element
addToList {list: Just t'} t
  | t == t'   = Nothing
55 56
addToList {menuType, setList} t = Just $ CM.contextMenuItem [ link ]
  where
57
    link = HTML.a { on: { click }, className: className } [ HTML.text (label menuType) ]
58
    label NewNgram = "Add to " <> termListName t
Nicolas Pouillard's avatar
Nicolas Pouillard committed
59
    label SetTermListItem = "Change to " <> termListName t
60
    className = "list-group-item list-group-item-" <> (termBootstrapClass t)
61
    click _ = setList t