Menu.purs 2.75 KB
Newer Older
arturo's avatar
arturo committed
1 2 3 4
module Gargantext.Components.Annotation.Menu
  ( annotationMenu
  , AnnotationMenu
  ) where
James Laver's avatar
James Laver committed
5

arturo's avatar
arturo committed
6
import Gargantext.Prelude
James Laver's avatar
James Laver committed
7

8
import Data.Maybe (Maybe(..))
arturo's avatar
arturo committed
9
import Data.String (toLower)
10
import Effect (Effect)
arturo's avatar
arturo committed
11 12 13
import Gargantext.Components.Annotation.Types (MenuType(..), termClass)
import Gargantext.Components.Bootstrap as B
import Gargantext.Components.Bootstrap.Types (ComponentStatus(..))
14
import Gargantext.Types (TermList(..), termListName)
15
import Gargantext.Utils.Reactix as R2
arturo's avatar
arturo committed
16 17 18
import Reactix as R
import Reactix.DOM.HTML as H
import Toestand as T
James Laver's avatar
James Laver committed
19

20 21
here :: R2.Here
here = R2.here "Gargantext.Components.Annotation.Menu"
22

23
type Props =
arturo's avatar
arturo committed
24
  ( menuRef :: R.Ref (Maybe (Record AnnotationMenu))
25
  )
26

arturo's avatar
arturo committed
27 28 29 30 31 32 33 34
type AnnotationMenu =
  ( closeCallback :: Unit -> Effect Unit
  , redrawMenu    :: T.Box Boolean
  , x             :: Number
  , y             :: Number
  , list          :: Maybe TermList
  , menuType      :: MenuType
  , setList       :: TermList -> Effect Unit -- not a state hook setter
35
  )
James Laver's avatar
James Laver committed
36

arturo's avatar
arturo committed
37 38 39
annotationMenu :: R2.Leaf Props
annotationMenu = R2.leaf annotationMenuCpt
annotationMenuCpt :: R.Component Props
40
annotationMenuCpt = here.component "main" cpt where
41
  cpt { menuRef } _ = do
arturo's avatar
arturo committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    -- Render
    pure $

      R2.fromMaybe (R.readRef menuRef) \props' ->

        B.contextMenu
        { x: props'.x
        , y: props'.y
        , closeCallback: props'.closeCallback
        } $
        (addToList props') <$> [ MapTerm, CandidateTerm, StopTerm ]

--------------------------------------------------------------------------

-- addToList :: Record AnnotationMenu -> TermList -> Maybe R.Element
-- addToList {list: Just t'} t
--   | t == t'   = Nothing

-- addToList {menuType, setList} t = Just $
--   B.contextMenuItem
--   { callback: click }
--   [
--     B.icon
--     { name: "circle"
--     , className: "mr-2 " <> termClass t
--     }
--   ,
--     H.text (label menuType)
--   ]

--   where
--     label NewNgram        = "Add to "    <> (toLower $ termListName t)
--     label SetTermListItem = "Change to " <> (toLower $ termListName t)
--     click _ = setList t

addToList :: Record AnnotationMenu -> TermList -> R.Element
addToList {list: Just t', menuType} t
  | t == t' =
    B.contextMenuItem
    { callback: const R.nothing
    , status: Disabled
    }
    [
      B.icon
      { name: "circle"
      , className: "mr-2 disabled-term"
      }
    ,
      H.text (label t menuType)
    ]

addToList {menuType, setList} t =
    B.contextMenuItem
    { callback: const $ setList t }
    [
      B.icon
      { name: "circle"
      , className: "mr-2 " <> termClass t
      }
    ,
      H.text (label t menuType)
    ]

label :: TermList -> MenuType -> String
label t NewNgram        = "Add to "    <> (toLower $ termListName t)
label t SetTermListItem = "Change to " <> (toLower $ termListName t)