@ea-lab/reactive-json 0.0.19 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/README.md +1 -1
  2. package/dist/esm/types/component/action/HashChangeListener.d.ts +9 -0
  3. package/{lib/component/action/Hide.jsx → dist/esm/types/component/action/Hide.d.ts} +2 -6
  4. package/dist/esm/types/component/action/MessageListener.d.ts +9 -0
  5. package/dist/esm/types/component/action/Popover.d.ts +5 -0
  6. package/dist/esm/types/component/action/ReactOnEvent.d.ts +26 -0
  7. package/dist/esm/types/component/action/Redirect.d.ts +9 -0
  8. package/dist/esm/types/component/action/Tooltip.d.ts +5 -0
  9. package/{lib/component/action/VisuallyHide.jsx → dist/esm/types/component/action/VisuallyHide.d.ts} +1 -4
  10. package/dist/esm/types/component/element/form/CheckBoxField.d.ts +7 -0
  11. package/dist/esm/types/component/element/form/DateField.d.ts +2 -0
  12. package/dist/esm/types/component/element/form/NumberField.d.ts +7 -0
  13. package/dist/esm/types/component/element/form/SelectField.d.ts +7 -0
  14. package/dist/esm/types/component/element/form/TextAreaField.d.ts +6 -0
  15. package/dist/esm/types/component/element/form/TextField.d.ts +6 -0
  16. package/dist/esm/types/component/element/form/formElementsCommon.d.ts +23 -0
  17. package/dist/esm/types/component/element/html/AccordionItem.d.ts +16 -0
  18. package/dist/esm/types/component/element/html/FolderSortableTree.d.ts +6 -0
  19. package/dist/esm/types/component/element/html/FormatNumeral.d.ts +7 -0
  20. package/dist/esm/types/component/element/html/Html.d.ts +8 -0
  21. package/dist/esm/types/component/element/html/LabelFromValue.d.ts +22 -0
  22. package/dist/esm/types/component/element/html/Modal.d.ts +6 -0
  23. package/dist/esm/types/component/element/html/ModalForm.d.ts +9 -0
  24. package/dist/esm/types/component/element/html/Paragraph.d.ts +5 -0
  25. package/dist/esm/types/component/element/html/PreformattedMarkup.d.ts +7 -0
  26. package/dist/esm/types/component/element/html/SortableTreeItemCollapseButton.d.ts +9 -0
  27. package/dist/esm/types/component/element/html/Tabs.d.ts +18 -0
  28. package/dist/esm/types/component/element/special/BootstrapElement.d.ts +10 -0
  29. package/dist/esm/types/component/element/special/Count.d.ts +13 -0
  30. package/dist/esm/types/component/element/special/DataFilter.d.ts +11 -0
  31. package/dist/esm/types/component/element/special/DelayedActions.d.ts +25 -0
  32. package/dist/esm/types/component/element/special/PageControls.d.ts +9 -0
  33. package/dist/esm/types/component/element/special/Phantom.d.ts +17 -0
  34. package/dist/esm/types/component/element/special/Switch.d.ts +6 -0
  35. package/dist/esm/types/component/hook/usePagination.d.ts +30 -0
  36. package/dist/esm/types/component/index.d.ts +5 -0
  37. package/dist/esm/types/component/reaction/addData.d.ts +6 -0
  38. package/dist/esm/types/component/reaction/fetchData.d.ts +8 -0
  39. package/dist/esm/types/component/reaction/moveData.d.ts +6 -0
  40. package/dist/esm/types/component/reaction/postMessage.d.ts +6 -0
  41. package/dist/esm/types/component/reaction/redirectNow.d.ts +6 -0
  42. package/dist/esm/types/component/reaction/removeData.d.ts +6 -0
  43. package/dist/esm/types/component/reaction/setClipboardData.d.ts +6 -0
  44. package/dist/esm/types/component/reaction/setData.d.ts +6 -0
  45. package/dist/esm/types/component/reaction/submitData.d.ts +8 -0
  46. package/dist/esm/types/component/reaction/triggerEvent.d.ts +6 -0
  47. package/dist/esm/types/component/utility/formatString.d.ts +17 -0
  48. package/dist/esm/types/engine/Actions.d.ts +19 -0
  49. package/dist/esm/types/engine/ComponentCollector.d.ts +12 -0
  50. package/{lib/engine/EventDispatcherContext.jsx → dist/esm/types/engine/EventDispatcherContext.d.ts} +4 -7
  51. package/dist/esm/types/engine/EventDispatcherProvider.d.ts +16 -0
  52. package/{lib/engine/GlobalDataContext.jsx → dist/esm/types/engine/GlobalDataContext.d.ts} +1 -4
  53. package/dist/esm/types/engine/GlobalDataContextProvider.d.ts +11 -0
  54. package/{lib/engine/PaginationContext.jsx → dist/esm/types/engine/PaginationContext.d.ts} +3 -4
  55. package/dist/esm/types/engine/PaginationProvider.d.ts +12 -0
  56. package/dist/esm/types/engine/ReactiveJsonRoot.d.ts +28 -0
  57. package/{lib/engine/TemplateContext.jsx → dist/esm/types/engine/TemplateContext.d.ts} +1 -4
  58. package/dist/esm/types/engine/TemplateSystem.d.ts +89 -0
  59. package/dist/esm/types/engine/View.d.ts +7 -0
  60. package/dist/esm/types/index.d.ts +6 -0
  61. package/dist/esm/types/main.d.ts +10 -0
  62. package/dist/index.cjs.js +9201 -0
  63. package/dist/index.cjs.js.map +1 -0
  64. package/dist/index.d.ts +44 -0
  65. package/dist/index.esm.js +9180 -0
  66. package/dist/index.esm.js.map +1 -0
  67. package/package.json +26 -15
  68. package/dist/reactive-json.css +0 -5
  69. package/dist/reactive-json.js +0 -47707
  70. package/dist/reactive-json.js.map +0 -1
  71. package/dist/reactive-json.umd.cjs +0 -366
  72. package/dist/reactive-json.umd.cjs.map +0 -1
  73. package/lib/component/action/HashChangeListener.jsx +0 -66
  74. package/lib/component/action/MessageListener.jsx +0 -62
  75. package/lib/component/action/Popover.jsx +0 -53
  76. package/lib/component/action/ReactOnEvent.jsx +0 -118
  77. package/lib/component/action/Redirect.jsx +0 -26
  78. package/lib/component/action/Tooltip.jsx +0 -27
  79. package/lib/component/element/form/CheckBoxField.jsx +0 -215
  80. package/lib/component/element/form/DateField.jsx +0 -42
  81. package/lib/component/element/form/NumberField.jsx +0 -29
  82. package/lib/component/element/form/SelectField.jsx +0 -130
  83. package/lib/component/element/form/TextAreaField.jsx +0 -48
  84. package/lib/component/element/form/TextField.jsx +0 -65
  85. package/lib/component/element/form/formElementsCommon.jsx +0 -54
  86. package/lib/component/element/html/AccordionItem.jsx +0 -42
  87. package/lib/component/element/html/FolderSortableTree.jsx +0 -307
  88. package/lib/component/element/html/FormatNumeral.jsx +0 -118
  89. package/lib/component/element/html/Html.jsx +0 -107
  90. package/lib/component/element/html/LabelFromValue.jsx +0 -89
  91. package/lib/component/element/html/Modal.jsx +0 -77
  92. package/lib/component/element/html/ModalForm.jsx +0 -30
  93. package/lib/component/element/html/Paragraph.jsx +0 -10
  94. package/lib/component/element/html/PreformattedMarkup.jsx +0 -54
  95. package/lib/component/element/html/SortableTreeItemCollapseButton.jsx +0 -20
  96. package/lib/component/element/html/Tabs.jsx +0 -55
  97. package/lib/component/element/special/BootstrapElement.jsx +0 -32
  98. package/lib/component/element/special/Count.jsx +0 -46
  99. package/lib/component/element/special/DataFilter.jsx +0 -156
  100. package/lib/component/element/special/DelayedActions.jsx +0 -119
  101. package/lib/component/element/special/PageControls.jsx +0 -19
  102. package/lib/component/element/special/Phantom.jsx +0 -25
  103. package/lib/component/element/special/Switch.jsx +0 -131
  104. package/lib/component/hook/usePagination.jsx +0 -184
  105. package/lib/component/reaction/addData.jsx +0 -23
  106. package/lib/component/reaction/fetchData.jsx +0 -83
  107. package/lib/component/reaction/moveData.jsx +0 -52
  108. package/lib/component/reaction/postMessage.jsx +0 -43
  109. package/lib/component/reaction/redirectNow.jsx +0 -17
  110. package/lib/component/reaction/removeData.jsx +0 -48
  111. package/lib/component/reaction/setClipboardData.jsx +0 -20
  112. package/lib/component/reaction/setData.jsx +0 -23
  113. package/lib/component/reaction/submitData.jsx +0 -136
  114. package/lib/component/reaction/triggerEvent.jsx +0 -62
  115. package/lib/component/utility/formatString.jsx +0 -59
  116. package/lib/engine/Actions.jsx +0 -392
  117. package/lib/engine/ComponentCollector.js +0 -28
  118. package/lib/engine/EventDispatcherProvider.jsx +0 -80
  119. package/lib/engine/GlobalDataContextProvider.jsx +0 -33
  120. package/lib/engine/PaginationProvider.jsx +0 -61
  121. package/lib/engine/ReactiveJsonRoot.jsx +0 -318
  122. package/lib/engine/TemplateSystem.jsx +0 -302
  123. package/lib/engine/View.jsx +0 -248
  124. package/lib/main.jsx +0 -52
@@ -1,392 +0,0 @@
1
- import {useContext} from "react";
2
- import GlobalDataContext from "./GlobalDataContext";
3
- import TemplateContext from "./TemplateContext";
4
- import {evaluateTemplateValue, isTemplateValue} from "./TemplateSystem";
5
-
6
- import HashChangeListener from "../component/action/HashChangeListener";
7
- import Hide from "../component/action/Hide";
8
- import MessageListener from "../component/action/MessageListener";
9
- import Popover from "../component/action/Popover";
10
- import ReactOnEvent, {reactionFunctions} from "../component/action/ReactOnEvent";
11
- import Redirect from "../component/action/Redirect";
12
- import Tooltip from "../component/action/Tooltip";
13
- import VisuallyHide from "../component/action/VisuallyHide";
14
- import {isEqual} from "lodash";
15
- import JSONPath from "jsonpath";
16
-
17
- /**
18
- * Contains the list of available actions in config.
19
- * @type {{}}
20
- */
21
- const actionsToEvaluate = {
22
- hide: Hide,
23
- popover: Popover,
24
- redirect: Redirect,
25
- tooltip: Tooltip,
26
- visuallyHide: VisuallyHide,
27
- };
28
-
29
- /**
30
- * Capitalizes the first letter.
31
- *
32
- * @param str
33
- * @returns {string}
34
- */
35
- const capitalizeFirstLetter = (str) => str && str.charAt(0).toUpperCase() + str.slice(1);
36
-
37
- /**
38
- * Checks whether the conditions are fulfilled.
39
- * @param {{andConditions, containedBy, containedByNot, contains, containsNot, is, isNot, orConditions, when, whenDataCountOf, ">", "<", ">=", "<=", compareAsDates}} condition
40
- * @param {object} templateContexts
41
- * @param {Map} additionalConditionHandlers
42
- * @returns {*}
43
- */
44
- export const isValid = (condition, templateContexts, additionalConditionHandlers) => {
45
- const {globalDataContext, templateContext} = templateContexts;
46
-
47
- if (Array.isArray(condition.andConditions)) {
48
- return condition.andConditions.reduce(
49
- (acc, cur) => {
50
- // All sub conditions must be true.
51
- return acc && isValid(cur, templateContexts, additionalConditionHandlers);
52
- },
53
- true
54
- );
55
- }
56
-
57
- if (Array.isArray(condition.orConditions)) {
58
- const orConditions = condition.orConditions;
59
-
60
- for (const condition of orConditions) {
61
- if (isValid(condition, templateContexts, additionalConditionHandlers)) {
62
- // A single condition is enough to validate.
63
- return true;
64
- }
65
- }
66
-
67
- return false;
68
- }
69
-
70
- if (condition.when !== undefined && !isTemplateValue(condition.when)) {
71
- return false;
72
- }
73
-
74
- const evaluateTemplateValueLocal = (toEvaluate) => {
75
- return evaluateTemplateValue({
76
- globalDataContext: globalDataContext,
77
- templateContext: templateContext,
78
- valueToEvaluate: toEvaluate,
79
- });
80
- }
81
-
82
- let valueToCompare;
83
-
84
- if (condition.when === undefined && condition.hasOwnProperty("whenDataCountOf")) {
85
- // Select where to count.
86
- let countFinalTemplateData;
87
-
88
- switch (condition.inContext) {
89
- case "root":
90
- countFinalTemplateData = globalDataContext.getRootContext().templateData;
91
- break;
92
-
93
- case "template":
94
- countFinalTemplateData = templateContext.templateData;
95
- break;
96
-
97
- default:
98
- countFinalTemplateData = globalDataContext.templateData;
99
- break;
100
- }
101
-
102
- valueToCompare = JSONPath.query(countFinalTemplateData, condition.whenDataCountOf).length;
103
- } else {
104
- // Other code may inject additional condition handler which can return the value to compare.
105
- valueToCompare = (() => {
106
- if (additionalConditionHandlers) {
107
- for (const [handlerId, additionalConditionHandler] of additionalConditionHandlers) {
108
- if (condition.hasOwnProperty(handlerId)) {
109
- // This additional condition handler will compute the value to compare.
110
- return additionalConditionHandler({
111
- condition,
112
- templateContexts,
113
- evaluateAgainstTemplates: (value) => {
114
- return evaluateTemplateValueLocal(value);
115
- }
116
- });
117
- }
118
- }
119
- }
120
-
121
- // Compute using the usual method.
122
- return evaluateTemplateValueLocal(condition.when);
123
- })();
124
- }
125
-
126
- if (condition.hasOwnProperty("isEmpty")) {
127
- const maybeInvert = val => condition.isEmpty === "not" ? !val : val;
128
-
129
- if (valueToCompare === null) {
130
- return maybeInvert(true);
131
- }
132
-
133
- switch (typeof valueToCompare) {
134
- case "undefined":
135
- return maybeInvert(true);
136
-
137
- case "string":
138
- return maybeInvert(valueToCompare.length === 0);
139
-
140
- case "object":
141
- return maybeInvert(Object.keys(valueToCompare).length === 0);
142
-
143
- default:
144
- return maybeInvert(!valueToCompare);
145
- }
146
- }
147
-
148
- // This can be useful when two differently formatted dates must be compared.
149
- const compareAsDates = condition.compareAsDates;
150
-
151
- if (condition.hasOwnProperty("isNot")) {
152
- return maybeDate(compareAsDates, valueToCompare) !== maybeDate(compareAsDates, evaluateTemplateValueLocal(condition.isNot));
153
- }
154
-
155
- if (condition.hasOwnProperty("is")) {
156
- return maybeDate(compareAsDates, valueToCompare) === maybeDate(compareAsDates, evaluateTemplateValueLocal(condition.is));
157
- }
158
-
159
- if (condition.hasOwnProperty("containsNot") || condition.hasOwnProperty("contains")) {
160
- // True if containsNot.
161
- const invertedMode = condition.hasOwnProperty("containsNot");
162
-
163
- const needleSource = invertedMode ? condition.containsNot : condition.contains;
164
-
165
- const needle = maybeDate(compareAsDates, evaluateTemplateValueLocal(needleSource));
166
-
167
- if (typeof valueToCompare === "string") {
168
- // Check as a string. Do a case-insensitive check.
169
- if (typeof needle !== "string") {
170
- // The types do not match.
171
- return invertedMode;
172
- }
173
-
174
- const result = valueToCompare.toLowerCase().indexOf(needle.toLowerCase()) !== -1;
175
-
176
- return invertedMode ? !result : result;
177
- } else {
178
- for (const item of Object.values(valueToCompare)) {
179
- if (isEqual(maybeDate(compareAsDates, item), needle)) {
180
- // The needle has been found. When in inverted mode (containsNot), return false.
181
- return !invertedMode;
182
- }
183
- }
184
- }
185
-
186
- // The needle has not been found. When in inverted mode (containsNot), return true.
187
- return invertedMode;
188
- }
189
-
190
- if (condition.hasOwnProperty("containedByNot") || condition.hasOwnProperty("containedBy")) {
191
- // True if containedByNot.
192
- const invertedMode = condition.hasOwnProperty("containedByNot");
193
-
194
- const needleSource = invertedMode ? condition.containedByNot : condition.containedBy;
195
-
196
- const container = maybeDate(compareAsDates, evaluateTemplateValueLocal(needleSource));
197
-
198
- if (typeof valueToCompare === "string") {
199
- // Check as a string. Do a case-insensitive check.
200
- if (typeof container !== "string") {
201
- // The types do not match.
202
- return invertedMode;
203
- }
204
-
205
- const result = container.toLowerCase().indexOf(valueToCompare.toLowerCase()) !== -1;
206
-
207
- return invertedMode ? !result : result;
208
- } else {
209
- for (const item of Object.values(container)) {
210
- if (isEqual(maybeDate(compareAsDates, item), valueToCompare)) {
211
- // The needle has been found. When in inverted mode (containsNot), return false.
212
- return !invertedMode;
213
- }
214
- }
215
- }
216
-
217
- // The needle has not been found. When in inverted mode (containsNot), return true.
218
- return invertedMode;
219
- }
220
-
221
- if (condition.hasOwnProperty(">")) {
222
- return maybeDate(compareAsDates, valueToCompare) > maybeDate(compareAsDates, evaluateTemplateValueLocal(condition[">"]));
223
- }
224
-
225
- if (condition.hasOwnProperty(">=")) {
226
- return maybeDate(compareAsDates, valueToCompare) >= maybeDate(compareAsDates, evaluateTemplateValueLocal(condition[">="]));
227
- }
228
-
229
- if (condition.hasOwnProperty("<")) {
230
- return maybeDate(compareAsDates, valueToCompare) < maybeDate(compareAsDates, evaluateTemplateValueLocal(condition["<"]));
231
- }
232
-
233
- if (condition.hasOwnProperty("<=")) {
234
- return maybeDate(compareAsDates, valueToCompare) <= maybeDate(compareAsDates, evaluateTemplateValueLocal(condition["<="]));
235
- }
236
-
237
- // No condition means always valid.
238
- return true;
239
- };
240
-
241
- /**
242
- * Gets the actions to execute for the current component.
243
- * @param {Array} actions
244
- * @param {object} templateContexts
245
- * @returns {*[]}
246
- */
247
- const getActionsToExecute = (actions, templateContexts) => {
248
- const result = [];
249
-
250
- let requiresReactionComponent = false;
251
-
252
- const reactionFunctionProps = {};
253
-
254
- if (!Array.isArray(actions)) {
255
- // Not a supported actions structure.
256
- // Dev note: we may also allow objects in the future, to allow specific overrides.
257
- return result;
258
- }
259
-
260
- // The index is useful to build the data path for the components created by the actions.
261
- // This is a requirement of the View components.
262
- // Dev note: could it be like the components, where we can specify string keys,
263
- // thus allowing objects in the actions definitions?
264
- for (const [index, item] of actions.entries()) {
265
- const what = item?.what ?? undefined;
266
-
267
- if (!what) {
268
- continue;
269
- }
270
-
271
- const Component = actionsToEvaluate[what];
272
- let reactionFunction = undefined;
273
-
274
- if (!Component) {
275
- // This is not a component. Maybe it's a reaction function...
276
- reactionFunction = reactionFunctions[what] ?? undefined;
277
-
278
- if (!reactionFunction) {
279
- // The component is unknown or not registered,
280
- // and it's not a reaction function.
281
- continue;
282
- }
283
-
284
- // This is a reaction function.
285
- if (item.on === undefined) {
286
- // Reaction functions are called only in response to events.
287
- // Dev note: evaluate if we can execute reaction functions on component mount,
288
- // with empty "on".
289
- continue;
290
- }
291
-
292
- if (!isValid(item, templateContexts)) {
293
- continue;
294
- }
295
-
296
- if (item.on === "message") {
297
- // "message" has a special handling. It adds the special MessageListener action component.
298
- // This is because the message event can only be listened to on the window object,
299
- // so it adds event listeners on the window object (not the current component).
300
- result.push({ActionComponent: MessageListener, actionProps: item, actionIndex: index});
301
- continue;
302
- }
303
-
304
- if (item.on === "hashchange") {
305
- // "hashchange" works in the same way than "message": it must be added on the window object.
306
- result.push({ActionComponent: HashChangeListener, actionProps: item, actionIndex: index});
307
- continue;
308
- }
309
-
310
- requiresReactionComponent = true;
311
-
312
- const normalizedEventName = "on" + capitalizeFirstLetter(item.on);
313
-
314
- if (!Array.isArray(reactionFunctionProps[normalizedEventName])) {
315
- // Initialize the key.
316
- reactionFunctionProps[normalizedEventName] = [];
317
- }
318
-
319
- // Append the reaction function definition that will be read
320
- // later by the ReactOnEvent action component.
321
- reactionFunctionProps[normalizedEventName].push(item);
322
-
323
- // Do not add the ReactOnEvent component yet in the result array.
324
- // It will be added at the end of the actions chain.
325
- continue;
326
- }
327
-
328
- if (!isValid(item, templateContexts)) {
329
- continue;
330
- }
331
-
332
- result.push({ActionComponent: Component, actionProps: item, actionIndex: index});
333
- }
334
-
335
- if (requiresReactionComponent) {
336
- // Add the final component for reaction.
337
- // It's added at the end because it will collect all definitions
338
- // and apply the reaction function properties on the real rendered element.
339
- // TODO: evaluate if the _reactOnEvent actionIndex may create issues.
340
- result.push({ActionComponent: ReactOnEvent, actionProps: reactionFunctionProps, actionIndex: "_reactOnEvent"});
341
- }
342
-
343
- return result;
344
- };
345
-
346
- /**
347
- * Maybe converts the given value to a date representation.
348
- *
349
- * Needed for date comparisons.
350
- *
351
- * @param {boolean} shouldCompareAsDates Tells if the value should be converted.
352
- * @param {any} a The value to maybe convert.
353
- *
354
- * @returns {Date|*} The converted or same value.
355
- */
356
- const maybeDate = (shouldCompareAsDates, a) => {
357
- return shouldCompareAsDates ? new Date(a) : a;
358
- };
359
-
360
- /**
361
- * Component that executes actions before rendering its children.
362
- *
363
- * Actions include a system to hide.
364
- *
365
- * @param props The props that served to build an action dependant component.
366
- * @returns {*}
367
- * @constructor
368
- */
369
- const ActionDependant = (props) => {
370
- const globalDataContext = useContext(GlobalDataContext);
371
- const templateContext = useContext(TemplateContext);
372
-
373
- if (!props) {
374
- // There is nothing to render or evaluate, so don't even bother checking the actions.
375
- return null;
376
- }
377
-
378
- const result = getActionsToExecute(props?.actions ?? [], {globalDataContext, templateContext});
379
-
380
- // Encapsulate into actions.
381
- return result.reverse().reduce((acc, {ActionComponent, actionProps, actionIndex}) => {
382
- // actionProps contains only the info that is related to the action.
383
- return <ActionComponent
384
- componentProps={props}
385
- actionProps={actionProps}
386
- actionIndex={actionIndex}>
387
- {acc}
388
- </ActionComponent>;
389
- }, props?.children ?? null);
390
- };
391
-
392
- export default ActionDependant;
@@ -1,28 +0,0 @@
1
- /**
2
- * Merges component collections (aka plugins).
3
- * @param {[{}]} collections Array of plugin collections.
4
- * @return {{hook: {}, reaction: {}, action: {}, utility: {}, element: {}}}
5
- */
6
- export function mergeComponentCollections(collections) {
7
- const mergedCollections = {
8
- "action": {},
9
- "element": {},
10
- "hook": {},
11
- "reaction": {},
12
- "utility": {},
13
- };
14
-
15
- collections.forEach((collection) => {
16
- for (const [k, v] of Object.entries(collection)) {
17
- if (!mergedCollections.hasOwnProperty(k)) {
18
- mergedCollections[k] = [];
19
- }
20
-
21
- for (const [componentName, Component] of Object.entries(v)) {
22
- mergedCollections[componentName] = Component;
23
- }
24
- }
25
- });
26
-
27
- return mergedCollections;
28
- }
@@ -1,80 +0,0 @@
1
- import {memo} from "react";
2
- import EventDispatcherContext from "./EventDispatcherContext";
3
-
4
- /**
5
- * Contains the registered listeners. It's shared between all the components.
6
- *
7
- * @type {{}}
8
- */
9
- const events = {};
10
-
11
- /**
12
- * The main dispatcher which will really be called by the browser's event system.
13
- *
14
- * @param {Event} event The event sent by the browser, which will be transferred to the registered listeners.
15
- */
16
- const mainDispatcher = (event) => {
17
- if (!events.hasOwnProperty(event.type)) {
18
- // Ignore this event.
19
- return;
20
- }
21
-
22
- for (const registeredListener of events[event.type]) {
23
- // Inject the event details into the registered listener.
24
- registeredListener(event);
25
- }
26
- };
27
-
28
- /**
29
- * Adds the listener to registered listeners.
30
- *
31
- * @param {string} type The event type.
32
- * @param {Function} listener The event listener.
33
- */
34
- export const addEventListener = (type, listener) => {
35
- if (events.hasOwnProperty(type)) {
36
- events[type].push(listener);
37
- } else {
38
- // This is a new event type. Add the event listener.
39
- window.addEventListener(type, mainDispatcher);
40
-
41
- events[type] = [listener];
42
- }
43
- };
44
-
45
- /**
46
- * Removes the specified listener.
47
- *
48
- * @param {string} type Event type.
49
- * @param {Function} listener Event listener.
50
- */
51
- export const removeEventListener = (type, listener) => {
52
- if (!events.hasOwnProperty(type)) {
53
- return;
54
- }
55
-
56
- const index = events[type].indexOf(listener);
57
-
58
- if (index > -1) {
59
- // Remove once the item.
60
- events[type].splice(index, 1);
61
- }
62
- };
63
-
64
- /**
65
- * Use this provider in combination with EventDispatcherContext.
66
- *
67
- * @param props
68
- *
69
- * @returns {JSX.Element}
70
- *
71
- * @constructor
72
- */
73
- const EventDispatcherProvider = (props) => {
74
- return <EventDispatcherContext.Provider value={{addEventListener, removeEventListener}}>
75
- {props.children}
76
- </EventDispatcherContext.Provider>;
77
- }
78
-
79
- // TODO: evaluate if memo is useful here.
80
- export default memo(EventDispatcherProvider);
@@ -1,33 +0,0 @@
1
- import GlobalDataContext from "./GlobalDataContext";
2
-
3
- /**
4
- * Standard implementation of the provider for GlobalDataContext.
5
- *
6
- * @param props Component props. Must have the "value" key.
7
- *
8
- * @returns {JSX.Element}
9
- *
10
- * @constructor
11
- */
12
- const GlobalDataContextProvider = (props) => {
13
- // Shallow copy. This will help to keep the original data representation.
14
- const valueCopy = {...props.value};
15
-
16
- if (valueCopy.getRootContext === undefined) {
17
- // The root context is unset.
18
- // Self reference this root object with this closure.
19
- // It's useful so that new global data contexts such as those created
20
- // by the DataFilter component can access the root global data context value
21
- // instead of the current GlobalDataContext value from useContext().
22
- // The submitData reaction function uses such root value.
23
- valueCopy.getRootContext = () => {
24
- return props.value;
25
- };
26
- }
27
-
28
- return <GlobalDataContext.Provider value={valueCopy}>
29
- {props.children}
30
- </GlobalDataContext.Provider>;
31
- }
32
-
33
- export default GlobalDataContextProvider;
@@ -1,61 +0,0 @@
1
- import {memo, useReducer, useRef, useState} from "react";
2
- import PaginationContext from "./PaginationContext";
3
-
4
- /**
5
- * Use this provider in combination with EventDispatcherContext.
6
- *
7
- * @param props
8
- *
9
- * @returns {JSX.Element}
10
- *
11
- * @constructor
12
- * @deprecated ne fonctionne pas, envisager un système de preemptiveActions pour filtrer les items en amont
13
- */
14
- const PaginationProvider = (props) => {
15
- const {
16
- after,
17
- before,
18
- contentToPaginate,
19
- pagination,
20
- } = props;
21
- // TODO: essayer d'obtenir le nombre réel à afficher.
22
-
23
- // TODO: essayer d'obtenir seulement les objets à afficher. (Hide lance dispatch(remove) par ex
24
- const completeItemCount = contentToPaginate.length;
25
-
26
-
27
- // Slice for the pagination if in effect.
28
- const contentSlice = props?.paginated
29
- ? contentToPaginate.slice(pagination.firstShownItemIndex, pagination.maxShownItemIndexExcluded)
30
- : contentToPaginate;
31
-
32
-
33
-
34
- // Dev note: on PhpStorm, disregard the Function signatures inspection errors of reducers.
35
- // See: https://youtrack.jetbrains.com/issue/WEB-53963.
36
- // noinspection JSCheckFunctionSignatures
37
- const [itemCountAdjustment, dispatchItemCountAdjustment] = useReducer((prevState, dispatched) => {
38
- switch (dispatched.type) {
39
- case "increment":
40
- return prevState + 1;
41
-
42
- case "decrement":
43
- return prevState - 1;
44
-
45
- default:
46
- // Unknown type.
47
- return prevState;
48
- }
49
- }, {updateId: 0, realCurrentData: {}});
50
-
51
-
52
-
53
- return <PaginationContext.Provider value={{dispatchItemCountAdjustment, pagination}}>
54
- {before}
55
- {contentSlice}
56
- {after}
57
- </PaginationContext.Provider>;
58
- }
59
-
60
- // TODO: evaluate if memo is useful here.
61
- export default PaginationProvider;