1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
171
172
173
174
175
176
177
178
179
180
181
182
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
304
305
306
307
308
309
310
module Gargantext.Components.Document.Layout
( layout
) where
import Gargantext.Prelude
import Data.Maybe (Maybe(..), fromMaybe, isJust, maybe)
import Data.Ord (greaterThan)
import Data.String (length)
import Data.String as String
import Data.Tuple.Nested ((/\))
import Gargantext.Components.Annotation.Field as AnnotatedField
import Gargantext.Components.Annotation.Types as AFT
import Gargantext.Components.AutoUpdate (autoUpdate)
import Gargantext.Components.Bootstrap as B
import Gargantext.Components.Bootstrap.Types (ComponentStatus(..), SpinnerTheme(..))
import Gargantext.Components.Document.Types (DocPath, Document(..), LoadedData, initialState)
import Gargantext.Components.NgramsTable.AutoSync (useAutoSync)
import Gargantext.Components.Node (NodePoly(..))
import Gargantext.Core.NgramsTable.Functions (addNewNgramA, applyNgramsPatches, coreDispatch, findNgramRoot, setTermListA)
import Gargantext.Core.NgramsTable.Types (CoreAction(..), Versioned(..), replace)
import Gargantext.Hooks.FirstEffect (useFirstEffect')
import Gargantext.Utils ((?))
import Gargantext.Utils as U
import Gargantext.Utils.Reactix as R2
import Reactix as R
import Reactix.DOM.HTML as H
import Toestand as T
type Props =
( loaded :: LoadedData
, path :: DocPath
| Options
)
type Options =
( sideControlsSlot :: Maybe R.Element
)
options :: Record Options
options =
{ sideControlsSlot: Nothing
}
here :: R2.Here
here = R2.here "Gargantext.Components.Document.layout"
layout :: forall r. R2.OptLeaf Options Props r
layout = R2.optLeaf layoutCpt options
layoutCpt :: R.Component Props
layoutCpt = here.component "main" cpt where
-- Component
cpt { path
, loaded:
loaded@{ ngramsTable: Versioned
{ data: initTable }
, document: NodePoly
{ hyperdata: Document doc
}
}
, sideControlsSlot
} _ = do
-- | States
-- |
state'@{ ngramsLocalPatch } /\ state <-
R2.useBox' $ initialState { loaded }
mode' /\ mode <- R2.useBox' AFT.AdditionMode
forceAdditionMode' /\ forceAdditionMode <- R2.useBox' false
let dispatch = coreDispatch path state
{ onPending, result } <- useAutoSync { state, action: dispatch }
onPending' <- R2.useLive' onPending
result' <- R2.useLive' result
-- | Computed
-- |
let
withAutoUpdate = false
ngrams = applyNgramsPatches state' initTable
annotate text = AnnotatedField.annotatedField
{ ngrams
, setTermList
, text
, mode: mode'
}
setTermListOrAddA ngram Nothing =
addNewNgramA ngram
setTermListOrAddA ngram (Just oldList) =
setTermListA ngram <<< replace oldList
setTermList ngram mOldList =
dispatch <<< setTermListOrAddA (findNgramRoot ngrams ngram) mOldList
hasAbstract = maybe false (not String.null) doc.abstract
-- | Hooks
-- |
-- (?) Limit large document feature with empirical length value
-- see #423
useFirstEffect' do
let len = maybe 0 (length) doc.abstract
if (len `greaterThan` 4500)
then
T.write_ true forceAdditionMode
*> T.write_ AFT.AdditionMode mode
else
T.write_ false forceAdditionMode
*> T.write_ AFT.EditionMode mode
-- | Behaviors
-- |
let
onModeChange = read >>> fromMaybe AFT.EditionMode >>> flip T.write_ mode
-- | Render
-- |
pure $
H.div
{ className: "document-layout" }
--DEBUG
--[ H.pre { rows: 30 } [
-- H.text (stringifyWithIndent 2 (encodeJson (fst state)))
-- ] ] <>
[
-- Header
H.div
{ className: "document-layout__header" }
[
H.div
{ className: "document-layout__main-controls" }
[
-- Viewing mode
B.wad
[ "d-flex", "align-items-center", "width-auto" ]
[
H.label
{ className: "mr-1"
}
[
B.icon
{ name: "tags" }
]
,
B.formSelect
{ value: show mode'
, callback: onModeChange
, status: forceAdditionMode' ?
Idled $
Enabled
}
[
H.option
{ value: show AFT.AdditionMode }
[ H.text "Add terms only" ]
,
H.option
{ value: show AFT.EditionMode }
[ H.text "Add and edit terms" ]
]
]
,
R2.when forceAdditionMode' $
B.wad
[ "color-warning", "font-size-100", "mx-2", "inline-block" ]
[
H.text "limited term feature due to abstract length"
]
,
R2.when withAutoUpdate $
-- (?) purpose? would still working with current code?
autoUpdate
{ duration: 5000
, effect: dispatch $ Synchronize
{ afterSync: \_ -> pure unit
}
}
-- @NOTE #386: revert manual for automatic sync
-- syncResetButtons
-- { afterSync
-- , ngramsLocalPatch
-- , performAction: dispatch
-- }
]
,
H.div
{ className: "document-layout__side-controls" }
[
-- Saving informations
H.div
{ className: "document-layout__saving" }
[
R2.when' onPending'
[
B.spinner
{ theme: GrowTheme
, className: "document-layout__saving__spinner"
}
]
,
R2.when (not onPending' && isJust result') $
B.icon
{ name: "check"
, className: "document-layout__saving__icon"
}
]
,
R2.fromMaybe sideControlsSlot identity
]
]
,
-- Body
H.div
{ className: "document-layout__body" }
[
B.div'
{ className: "document-layout__separator-label" }
"Title"
,
H.div
{ className: "document-layout__title" }
[
annotate doc.title
]
,
R2.fromMaybe doc.authors \authors ->
H.div
{ className: "document-layout__authors" }
[
B.div'
{ className: "document-layout__authors__label" }
"Authors"
,
H.div
{ className: "document-layout__authors__content" }
[
-- @NOTE #386: annotate for "Authors" ngrams list
annotate (Just authors)
]
]
,
R2.fromMaybe doc.source \source ->
H.div
{ className: "document-layout__source" }
[
B.div'
{ className: "document-layout__source__label" }
"Source"
,
B.div'
{ className: "document-layout__source__content" }
source
]
,
H.div
{ className: "document-layout__date" }
[
B.div'
{ className: "document-layout__date__label" }
"Date"
,
B.div'
{ className: "document-layout__date__content" }
(publicationDate $ Document doc)
]
,
R2.when hasAbstract $
H.div
{ className: "document-layout__abstract" }
[
B.div'
{ className: "document-layout__separator-label" }
"Abstract"
,
H.div
{ className: "document-layout__abstract__content" }
[
annotate doc.abstract
]
]
-- (?) remove "Full text" block (unused feature for now,
-- see #334)
-- , H.div { className: "jumbotron" } [ H.p {} [ H.text "Empty Full Text" ] ]
]
]
-------------------------------------------------------------
publicationDate :: Document -> String
publicationDate (Document {publication_year: Nothing}) = ""
publicationDate (Document {publication_year: Just py, publication_month: Nothing}) = U.zeroPad 2 py
publicationDate (Document {publication_year: Just py, publication_month: Just pm, publication_day: Nothing}) = (U.zeroPad 2 py) <> "-" <> (U.zeroPad 2 pm)
publicationDate (Document {publication_year: Just py, publication_month: Just pm, publication_day: Just pd}) = (U.zeroPad 2 py) <> "-" <> (U.zeroPad 2 pm) <> "-" <> (U.zeroPad 2 pd)