Box.purs 10.9 KB
Newer Older
1
module Gargantext.Components.Forest.Tree.Node.Box where
2

3 4
import Gargantext.Prelude

5
import Data.Array as A
6
import Data.Foldable (intercalate)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
7
import Data.Maybe (Maybe(..))
8
import Effect.Aff (Aff)
arturo's avatar
arturo committed
9
import Gargantext.Components.App.Store (Boxes)
10
import Gargantext.Components.Bootstrap as B
11
import Gargantext.Components.Bootstrap.Types (ComponentStatus(..), Elevation(..))
12
import Gargantext.Components.Forest.Tree.Node.Action.Add (addNodeView)
13
import Gargantext.Components.Forest.Tree.Node.Action.Contact as Contact
14
import Gargantext.Components.Forest.Tree.Node.Action.Delete (actionDelete)
15
import Gargantext.Components.Forest.Tree.Node.Action.Documentation (actionDoc)
16
import Gargantext.Components.Forest.Tree.Node.Action.Download (actionDownload)
17
import Gargantext.Components.Forest.Tree.Node.Action.Link (linkNode)
18
import Gargantext.Components.Forest.Tree.Node.Action.ManageTeam (actionManageTeam)
19 20
import Gargantext.Components.Forest.Tree.Node.Action.Merge (mergeNode)
import Gargantext.Components.Forest.Tree.Node.Action.Move (moveNode)
21
import Gargantext.Components.Forest.Tree.Node.Action.Rename (renameAction)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
22
import Gargantext.Components.Forest.Tree.Node.Action.Search (actionSearch)
23
import Gargantext.Components.Forest.Tree.Node.Action.Share as Share
24
import Gargantext.Components.Forest.Tree.Node.Action.Types (Action)
25 26
import Gargantext.Components.Forest.Tree.Node.Action.Update (update)
import Gargantext.Components.Forest.Tree.Node.Action.Upload (actionUpload)
27
import Gargantext.Components.Forest.Tree.Node.Action.WriteNodesDocuments (actionWriteNodesDocuments)
28
import Gargantext.Components.Forest.Tree.Node.Box.Types (NodePopupProps, NodePopupS)
29
import Gargantext.Components.Forest.Tree.Node.Settings (NodeAction(..), SettingsBox(..), glyphiconNodeAction, settingsBox)
30
import Gargantext.Components.Forest.Tree.Node.Status (Status(..), hasStatus)
31
import Gargantext.Components.Forest.Tree.Node.Tools (fragmentPT, textInputBox)
32
import Gargantext.Sessions (Session)
33
import Gargantext.Types (ID, Name, prettyNodeType)
34
import Gargantext.Types as GT
35
import Gargantext.Utils ((?))
36
import Gargantext.Utils.Reactix as R2
37 38 39
import Reactix as R
import Reactix.DOM.HTML as H
import Toestand as T
40

James Laver's avatar
James Laver committed
41 42
here :: R2.Here
here = R2.here "Gargantext.Components.Forest.Tree.Node.Box"
43

44
type CommonProps =
45 46 47
  ( dispatch  :: Action -> Aff Unit
  , session   :: Session
  )
48

49
nodePopupView :: R2.Leaf NodePopupProps
50
nodePopupView = R2.leaf nodePopupViewCpt
51 52
nodePopupViewCpt :: R.Component NodePopupProps
nodePopupViewCpt = here.component "nodePopupView" cpt where
James Laver's avatar
James Laver committed
53
  cpt p@{ id, name, nodeType }  _ = do
54
    renameIsOpen <- T.useBox false
James Laver's avatar
James Laver committed
55
    open <- T.useLive T.unequal renameIsOpen
56 57
    nodePopup <- T.useBox { action: Nothing, id, name, nodeType }
    action <- T.useFocused (_.action) (\a b -> b { action = a }) nodePopup
James Laver's avatar
James Laver committed
58
    nodePopup' <- T.useLive T.unequal nodePopup
59

60 61 62 63
    pure $

      H.div
      { className: "node-popup-tooltip"
64
      , title: "Type: " <> prettyNodeType nodeType
65 66 67 68 69 70 71 72 73 74
      }
      [
        H.div
        { className: "popup-container card" }
        [
          panelHeading renameIsOpen open p
        ,
          panelBody    action p
        ,
          mPanelAction nodePopup' p
75 76
        ]
      ]
77

James Laver's avatar
James Laver committed
78
  panelHeading renameIsOpen open p@{ dispatch, id, name, nodeType } =
79 80 81 82 83 84 85
    H.div
    { className: "popup-container__header card-header" }
    [
      B.wad
      [ "d-flex", "align-items-center" ]
      [
        B.wad
86
        [ "w-2/12" ]
87 88 89
        [
          H.span
          { className: GT.fldr nodeType true} [] -- TODO fix names
90 91
        -- ,
        --   B.span' { className: "small" } $ prettyNodeType nodeType
92 93 94
        ]
      ,
        B.wad
95
        [ "w-8/12 text-center" ]
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        [
          if open then
            textInputBox
            { boxAction: renameAction
            , boxName: "Rename"
            , dispatch
            , id
            , text: name
            , isOpen: renameIsOpen
            } []
          else
            B.wad'
            [ "text-primary" ]
            p.name
        ]
      ,
        B.wad
        [ "w-2/12", "text-right" ]
        [
          editIcon renameIsOpen open
        ,
          B.wad_ [ "d-inline-block", "w-3" ]
        ,
          B.iconButton
          { callback: const $ p.closeCallback unit
          , title: "Close"
          , name: "times"
123
          , elevation: Level2
124
          }
125
        ]
126 127 128 129
      ]
    ]

  editIcon _      true  = mempty
James Laver's avatar
James Laver committed
130
  editIcon isOpen false =
131 132 133 134
    B.iconButton
    { name: "pencil"
    , title: "Rename"
    , callback: const $ T.write_ true isOpen
135
    , elevation: Level2
136 137
    }

138
  panelBody :: T.Box (Maybe NodeAction) -> Record NodePopupProps -> R.Element
139
  panelBody nodePopupState { nodeType } =
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    let (SettingsBox { doc, buttons }) = settingsBox nodeType
    in
      H.div
      { className: intercalate " "
          [ "popup-container__body"
          , "card-body"
          ]
      } $
      [
        buttonClick
        { action: doc
        , state: nodePopupState
        , nodeType
        }
      ]
      <>
        (
          buttons <#> \t ->

            buttonClick
            { action: t
            , state: nodePopupState
            , nodeType
            }
        )

      -- FIXME trick to increase the size of the box
      <> if A.length buttons < 2
          then [ H.div { className: "col-4" } [] ]
          else []

James Laver's avatar
James Laver committed
171 172
  mPanelAction :: Record NodePopupS -> Record NodePopupProps -> R.Element
  mPanelAction { action: Just action }
173
               { boxes, dispatch, id, name, nodeType, session } =
174
    panelAction { action
175
                , boxes
176 177 178 179
                , dispatch
                , id
                , name
                , nodeType
180 181
                , session
                }
James Laver's avatar
James Laver committed
182
  mPanelAction { action: Nothing } _ =
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    H.div
    { className: "popup-container__footer card-footer" }
    [
      H.h6
      {}
      [
        B.icon
        { name: "hand-pointer-o"
        , className: "mr-1"
        }
      ,
        H.text "Select available actions of this node"
      ]
    ,
      H.ul
      { className: "panel-actions" }
      [
        H.div
        { className: "panel-actions__ok-to-use" }
        [
          B.icon
          { name: "circle" }
        ,
          B.span_ "usable"
        ]
      ,
        H.div
        { className: "panel-actions__almost-useable" }
        [
          B.icon
          { name: "circle" }
        ,
          B.span_ "almost useable"
        ]
      ,
        H.div
        { className: "panel-actions__development-in-progress" }
        [
          B.icon
          { name: "circle" }
        ,
          B.span_ "development in progress"
        ]
      ]
    ]

229

230
type ActionState =
231 232 233
  ( action   :: Maybe NodeAction
  , id       :: ID
  , name     :: Name
234 235 236 237
  , nodeType :: GT.NodeType
  )

type ButtonClickProps =
James Laver's avatar
James Laver committed
238
  ( action   :: NodeAction
239
  , state    :: T.Box (Maybe NodeAction)
240
  , nodeType :: GT.NodeType
241 242
  )

243 244
buttonClick :: R2.Leaf ButtonClickProps
buttonClick = R2.leaf buttonClickCpt
245 246

buttonClickCpt :: R.Component ButtonClickProps
James Laver's avatar
James Laver committed
247
buttonClickCpt = here.component "buttonClick" cpt where
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  cpt { action: todo
      , state
      , nodeType
      } _ = do
    -- | States
    -- |
    action <- R2.useLive' state

    -- | Behaviors
    -- |
    let
      click _ = T.write_ (Just todo) state

    -- | Render
    pure $

      -- B.iconButton
      -- { className: intercalate " "
      --     [ "popup-container__body__button"
      --     , modifierClassName (hasStatus nodeType todo)
      --     ]
      -- , name: glyphiconNodeAction todo
      -- , title: show todo
      -- , callback: click
      -- , elevation: Level2
      -- , status: action == Just todo ?
      --     Disabled $
      --     Enabled
      -- }

      H.div
      { className: intercalate " "
          [ "popup-container__cta"
          , modifierClassName (hasStatus nodeType todo)
          ]
      }
      [
        B.iconButton
        { className: "popup-container__cta__button"
        , name: glyphiconNodeAction todo
        , title: show todo
        , callback: click
        , elevation: Level2
        , status: action == Just todo ?
            Disabled $
            Enabled
        }
      ,
        B.icon
        { className: "popup-container__cta__icon"
        , name: "circle"
        }
      ]

  -- | Helpers
  -- |
304

305 306 307 308 309 310 311
  modifierClassName :: Status -> String
  modifierClassName = case _ of
    Stable -> blk <> "--ok-to-use"
    Test   -> blk <> "--almost-useable"
    Dev    -> blk <> "--development-in-progress"
    where
      blk = "popup-container__cta"
312

313 314 315
type NodeProps =
  ( id       :: ID
  , name     :: Name
316
  , nodeType :: GT.NodeType
317 318 319
  )


320
type PanelActionProps =
321 322 323
  ( action    :: NodeAction
  , boxes     :: Boxes
  , id        :: ID
324 325 326 327
  , dispatch  :: Action -> Aff Unit
  , name      :: Name
  , nodeType  :: GT.NodeType
  , session   :: Session
328 329
  )

330
panelAction :: R2.Leaf PanelActionProps
331
panelAction = R2.leaf panelActionCpt
332
panelActionCpt :: R.Component PanelActionProps
James Laver's avatar
James Laver committed
333
panelActionCpt = here.component "panelAction" cpt
334
  where
335 336 337 338
    cpt { action: Documentation nodeType}                  _ = pure $ actionDoc { nodeType } []
    cpt { action: Download, id, nodeType, session}         _ = pure $ actionDownload { id, nodeType, session } []
    cpt { action: Upload, dispatch, id, nodeType, session} _ = pure $ actionUpload { dispatch, id, nodeType, session } []
    cpt { action: Delete, nodeType, dispatch}              _ = pure $ actionDelete { dispatch, nodeType } []
339
    cpt { action: ManageTeam, nodeType, id, session}       _ = pure $ actionManageTeam { id, nodeType, session } []
340
    cpt { action: Add xs, dispatch, id, name, nodeType} _ =
341
      pure $ addNodeView {dispatch, id, name, nodeType, nodeTypes: xs} []
342 343
    cpt { action: Refresh , dispatch, nodeType } _ = pure $ update { dispatch, nodeType } []
    cpt { action: Config, nodeType } _ =
344
      pure $ fragmentPT $ "Config " <> show nodeType
345
    -- Functions using SubTree
346 347
    cpt { action: Merge {subTreeParams}, boxes, dispatch, id, nodeType, session } _ =
      pure $ mergeNode { boxes, dispatch, id, nodeType, session, subTreeParams } []
348
    cpt { action: Move {subTreeParams}, boxes, dispatch, id, nodeType, session } _ =
349
      pure $ moveNode { boxes, dispatch, id, nodeType, session, subTreeParams } []
350
    cpt { action: Link {subTreeParams}, boxes, dispatch, id, nodeType, session } _ =
351
      pure $ linkNode { boxes, dispatch, id, nodeType, session, subTreeParams } []
352
    cpt { action : Share, dispatch, id, session } _ = pure $ Share.shareNode { dispatch, id, session } []
353 354
    cpt { action : AddingContact, dispatch, id } _ = pure $ Contact.actionAddContact { dispatch, id } []
    cpt { action : Publish {subTreeParams}, boxes, dispatch, id, nodeType, session } _ =
355
      pure $ Share.publishNode { boxes, dispatch, id, nodeType, session, subTreeParams } []
356
    cpt { action: SearchBox, boxes, dispatch, id, session } _ =
357
      pure $ actionSearch { boxes, dispatch, id: Just id, session } []
358 359
    cpt { action: WriteNodesDocuments, boxes, dispatch, id, session } _ =
      pure $ actionWriteNodesDocuments { boxes, dispatch, id, session } []
James Laver's avatar
James Laver committed
360
    cpt _ _ = pure $ H.div {} []