Commit c36604c7 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski Committed by Alexandre Delanoë

[ngrams] make table work in offline mode

parent 717f6120
...@@ -143,7 +143,7 @@ tableContainerCpt { dispatch ...@@ -143,7 +143,7 @@ tableContainerCpt { dispatch
, tabNgramType , tabNgramType
} = R.hooksComponent "G.C.NT.tableContainer" cpt } = R.hooksComponent "G.C.NT.tableContainer" cpt
where where
cpt props _ = cpt props _ = do
pure $ H.div {className: "container-fluid"} [ pure $ H.div {className: "container-fluid"} [
H.div {className: "jumbotron1"} H.div {className: "jumbotron1"}
[ R2.row [ R2.row
...@@ -457,22 +457,39 @@ mainNgramsTable :: Record MainNgramsTableProps -> R.Element ...@@ -457,22 +457,39 @@ mainNgramsTable :: Record MainNgramsTableProps -> R.Element
mainNgramsTable props = R.createElement mainNgramsTableCpt props [] mainNgramsTable props = R.createElement mainNgramsTableCpt props []
mainNgramsTableCpt :: R.Component MainNgramsTableProps mainNgramsTableCpt :: R.Component MainNgramsTableProps
mainNgramsTableCpt = R.hooksComponent "MainNgramsTable" cpt mainNgramsTableCpt = R.hooksComponent "G.C.NT.mainNgramsTable" cpt
where where
cpt {nodeId, defaultListId, tabType, session, tabNgramType, withAutoUpdate} _ = do cpt {nodeId, defaultListId, tabType, session, tabNgramType, withAutoUpdate} _ = do
path /\ setPath <- R.useState' $ initialPageParams session nodeId [defaultListId] tabType let path = initialPageParams session nodeId [defaultListId] tabType
let paint versioned = loadedNgramsTable' {
path: path /\ setPath
, tabNgramType
, versioned
, withAutoUpdate
}
pure $ loader path loadNgramsTableAll \loaded -> do pure $ loader path loadNgramsTableAll \loaded -> do
case Map.lookup tabType loaded of case Map.lookup tabType loaded of
Just (versioned :: VersionedNgramsTable) -> paint versioned Just (versioned :: VersionedNgramsTable) -> mainNgramsTablePaint {path, tabNgramType, versioned, withAutoUpdate}
Nothing -> loadingSpinner {} Nothing -> loadingSpinner {}
type MainNgramsTablePaintProps =
(
path :: PageParams
, tabNgramType :: CTabNgramType
, versioned :: VersionedNgramsTable
, withAutoUpdate :: Boolean
)
mainNgramsTablePaint :: Record MainNgramsTablePaintProps -> R.Element
mainNgramsTablePaint p = R.createElement mainNgramsTablePaintCpt p []
mainNgramsTablePaintCpt :: R.Component MainNgramsTablePaintProps
mainNgramsTablePaintCpt = R.hooksComponent "G.C.NT.mainNgramsTablePaint" cpt
where
cpt {path, tabNgramType, versioned, withAutoUpdate} _ = do
pathS <- R.useState' path
pure $ loadedNgramsTable' {
path: pathS
, tabNgramType
, versioned
, withAutoUpdate
}
type NgramsDepth = {ngrams :: NgramsTerm, depth :: Int} type NgramsDepth = {ngrams :: NgramsTerm, depth :: Int}
type NgramsClick = NgramsDepth -> Maybe (Effect Unit) type NgramsClick = NgramsDepth -> Maybe (Effect Unit)
...@@ -641,5 +658,5 @@ nextTermList StopTerm = CandidateTerm ...@@ -641,5 +658,5 @@ nextTermList StopTerm = CandidateTerm
nextTermList CandidateTerm = GraphTerm nextTermList CandidateTerm = GraphTerm
optps1 :: forall a. Show a => { desc :: String, mval :: Maybe a } -> R.Element optps1 :: forall a. Show a => { desc :: String, mval :: Maybe a } -> R.Element
optps1 { desc, mval } = H.option { defaultValue: value } [H.text desc] optps1 { desc, mval } = H.option { value: value } [H.text desc]
where value = maybe "" show mval where value = maybe "" show mval
...@@ -134,31 +134,25 @@ tableCpt :: R.Component Props ...@@ -134,31 +134,25 @@ tableCpt :: R.Component Props
tableCpt = R.hooksComponent "G.C.Table.table" cpt tableCpt = R.hooksComponent "G.C.Table.table" cpt
where where
cpt {container, colNames, wrapColElts, totalRecords, rows, params} _ = do cpt {container, colNames, wrapColElts, totalRecords, rows, params} _ = do
let initialState = paramsState $ fst params
pageSize@(pageSize' /\ setPageSize) <- R.useState' initialState.pageSize
(page /\ setPage) <- R.useState' initialState.page
(orderBy /\ setOrderBy) <- R.useState' initialState.orderBy
let let
state = {pageSize: pageSize', orderBy, page} state = paramsState $ fst params
ps = pageSizes2Int pageSize' ps = pageSizes2Int state.pageSize
totalPages = (totalRecords / ps) + min 1 (totalRecords `mod` ps) totalPages = (totalRecords / ps) + min 1 (totalRecords `mod` ps)
colHeader :: ColumnName -> R.Element colHeader :: ColumnName -> R.Element
colHeader c = H.th {scope: "col"} [ H.b {} cs ] colHeader c = H.th {scope: "col"} [ H.b {} cs ]
where where
lnk mc = effectLink (setOrderBy (const mc)) lnk mc = effectLink $ snd params $ _ { orderBy = mc }
cs :: Array R.Element cs :: Array R.Element
cs = cs =
wrapColElts c $ wrapColElts c $
case orderBy of case state.orderBy of
Just (ASC d) | c == d -> [lnk (Just (DESC c)) "ASC ", lnk Nothing (columnName c)] 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)] Just (DESC d) | c == d -> [lnk (Just (ASC c)) "DESC ", lnk Nothing (columnName c)]
_ -> [lnk (Just (ASC c)) (columnName c)] _ -> [lnk (Just (ASC c)) (columnName c)]
R.useEffect2' (fst params) state do
when (fst params /= stateParams state) $ (snd params) (const $ stateParams state)
pure $ container pure $ container
{ pageSizeControl: sizeDD pageSize { pageSizeControl: sizeDD params
, pageSizeDescription: textDescription page pageSize' totalRecords , pageSizeDescription: textDescription state.page state.pageSize totalRecords
, paginationLinks: pagination setPage totalPages page , paginationLinks: pagination params totalPages
, tableHead: H.tr {} (colHeader <$> colNames) , tableHead: H.tr {} (colHeader <$> colNames)
, tableBody: map _.row rows , tableBody: map _.row rows
} }
...@@ -203,12 +197,13 @@ graphContainer {title} props = ...@@ -203,12 +197,13 @@ graphContainer {title} props =
-- , props.pageSizeDescription -- , props.pageSizeDescription
-- , props.paginationLinks -- , props.paginationLinks
sizeDD :: R.State PageSizes -> R.Element sizeDD :: R.State Params -> R.Element
sizeDD (ps /\ setPageSize) = sizeDD (params /\ setParams) =
H.span {} [ R2.select { className, defaultValue: ps, on: {change} } sizes ] H.span {} [ R2.select { className, defaultValue: pageSize, on: {change} } sizes ]
where where
{pageSize} = paramsState params
className = "form-control" className = "form-control"
change e = setPageSize $ const (string2PageSize $ R2.unsafeEventValue e) change e = setParams $ \p -> stateParams $ (paramsState p) { pageSize = string2PageSize $ R2.unsafeEventValue e }
sizes = map option pageSizes sizes = map option pageSizes
option size = H.option {value} [H.text value] option size = H.option {value} [H.text value]
where value = show size where value = show size
...@@ -222,51 +217,53 @@ textDescription currPage pageSize totalRecords = ...@@ -222,51 +217,53 @@ textDescription currPage pageSize totalRecords =
end = if end' > totalRecords then totalRecords else end' end = if end' > totalRecords then totalRecords else end'
msg = "Showing " <> show start <> " to " <> show end <> " of " <> show totalRecords msg = "Showing " <> show start <> " to " <> show end <> " of " <> show totalRecords
pagination :: (R2.Setter Int) -> Int -> Int -> R.Element pagination :: R.State Params -> Int -> R.Element
pagination changePage tp cp = pagination (params /\ setParams) tp =
H.span {} $ H.span {} $
[ H.text " ", prev, first, ldots] [ H.text " ", prev, first, ldots]
<> <>
lnums lnums
<> <>
[H.b {} [H.text $ " " <> show cp <> " "]] [H.b {} [H.text $ " " <> show page <> " "]]
<> <>
rnums rnums
<> <>
[ rdots, last, next ] [ rdots, last, next ]
where where
prev = if cp == 1 then {page} = paramsState params
changePage page = setParams $ \p -> stateParams $ (paramsState p) { page = page }
prev = if page == 1 then
H.text " Prev. " H.text " Prev. "
else else
changePageLink (cp - 1) "Prev." changePageLink (page - 1) "Prev."
next = if cp == tp then next = if page == tp then
H.text " Next " H.text " Next "
else else
changePageLink (cp + 1) "Next" changePageLink (page + 1) "Next"
first = if cp == 1 then first = if page == 1 then
H.text "" H.text ""
else else
changePageLink' 1 changePageLink' 1
last = if cp == tp then last = if page == tp then
H.text "" H.text ""
else else
changePageLink' tp changePageLink' tp
ldots = if cp >= 5 then ldots = if page >= 5 then
H.text " ... " H.text " ... "
else else
H.text "" H.text ""
rdots = if cp + 3 < tp then rdots = if page + 3 < tp then
H.text " ... " H.text " ... "
else else
H.text "" H.text ""
lnums = map changePageLink' $ A.filter (1 < _) [cp - 2, cp - 1] lnums = map changePageLink' $ A.filter (1 < _) [page - 2, page - 1]
rnums = map changePageLink' $ A.filter (tp > _) [cp + 1, cp + 2] rnums = map changePageLink' $ A.filter (tp > _) [page + 1, page + 2]
changePageLink :: Int -> String -> R.Element changePageLink :: Int -> String -> R.Element
changePageLink i s = changePageLink i s =
H.span {} H.span {}
[ H.text " " [ H.text " "
, effectLink (changePage (const i)) s , effectLink (changePage i) s
, H.text " " , H.text " "
] ]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment