@iobroker/json-config 6.17.6 → 6.17.12

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 (173) hide show
  1. package/LICENSE +21 -21
  2. package/build/JsonConfig.d.ts +1 -1
  3. package/build/JsonConfig.js +18 -19
  4. package/build/JsonConfig.js.map +1 -1
  5. package/build/JsonConfigComponent/ChipInput.d.ts +89 -11
  6. package/build/JsonConfigComponent/ChipInput.js +48 -137
  7. package/build/JsonConfigComponent/ChipInput.js.map +1 -1
  8. package/build/JsonConfigComponent/ConfigAccordion.d.ts +7 -14
  9. package/build/JsonConfigComponent/ConfigAccordion.js +10 -21
  10. package/build/JsonConfigComponent/ConfigAccordion.js.map +1 -1
  11. package/build/JsonConfigComponent/ConfigAlive.d.ts +5 -1
  12. package/build/JsonConfigComponent/ConfigAlive.js +0 -1
  13. package/build/JsonConfigComponent/ConfigAlive.js.map +1 -1
  14. package/build/JsonConfigComponent/ConfigAutocomplete.d.ts +5 -29
  15. package/build/JsonConfigComponent/ConfigAutocomplete.js +7 -5
  16. package/build/JsonConfigComponent/ConfigAutocomplete.js.map +1 -1
  17. package/build/JsonConfigComponent/ConfigAutocompleteSendTo.d.ts +5 -14
  18. package/build/JsonConfigComponent/ConfigAutocompleteSendTo.js +3 -3
  19. package/build/JsonConfigComponent/ConfigAutocompleteSendTo.js.map +1 -1
  20. package/build/JsonConfigComponent/ConfigCRON.d.ts +1 -1
  21. package/build/JsonConfigComponent/ConfigCertCollection.d.ts +7 -13
  22. package/build/JsonConfigComponent/ConfigCertCollection.js +8 -18
  23. package/build/JsonConfigComponent/ConfigCertCollection.js.map +1 -1
  24. package/build/JsonConfigComponent/ConfigCertificateSelect.d.ts +7 -13
  25. package/build/JsonConfigComponent/ConfigCertificateSelect.js +5 -18
  26. package/build/JsonConfigComponent/ConfigCertificateSelect.js.map +1 -1
  27. package/build/JsonConfigComponent/ConfigCertificates.d.ts +7 -13
  28. package/build/JsonConfigComponent/ConfigCertificates.js +7 -18
  29. package/build/JsonConfigComponent/ConfigCertificates.js.map +1 -1
  30. package/build/JsonConfigComponent/ConfigCheckbox.d.ts +3 -11
  31. package/build/JsonConfigComponent/ConfigCheckbox.js +2 -2
  32. package/build/JsonConfigComponent/ConfigCheckbox.js.map +1 -1
  33. package/build/JsonConfigComponent/ConfigChip.d.ts +1 -1
  34. package/build/JsonConfigComponent/ConfigColor.d.ts +13 -20
  35. package/build/JsonConfigComponent/ConfigColor.js +9 -23
  36. package/build/JsonConfigComponent/ConfigColor.js.map +1 -1
  37. package/build/JsonConfigComponent/ConfigCoordinates.d.ts +8 -13
  38. package/build/JsonConfigComponent/ConfigCoordinates.js +29 -39
  39. package/build/JsonConfigComponent/ConfigCoordinates.js.map +1 -1
  40. package/build/JsonConfigComponent/ConfigCustom.d.ts +4 -50
  41. package/build/JsonConfigComponent/ConfigCustom.js +0 -1
  42. package/build/JsonConfigComponent/ConfigCustom.js.map +1 -1
  43. package/build/JsonConfigComponent/ConfigDatePicker.d.ts +8 -3
  44. package/build/JsonConfigComponent/ConfigDatePicker.js +21 -13
  45. package/build/JsonConfigComponent/ConfigDatePicker.js.map +1 -1
  46. package/build/JsonConfigComponent/ConfigDeviceManager.d.ts +5 -1
  47. package/build/JsonConfigComponent/ConfigDeviceManager.js +1 -1
  48. package/build/JsonConfigComponent/ConfigDeviceManager.js.map +1 -1
  49. package/build/JsonConfigComponent/ConfigFile.d.ts +1 -1
  50. package/build/JsonConfigComponent/ConfigFunc.d.ts +1 -1
  51. package/build/JsonConfigComponent/ConfigGeneric.d.ts +46 -32
  52. package/build/JsonConfigComponent/ConfigGeneric.js +23 -12
  53. package/build/JsonConfigComponent/ConfigGeneric.js.map +1 -1
  54. package/build/JsonConfigComponent/ConfigIP.d.ts +1 -1
  55. package/build/JsonConfigComponent/ConfigImageSendTo.d.ts +1 -1
  56. package/build/JsonConfigComponent/ConfigInstanceSelect.d.ts +1 -1
  57. package/build/JsonConfigComponent/ConfigInterface.d.ts +1 -1
  58. package/build/JsonConfigComponent/ConfigJsonEditor.d.ts +1 -1
  59. package/build/JsonConfigComponent/ConfigLanguage.d.ts +3 -2
  60. package/build/JsonConfigComponent/ConfigLanguage.js +2 -2
  61. package/build/JsonConfigComponent/ConfigLanguage.js.map +1 -1
  62. package/build/JsonConfigComponent/ConfigNumber.d.ts +3 -13
  63. package/build/JsonConfigComponent/ConfigNumber.js +2 -2
  64. package/build/JsonConfigComponent/ConfigNumber.js.map +1 -1
  65. package/build/JsonConfigComponent/ConfigObjectId.d.ts +1 -1
  66. package/build/JsonConfigComponent/ConfigPanel.d.ts +7 -2
  67. package/build/JsonConfigComponent/ConfigPanel.js +5 -40
  68. package/build/JsonConfigComponent/ConfigPanel.js.map +1 -1
  69. package/build/JsonConfigComponent/ConfigPassword.d.ts +1 -1
  70. package/build/JsonConfigComponent/ConfigPort.d.ts +6 -1
  71. package/build/JsonConfigComponent/ConfigPort.js +2 -3
  72. package/build/JsonConfigComponent/ConfigPort.js.map +1 -1
  73. package/build/JsonConfigComponent/ConfigRoom.d.ts +1 -1
  74. package/build/JsonConfigComponent/ConfigSelect.d.ts +1 -1
  75. package/build/JsonConfigComponent/ConfigSelectSendTo.d.ts +7 -16
  76. package/build/JsonConfigComponent/ConfigSelectSendTo.js +15 -61
  77. package/build/JsonConfigComponent/ConfigSelectSendTo.js.map +1 -1
  78. package/build/JsonConfigComponent/ConfigSendto.d.ts +3 -20
  79. package/build/JsonConfigComponent/ConfigSendto.js +8 -8
  80. package/build/JsonConfigComponent/ConfigSendto.js.map +1 -1
  81. package/build/JsonConfigComponent/ConfigSetState.d.ts +1 -1
  82. package/build/JsonConfigComponent/ConfigSlider.d.ts +1 -1
  83. package/build/JsonConfigComponent/ConfigStaticDivider.d.ts +1 -1
  84. package/build/JsonConfigComponent/ConfigStaticHeader.d.ts +1 -1
  85. package/build/JsonConfigComponent/ConfigStaticImage.d.ts +1 -1
  86. package/build/JsonConfigComponent/ConfigStaticText.d.ts +1 -1
  87. package/build/JsonConfigComponent/ConfigTable.d.ts +4 -25
  88. package/build/JsonConfigComponent/ConfigTable.js +5 -4
  89. package/build/JsonConfigComponent/ConfigTable.js.map +1 -1
  90. package/build/JsonConfigComponent/ConfigTabs.d.ts +8 -33
  91. package/build/JsonConfigComponent/ConfigTabs.js +10 -43
  92. package/build/JsonConfigComponent/ConfigTabs.js.map +1 -1
  93. package/build/JsonConfigComponent/ConfigText.d.ts +7 -13
  94. package/build/JsonConfigComponent/ConfigText.js +13 -18
  95. package/build/JsonConfigComponent/ConfigText.js.map +1 -1
  96. package/build/JsonConfigComponent/ConfigTextSendTo.d.ts +7 -9
  97. package/build/JsonConfigComponent/ConfigTextSendTo.js +11 -21
  98. package/build/JsonConfigComponent/ConfigTextSendTo.js.map +1 -1
  99. package/build/JsonConfigComponent/ConfigTimePicker.d.ts +9 -3
  100. package/build/JsonConfigComponent/ConfigTimePicker.js +28 -18
  101. package/build/JsonConfigComponent/ConfigTimePicker.js.map +1 -1
  102. package/build/JsonConfigComponent/ConfigTopic.d.ts +1 -1
  103. package/build/JsonConfigComponent/ConfigUUID.d.ts +12 -13
  104. package/build/JsonConfigComponent/ConfigUUID.js +1 -6
  105. package/build/JsonConfigComponent/ConfigUUID.js.map +1 -1
  106. package/build/JsonConfigComponent/ConfigUser.d.ts +1 -1
  107. package/build/JsonConfigComponent/index.d.ts +66 -25
  108. package/build/JsonConfigComponent/index.js +48 -61
  109. package/build/JsonConfigComponent/index.js.map +1 -1
  110. package/build/index.d.ts +2 -2
  111. package/build/index.js +2 -2
  112. package/build/index.js.map +1 -1
  113. package/package.json +27 -26
  114. package/src/JsonConfig.tsx +710 -0
  115. package/src/JsonConfigComponent/ChipInput.tsx +752 -0
  116. package/src/JsonConfigComponent/ConfigAccordion.tsx +278 -0
  117. package/src/JsonConfigComponent/ConfigAlive.tsx +74 -0
  118. package/src/JsonConfigComponent/ConfigAutocomplete.tsx +108 -0
  119. package/src/JsonConfigComponent/ConfigAutocompleteSendTo.tsx +183 -0
  120. package/src/JsonConfigComponent/ConfigCRON.jsx +101 -0
  121. package/src/JsonConfigComponent/ConfigCertCollection.tsx +102 -0
  122. package/src/JsonConfigComponent/ConfigCertificateSelect.tsx +92 -0
  123. package/src/JsonConfigComponent/ConfigCertificates.tsx +202 -0
  124. package/src/JsonConfigComponent/ConfigCheckLicense.jsx +662 -0
  125. package/src/JsonConfigComponent/ConfigCheckbox.tsx +67 -0
  126. package/src/JsonConfigComponent/ConfigChip.jsx +81 -0
  127. package/src/JsonConfigComponent/ConfigColor.tsx +86 -0
  128. package/src/JsonConfigComponent/ConfigCoordinates.tsx +234 -0
  129. package/src/JsonConfigComponent/ConfigCustom.tsx +246 -0
  130. package/src/JsonConfigComponent/ConfigDatePicker.tsx +48 -0
  131. package/src/JsonConfigComponent/ConfigDeviceManager.tsx +33 -0
  132. package/src/JsonConfigComponent/ConfigFile.jsx +181 -0
  133. package/src/JsonConfigComponent/ConfigFileSelector.jsx +520 -0
  134. package/src/JsonConfigComponent/ConfigFunc.jsx +90 -0
  135. package/src/JsonConfigComponent/ConfigGeneric.tsx +1027 -0
  136. package/src/JsonConfigComponent/ConfigIP.jsx +96 -0
  137. package/src/JsonConfigComponent/ConfigImageSendTo.jsx +79 -0
  138. package/src/JsonConfigComponent/ConfigImageUpload.jsx +114 -0
  139. package/src/JsonConfigComponent/ConfigInstanceSelect.jsx +172 -0
  140. package/src/JsonConfigComponent/ConfigInterface.jsx +112 -0
  141. package/src/JsonConfigComponent/ConfigJsonEditor.jsx +103 -0
  142. package/src/JsonConfigComponent/ConfigLanguage.tsx +153 -0
  143. package/src/JsonConfigComponent/ConfigLicense.jsx +148 -0
  144. package/src/JsonConfigComponent/ConfigNumber.tsx +207 -0
  145. package/src/JsonConfigComponent/ConfigObjectId.jsx +113 -0
  146. package/src/JsonConfigComponent/ConfigPanel.tsx +360 -0
  147. package/src/JsonConfigComponent/ConfigPassword.jsx +160 -0
  148. package/src/JsonConfigComponent/ConfigPattern.jsx +50 -0
  149. package/src/JsonConfigComponent/ConfigPort.tsx +232 -0
  150. package/src/JsonConfigComponent/ConfigRoom.jsx +90 -0
  151. package/src/JsonConfigComponent/ConfigSelect.jsx +124 -0
  152. package/src/JsonConfigComponent/ConfigSelectSendTo.tsx +251 -0
  153. package/src/JsonConfigComponent/ConfigSendto.tsx +340 -0
  154. package/src/JsonConfigComponent/ConfigSetState.jsx +116 -0
  155. package/src/JsonConfigComponent/ConfigSlider.jsx +97 -0
  156. package/src/JsonConfigComponent/ConfigStaticDivider.jsx +51 -0
  157. package/src/JsonConfigComponent/ConfigStaticHeader.jsx +63 -0
  158. package/src/JsonConfigComponent/ConfigStaticImage.jsx +48 -0
  159. package/src/JsonConfigComponent/ConfigStaticText.jsx +72 -0
  160. package/src/JsonConfigComponent/ConfigTable.tsx +1040 -0
  161. package/src/JsonConfigComponent/ConfigTabs.tsx +150 -0
  162. package/src/JsonConfigComponent/ConfigText.tsx +188 -0
  163. package/src/JsonConfigComponent/ConfigTextSendTo.tsx +102 -0
  164. package/src/JsonConfigComponent/ConfigTimePicker.tsx +63 -0
  165. package/src/JsonConfigComponent/ConfigTopic.jsx +78 -0
  166. package/src/JsonConfigComponent/ConfigUUID.tsx +55 -0
  167. package/src/JsonConfigComponent/ConfigUser.jsx +104 -0
  168. package/src/JsonConfigComponent/index.tsx +435 -0
  169. package/src/JsonConfigComponent/wrapper/Components/CustomModal.jsx +145 -0
  170. package/src/JsonConfigComponent/wrapper/Components/Editor.jsx +65 -0
  171. package/src/Utils.jsx +1683 -0
  172. package/src/index.tsx +14 -0
  173. package/src/types.d.ts +372 -0
@@ -0,0 +1,67 @@
1
+ import React from 'react';
2
+ import { withStyles } from '@mui/styles';
3
+
4
+ import {
5
+ FormControlLabel,
6
+ Checkbox,
7
+ FormHelperText,
8
+ FormControl,
9
+ } from '@mui/material';
10
+
11
+ import { type AdminConnection, I18n } from '@iobroker/adapter-react-v5';
12
+ import type { ThemeName, ThemeType } from '@iobroker/adapter-react-v5/types';
13
+
14
+ import ConfigGeneric, { type ConfigGenericProps, type ConfigGenericState } from './ConfigGeneric';
15
+ import type {ConfigItemCheckbox} from "#JC/types";
16
+
17
+ const styles: Record<string, any> = {
18
+ error: {
19
+ color: 'red',
20
+ },
21
+ };
22
+
23
+ interface ConfigCheckboxProps extends ConfigGenericProps {
24
+ schema: ConfigItemCheckbox;
25
+ }
26
+
27
+ class ConfigCheckbox extends ConfigGeneric<ConfigCheckboxProps, ConfigGenericState> {
28
+ renderItem(error: unknown, disabled: boolean): React.JSX.Element {
29
+ const value = ConfigGeneric.getValue(this.props.data, this.props.attr);
30
+ const isIndeterminate = Array.isArray(value);
31
+
32
+ return <FormControl className={this.props.classes.fullWidth} variant="standard">
33
+ <FormControlLabel
34
+ onClick={e => {
35
+ e.preventDefault();
36
+ e.stopPropagation();
37
+
38
+ if (!disabled) {
39
+ this.onChange(this.props.attr, !value);
40
+ }
41
+ }}
42
+ control={<Checkbox
43
+ indeterminate={isIndeterminate}
44
+ checked={!!value}
45
+ onChange={e => {
46
+ if (isIndeterminate) {
47
+ this.onChange(this.props.attr, true);
48
+ } else {
49
+ this.onChange(this.props.attr, e.target.checked);
50
+ }
51
+ }}
52
+ disabled={disabled}
53
+ />}
54
+ label={this.getText(this.props.schema.label)}
55
+ />
56
+ <FormHelperText className={this.props.classes.error}>
57
+ {
58
+ error ? (this.props.schema.validatorErrorText ? I18n.t(this.props.schema.validatorErrorText) : I18n.t('ra_Error')) :
59
+ null
60
+ }
61
+ </FormHelperText>
62
+ {this.props.schema.help ? <FormHelperText>{this.renderHelp(this.props.schema.help, this.props.schema.helpLink, this.props.schema.noTranslation)}</FormHelperText> : null}
63
+ </FormControl>;
64
+ }
65
+ }
66
+
67
+ export default withStyles(styles)(ConfigCheckbox);
@@ -0,0 +1,81 @@
1
+ import React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { withStyles } from '@mui/styles';
4
+
5
+ import {
6
+ FormHelperText,
7
+ FormControl,
8
+ } from '@mui/material';
9
+
10
+ import ConfigGeneric from './ConfigGeneric';
11
+ import ChipInput from './ChipInput';
12
+
13
+ const styles = () => ({
14
+ fullWidth: {
15
+ width: '100%',
16
+ },
17
+ });
18
+
19
+ class ConfigLanguage extends ConfigGeneric {
20
+ componentDidMount() {
21
+ super.componentDidMount();
22
+ const { data, attr } = this.props;
23
+ const value = ConfigGeneric.getValue(data, attr);
24
+ if (this.props.schema.delimiter && typeof value === 'string') {
25
+ const parts = value.split(this.props.schema.delimiter).map(a => a.trim()).filter(a => a);
26
+ this.setState({ value: parts });
27
+ } else {
28
+ this.setState({ value: value || [] });
29
+ }
30
+ }
31
+
32
+ renderItem(error, disabled /* , defaultValue */) {
33
+ const { attr, schema } = this.props;
34
+ const { value } = this.state;
35
+ return <FormControl className={this.props.classes.fullWidth} variant="standard">
36
+ <ChipInput
37
+ value={value}
38
+ disabled={!!disabled}
39
+ label={this.getText(schema.label)}
40
+ error={!!error}
41
+ onAdd={chip => {
42
+ const newValue = JSON.parse(JSON.stringify(value));
43
+ newValue.push(chip);
44
+ this.setState({ value: newValue, prevValue: '' }, () => {
45
+ if (this.props.schema.delimiter) {
46
+ this.onChange(attr, newValue.join(`${this.props.schema.delimiter} `));
47
+ } else {
48
+ this.onChange(attr, newValue);
49
+ }
50
+ });
51
+ }}
52
+ onDelete={(chip, index) => {
53
+ const newValue = JSON.parse(JSON.stringify(value));
54
+ newValue.splice(index, 1);
55
+ this.setState({ value: newValue, prevValue: '' }, () => {
56
+ if (this.props.schema.delimiter) {
57
+ this.onChange(attr, newValue.join(`${this.props.schema.delimiter} `));
58
+ } else {
59
+ this.onChange(attr, newValue);
60
+ }
61
+ });
62
+ }}
63
+ />
64
+ {this.props.schema.help ? <FormHelperText>{this.renderHelp(this.props.schema.help, this.props.schema.helpLink, this.props.schema.noTranslation)}</FormHelperText> : null}
65
+ </FormControl>;
66
+ }
67
+ }
68
+
69
+ ConfigLanguage.propTypes = {
70
+ socket: PropTypes.object.isRequired,
71
+ themeType: PropTypes.string,
72
+ themeName: PropTypes.string,
73
+ style: PropTypes.object,
74
+ className: PropTypes.string,
75
+ data: PropTypes.object.isRequired,
76
+ schema: PropTypes.object,
77
+ onError: PropTypes.func,
78
+ onChange: PropTypes.func,
79
+ };
80
+
81
+ export default withStyles(styles)(ConfigLanguage);
@@ -0,0 +1,86 @@
1
+ import React from 'react';
2
+ import { ChromePicker, type ColorResult, type RGBColor } from 'react-color';
3
+
4
+ import {
5
+ IconButton,
6
+ TextField,
7
+ Dialog,
8
+ } from '@mui/material';
9
+
10
+ import { Close as ClearIcon } from '@mui/icons-material';
11
+
12
+ import { Utils } from '@iobroker/adapter-react-v5';
13
+
14
+ import type { ConfigItemText } from '#JC/types';
15
+ import ConfigGeneric, { type ConfigGenericProps, type ConfigGenericState } from './ConfigGeneric';
16
+
17
+ interface ConfigColorProps extends ConfigGenericProps {
18
+ schema: ConfigItemText;
19
+ }
20
+
21
+ interface ConfigColorState extends ConfigGenericState {
22
+ showColorDialog?: boolean;
23
+ colorDialogValue?: string;
24
+ }
25
+
26
+ class ConfigColor extends ConfigGeneric<ConfigColorProps, ConfigColorState> {
27
+ renderColorDialog() {
28
+ return !!this.state.showColorDialog && <Dialog
29
+ onClose={() => this.setState({ showColorDialog: false })}
30
+ open={this.state.showColorDialog}
31
+ >
32
+ <ChromePicker
33
+ color={this.state.colorDialogValue}
34
+ onChange={(color: ColorResult) =>
35
+ this.setState({ colorDialogValue: color.hex }, () =>
36
+ this.onChange(this.props.attr, this.state.colorDialogValue))}
37
+ />
38
+ </Dialog>;
39
+ }
40
+
41
+ renderItem(_error: unknown, disabled: boolean /* , defaultValue */) {
42
+ const value = ConfigGeneric.getValue(this.props.data, this.props.attr);
43
+ let textColor = Utils.isUseBright(value, null);
44
+ if (textColor === null) {
45
+ textColor = undefined;
46
+ }
47
+ return <>
48
+ {this.renderColorDialog()}
49
+ <TextField
50
+ variant="standard"
51
+ disabled={!!disabled}
52
+ style={{ minWidth: 100, width: 'calc(100% - 8px)' }}
53
+ label={this.getText(this.props.schema.label)}
54
+ value={value || ''}
55
+ onClick={() => !this.props.schema.readOnly && this.setState({ showColorDialog: true, colorDialogValue: value || '' })}
56
+ onChange={e => {
57
+ const color = e.target.value;
58
+ this.onChange(this.props.attr, color);
59
+ }}
60
+ inputProps={{
61
+ style: {
62
+ // paddingLeft: noPadding ? 0 : 8,
63
+ backgroundColor: value,
64
+ color: textColor ? '#FFF' : '#000',
65
+ },
66
+ readOnly: this.props.schema.readOnly || false,
67
+ }}
68
+ // eslint-disable-next-line react/jsx-no-duplicate-props
69
+ InputProps={{
70
+ endAdornment: value && !this.props.schema.noClearButton ? <IconButton
71
+ size="small"
72
+ onClick={e => {
73
+ e.stopPropagation();
74
+ this.onChange(this.props.attr, '');
75
+ }}
76
+ >
77
+ <ClearIcon />
78
+ </IconButton> : undefined,
79
+ }}
80
+ InputLabelProps={{ shrink: true }}
81
+ />
82
+ </>;
83
+ }
84
+ }
85
+
86
+ export default ConfigColor;
@@ -0,0 +1,234 @@
1
+ import React from 'react';
2
+ import { withStyles } from '@mui/styles';
3
+
4
+ import {
5
+ TextField,
6
+ Fab,
7
+ FormControlLabel,
8
+ Checkbox,
9
+ } from '@mui/material';
10
+
11
+ import {
12
+ GpsFixed as IconGpsFixed,
13
+ LocationOn as IconLocationOn,
14
+ } from '@mui/icons-material';
15
+
16
+ import { I18n } from '@iobroker/adapter-react-v5';
17
+
18
+ import ConfigGeneric, { type ConfigGenericProps, type ConfigGenericState } from './ConfigGeneric';
19
+ import type { ConfigItemCoordinates } from '#JC/types';
20
+
21
+ const styles: Record<string, any> = {
22
+ width: {
23
+ width: 'calc(100% - 85px)',
24
+ },
25
+ width50: {
26
+ width: 120,
27
+ marginRight: 5,
28
+ },
29
+ };
30
+
31
+ interface ConfigCoordinatesProps extends ConfigGenericProps {
32
+ classes: Record<string, string>;
33
+ schema: ConfigItemCoordinates;
34
+ }
35
+
36
+ interface ConfigCoordinatesState extends ConfigGenericState {
37
+ useSystem?: boolean;
38
+ longitude?: string | number;
39
+ latitude?: string | number;
40
+ }
41
+
42
+ class ConfigCoordinates extends ConfigGeneric<ConfigCoordinatesProps, ConfigCoordinatesState> {
43
+ componentDidMount() {
44
+ super.componentDidMount();
45
+
46
+ const value = ConfigGeneric.getValue(this.props.data, this.props.attr);
47
+ this.setState({ value });
48
+
49
+ const newState: Partial<ConfigCoordinatesState> = {};
50
+ if (this.props.schema.useSystemName) {
51
+ newState.useSystem = !!ConfigGeneric.getValue(this.props.data, this.props.schema.useSystemName);
52
+ }
53
+ if (this.props.schema.longitudeName && this.props.schema.latitudeName) {
54
+ newState.longitude = ConfigGeneric.getValue(this.props.data, this.props.schema.longitudeName);
55
+ newState.latitude = ConfigGeneric.getValue(this.props.data, this.props.schema.latitudeName);
56
+ if (!newState.longitude && !newState.latitude && this.props.schema.autoInit) {
57
+ setTimeout(() => this.getCoordinates(), 300);
58
+ }
59
+ } else {
60
+ const value_ = ConfigGeneric.getValue(this.props.data, this.props.attr);
61
+ if (!value_ && this.props.schema.autoInit) {
62
+ setTimeout(() => this.getCoordinates(), 300);
63
+ }
64
+ }
65
+ Object.keys(newState).length && setTimeout(() => this.setState(newState as ConfigCoordinatesState), 50);
66
+ }
67
+
68
+ async getSystemCoordinates() {
69
+ const obj = await this.props.socket.getCompactSystemConfig();
70
+ if (obj?.common && (obj.common.longitude || obj.common.latitude)) {
71
+ window.alert(I18n.t('ra_Used system settings'));
72
+ if (this.props.schema.longitudeName && this.props.schema.latitudeName) {
73
+ this.setState(
74
+ {
75
+ longitude: obj.common.longitude,
76
+ latitude: obj.common.latitude,
77
+ },
78
+ async () => {
79
+ await this.onChange(this.props.schema.longitudeName, (obj.common.longitude || '').trim());
80
+ await this.onChange(this.props.schema.latitudeName, (obj.common.latitude || '').trim());
81
+ },
82
+ );
83
+ } else {
84
+ const value = obj.common.latitude + (this.props.schema.divider || ',') + obj.common.longitude;
85
+ this.setState(
86
+ {
87
+ value,
88
+ },
89
+ () => {
90
+ this.onChange(this.props.attr, value);
91
+ },
92
+ );
93
+ }
94
+ } else {
95
+ window.alert(
96
+ I18n.t(
97
+ 'ra_Cannot determine position: System settings are empty and GPS detection is disabled in browser',
98
+ ),
99
+ );
100
+ }
101
+ }
102
+
103
+ getCoordinates() {
104
+ if (navigator.geolocation) {
105
+ navigator.geolocation.getCurrentPosition(
106
+ position => {
107
+ if (position?.coords) {
108
+ if (this.props.schema.longitudeName && this.props.schema.latitudeName) {
109
+ this.setState(
110
+ {
111
+ longitude: position.coords.longitude,
112
+ latitude: position.coords.latitude,
113
+ },
114
+ async () => {
115
+ await this.onChange(
116
+ this.props.schema.longitudeName,
117
+ position.coords.longitude || '',
118
+ );
119
+ await this.onChange(this.props.schema.latitudeName, position.coords.latitude || '');
120
+ },
121
+ );
122
+ } else {
123
+ const value =
124
+ position.coords.latitude +
125
+ (this.props.schema.divider || ',') +
126
+ position.coords.longitude;
127
+ this.setState({ value }, () => {
128
+ this.onChange(this.props.attr, value);
129
+ });
130
+ }
131
+ } else {
132
+ this.getSystemCoordinates();
133
+ }
134
+ },
135
+ error => {
136
+ console.error(`Cannot determine coordinates from browser: ${error}`);
137
+ this.getSystemCoordinates();
138
+ },
139
+ );
140
+ } else {
141
+ this.getSystemCoordinates();
142
+ }
143
+ }
144
+
145
+ renderItem(error: string, disabled: boolean /* , defaultValue */) {
146
+ return <>
147
+ {this.props.schema.useSystemName ? <FormControlLabel
148
+ control={
149
+ <Checkbox
150
+ checked={!!this.state.useSystem}
151
+ onChange={e => {
152
+ const useSystem = e.target.checked;
153
+ if (useSystem) {
154
+ this.getSystemCoordinates();
155
+ }
156
+ this.setState({ useSystem }, () =>
157
+ this.onChange(this.props.schema.useSystemName, useSystem));
158
+ }}
159
+ />
160
+ }
161
+ label={I18n.t('ra_Use system settings for position')}
162
+ /> : null}
163
+ {this.props.schema.longitudeName && this.props.schema.latitudeName ?
164
+ <TextField
165
+ variant="standard"
166
+ className={this.props.classes.width50}
167
+ value={this.state.longitude ?? ''}
168
+ error={!!error}
169
+ disabled={this.state.useSystem || !!disabled}
170
+ onChange={e => {
171
+ const longitude = e.target.value;
172
+ this.setState({ longitude }, () => {
173
+ this.onChange(this.props.schema.longitudeName, (longitude || '').trim());
174
+ });
175
+ }}
176
+ label={I18n.t('ra_Longitude')}
177
+ /> : null}
178
+ {this.props.schema.longitudeName && this.props.schema.latitudeName ?
179
+ <TextField
180
+ variant="standard"
181
+ className={this.props.classes.width50}
182
+ value={this.state.latitude ?? ''}
183
+ error={!!error}
184
+ disabled={this.state.useSystem || !!disabled}
185
+ onChange={e => {
186
+ const latitude = e.target.value;
187
+ this.setState({ latitude }, () =>
188
+ this.onChange(this.props.schema.latitudeName, (latitude || '').trim()));
189
+ }}
190
+ label={I18n.t('ra_Latitude')}
191
+ /> : null}
192
+ {!this.props.schema.longitudeName || !this.props.schema.latitudeName ?
193
+ <TextField
194
+ variant="standard"
195
+ className={this.props.classes.width}
196
+ value={this.state.value === null || this.state.value === undefined ? '' : this.state.value}
197
+ error={!!error}
198
+ disabled={!!disabled}
199
+ inputProps={{
200
+ maxLength: this.props.schema.maxLength || this.props.schema.max || undefined,
201
+ readOnly: this.state.useSystem,
202
+ }}
203
+ onChange={e => {
204
+ const value = e.target.value;
205
+ this.setState({ value }, () => this.onChange(this.props.attr, (value || '').trim()));
206
+ }}
207
+ placeholder={this.getText(this.props.schema.placeholder)}
208
+ label={this.getText(this.props.schema.label)}
209
+ helperText={this.renderHelp(
210
+ this.props.schema.help,
211
+ this.props.schema.helpLink,
212
+ this.props.schema.noTranslation,
213
+ )}
214
+ /> : null}
215
+ {!this.state.useSystem ? <Fab
216
+ size="small"
217
+ onClick={() => this.getCoordinates()}
218
+ title={I18n.t('ra_Take browser position')}
219
+ style={{ marginRight: 4 }}
220
+ >
221
+ <IconLocationOn />
222
+ </Fab> : null}
223
+ {!this.state.useSystem ? <Fab
224
+ size="small"
225
+ onClick={() => this.getSystemCoordinates()}
226
+ title={I18n.t('ra_Take position from system settings')}
227
+ >
228
+ <IconGpsFixed />
229
+ </Fab> : null}
230
+ </>;
231
+ }
232
+ }
233
+
234
+ export default withStyles(styles)(ConfigCoordinates);