@luomus/laji-form 15.1.4 → 15.1.6

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 (235) hide show
  1. package/lib/ApiClient.d.ts +46 -0
  2. package/lib/ApiClient.js +93 -0
  3. package/lib/Context.d.ts +8 -0
  4. package/lib/Context.js +18 -0
  5. package/lib/ReactContext.d.ts +7 -0
  6. package/lib/ReactContext.js +5 -0
  7. package/lib/components/BaseComponent.d.ts +9 -0
  8. package/lib/components/BaseComponent.js +29 -0
  9. package/lib/components/LajiForm.d.ts +221 -0
  10. package/lib/components/LajiForm.js +616 -0
  11. package/lib/components/VirtualSchemaField.d.ts +42 -0
  12. package/lib/components/VirtualSchemaField.js +74 -0
  13. package/lib/components/components.d.ts +119 -0
  14. package/lib/components/components.js +606 -0
  15. package/lib/components/fields/AnnotationField.d.ts +34 -0
  16. package/lib/components/fields/AnnotationField.js +242 -0
  17. package/lib/components/fields/AnyToBooleanField.d.ts +18 -0
  18. package/lib/components/fields/AnyToBooleanField.js +20 -0
  19. package/lib/components/fields/ArrayBulkField.d.ts +23 -0
  20. package/lib/components/fields/ArrayBulkField.js +82 -0
  21. package/lib/components/fields/ArrayCombinerField.d.ts +36 -0
  22. package/lib/components/fields/ArrayCombinerField.js +138 -0
  23. package/lib/components/fields/ArrayField.d.ts +39 -0
  24. package/lib/components/fields/ArrayField.js +148 -0
  25. package/lib/components/fields/AsArrayField.d.ts +292 -0
  26. package/lib/components/fields/AsArrayField.js +38 -0
  27. package/lib/components/fields/AudioArrayField.d.ts +22 -0
  28. package/lib/components/fields/AudioArrayField.js +100 -0
  29. package/lib/components/fields/AutoArrayField.d.ts +24 -0
  30. package/lib/components/fields/AutoArrayField.js +56 -0
  31. package/lib/components/fields/AutosuggestField.d.ts +69 -0
  32. package/lib/components/fields/AutosuggestField.js +299 -0
  33. package/lib/components/fields/CombinedValueDisplayField.d.ts +41 -0
  34. package/lib/components/fields/CombinedValueDisplayField.js +100 -0
  35. package/lib/components/fields/ConditionalOnChangeField.d.ts +42 -0
  36. package/lib/components/fields/ConditionalOnChangeField.js +107 -0
  37. package/lib/components/fields/ConditionalUiSchemaField.d.ts +123 -0
  38. package/lib/components/fields/ConditionalUiSchemaField.js +143 -0
  39. package/lib/components/fields/ContextInjectionField.d.ts +24 -0
  40. package/lib/components/fields/ContextInjectionField.js +68 -0
  41. package/lib/components/fields/DataLeakerField.d.ts +40 -0
  42. package/lib/components/fields/DataLeakerField.js +68 -0
  43. package/lib/components/fields/DefaultValueArrayField.d.ts +34 -0
  44. package/lib/components/fields/DefaultValueArrayField.js +59 -0
  45. package/lib/components/fields/DependentBooleanField.d.ts +38 -0
  46. package/lib/components/fields/DependentBooleanField.js +87 -0
  47. package/lib/components/fields/DependentDisableField.d.ts +35 -0
  48. package/lib/components/fields/DependentDisableField.js +71 -0
  49. package/lib/components/fields/EnumRangeArrayField.d.ts +27 -0
  50. package/lib/components/fields/EnumRangeArrayField.js +115 -0
  51. package/lib/components/fields/ExtraLabelRowField.d.ts +38 -0
  52. package/lib/components/fields/ExtraLabelRowField.js +100 -0
  53. package/lib/components/fields/FakePropertyField.d.ts +18 -0
  54. package/lib/components/fields/FakePropertyField.js +48 -0
  55. package/lib/components/fields/FilterArrayField.d.ts +73 -0
  56. package/lib/components/fields/FilterArrayField.js +121 -0
  57. package/lib/components/fields/FlatField.d.ts +29 -0
  58. package/lib/components/fields/FlatField.js +171 -0
  59. package/lib/components/fields/GeocoderField.d.ts +47 -0
  60. package/lib/components/fields/GeocoderField.js +372 -0
  61. package/lib/components/fields/GridLayoutField.d.ts +18 -0
  62. package/lib/components/fields/GridLayoutField.js +32 -0
  63. package/lib/components/fields/HiddenField.d.ts +10 -0
  64. package/lib/components/fields/HiddenField.js +11 -0
  65. package/lib/components/fields/ImageArrayField.d.ts +172 -0
  66. package/lib/components/fields/ImageArrayField.js +699 -0
  67. package/lib/components/fields/ImageDisplayField.d.ts +18 -0
  68. package/lib/components/fields/ImageDisplayField.js +44 -0
  69. package/lib/components/fields/InitiallyHiddenField.d.ts +21 -0
  70. package/lib/components/fields/InitiallyHiddenField.js +58 -0
  71. package/lib/components/fields/InjectDefaultValueField.d.ts +37 -0
  72. package/lib/components/fields/InjectDefaultValueField.js +68 -0
  73. package/lib/components/fields/InjectField.d.ts +46 -0
  74. package/lib/components/fields/InjectField.js +88 -0
  75. package/lib/components/fields/InputWithDefaultValueButtonField.d.ts +25 -0
  76. package/lib/components/fields/InputWithDefaultValueButtonField.js +61 -0
  77. package/lib/components/fields/LocalityField.d.ts +22 -0
  78. package/lib/components/fields/LocalityField.js +94 -0
  79. package/lib/components/fields/LocationChooserField.d.ts +27 -0
  80. package/lib/components/fields/LocationChooserField.js +440 -0
  81. package/lib/components/fields/MapArrayField.d.ts +78 -0
  82. package/lib/components/fields/MapArrayField.js +1783 -0
  83. package/lib/components/fields/MapField.d.ts +48 -0
  84. package/lib/components/fields/MapField.js +434 -0
  85. package/lib/components/fields/MultiAnyToBooleanField.d.ts +25 -0
  86. package/lib/components/fields/MultiAnyToBooleanField.js +100 -0
  87. package/lib/components/fields/MultiArrayField.d.ts +53 -0
  88. package/lib/components/fields/MultiArrayField.js +224 -0
  89. package/lib/components/fields/MultiLanguageField.d.ts +13 -0
  90. package/lib/components/fields/MultiLanguageField.js +52 -0
  91. package/lib/components/fields/MultiTagArrayField.d.ts +36 -0
  92. package/lib/components/fields/MultiTagArrayField.js +142 -0
  93. package/lib/components/fields/NamedPlaceChooserField.d.ts +29 -0
  94. package/lib/components/fields/NamedPlaceChooserField.js +380 -0
  95. package/lib/components/fields/NamedPlaceSaverField.d.ts +29 -0
  96. package/lib/components/fields/NamedPlaceSaverField.js +237 -0
  97. package/lib/components/fields/NestField.d.ts +148 -0
  98. package/lib/components/fields/NestField.js +289 -0
  99. package/lib/components/fields/ObjectField.d.ts +2 -0
  100. package/lib/components/fields/ObjectField.js +119 -0
  101. package/lib/components/fields/PdfArrayField.d.ts +21 -0
  102. package/lib/components/fields/PdfArrayField.js +40 -0
  103. package/lib/components/fields/PrefillingArrayField.d.ts +22 -0
  104. package/lib/components/fields/PrefillingArrayField.js +65 -0
  105. package/lib/components/fields/SchemaField.d.ts +7 -0
  106. package/lib/components/fields/SchemaField.js +109 -0
  107. package/lib/components/fields/ScopeField.d.ts +85 -0
  108. package/lib/components/fields/ScopeField.js +521 -0
  109. package/lib/components/fields/SectionArrayField.d.ts +38 -0
  110. package/lib/components/fields/SectionArrayField.js +618 -0
  111. package/lib/components/fields/SelectTreeField.d.ts +39 -0
  112. package/lib/components/fields/SelectTreeField.js +143 -0
  113. package/lib/components/fields/SingleActiveArrayField.d.ts +49 -0
  114. package/lib/components/fields/SingleActiveArrayField.js +974 -0
  115. package/lib/components/fields/SingleItemArrayField.d.ts +15 -0
  116. package/lib/components/fields/SingleItemArrayField.js +60 -0
  117. package/lib/components/fields/SortArrayField.d.ts +73 -0
  118. package/lib/components/fields/SortArrayField.js +351 -0
  119. package/lib/components/fields/SplitField.d.ts +28 -0
  120. package/lib/components/fields/SplitField.js +65 -0
  121. package/lib/components/fields/StringToArrayField.d.ts +24 -0
  122. package/lib/components/fields/StringToArrayField.js +48 -0
  123. package/lib/components/fields/SumField.d.ts +35 -0
  124. package/lib/components/fields/SumField.js +83 -0
  125. package/lib/components/fields/TableField.d.ts +18 -0
  126. package/lib/components/fields/TableField.js +136 -0
  127. package/lib/components/fields/TagArrayField.d.ts +38 -0
  128. package/lib/components/fields/TagArrayField.js +128 -0
  129. package/lib/components/fields/ToggleAdditionalArrayFieldsField.d.ts +26 -0
  130. package/lib/components/fields/ToggleAdditionalArrayFieldsField.js +81 -0
  131. package/lib/components/fields/UiFieldApplierField.d.ts +30 -0
  132. package/lib/components/fields/UiFieldApplierField.js +106 -0
  133. package/lib/components/fields/UiFieldMapperArrayField.d.ts +42 -0
  134. package/lib/components/fields/UiFieldMapperArrayField.js +121 -0
  135. package/lib/components/fields/UnitCountShorthandField.d.ts +22 -0
  136. package/lib/components/fields/UnitCountShorthandField.js +149 -0
  137. package/lib/components/fields/UnitListShorthandArrayField.d.ts +26 -0
  138. package/lib/components/fields/UnitListShorthandArrayField.js +108 -0
  139. package/lib/components/fields/UnitShorthandField.d.ts +32 -0
  140. package/lib/components/fields/UnitShorthandField.js +220 -0
  141. package/lib/components/templates/ArrayFieldTemplate.d.ts +85 -0
  142. package/lib/components/templates/ArrayFieldTemplate.js +416 -0
  143. package/lib/components/templates/BaseInputTemplate.d.ts +11 -0
  144. package/lib/components/templates/BaseInputTemplate.js +80 -0
  145. package/lib/components/templates/DescriptionField.d.ts +1 -0
  146. package/lib/components/templates/DescriptionField.js +37 -0
  147. package/lib/components/templates/ErrorListTemplate.d.ts +9 -0
  148. package/lib/components/templates/ErrorListTemplate.js +95 -0
  149. package/lib/components/templates/FieldTemplate.d.ts +6 -0
  150. package/lib/components/templates/FieldTemplate.js +112 -0
  151. package/lib/components/templates/ObjectFieldTemplate.d.ts +7 -0
  152. package/lib/components/templates/ObjectFieldTemplate.js +66 -0
  153. package/lib/components/templates/TitleField.d.ts +9 -0
  154. package/lib/components/templates/TitleField.js +51 -0
  155. package/lib/components/widgets/AnyToBooleanWidget.d.ts +20 -0
  156. package/lib/components/widgets/AnyToBooleanWidget.js +49 -0
  157. package/lib/components/widgets/AutosuggestWidget.d.ts +66 -0
  158. package/lib/components/widgets/AutosuggestWidget.js +1127 -0
  159. package/lib/components/widgets/CheckboxWidget.d.ts +37 -0
  160. package/lib/components/widgets/CheckboxWidget.js +139 -0
  161. package/lib/components/widgets/DateTimeWidget.d.ts +62 -0
  162. package/lib/components/widgets/DateTimeWidget.js +251 -0
  163. package/lib/components/widgets/DateWidget.d.ts +23 -0
  164. package/lib/components/widgets/DateWidget.js +41 -0
  165. package/lib/components/widgets/HiddenWidget.d.ts +15 -0
  166. package/lib/components/widgets/HiddenWidget.js +19 -0
  167. package/lib/components/widgets/ImageSelectWidget.d.ts +14 -0
  168. package/lib/components/widgets/ImageSelectWidget.js +57 -0
  169. package/lib/components/widgets/InformalTaxonGroupChooserWidget.d.ts +43 -0
  170. package/lib/components/widgets/InformalTaxonGroupChooserWidget.js +237 -0
  171. package/lib/components/widgets/InputGroupWidget.d.ts +21 -0
  172. package/lib/components/widgets/InputGroupWidget.js +36 -0
  173. package/lib/components/widgets/InputWithDefaultValueButtonWidget.d.ts +21 -0
  174. package/lib/components/widgets/InputWithDefaultValueButtonWidget.js +47 -0
  175. package/lib/components/widgets/NumberWidget.d.ts +13 -0
  176. package/lib/components/widgets/NumberWidget.js +21 -0
  177. package/lib/components/widgets/PlainTextWidget.d.ts +12 -0
  178. package/lib/components/widgets/PlainTextWidget.js +24 -0
  179. package/lib/components/widgets/SelectWidget.d.ts +52 -0
  180. package/lib/components/widgets/SelectWidget.js +158 -0
  181. package/lib/components/widgets/SeparatedDateTimeWidget.d.ts +19 -0
  182. package/lib/components/widgets/SeparatedDateTimeWidget.js +59 -0
  183. package/lib/components/widgets/TaxonImageWidget.d.ts +13 -0
  184. package/lib/components/widgets/TaxonImageWidget.js +30 -0
  185. package/lib/components/widgets/TextSelectWidget.d.ts +25 -0
  186. package/lib/components/widgets/TextSelectWidget.js +94 -0
  187. package/lib/components/widgets/TextareaWidget.d.ts +32 -0
  188. package/lib/components/widgets/TextareaWidget.js +87 -0
  189. package/lib/components/widgets/TimeWidget.d.ts +11 -0
  190. package/lib/components/widgets/TimeWidget.js +19 -0
  191. package/lib/components/widgets/URLWidget.d.ts +14 -0
  192. package/lib/components/widgets/URLWidget.js +12 -0
  193. package/lib/components/widgets/UpperCaseWidget.d.ts +13 -0
  194. package/lib/components/widgets/UpperCaseWidget.js +21 -0
  195. package/lib/index.d.ts +20 -0
  196. package/lib/index.js +50 -0
  197. package/lib/services/blocker-service.d.ts +11 -0
  198. package/lib/services/blocker-service.js +55 -0
  199. package/lib/services/custom-event-service.d.ts +17 -0
  200. package/lib/services/custom-event-service.js +35 -0
  201. package/lib/services/dom-id-service.d.ts +8 -0
  202. package/lib/services/dom-id-service.js +30 -0
  203. package/lib/services/focus-service.d.ts +13 -0
  204. package/lib/services/focus-service.js +60 -0
  205. package/lib/services/id-service.d.ts +22 -0
  206. package/lib/services/id-service.js +130 -0
  207. package/lib/services/key-handler-service.d.ts +56 -0
  208. package/lib/services/key-handler-service.js +184 -0
  209. package/lib/services/root-instance-service.d.ts +25 -0
  210. package/lib/services/root-instance-service.js +49 -0
  211. package/lib/services/settings-service.d.ts +34 -0
  212. package/lib/services/settings-service.js +154 -0
  213. package/lib/services/singleton-map-service.d.ts +23 -0
  214. package/lib/services/singleton-map-service.js +44 -0
  215. package/lib/services/submit-hook-service.d.ts +24 -0
  216. package/lib/services/submit-hook-service.js +73 -0
  217. package/lib/styles.d.ts +1 -0
  218. package/lib/styles.js +4 -0
  219. package/lib/themes/bs3.d.ts +3 -0
  220. package/lib/themes/bs3.js +133 -0
  221. package/lib/themes/bs5.d.ts +3 -0
  222. package/lib/themes/bs5.js +111 -0
  223. package/lib/themes/glyphicon-fa-mapping.d.ts +3 -0
  224. package/lib/themes/glyphicon-fa-mapping.js +271 -0
  225. package/lib/themes/stub.d.ts +3 -0
  226. package/lib/themes/stub.js +82 -0
  227. package/lib/themes/theme.d.ts +233 -0
  228. package/lib/themes/theme.js +2 -0
  229. package/lib/translations.json +847 -0
  230. package/lib/utils.d.ts +167 -0
  231. package/lib/utils.js +1185 -0
  232. package/lib/validation.d.ts +7 -0
  233. package/lib/validation.js +141 -0
  234. package/package.json +2 -2
  235. package/patches/protractor++webdriver-manager+12.1.8.patch +0 -20395
@@ -0,0 +1,380 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ const React = require("react");
10
+ const react_dom_1 = require("react-dom");
11
+ const PropTypes = require("prop-types");
12
+ const utils_1 = require("../../utils");
13
+ const components_1 = require("../components");
14
+ const Spinner = require("react-spinner");
15
+ const Context_1 = require("../../Context");
16
+ const ReactContext_1 = require("../../ReactContext");
17
+ const BaseComponent_1 = require("../BaseComponent");
18
+ const MapArrayField_1 = require("./MapArrayField");
19
+ const globals_1 = require("@luomus/laji-map/lib/globals");
20
+ const SelectWidget_1 = require("../widgets/SelectWidget");
21
+ const PLACES_FETCH_FAIL = "PLACES_FETCH_FAIL";
22
+ const PLACE_USE_FAIL = "PLACE_USE_FAIL";
23
+ const PLACE_DELETE_FAIL = "PLACE_DELETE_FAIL";
24
+ /**
25
+ * Compatible only with gatherings array and gathering object.
26
+ */
27
+ let NamedPlaceChooserField = class NamedPlaceChooserField extends React.Component {
28
+ constructor(props) {
29
+ super(props);
30
+ this.getUiSchema = (props) => {
31
+ if (this.buttonDefinition) {
32
+ this.buttonDefinition = Object.assign(Object.assign({}, this.buttonDefinition), { label: props.formContext.translations.ChooseFromNamedPlace });
33
+ const innerUiSchema = utils_1.getInnerUiSchema(props.uiSchema);
34
+ const options = utils_1.getUiOptions(innerUiSchema);
35
+ return Object.assign(Object.assign({}, innerUiSchema), { "ui:options": Object.assign(Object.assign({}, options), { buttons: [
36
+ this.buttonDefinition,
37
+ ...(options.buttons || [])
38
+ ] }) });
39
+ }
40
+ else {
41
+ return utils_1.getInnerUiSchema(props.uiSchema);
42
+ }
43
+ };
44
+ this.isGatheringsArray = () => this.props.schema.type === "array";
45
+ this.onPlaceSelected = (place) => {
46
+ const getGathering = (schema) => {
47
+ const whitelist = ["geometry", "country", "administrativeProvince", "biologicalProvince", "municipality", "locality", "localityDescription", "habitat", "habitatDescription"];
48
+ let gathering = utils_1.getDefaultFormState(schema);
49
+ const placeGathering = place.prepopulatedDocument.gatherings[0];
50
+ whitelist.forEach(prop => {
51
+ if (prop in placeGathering && prop in schema.properties) {
52
+ gathering[prop] = placeGathering[prop];
53
+ }
54
+ });
55
+ gathering.namedPlaceID = place.id;
56
+ const tmpIdTree = this.props.formContext.services.ids.getRelativeTmpIdTree(this.props.idSchema.$id);
57
+ const [withLajiFormIds] = utils_1.addLajiFormIds(gathering, tmpIdTree, false);
58
+ return withLajiFormIds;
59
+ };
60
+ try {
61
+ let targetId, gathering, newFormData, zoomToData;
62
+ if (this.isGatheringsArray()) {
63
+ gathering = getGathering(this.props.schema.items);
64
+ newFormData = [
65
+ ...(this.props.formData || []),
66
+ gathering
67
+ ];
68
+ this.setState({ show: false });
69
+ targetId = this.props.idSchema.$id;
70
+ this.props.formContext.services.customEvents.send(targetId, "activeIdx", (this.props.formData || []).length);
71
+ }
72
+ else { // gathering object
73
+ gathering = getGathering(this.props.schema);
74
+ gathering.namedPlaceID = place.id;
75
+ newFormData = Object.assign(Object.assign({}, this.props.formData), gathering);
76
+ this.setState({ show: false });
77
+ const splits = this.props.idSchema.$id.split("_");
78
+ splits.pop();
79
+ targetId = splits.join("_");
80
+ zoomToData = true;
81
+ }
82
+ this.props.onChange(newFormData);
83
+ if (zoomToData)
84
+ this.props.formContext.services.customEvents.send(targetId, "zoomToData", undefined);
85
+ }
86
+ catch (e) {
87
+ this.setState({ failed: PLACE_USE_FAIL });
88
+ }
89
+ };
90
+ this.onPlaceDeleted = (place, success) => {
91
+ this.apiClient.fetchRaw(`/named-places/${place.id}`, undefined, { method: "DELETE" }).then(response => {
92
+ if (response.status === 204) {
93
+ // It takes a while for API to remove the place, so we remove it locally and then invalidate again after some time.
94
+ this.removeIds[place.id] = true;
95
+ this.apiClient.invalidateCachePath("/named-places");
96
+ setTimeout(() => this.apiClient.invalidateCachePath("/named-places"), 2000);
97
+ success();
98
+ }
99
+ }).catch(() => {
100
+ this.setState({ failed: PLACE_DELETE_FAIL });
101
+ this.props.formContext.notifier.error(this.props.formContext.translations.PlaceRemovalFailed);
102
+ });
103
+ };
104
+ this.onButtonClick = () => () => {
105
+ this.setState({ show: true });
106
+ };
107
+ this.updatePlaces = () => {
108
+ this.buttonDefinition = undefined;
109
+ this.apiClient.fetchCached("/named-places", { includePublic: false, pageSize: 100000 }).then(response => {
110
+ if (!this.mounted)
111
+ return;
112
+ const state = { places: response.results.filter(p => !this.removeIds[p.id]).sort((a, b) => {
113
+ if (a.name < b.name)
114
+ return -1;
115
+ if (a.name > b.name)
116
+ return 1;
117
+ return 0;
118
+ }) };
119
+ if (response.results && response.results.length) {
120
+ this.buttonDefinition = {
121
+ fn: this.onButtonClick,
122
+ fnName: "addNamedPlace",
123
+ glyph: "map-marker",
124
+ label: this.props.formContext.translations.ChooseFromNamedPlace,
125
+ id: this.props.idSchema.$id,
126
+ changesFormData: true,
127
+ variant: "primary"
128
+ };
129
+ if (this.isGatheringsArray()) {
130
+ this.buttonDefinition.rules = { canAdd: true };
131
+ }
132
+ else {
133
+ this.buttonDefinition.position = "top";
134
+ }
135
+ const uiSchema = this.getUiSchema(this.props);
136
+ state.uiSchema = uiSchema;
137
+ }
138
+ this.setState(state);
139
+ }).catch(() => {
140
+ this.setState({ failed: PLACES_FETCH_FAIL });
141
+ });
142
+ };
143
+ this.onHide = () => this.setState({ show: false });
144
+ this.apiClient = props.formContext.apiClient;
145
+ this.removeIds = {};
146
+ }
147
+ getStateFromProps(props) {
148
+ return { uiSchema: this.getUiSchema(props) };
149
+ }
150
+ componentDidMount() {
151
+ this.mounted = true;
152
+ this.updatePlaces();
153
+ this.apiClient.onCachePathInvalidation("/named-places", this.updatePlaces);
154
+ }
155
+ componentWillUnmount() {
156
+ this.mounted = false;
157
+ this.apiClient.removeOnCachePathInvalidation("/named-places", this.updatePlaces);
158
+ }
159
+ render() {
160
+ const { registry: { fields: { SchemaField } }, formContext } = this.props;
161
+ const { translations } = formContext;
162
+ const { failed } = this.state;
163
+ const { Modal, Alert } = this.context.theme;
164
+ return (React.createElement(React.Fragment, null,
165
+ React.createElement(SchemaField, Object.assign({}, this.props, { uiSchema: this.state.uiSchema })),
166
+ this.state.show ? (React.createElement(Modal, { dialogClassName: "laji-form map-dialog named-place-chooser-modal", show: true, onHide: this.onHide },
167
+ React.createElement(Modal.Header, { closeButton: true }, translations.ChooseNamedPlace),
168
+ React.createElement(Modal.Body, null,
169
+ failed === PLACE_USE_FAIL && React.createElement(Alert, { variant: "danger" }, translations.NamedPlacesUseFail),
170
+ failed === PLACES_FETCH_FAIL && React.createElement(Alert, { variant: "danger" }, translations.NamedPlacesFetchFail),
171
+ React.createElement(NamedPlaceChooser, { places: this.state.places, failed: failed === PLACES_FETCH_FAIL ? true : false, formContext: formContext, onSelected: this.onPlaceSelected, onDeleted: this.onPlaceDeleted })))) : null));
172
+ }
173
+ };
174
+ NamedPlaceChooserField.contextType = ReactContext_1.default;
175
+ NamedPlaceChooserField.propTypes = {
176
+ schema: PropTypes.shape({
177
+ type: PropTypes.oneOf(["object", "array"])
178
+ }).isRequired,
179
+ formData: PropTypes.oneOfType([PropTypes.object, PropTypes.array])
180
+ };
181
+ NamedPlaceChooserField = __decorate([
182
+ BaseComponent_1.default
183
+ ], NamedPlaceChooserField);
184
+ exports.default = NamedPlaceChooserField;
185
+ class NamedPlaceChooser extends React.Component {
186
+ constructor(props) {
187
+ super(props);
188
+ this.onPlaceSelected = (place) => {
189
+ this.props.onSelected(place);
190
+ };
191
+ this.onPlaceDeleted = (place) => {
192
+ const onDelete = () => {
193
+ if (this.popupContainerElem && this.popupElem) {
194
+ this.popupContainerElem.appendChild(react_dom_1.findDOMNode(this.popupElem));
195
+ }
196
+ this.mapElem.map.map.closePopup();
197
+ this.setState({ deleting: false, popupIdx: undefined });
198
+ };
199
+ this.setState({ deleting: true });
200
+ this.props.onDeleted(place, onDelete);
201
+ };
202
+ this.onSelectChange = (idx) => {
203
+ if (idx === undefined) {
204
+ return;
205
+ }
206
+ const { map } = this.mapElem;
207
+ const layers = this.idxToLayer[idx];
208
+ const [layer] = layers;
209
+ const bounds = map.getBoundsForLayers(layers);
210
+ const center = bounds.getCenter();
211
+ map.fitBounds(bounds, { animate: false });
212
+ this.props.formContext.setTimeout(() => layer.fire("click", { latlng: center }), 10);
213
+ };
214
+ this.onMapChange = (events) => {
215
+ events.some(({ type, idx }) => {
216
+ if (type === "active") {
217
+ this.setState({ popupIdx: idx });
218
+ const { map } = this.mapElem;
219
+ if (this.geometryCollectionData) {
220
+ map.setData();
221
+ }
222
+ this.geometryCollectionData = {
223
+ geoData: this.props.places[idx].geometry
224
+ };
225
+ map.setData(this.geometryCollectionData);
226
+ return true;
227
+ }
228
+ });
229
+ };
230
+ this.getFeatureStyle = (data) => {
231
+ const { feature = {} } = data || {};
232
+ function getColor(idx) {
233
+ const r = globals_1.NORMAL_COLOR.substring(1, 3);
234
+ const g = globals_1.NORMAL_COLOR.substring(3, 5);
235
+ const b = globals_1.NORMAL_COLOR.substring(5, 7);
236
+ return [r, g, b].reduce((rgb, hex) => {
237
+ const decimal = parseInt(hex, 16);
238
+ const amount = 40;
239
+ const _decimal = (idx % 2) ? decimal : Math.min(decimal + amount, 255);
240
+ return rgb + _decimal.toString(16);
241
+ }, "#");
242
+ }
243
+ const { idx } = feature.properties || {};
244
+ const color = idx === this.state.popupIdx ? globals_1.ACTIVE_COLOR : getColor(idx);
245
+ return { color, fillColor: color };
246
+ };
247
+ this.setPopupContainerRef = (elem) => {
248
+ this.popupContainerElem = elem;
249
+ };
250
+ this.state = {};
251
+ }
252
+ componentDidUpdate(prevProps, prevState) {
253
+ const { map } = this.mapElem;
254
+ this.idxToLayer = map.data.reduce((idxToLayer, item) => {
255
+ const layerGroup = item.group;
256
+ layerGroup.eachLayer(layer => {
257
+ if (!idxToLayer[layer.feature.properties.idx]) {
258
+ idxToLayer[layer.feature.properties.idx] = [];
259
+ }
260
+ idxToLayer[layer.feature.properties.idx].push(layer);
261
+ });
262
+ return idxToLayer;
263
+ }, {});
264
+ if (this.state.popupIdx !== prevState.popupIdx) {
265
+ const prevLayers = this.idxToLayer[prevState.popupIdx] || [];
266
+ const layers = this.idxToLayer[this.state.popupIdx] || [];
267
+ [prevLayers, layers].forEach(layers => {
268
+ layers.forEach(layer => {
269
+ if (layer) {
270
+ map.setLayerStyle(layer, this.getFeatureStyle({ feature: layer.feature }));
271
+ }
272
+ });
273
+ });
274
+ }
275
+ }
276
+ render() {
277
+ const { places, failed, formContext: { translations } } = this.props;
278
+ const that = this;
279
+ function getPopupRef(elem) {
280
+ that.popupElem = elem;
281
+ }
282
+ function getMapRef(elem) {
283
+ that.mapElem = elem;
284
+ }
285
+ function getPopup({ feature }, callback) {
286
+ const { idx } = feature.properties;
287
+ that.setState({ popupIdx: idx });
288
+ callback(react_dom_1.findDOMNode(that.popupElem));
289
+ }
290
+ const { Alert } = this.context.theme;
291
+ if (failed) {
292
+ return React.createElement(Alert, { variant: "danger" }, `${translations.NamedPlacesFetchFail} ${translations.TryAgainLater}`);
293
+ }
294
+ else {
295
+ const enums = (places || []).map((place, idx) => {
296
+ return { value: idx, label: place.name };
297
+ });
298
+ let pointer = 1;
299
+ const data = (places || []).reduce((data, place, i) => {
300
+ const { geometry } = place;
301
+ let isCollection = false;
302
+ if (geometry.type === "GeometryCollection") {
303
+ isCollection = true;
304
+ }
305
+ if (isCollection) {
306
+ data[pointer] = {
307
+ geoData: { type: "FeatureCollection", features: geometry.geometries.map(geom => { return { type: "Feature", geometry: geom, properties: { idx: i } }; }) },
308
+ getFeatureStyle: this.getFeatureStyle,
309
+ getPopup,
310
+ highlightOnHover: true
311
+ };
312
+ pointer++;
313
+ }
314
+ else {
315
+ if (!data[0])
316
+ data[0] = {
317
+ geoData: { type: "FeatureCollection", features: [] },
318
+ getFeatureStyle: this.getFeatureStyle,
319
+ getPopup,
320
+ cluster: true,
321
+ highlightOnHover: true
322
+ };
323
+ data[0].geoData.features.push({ type: "Feature", geometry, properties: { idx: i } });
324
+ }
325
+ return data;
326
+ }, []);
327
+ return (React.createElement("div", { style: { height: "inherit" } },
328
+ React.createElement(SelectWidget_1.default, { disabled: !places, options: { enumOptions: enums, placeholder: `${translations.SelectPlaceFromList}...` }, onChange: this.onSelectChange, selectOnChange: false, schema: {}, id: "named-place-chooser-select", formContext: this.props.formContext }),
329
+ React.createElement(MapArrayField_1.Map, { ref: getMapRef, data: data, markerPopupOffset: 45, featurePopupOffset: 5, controls: { draw: false }, lang: this.props.formContext.lang, bodyAsDialogRoot: false, formContext: this.props.formContext }),
330
+ (!places) ? React.createElement(Spinner, null) : null,
331
+ React.createElement("div", { style: { display: "none" }, ref: this.setPopupContainerRef },
332
+ React.createElement(Popup, { ref: getPopupRef, place: (places || [])[this.state.popupIdx], onPlaceSelected: this.onPlaceSelected, onPlaceDeleted: this.onPlaceDeleted, deleting: this.state.deleting, formContext: this.props.formContext, translations: translations }))));
333
+ }
334
+ }
335
+ }
336
+ NamedPlaceChooser.contextType = ReactContext_1.default;
337
+ class Popup extends React.Component {
338
+ constructor() {
339
+ super(...arguments);
340
+ this._onPlaceSelected = () => {
341
+ this.props.onPlaceSelected(this.props.place);
342
+ };
343
+ this._onPlaceDeleted = () => {
344
+ this.props.onPlaceDeleted(this.props.place);
345
+ };
346
+ this.getButtonRef = (elem) => {
347
+ this.buttonElem = elem;
348
+ };
349
+ }
350
+ componentDidUpdate() {
351
+ this.props.formContext.setTimeout(() => {
352
+ if (this.buttonElem)
353
+ react_dom_1.findDOMNode(this.buttonElem).focus();
354
+ });
355
+ }
356
+ render() {
357
+ const { place, translations, deleting } = this.props;
358
+ return place ? (React.createElement("div", { className: "named-place-popup" },
359
+ React.createElement("table", null,
360
+ React.createElement("tbody", null, [
361
+ ["Name", "name"],
362
+ ["Notes", "notes"]
363
+ ].reduce((fieldset, [translationKey, fieldName], i) => {
364
+ if (!utils_1.isEmptyString(place[fieldName]))
365
+ fieldset.push(React.createElement("tr", { key: i },
366
+ React.createElement("td", null,
367
+ React.createElement("b", null,
368
+ translations[translationKey],
369
+ ": ")),
370
+ React.createElement("td", null, place[fieldName])));
371
+ return fieldset;
372
+ }, []))),
373
+ React.createElement(components_1.Button, { block: true, ref: this.getButtonRef, onClick: this._onPlaceSelected }, translations.UseThisPlace),
374
+ React.createElement(components_1.DeleteButton, { block: true, onClick: this._onPlaceDeleted, disabled: deleting, glyphButton: false, confirm: true, confirmStyle: "browser", translations: translations },
375
+ translations.Remove,
376
+ " ",
377
+ deleting && React.createElement(Spinner, null)))) : React.createElement(Spinner, null);
378
+ }
379
+ }
380
+ Context_1.default("SCHEMA_FIELD_WRAPPERS").NamedPlaceChooserField = true;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Compatible only with gathering field.
3
+ */
4
+ export default class NamedPlaceSaverField extends React.Component<any, any, any> {
5
+ static contextType: React.Context<import("../../ReactContext").ContextProps>;
6
+ static propTypes: {
7
+ schema: PropTypes.Validator<PropTypes.InferProps<{
8
+ type: PropTypes.Requireable<string>;
9
+ }>>;
10
+ formData: PropTypes.Requireable<object>;
11
+ };
12
+ constructor(props: any);
13
+ constructor(props: any, context: any);
14
+ getStateFromProps(props: any): {
15
+ uiSchema: any;
16
+ };
17
+ getButton(props: any): {
18
+ label: any;
19
+ fn: () => () => void;
20
+ glyph: string;
21
+ position: string;
22
+ disabled: boolean;
23
+ };
24
+ onButtonClick: () => () => void;
25
+ onSave: (place: any) => void;
26
+ onHide: () => void;
27
+ }
28
+ import * as React from "react";
29
+ import * as PropTypes from "prop-types";
@@ -0,0 +1,237 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __rest = (this && this.__rest) || function (s, e) {
9
+ var t = {};
10
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
11
+ t[p] = s[p];
12
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
13
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
14
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
15
+ t[p[i]] = s[p[i]];
16
+ }
17
+ return t;
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ const React = require("react");
21
+ const react_dom_1 = require("react-dom");
22
+ const PropTypes = require("prop-types");
23
+ const utils_1 = require("../../utils");
24
+ const components_1 = require("../components");
25
+ const Context_1 = require("../../Context");
26
+ const ReactContext_1 = require("../../ReactContext");
27
+ const BaseComponent_1 = require("../BaseComponent");
28
+ const Spinner = require("react-spinner");
29
+ const MapArrayField_1 = require("./MapArrayField");
30
+ const SAVE = "SAVE", FETCH = "FETCH";
31
+ /**
32
+ * Compatible only with gathering field.
33
+ */
34
+ let NamedPlaceSaverField = class NamedPlaceSaverField extends React.Component {
35
+ constructor() {
36
+ super(...arguments);
37
+ this.onButtonClick = () => () => {
38
+ this.setState({ show: !this.state.show });
39
+ };
40
+ this.onSave = (place) => {
41
+ this.setState({ show: false }, () => {
42
+ this.props.onChange(Object.assign(Object.assign({}, (this.props.formData || {})), { namedPlaceID: place.id }));
43
+ });
44
+ };
45
+ this.onHide = () => this.setState({ show: false });
46
+ }
47
+ getStateFromProps(props) {
48
+ const innerUiSchema = utils_1.getInnerUiSchema(props.uiSchema);
49
+ const uiSchema = Object.assign(Object.assign({}, innerUiSchema), { "ui:options": Object.assign(Object.assign({}, utils_1.getUiOptions(innerUiSchema)), { buttons: [
50
+ ...(utils_1.getUiOptions(innerUiSchema).buttons || []),
51
+ this.getButton(props)
52
+ ] }) });
53
+ return { uiSchema };
54
+ }
55
+ getButton(props) {
56
+ return {
57
+ label: props.formContext.translations.SaveNamedPlace,
58
+ fn: this.onButtonClick,
59
+ glyph: "floppy-disk",
60
+ position: "top",
61
+ disabled: !MapArrayField_1.parseGeometries((props.formData || {}).geometry).length
62
+ };
63
+ }
64
+ componentDidUpdate(prevProps, prevState) {
65
+ if (prevState.show !== this.state.show)
66
+ this.setState(this.getStateFromProps(this.props));
67
+ }
68
+ render() {
69
+ const { registry: { fields: { SchemaField } }, formContext } = this.props;
70
+ const { uiSchema } = this.state;
71
+ const { Modal } = this.context.theme;
72
+ return (React.createElement("div", null,
73
+ React.createElement(SchemaField, Object.assign({}, this.props, { uiSchema: uiSchema })),
74
+ this.state && this.state.show
75
+ ? (React.createElement(Modal, { dialogClassName: "laji-form", show: true, onHide: this.onHide },
76
+ React.createElement(Modal.Header, null,
77
+ React.createElement(Modal.Title, null, formContext.translations.NamedPlaces)),
78
+ React.createElement(Modal.Body, null,
79
+ React.createElement(PlaceSaverDialog, { formContext: formContext, onSave: this.onSave, gathering: this.props.formData || {} })))) : null));
80
+ }
81
+ };
82
+ NamedPlaceSaverField.contextType = ReactContext_1.default;
83
+ NamedPlaceSaverField.propTypes = {
84
+ schema: PropTypes.shape({
85
+ type: PropTypes.oneOf(["object"])
86
+ }).isRequired,
87
+ formData: PropTypes.object
88
+ };
89
+ NamedPlaceSaverField = __decorate([
90
+ BaseComponent_1.default
91
+ ], NamedPlaceSaverField);
92
+ exports.default = NamedPlaceSaverField;
93
+ class PlaceSaverDialog extends React.Component {
94
+ constructor(props) {
95
+ super(props);
96
+ this.onInputChange = ({ target: { value } }) => {
97
+ this.setState({ value });
98
+ };
99
+ this.getGathering = () => {
100
+ return ["geometry", "country", "administrativeProvince", "biologicalProvince", "municipality", "locality", "localityDescription", "habitat", "habitatDescription", "coordinateRadius"].reduce((gathering, key) => {
101
+ gathering[key] = this.props.gathering[key];
102
+ return gathering;
103
+ }, {});
104
+ };
105
+ this.onSaveNew = (e) => {
106
+ e.preventDefault();
107
+ const _a = this.getGathering(), { namedPlaceID } = _a, gathering = __rest(_a, ["namedPlaceID"]); //eslint-disable-line @typescript-eslint/no-unused-vars
108
+ this.onSave({
109
+ name: this.state.value,
110
+ prepopulatedDocument: { gatherings: [gathering] }
111
+ });
112
+ };
113
+ this.onOverwriteCurrent = () => {
114
+ const { namedPlaceID } = this.props.gathering;
115
+ const gathering = this.getGathering(); //eslint-disable-line no-unused-vars
116
+ this.onSave(Object.assign(Object.assign({}, this.state.placeIdsToPlaces[namedPlaceID]), { name: this.state.value, prepopulatedDocument: { gatherings: [gathering] } }));
117
+ };
118
+ this.onOverwriteSelected = (place) => () => {
119
+ const gathering = this.getGathering(); //eslint-disable-line no-unused-vars
120
+ this.onSave(Object.assign(Object.assign({}, place), { name: this.state.value, prepopulatedDocument: { gatherings: [gathering] } }));
121
+ };
122
+ this.onOverwriteExisting = () => {
123
+ this.onOverwriteSelected(this.state.placeNamesToPlaces[this.state.value])();
124
+ };
125
+ // Hack that fixed autofocus cursor position
126
+ this.onFocus = (e) => {
127
+ var val = e.target.value;
128
+ e.target.value = "";
129
+ e.target.value = val;
130
+ };
131
+ this.onSubmit = (e) => {
132
+ e.preventDefault();
133
+ };
134
+ this.setInputRef = (elem) => {
135
+ this.inputRef = elem;
136
+ };
137
+ const { namedPlaceID, locality = "" } = props.gathering;
138
+ this.state = { value: locality };
139
+ if (namedPlaceID) {
140
+ this.state = { value: "", loading: true };
141
+ }
142
+ this.apiClient = props.formContext.apiClient;
143
+ }
144
+ componentWillUnmount() {
145
+ this.mounted = false;
146
+ }
147
+ componentDidMount() {
148
+ this.mounted = true;
149
+ this.apiClient.fetchCached("/named-places", { includePublic: false, pageSize: 1000 }).then(response => {
150
+ if (!this.mounted)
151
+ return;
152
+ const state = {
153
+ places: response.results,
154
+ placeIdsToPlaces: response.results.reduce((idsToPlaces, place) => {
155
+ idsToPlaces[place.id] = place;
156
+ return idsToPlaces;
157
+ }, {}),
158
+ placeNamesToPlaces: response.results.reduce((namesToPlaces, place) => {
159
+ if (!namesToPlaces[place.name])
160
+ namesToPlaces[place.name] = [];
161
+ namesToPlaces[place.name].push(place);
162
+ return namesToPlaces;
163
+ }, {})
164
+ };
165
+ const { namedPlaceID } = this.props.gathering;
166
+ if (namedPlaceID) {
167
+ state.value = state.placeIdsToPlaces[namedPlaceID].name;
168
+ state.loading = false;
169
+ }
170
+ this.setState(state, () => {
171
+ if (namedPlaceID) {
172
+ react_dom_1.findDOMNode(this.inputRef).focus();
173
+ }
174
+ });
175
+ }).catch(() => {
176
+ this.setState({ failed: FETCH });
177
+ });
178
+ }
179
+ onSave(place) {
180
+ place = Object.assign(Object.assign({}, place), { geometry: place.prepopulatedDocument.gatherings[0].geometry });
181
+ this.props.formContext.services.blocker.push();
182
+ this.apiClient.fetchRaw(`/named-places${place.id ? `/${place.id}` : ""}`, undefined, {
183
+ method: place.id ? "PUT" : "POST",
184
+ headers: {
185
+ "accept": "application/json",
186
+ "content-type": "application/json"
187
+ },
188
+ body: JSON.stringify(place)
189
+ }).then(response => {
190
+ this.props.formContext.services.blocker.pop();
191
+ this.apiClient.invalidateCachePath("/named-places");
192
+ return response.json();
193
+ }).then(this.props.onSave)
194
+ .catch(() => {
195
+ this.props.formContext.services.blocker.pop();
196
+ this.setState({ failed: SAVE });
197
+ });
198
+ }
199
+ render() {
200
+ const { value, placeNamesToPlaces = {}, loading } = this.state;
201
+ const { translations } = this.props.formContext;
202
+ const existingPlaces = (placeNamesToPlaces[value] || []).filter(place => {
203
+ return (place.owners || []).includes(this.props.formContext.uiSchemaContext.creator);
204
+ });
205
+ const getButton = (onClick, text) => {
206
+ return React.createElement(components_1.Button, { small: true, onClick: onClick, disabled: loading || utils_1.isEmptyString(value) }, text);
207
+ };
208
+ const { Panel, FormGroup, FormControl, ListGroup, ListGroupItem, Alert, Form, ControlLabel } = this.context.theme;
209
+ return this.state.failed ? (React.createElement(Alert, { variant: "danger" }, `${translations[`NamedPlaces${this.state.failed === FETCH ? "Fetch" : "Save"}Fail`]} ${translations.TryAgainLater}`)) : (React.createElement("div", { className: "place-saver-dialog" },
210
+ React.createElement(Form, { inline: true, onSubmit: this.onSubmit },
211
+ React.createElement(FormGroup, null,
212
+ React.createElement(ControlLabel, null,
213
+ translations.Name,
214
+ ": "),
215
+ " ",
216
+ React.createElement(FormControl, { type: "text", value: value, onChange: this.onInputChange, autoFocus: true, onFocus: this.onFocus, disabled: loading, ref: this.setInputRef }),
217
+ " ",
218
+ !existingPlaces.length ?
219
+ getButton(this.onSaveNew, `${translations.Save} ${translations.new}`) : null,
220
+ " ",
221
+ this.props.gathering.namedPlaceID ?
222
+ getButton(this.onOverwriteCurrent, translations.SaveCurrentOverwrite) : null,
223
+ existingPlaces.length === 1 && !this.props.gathering.namedPlaceID ?
224
+ getButton(this.onOverwriteExisting, translations.SaveExistingOverwrite) : null),
225
+ loading ? React.createElement("div", { className: "pull-right" },
226
+ React.createElement(Spinner, null)) : null),
227
+ !this.props.gathering.namedPlaceID && existingPlaces.length ?
228
+ React.createElement(Alert, { variant: "warning" }, `${translations.Warning}: ${translations.UsedNamedPlaceName}.${existingPlaces.length > 1 ? ` ${translations.UsedNamedPlaceNameMultiple}.` : ""}`)
229
+ : null,
230
+ existingPlaces.length > 1 ? (React.createElement(FormGroup, null,
231
+ React.createElement(Panel, null,
232
+ React.createElement(Panel.Heading, null, translations.ClickPlaceToOverwrite),
233
+ React.createElement(ListGroup, { fill: "fill" }, existingPlaces.map(place => React.createElement(ListGroupItem, { header: place.name, key: place.id, onClick: loading ? undefined : this.onOverwriteSelected(place), disabled: loading }, place.notes)))))) : null));
234
+ }
235
+ }
236
+ PlaceSaverDialog.contextType = ReactContext_1.default;
237
+ Context_1.default("SCHEMA_FIELD_WRAPPERS").NamedPlaceSaverField = true;