Table.purs 9.72 KB
Newer Older
Nicolas Pouillard's avatar
Nicolas Pouillard committed
1 2
module Gargantext.Components.Table where

3
import Prelude
4
import Data.Array as A
5 6
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
7
import Data.List as L
8
import Data.Maybe (Maybe(..))
9
import Data.Tuple (fst, snd)
10
import Data.Tuple.Nested ((/\))
11
import DOM.Simple.Console (log2)
12 13
import Reactix as R
import Reactix.DOM.HTML as H
14
import Gargantext.Utils.Reactix as R2
15
import Gargantext.Utils.Reactix (effectLink)
Nicolas Pouillard's avatar
Nicolas Pouillard committed
16

17
type TableContainerProps =
18 19 20 21 22 23
  ( pageSizeControl     :: R.Element
  , pageSizeDescription :: R.Element
  , paginationLinks     :: R.Element
  , tableHead           :: R.Element
  , tableBody           :: Array R.Element
  )
24

25
type Row = { row :: R.Element, delete :: Boolean }
26
type Rows = L.List Row
Nicolas Pouillard's avatar
Nicolas Pouillard committed
27

28 29
type OrderBy = Maybe (OrderByDirection ColumnName)

30
type Params = { offset :: Int, limit :: Int, orderBy :: OrderBy }
31 32 33

newtype ColumnName = ColumnName String

34 35 36 37 38
derive instance genericColumnName :: Generic ColumnName _

instance showColumnName :: Show ColumnName where
  show = genericShow

39 40 41 42 43 44
derive instance eqColumnName :: Eq ColumnName

columnName :: ColumnName -> String
columnName (ColumnName c) = c

data OrderByDirection a = ASC a | DESC a
Nicolas Pouillard's avatar
Nicolas Pouillard committed
45

46 47 48 49 50
derive instance genericOrderByDirection :: Generic (OrderByDirection a) _

instance showOrderByDirection :: Show a => Show (OrderByDirection a) where
  show = genericShow

51 52
derive instance eqOrderByDirection :: Eq a => Eq (OrderByDirection a)

53
type Props =
54
  ( colNames     :: Array ColumnName
55 56
  , wrapColElts  :: ColumnName -> Array R.Element -> Array R.Element
                 -- ^ Use `const identity` as a default behavior.
Nicolas Pouillard's avatar
Nicolas Pouillard committed
57
  , totalRecords :: Int
58
  , params       :: R.State Params
59
  , rows         :: Rows
60
  , container    :: Record TableContainerProps -> R.Element
Nicolas Pouillard's avatar
Nicolas Pouillard committed
61 62 63
  )

type State =
64 65 66
  { page     :: Int
  , pageSize :: PageSizes
  , orderBy  :: OrderBy
Nicolas Pouillard's avatar
Nicolas Pouillard committed
67 68
  }

69 70 71 72 73 74
paramsState :: Params -> State
paramsState {offset, limit, orderBy} = {pageSize, page, orderBy}
  where
    pageSize = int2PageSizes limit
    page = offset / limit + 1

75 76 77 78 79 80 81 82 83 84 85 86 87
stateParams :: State -> Params
stateParams {pageSize, page, orderBy} = {offset, limit, orderBy}
  where
    limit = pageSizes2Int pageSize
    offset = limit * (page - 1)

type TableHeaderLayoutProps =
  ( title :: String
  , desc  :: String
  , query :: String
  , date  :: String
  , user  :: String
  )
Nicolas Pouillard's avatar
Nicolas Pouillard committed
88

89
initialParams :: Params
90
initialParams = stateParams {page: 1, pageSize: PS10, orderBy: Nothing}
91 92 93 94 95 96
-- TODO: Not sure this is the right place for this

tableHeaderLayout :: Record TableHeaderLayoutProps -> R.Element
tableHeaderLayout props = R.createElement tableHeaderLayoutCpt props []

tableHeaderLayoutCpt :: R.Component TableHeaderLayoutProps
97
tableHeaderLayoutCpt = R.hooksComponent "G.C.Table.tableHeaderLayout" cpt
98 99
  where
    cpt {title, desc, query, date, user} _ =
100
      pure $ R.fragment
101
      [ R2.row
102 103 104 105
        [ H.div {className: "col-md-3"} [ H.h3 {} [H.text title] ]
        , H.div {className: "col-md-9"}
          [ H.hr {style: {height: "2px", backgroundColor: "black"}} ]
        ]
106
      , R2.row
107 108 109 110 111 112 113 114 115
        [ H.div {className: "jumbotron1", style: {padding: "12px 0px 20px 12px"}}
          [ H.div {className: "col-md-8 content"}
            [ H.p {}
              [ H.i {className: "glyphicon glyphicon-globe"} []
              , H.text $ " " <> desc
          ]
            , H.p {}
              [ H.i {className: "glyphicon glyphicon-zoom-in"} []
              , H.text $ " " <> query
116
              ]
117 118 119 120 121
            ]
          , H.div {className: "col-md-4 content"}
            [ H.p {}
              [ H.i {className: "glyphicon glyphicon-calendar"} []
              , H.text $ " " <> date
122
              ]
123 124 125 126 127 128
            , H.p {}
              [ H.i {className: "glyphicon glyphicon-user"} []
              , H.text $ " " <> user
              ]
            ]
          ]
129
        ]
130 131 132 133
      ]
  
table :: Record Props -> R.Element
table props = R.createElement tableCpt props []
134

135
tableCpt :: R.Component Props
136
tableCpt = R.hooksComponent "G.C.Table.table" cpt
Nicolas Pouillard's avatar
Nicolas Pouillard committed
137
  where
138
    cpt {container, colNames, wrapColElts, totalRecords, rows, params} _ = do
139
      let
140 141
        state = paramsState $ fst params
        ps = pageSizes2Int state.pageSize
142 143 144 145
        totalPages = (totalRecords / ps) + min 1 (totalRecords `mod` ps)
        colHeader :: ColumnName -> R.Element
        colHeader c = H.th {scope: "col"} [ H.b {} cs ]
          where
146
            lnk mc = effectLink $ snd params $ _ { orderBy = mc }
147 148
            cs :: Array R.Element
            cs =
149
              wrapColElts c $
150
              case state.orderBy of
151 152
                Just (ASC d)  | c == d -> [lnk (Just (DESC c)) "ASC ", lnk Nothing (columnName c)]
                Just (DESC d) | c == d -> [lnk (Just (ASC  c)) "DESC ",  lnk Nothing (columnName c)]
153
                _ -> [lnk (Just (ASC c)) (columnName c)]
154
      pure $ container
155
        { pageSizeControl: sizeDD { params }
156 157
        , pageSizeDescription: textDescription state.page state.pageSize totalRecords
        , paginationLinks: pagination params totalPages
158
        , tableBody: map _.row $ A.fromFoldable rows
159
        , tableHead: H.tr {} (colHeader <$> colNames)
160
        }
Nicolas Pouillard's avatar
Nicolas Pouillard committed
161

162 163 164
makeRow :: Array R.Element -> R.Element
makeRow els = H.tr {} $ (\c -> H.td {} [c]) <$> els

165 166 167 168 169 170

type FilterRowsParams =
  (
    params :: Params
  )

171
filterRows :: forall a. Record FilterRowsParams -> L.List a -> L.List a
172 173
filterRows { params: { limit, offset, orderBy } } rs = newRs
  where
174
    newRs = L.take limit $ L.drop offset $ rs
175

176 177
defaultContainer :: {title :: String} -> Record TableContainerProps -> R.Element
defaultContainer {title} props = R.fragment
178
  [ R2.row
179 180 181
    [ H.div {className: "col-md-4"} [ props.pageSizeDescription ]
    , H.div {className: "col-md-4"} [ props.paginationLinks ]
    , H.div {className: "col-md-4"} [ props.pageSizeControl ]
182
    ]
183 184 185
  , H.table {className: "table"}
    [ H.thead {className: "thead-dark"} [ props.tableHead ]
    , H.tbody {} props.tableBody
186 187
    ]
  ]
Nicolas Pouillard's avatar
Nicolas Pouillard committed
188

189
-- TODO: this needs to be in Gargantext.Pages.Corpus.Graph.Tabs
190
graphContainer :: {title :: String} -> Record TableContainerProps -> R.Element
191
graphContainer {title} props =
192 193 194 195 196
  -- TODO title in tabs name (above)
  H.table {className: "table"}
  [ H.thead {className: "thead-dark"} [ props.tableHead ]
  , H.tbody {} props.tableBody
  ]
197 198 199 200
   -- TODO better rendering of the paginationLinks
   -- , props.pageSizeControl
   -- , props.pageSizeDescription
   -- , props.paginationLinks
201

202 203 204 205 206 207 208 209 210 211
type SizeDDProps =
  (
    params :: R.State Params
  )

sizeDD :: Record SizeDDProps -> R.Element
sizeDD p = R.createElement sizeDDCpt p []

sizeDDCpt :: R.Component SizeDDProps
sizeDDCpt = R.hooksComponent "G.C.T.sizeDD" cpt
212
  where
213 214 215 216 217 218 219 220 221 222 223 224 225
    cpt {params: params /\ setParams} _ = do
      pure $ H.span {} [
        R2.select { className, defaultValue: show pageSize, on: {change} } sizes
      ]
      where
        {pageSize} = paramsState params
        className = "form-control"
        change e = do
          let ps = string2PageSize $ R2.unsafeEventValue e
          setParams $ \p -> stateParams $ (paramsState p) { pageSize = ps }
        sizes = map option pageSizes
        option size = H.option {value} [H.text value]
          where value = show size
Nicolas Pouillard's avatar
Nicolas Pouillard committed
226

227 228 229
textDescription :: Int -> PageSizes -> Int -> R.Element
textDescription currPage pageSize totalRecords =
  H.div {className: "row1"} [ H.div {className: ""} [ H.text msg ] ] -- TODO or col-md-6 ?
230
  where
231 232 233 234 235
    start = (currPage - 1) * pageSizes2Int pageSize + 1
    end' = currPage * pageSizes2Int pageSize
    end  = if end' > totalRecords then totalRecords else end'
    msg = "Showing " <> show start <> " to " <> show end <> " of " <> show totalRecords

236 237
pagination :: R.State Params -> Int -> R.Element
pagination (params /\ setParams) tp =
238 239
  H.span {} $
    [ H.text " ", prev, first, ldots]
Nicolas Pouillard's avatar
Nicolas Pouillard committed
240 241 242
    <>
    lnums
    <>
243
    [H.b {} [H.text $ " " <> show page <> " "]]
Nicolas Pouillard's avatar
Nicolas Pouillard committed
244 245 246 247 248
    <>
    rnums
    <>
    [ rdots, last, next ]
    where
249 250 251
      {page} = paramsState params
      changePage page = setParams $ \p -> stateParams $ (paramsState p) { page = page }
      prev = if page == 1 then
252
               H.text " Prev. "
253
             else
254 255
               changePageLink (page - 1) "Prev."
      next = if page == tp then
256
               H.text " Next "
257
             else
258 259
               changePageLink (page + 1) "Next"
      first = if page == 1 then
260
                H.text ""
261 262
              else
                changePageLink' 1
263
      last = if page == tp then
264
               H.text ""
Nicolas Pouillard's avatar
Nicolas Pouillard committed
265
             else
266
               changePageLink' tp
267
      ldots = if page >= 5 then
268
                H.text " ... "
Nicolas Pouillard's avatar
Nicolas Pouillard committed
269
                else
270
                H.text ""
271
      rdots = if page + 3 < tp then
272
                H.text " ... "
Nicolas Pouillard's avatar
Nicolas Pouillard committed
273
                else
274
                H.text ""
275 276
      lnums = map changePageLink' $ A.filter (1  < _) [page - 2, page - 1]
      rnums = map changePageLink' $ A.filter (tp > _) [page + 1, page + 2]
Nicolas Pouillard's avatar
Nicolas Pouillard committed
277

278 279 280 281
      changePageLink :: Int -> String -> R.Element
      changePageLink i s =
        H.span {}
          [ H.text " "
282
          , effectLink (changePage i) s
283
          , H.text " "
284 285
          ]

286
      changePageLink' :: Int -> R.Element
287
      changePageLink' i = changePageLink i (show i)
Nicolas Pouillard's avatar
Nicolas Pouillard committed
288

289
data PageSizes = PS10 | PS20 | PS50 | PS100 | PS200
Nicolas Pouillard's avatar
Nicolas Pouillard committed
290 291 292 293 294 295 296 297

derive instance eqPageSizes :: Eq PageSizes

instance showPageSize :: Show PageSizes where
  show PS10  = "10"
  show PS20  = "20"
  show PS50  = "50"
  show PS100 = "100"
298
  show PS200 = "200"
Nicolas Pouillard's avatar
Nicolas Pouillard committed
299

300 301 302
int2PageSizes :: Int -> PageSizes
int2PageSizes i = string2PageSize $ show i

Nicolas Pouillard's avatar
Nicolas Pouillard committed
303 304 305 306 307
pageSizes2Int :: PageSizes -> Int
pageSizes2Int PS10  = 10
pageSizes2Int PS20  = 20
pageSizes2Int PS50  = 50
pageSizes2Int PS100 = 100
308
pageSizes2Int PS200 = 200
Nicolas Pouillard's avatar
Nicolas Pouillard committed
309

310 311
pageSizes :: Array PageSizes
pageSizes = [PS10, PS20, PS50, PS100, PS200]
Nicolas Pouillard's avatar
Nicolas Pouillard committed
312 313 314 315 316 317

string2PageSize :: String -> PageSizes
string2PageSize "10" = PS10
string2PageSize "20" = PS20
string2PageSize "50" = PS50
string2PageSize "100" = PS100
318
string2PageSize "200" = PS200
Nicolas Pouillard's avatar
Nicolas Pouillard committed
319
string2PageSize _    = PS10