@iobroker/adapter-react-v5 7.0.1 → 7.0.2

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 (219) hide show
  1. package/Components/404.js +13 -13
  2. package/Components/FileBrowser.js +42 -16
  3. package/Components/Loader.js +223 -223
  4. package/Components/Loaders/PT.css +108 -108
  5. package/Components/Loaders/PT.js +103 -103
  6. package/Components/Loaders/Vendor.css +13 -13
  7. package/Components/Loaders/Vendor.js +7 -7
  8. package/Components/ObjectBrowser.d.ts +10 -0
  9. package/Components/ObjectBrowser.js +257 -109
  10. package/Components/UploadImage.js +305 -305
  11. package/Components/loader.css +221 -221
  12. package/Components/types.d.ts +82 -82
  13. package/GenericApp.js +49 -49
  14. package/LICENSE +22 -22
  15. package/Prompt.js +7 -7
  16. package/README.md +30 -21
  17. package/assets/devices/Alarm Systems.svg +18 -18
  18. package/assets/devices/Amplifier.svg +21 -21
  19. package/assets/devices/Awnings.svg +4 -4
  20. package/assets/devices/Battery Status.svg +4 -4
  21. package/assets/devices/Ceiling Spotlights.svg +15 -15
  22. package/assets/devices/Chandelier.svg +6 -6
  23. package/assets/devices/Climate.svg +11 -11
  24. package/assets/devices/Coffee Makers.svg +5 -5
  25. package/assets/devices/Cold Water.svg +31 -31
  26. package/assets/devices/Computer.svg +21 -21
  27. package/assets/devices/Consumption.svg +7 -7
  28. package/assets/devices/Curtains.svg +43 -43
  29. package/assets/devices/Dishwashers.svg +11 -11
  30. package/assets/devices/Doors.svg +5 -5
  31. package/assets/devices/Doorstep.svg +35 -35
  32. package/assets/devices/Dryer.svg +13 -13
  33. package/assets/devices/Fan.svg +20 -20
  34. package/assets/devices/Floor Lamps.svg +4 -4
  35. package/assets/devices/Garage Doors.svg +9 -9
  36. package/assets/devices/Gates.svg +32 -32
  37. package/assets/devices/Hairdryer.svg +23 -23
  38. package/assets/devices/Handle.svg +6 -6
  39. package/assets/devices/Hanging Lamps.svg +8 -8
  40. package/assets/devices/Heater.svg +44 -44
  41. package/assets/devices/Hoods.svg +11 -11
  42. package/assets/devices/Hot Water.svg +9 -9
  43. package/assets/devices/Humidity.svg +41 -41
  44. package/assets/devices/Iron.svg +4 -4
  45. package/assets/devices/Irrigation.svg +22 -22
  46. package/assets/devices/Led Strip.svg +30 -30
  47. package/assets/devices/Light.svg +29 -29
  48. package/assets/devices/Lightings.svg +46 -46
  49. package/assets/devices/Lock.svg +19 -19
  50. package/assets/devices/Louvre.svg +6 -6
  51. package/assets/devices/Mowing Machine.svg +8 -8
  52. package/assets/devices/Music.svg +12 -12
  53. package/assets/devices/Outdoor Blinds.svg +6 -6
  54. package/assets/devices/People.svg +19 -19
  55. package/assets/devices/Pool.svg +7 -7
  56. package/assets/devices/Power Consumption.svg +12 -12
  57. package/assets/devices/Printer.svg +9 -9
  58. package/assets/devices/Pump.svg +9 -9
  59. package/assets/devices/Receiver.svg +18 -18
  60. package/assets/devices/Sconces.svg +9 -9
  61. package/assets/devices/Security.svg +34 -34
  62. package/assets/devices/Shading.svg +4 -4
  63. package/assets/devices/Shutters.svg +10 -10
  64. package/assets/devices/SmokeDetector.svg +12 -12
  65. package/assets/devices/Sockets.svg +13 -13
  66. package/assets/devices/Speaker.svg +35 -35
  67. package/assets/devices/Stove.svg +11 -11
  68. package/assets/devices/Table Lamps.svg +11 -11
  69. package/assets/devices/Temperature Sensors.svg +28 -28
  70. package/assets/devices/Tv.svg +7 -7
  71. package/assets/devices/Vacuum Cleaner.svg +15 -15
  72. package/assets/devices/Ventilation.svg +12 -12
  73. package/assets/devices/Washing Machines.svg +15 -15
  74. package/assets/devices/Water Consumption.svg +5 -5
  75. package/assets/devices/Water Heater.svg +8 -8
  76. package/assets/devices/Water.svg +40 -40
  77. package/assets/devices/Weather.svg +28 -28
  78. package/assets/devices/Window.svg +7 -7
  79. package/assets/lamp_ceiling.svg +8 -8
  80. package/assets/lamp_table.svg +7 -7
  81. package/assets/no_icon.svg +9 -9
  82. package/assets/rooms/Anteroom.svg +52 -52
  83. package/assets/rooms/Attic.svg +21 -21
  84. package/assets/rooms/Balcony.svg +12 -12
  85. package/assets/rooms/Barn.svg +5 -5
  86. package/assets/rooms/Basement.svg +4 -4
  87. package/assets/rooms/Bathroom.svg +38 -38
  88. package/assets/rooms/Bedroom.svg +5 -5
  89. package/assets/rooms/Boiler Room.svg +12 -12
  90. package/assets/rooms/Carport.svg +17 -17
  91. package/assets/rooms/Cellar.svg +89 -89
  92. package/assets/rooms/Chamber.svg +9 -9
  93. package/assets/rooms/Corridor.svg +52 -52
  94. package/assets/rooms/Dining Area.svg +37 -37
  95. package/assets/rooms/Dining Room.svg +37 -37
  96. package/assets/rooms/Dining.svg +37 -37
  97. package/assets/rooms/Dressing Room.svg +4 -4
  98. package/assets/rooms/Driveway.svg +14 -14
  99. package/assets/rooms/Entrance.svg +44 -44
  100. package/assets/rooms/Equipment Room.svg +14 -14
  101. package/assets/rooms/Front Yard.svg +64 -64
  102. package/assets/rooms/Gallery.svg +13 -13
  103. package/assets/rooms/Garage.svg +20 -20
  104. package/assets/rooms/Garden.svg +12 -12
  105. package/assets/rooms/Ground Floor.svg +95 -95
  106. package/assets/rooms/Guest Bathroom.svg +32 -32
  107. package/assets/rooms/Guest Room.svg +5 -5
  108. package/assets/rooms/Gym.svg +4 -4
  109. package/assets/rooms/Hall.svg +19 -19
  110. package/assets/rooms/Home Theater.svg +7 -7
  111. package/assets/rooms/Kitchen.svg +17 -17
  112. package/assets/rooms/Laundry Room.svg +11 -11
  113. package/assets/rooms/Living Area.svg +10 -10
  114. package/assets/rooms/Living Room.svg +10 -10
  115. package/assets/rooms/Locker Room.svg +16 -16
  116. package/assets/rooms/Nursery.svg +4 -4
  117. package/assets/rooms/Office.svg +8 -8
  118. package/assets/rooms/Outdoors.svg +7 -7
  119. package/assets/rooms/Playroom.svg +5 -5
  120. package/assets/rooms/Pool.svg +7 -7
  121. package/assets/rooms/Rear Wall.svg +30 -30
  122. package/assets/rooms/Second Floor.svg +95 -95
  123. package/assets/rooms/Shed.svg +16 -16
  124. package/assets/rooms/Sleeping Area.svg +22 -22
  125. package/assets/rooms/Stairway.svg +4 -4
  126. package/assets/rooms/Stairwell.svg +15 -15
  127. package/assets/rooms/Storeroom.svg +4 -4
  128. package/assets/rooms/Summer House.svg +27 -27
  129. package/assets/rooms/Swimming Pool.svg +21 -21
  130. package/assets/rooms/Terrace.svg +6 -6
  131. package/assets/rooms/Toilet.svg +10 -10
  132. package/assets/rooms/Upstairs.svg +5 -5
  133. package/assets/rooms/Wardrobe.svg +60 -60
  134. package/assets/rooms/Washroom.svg +19 -19
  135. package/assets/rooms/Wc.svg +10 -10
  136. package/assets/rooms/Windscreen.svg +60 -60
  137. package/assets/rooms/Workshop.svg +22 -22
  138. package/assets/rooms/Workspace.svg +8 -8
  139. package/craco-module-federation.js +71 -71
  140. package/i18n/de.json +4 -1
  141. package/i18n/en.json +4 -1
  142. package/i18n/es.json +4 -1
  143. package/i18n/fr.json +4 -1
  144. package/i18n/it.json +4 -1
  145. package/i18n/nl.json +4 -1
  146. package/i18n/pl.json +4 -1
  147. package/i18n/pt.json +4 -1
  148. package/i18n/ru.json +4 -1
  149. package/i18n/uk.json +4 -1
  150. package/i18n/zh-cn.json +4 -1
  151. package/icons/IconFx.js +1 -1
  152. package/icons/IconLogout.js +1 -1
  153. package/index.css +54 -54
  154. package/modulefederation.admin.config.js +31 -31
  155. package/package.json +5 -5
  156. package/src/AdminConnection.tsx +3 -3
  157. package/src/Components/404.tsx +121 -121
  158. package/src/Components/ColorPicker.tsx +315 -315
  159. package/src/Components/ComplexCron.tsx +507 -507
  160. package/src/Components/CopyToClipboard.tsx +165 -165
  161. package/src/Components/CustomModal.tsx +163 -163
  162. package/src/Components/FileBrowser.tsx +2443 -2414
  163. package/src/Components/FileViewer.tsx +393 -393
  164. package/src/Components/Icon.tsx +210 -210
  165. package/src/Components/IconPicker.tsx +149 -149
  166. package/src/Components/IconSelector.tsx +2202 -2202
  167. package/src/Components/Image.tsx +176 -176
  168. package/src/Components/Loader.tsx +304 -304
  169. package/src/Components/Logo.tsx +166 -166
  170. package/src/Components/MDUtils.tsx +100 -100
  171. package/src/Components/ObjectBrowser.tsx +8215 -8032
  172. package/src/Components/Router.tsx +90 -90
  173. package/src/Components/SaveCloseButtons.tsx +113 -113
  174. package/src/Components/Schedule.tsx +1724 -1724
  175. package/src/Components/SelectWithIcon.tsx +197 -197
  176. package/src/Components/TabContainer.tsx +55 -55
  177. package/src/Components/TabContent.tsx +37 -37
  178. package/src/Components/TabHeader.tsx +19 -19
  179. package/src/Components/TableResize.tsx +259 -259
  180. package/src/Components/TextWithIcon.tsx +148 -148
  181. package/src/Components/ToggleThemeMenu.tsx +34 -34
  182. package/src/Components/TreeTable.tsx +919 -919
  183. package/src/Components/UploadImage.tsx +599 -599
  184. package/src/Components/Utils.tsx +1794 -1794
  185. package/src/Components/loader.css +221 -221
  186. package/src/Components/withWidth.tsx +21 -21
  187. package/src/Connection.tsx +7 -7
  188. package/src/Dialogs/ComplexCron.tsx +129 -129
  189. package/src/Dialogs/Confirm.tsx +162 -162
  190. package/src/Dialogs/Cron.tsx +182 -182
  191. package/src/Dialogs/Error.tsx +72 -72
  192. package/src/Dialogs/Message.tsx +71 -71
  193. package/src/Dialogs/SelectFile.tsx +270 -270
  194. package/src/Dialogs/SelectID.tsx +298 -298
  195. package/src/Dialogs/SimpleCron.tsx +100 -100
  196. package/src/Dialogs/TextInput.tsx +107 -107
  197. package/src/GenericApp.tsx +976 -976
  198. package/src/LegacyConnection.tsx +3589 -3589
  199. package/src/Prompt.tsx +20 -20
  200. package/src/Theme.tsx +479 -479
  201. package/src/icons/IconAdapter.tsx +20 -20
  202. package/src/icons/IconAlias.tsx +20 -20
  203. package/src/icons/IconChannel.tsx +21 -21
  204. package/src/icons/IconClearFilter.tsx +22 -22
  205. package/src/icons/IconClosed.tsx +17 -17
  206. package/src/icons/IconCopy.tsx +16 -16
  207. package/src/icons/IconDevice.tsx +27 -27
  208. package/src/icons/IconDocument.tsx +17 -17
  209. package/src/icons/IconDocumentReadOnly.tsx +18 -18
  210. package/src/icons/IconExpert.tsx +18 -18
  211. package/src/icons/IconFx.tsx +36 -36
  212. package/src/icons/IconInstance.tsx +20 -20
  213. package/src/icons/IconLogout.tsx +30 -30
  214. package/src/icons/IconNoIcon.tsx +19 -19
  215. package/src/icons/IconOpen.tsx +17 -17
  216. package/src/icons/IconProps.tsx +15 -15
  217. package/src/icons/IconState.tsx +17 -17
  218. package/src/index.css +54 -54
  219. package/types.d.ts +0 -134
@@ -1,315 +1,315 @@
1
- /**
2
- * Copyright 2018-2024 Denis Haev (bluefox) <dogafox@gmail.com>
3
- *
4
- * Licensed under the Creative Commons Attribution-NonCommercial License, Version 4.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * https://creativecommons.org/licenses/by-nc/4.0/legalcode.txt
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- import React, { Component } from 'react';
17
- import { ChromePicker, type RGBColor } from 'react-color';
18
-
19
- import {
20
- TextField, Menu,
21
- IconButton, Button,
22
- Box,
23
- } from '@mui/material';
24
-
25
- import {
26
- Delete as IconDelete,
27
- Close as IconClose,
28
- } from '@mui/icons-material';
29
-
30
- import I18n from '../i18n';
31
-
32
- import { IobTheme } from '../types';
33
-
34
- const styles: Record<string, any> = {
35
- color: {
36
- width: 36,
37
- height: 14,
38
- borderRadius: 2,
39
- },
40
- delButton: {
41
- marginTop: 16,
42
- },
43
- swatch: {
44
- mt: '16px',
45
- p: '5px',
46
- backgroundColor: 'background.paper',
47
- borderRadius: '1px',
48
- boxShadow: '0 0 0 1px rgba(0,0,0,.1)',
49
- display: 'inline-block',
50
- cursor: 'pointer',
51
- verticalAlign: 'middle',
52
- },
53
- swatchDisabled: {
54
- opacity: 0.5,
55
- cursor: 'default',
56
- },
57
- popover: {
58
- '& .MuiPaper-root': {
59
- textAlign: 'right',
60
- backgroundColor: '#00000000',
61
- boxShadow: 'none',
62
- },
63
- },
64
- popoverList: {
65
- padding: 0,
66
- },
67
- closeButton: (theme: IobTheme) => ({
68
- backgroundColor: `${theme.palette.background.paper} !important`,
69
- borderRadius: '0 0 25% 25%',
70
- '&:hover': {
71
- backgroundColor: `${theme.palette.secondary.main} !important`,
72
- },
73
- }),
74
- cover: {
75
- position: 'fixed',
76
- top: 0,
77
- right: 0,
78
- bottom: 0,
79
- left: 0,
80
- },
81
- textDense: {
82
- mt: 0,
83
- mb: 0,
84
- },
85
- picker: (theme: IobTheme) => ({
86
- background: `${theme.palette.background.paper} !important`,
87
- }),
88
- iconButton: {
89
- width: 16,
90
- height: 16,
91
- },
92
- button: {
93
- width: 32,
94
- height: 32,
95
- minWidth: 32,
96
- minHeight: 32,
97
- },
98
- };
99
-
100
- interface ColorPickerProps {
101
- /** Set to true to disable the color picker. */
102
- disabled?: boolean;
103
- /** The currently selected color. */
104
- value?: string;
105
- /** @deprecated The currently selected color use value */
106
- color?: string;
107
- /** The color change callback. */
108
- onChange: (rgba: string) => void;
109
- /** Label of the color picker. */
110
- label?: string;
111
- /** @deprecated TLabel of the color picker use label */
112
- name?: string;
113
- /** Additional styling for this component. */
114
- style?: React.CSSProperties;
115
- /** The CSS class name. */
116
- className?: string;
117
- customPalette?: string[];
118
- noInputField?: boolean;
119
- barWidth?: number;
120
- sx?: Record<string, any>;
121
- theme?: IobTheme;
122
- }
123
-
124
- interface ColorPickerState {
125
- displayColorPicker: boolean;
126
- color: string | RGBColor;
127
- anchorEl: HTMLDivElement | null;
128
- }
129
-
130
- class ColorPicker extends Component<ColorPickerProps, ColorPickerState> {
131
- constructor(props: ColorPickerProps) {
132
- super(props);
133
- this.state = {
134
- displayColorPicker: false,
135
- color: this.props.value || this.props.color || '',
136
- anchorEl: null,
137
- };
138
- }
139
-
140
- private handleClick = (e: React.MouseEvent<HTMLDivElement>) => {
141
- this.setState({
142
- displayColorPicker: !this.state.displayColorPicker,
143
- anchorEl: this.state.displayColorPicker ? null : e.currentTarget,
144
- });
145
- };
146
-
147
- private handleClose = () => {
148
- this.setState({ displayColorPicker: false, anchorEl: null });
149
- };
150
-
151
- /**
152
- * Convert the given color to hex ('#rrggbb') or rgba ('rgba(r,g,b,a)') format.
153
- * @returns the hex or rgba representation of the given color.
154
- */
155
- static getColor(
156
- color: string | { rgb: RGBColor } | RGBColor,
157
- /** The returning string should be in hex format */
158
- isHex?: boolean,
159
- ): string {
160
- if (color && typeof color === 'object') {
161
- const oColor = color as { rgb: RGBColor };
162
- if (oColor.rgb) {
163
- if (isHex) {
164
- return `#${oColor.rgb.r.toString(16).padStart(2, '0')}${oColor.rgb.g.toString(16).padStart(2, '0')}${oColor.rgb.b.toString(16).padStart(2, '0')}`;
165
- }
166
- return `rgba(${oColor.rgb.r},${oColor.rgb.g},${oColor.rgb.b},${oColor.rgb.a})`;
167
- }
168
- const rColor = color as RGBColor;
169
- if (isHex) {
170
- return `#${rColor.r.toString(16).padStart(2, '0')}${rColor.g.toString(16).padStart(2, '0')}${rColor.b.toString(16).padStart(2, '0')}`;
171
- }
172
- return `rgba(${rColor.r},${rColor.g},${rColor.b},${rColor.a})`;
173
- }
174
- return isHex ? ColorPicker.rgb2hex(color as string || '') : color as string || '';
175
- }
176
-
177
- /**
178
- * Convert rgb() or rgba() format to hex format #rrggbb.
179
- */
180
- static rgb2hex(rgb: string): string {
181
- const m = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
182
-
183
- if (m) {
184
- const r = parseInt(m[1], 10).toString(16).padStart(2, '0');
185
- const g = parseInt(m[2], 10).toString(16).padStart(2, '0');
186
- const b = parseInt(m[3], 10).toString(16).padStart(2, '0');
187
-
188
- return m?.length === 4 ? `#${r}${g}${b}` : rgb;
189
- }
190
- return rgb;
191
- }
192
-
193
- private handleChange = (color: string | RGBColor) => {
194
- this.setState({ color }, () =>
195
- this.props.onChange && this.props.onChange(ColorPicker.getColor(color)));
196
- };
197
-
198
- /**
199
- * IF the props are updated from outside, they should override the state
200
- */
201
- componentDidUpdate(_prevProps: ColorPickerProps, prevState: ColorPickerState) {
202
- const color = ColorPicker.getColor(this.props.color || this.props.value || '');
203
-
204
- if (color !== prevState.color) {
205
- this.setState({ color });
206
- }
207
- }
208
-
209
- renderCustomPalette() {
210
- if (!this.props.customPalette) {
211
- return null;
212
- }
213
- return <div style={{ width: '100%', display: 'flex', flexWrap: 'wrap' }}>
214
- {this.props.customPalette.map(color => <Button
215
- style={styles.button}
216
- key={color}
217
- onClick={() => {
218
- this.handleChange(color);
219
- setTimeout(() => this.handleClose(), 300);
220
- }}
221
- >
222
- <div
223
- style={{ ...styles.iconButton, background: color }}
224
- />
225
- </Button>)}
226
- </div>;
227
- }
228
-
229
- render() {
230
- const style = { ...(this.props.style || {}) };
231
- style.position = 'relative';
232
- const { color } = this.state;
233
-
234
- return <Box
235
- component="div"
236
- style={style}
237
- sx={this.props.sx || undefined}
238
- className={this.props.className || ''}
239
- >
240
- {this.props.noInputField ? null : <TextField
241
- disabled={this.props.disabled}
242
- variant="standard"
243
- id="ar_color_picker_name"
244
- label={this.props.label || this.props.name}
245
- value={color || ''}
246
- margin="dense"
247
- sx={{
248
- '&.MuiFormControl-root': styles.textDense,
249
- width: color ? 'calc(100% - 80px)' : 'calc(100% - 56px)',
250
- mr: color ? undefined : 1,
251
- }}
252
- onChange={e => this.handleChange(e.target.value)}
253
- />}
254
- {!this.props.noInputField && color ? <IconButton
255
- disabled={this.props.disabled}
256
- onClick={() => this.handleChange('')}
257
- size="small"
258
- style={{ ...(this.props.label || this.props.name ? styles.delButton : undefined), ...(color ? undefined : { opacity: 0, cursor: 'default' }) }}
259
- >
260
- <IconDelete />
261
- </IconButton> : null}
262
- <Box
263
- component="div"
264
- onClick={e => !this.props.disabled && this.handleClick(e)}
265
- title={I18n.t('ra_Select color')}
266
- sx={{
267
- ...styles.swatch,
268
- ...(this.props.disabled ? styles.swatchDisabled : undefined),
269
- background: color ? undefined : 'transparent',
270
- border: color ? undefined : '1px dashed #ccc',
271
- boxSizing: 'border-box',
272
- marginTop: this.props.noInputField || !(this.props.label || this.props.name) ? 0 : undefined,
273
- }}
274
- >
275
- <div
276
- style={{
277
- ...styles.color,
278
- background: ColorPicker.getColor(color),
279
- width: this.props.noInputField ? (this.props.barWidth || 16) : this.props.barWidth || 36,
280
- }}
281
- />
282
- </Box>
283
- {this.state.displayColorPicker && !this.props.disabled ? <Menu
284
- sx={{
285
- ...styles.popover,
286
- '&. MuiMenu-list': styles.popoverList,
287
- }}
288
- anchorEl={this.state.anchorEl}
289
- open={!0}
290
- onClose={() => this.handleClose()}
291
- >
292
- <ChromePicker
293
- color={this.state.color || undefined}
294
- onChangeComplete={(_color: { rgb: RGBColor }) => this.handleChange(_color.rgb)}
295
- styles={{
296
- default: {
297
- picker: {
298
- backgroundColor: this.props.theme?.palette.background.paper || '#888',
299
- },
300
- },
301
- }}
302
- />
303
- {color && this.props.noInputField ? <IconButton sx={styles.closeButton} onClick={() => this.handleChange('')}>
304
- <IconDelete />
305
- </IconButton> : null}
306
- <IconButton sx={styles.closeButton} onClick={() => this.handleClose()}>
307
- <IconClose />
308
- </IconButton>
309
- {this.renderCustomPalette()}
310
- </Menu> : null}
311
- </Box>;
312
- }
313
- }
314
-
315
- export default ColorPicker;
1
+ /**
2
+ * Copyright 2018-2024 Denis Haev (bluefox) <dogafox@gmail.com>
3
+ *
4
+ * Licensed under the Creative Commons Attribution-NonCommercial License, Version 4.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://creativecommons.org/licenses/by-nc/4.0/legalcode.txt
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import React, { Component } from 'react';
17
+ import { ChromePicker, type RGBColor } from 'react-color';
18
+
19
+ import {
20
+ TextField, Menu,
21
+ IconButton, Button,
22
+ Box,
23
+ } from '@mui/material';
24
+
25
+ import {
26
+ Delete as IconDelete,
27
+ Close as IconClose,
28
+ } from '@mui/icons-material';
29
+
30
+ import I18n from '../i18n';
31
+
32
+ import { IobTheme } from '../types';
33
+
34
+ const styles: Record<string, any> = {
35
+ color: {
36
+ width: 36,
37
+ height: 14,
38
+ borderRadius: 2,
39
+ },
40
+ delButton: {
41
+ marginTop: 16,
42
+ },
43
+ swatch: {
44
+ mt: '16px',
45
+ p: '5px',
46
+ backgroundColor: 'background.paper',
47
+ borderRadius: '1px',
48
+ boxShadow: '0 0 0 1px rgba(0,0,0,.1)',
49
+ display: 'inline-block',
50
+ cursor: 'pointer',
51
+ verticalAlign: 'middle',
52
+ },
53
+ swatchDisabled: {
54
+ opacity: 0.5,
55
+ cursor: 'default',
56
+ },
57
+ popover: {
58
+ '& .MuiPaper-root': {
59
+ textAlign: 'right',
60
+ backgroundColor: '#00000000',
61
+ boxShadow: 'none',
62
+ },
63
+ },
64
+ popoverList: {
65
+ padding: 0,
66
+ },
67
+ closeButton: (theme: IobTheme) => ({
68
+ backgroundColor: `${theme.palette.background.paper} !important`,
69
+ borderRadius: '0 0 25% 25%',
70
+ '&:hover': {
71
+ backgroundColor: `${theme.palette.secondary.main} !important`,
72
+ },
73
+ }),
74
+ cover: {
75
+ position: 'fixed',
76
+ top: 0,
77
+ right: 0,
78
+ bottom: 0,
79
+ left: 0,
80
+ },
81
+ textDense: {
82
+ mt: 0,
83
+ mb: 0,
84
+ },
85
+ picker: (theme: IobTheme) => ({
86
+ background: `${theme.palette.background.paper} !important`,
87
+ }),
88
+ iconButton: {
89
+ width: 16,
90
+ height: 16,
91
+ },
92
+ button: {
93
+ width: 32,
94
+ height: 32,
95
+ minWidth: 32,
96
+ minHeight: 32,
97
+ },
98
+ };
99
+
100
+ interface ColorPickerProps {
101
+ /** Set to true to disable the color picker. */
102
+ disabled?: boolean;
103
+ /** The currently selected color. */
104
+ value?: string;
105
+ /** @deprecated The currently selected color use value */
106
+ color?: string;
107
+ /** The color change callback. */
108
+ onChange: (rgba: string) => void;
109
+ /** Label of the color picker. */
110
+ label?: string;
111
+ /** @deprecated TLabel of the color picker use label */
112
+ name?: string;
113
+ /** Additional styling for this component. */
114
+ style?: React.CSSProperties;
115
+ /** The CSS class name. */
116
+ className?: string;
117
+ customPalette?: string[];
118
+ noInputField?: boolean;
119
+ barWidth?: number;
120
+ sx?: Record<string, any>;
121
+ theme?: IobTheme;
122
+ }
123
+
124
+ interface ColorPickerState {
125
+ displayColorPicker: boolean;
126
+ color: string | RGBColor;
127
+ anchorEl: HTMLDivElement | null;
128
+ }
129
+
130
+ class ColorPicker extends Component<ColorPickerProps, ColorPickerState> {
131
+ constructor(props: ColorPickerProps) {
132
+ super(props);
133
+ this.state = {
134
+ displayColorPicker: false,
135
+ color: this.props.value || this.props.color || '',
136
+ anchorEl: null,
137
+ };
138
+ }
139
+
140
+ private handleClick = (e: React.MouseEvent<HTMLDivElement>) => {
141
+ this.setState({
142
+ displayColorPicker: !this.state.displayColorPicker,
143
+ anchorEl: this.state.displayColorPicker ? null : e.currentTarget,
144
+ });
145
+ };
146
+
147
+ private handleClose = () => {
148
+ this.setState({ displayColorPicker: false, anchorEl: null });
149
+ };
150
+
151
+ /**
152
+ * Convert the given color to hex ('#rrggbb') or rgba ('rgba(r,g,b,a)') format.
153
+ * @returns the hex or rgba representation of the given color.
154
+ */
155
+ static getColor(
156
+ color: string | { rgb: RGBColor } | RGBColor,
157
+ /** The returning string should be in hex format */
158
+ isHex?: boolean,
159
+ ): string {
160
+ if (color && typeof color === 'object') {
161
+ const oColor = color as { rgb: RGBColor };
162
+ if (oColor.rgb) {
163
+ if (isHex) {
164
+ return `#${oColor.rgb.r.toString(16).padStart(2, '0')}${oColor.rgb.g.toString(16).padStart(2, '0')}${oColor.rgb.b.toString(16).padStart(2, '0')}`;
165
+ }
166
+ return `rgba(${oColor.rgb.r},${oColor.rgb.g},${oColor.rgb.b},${oColor.rgb.a})`;
167
+ }
168
+ const rColor = color as RGBColor;
169
+ if (isHex) {
170
+ return `#${rColor.r.toString(16).padStart(2, '0')}${rColor.g.toString(16).padStart(2, '0')}${rColor.b.toString(16).padStart(2, '0')}`;
171
+ }
172
+ return `rgba(${rColor.r},${rColor.g},${rColor.b},${rColor.a})`;
173
+ }
174
+ return isHex ? ColorPicker.rgb2hex(color as string || '') : color as string || '';
175
+ }
176
+
177
+ /**
178
+ * Convert rgb() or rgba() format to hex format #rrggbb.
179
+ */
180
+ static rgb2hex(rgb: string): string {
181
+ const m = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
182
+
183
+ if (m) {
184
+ const r = parseInt(m[1], 10).toString(16).padStart(2, '0');
185
+ const g = parseInt(m[2], 10).toString(16).padStart(2, '0');
186
+ const b = parseInt(m[3], 10).toString(16).padStart(2, '0');
187
+
188
+ return m?.length === 4 ? `#${r}${g}${b}` : rgb;
189
+ }
190
+ return rgb;
191
+ }
192
+
193
+ private handleChange = (color: string | RGBColor) => {
194
+ this.setState({ color }, () =>
195
+ this.props.onChange && this.props.onChange(ColorPicker.getColor(color)));
196
+ };
197
+
198
+ /**
199
+ * IF the props are updated from outside, they should override the state
200
+ */
201
+ componentDidUpdate(_prevProps: ColorPickerProps, prevState: ColorPickerState) {
202
+ const color = ColorPicker.getColor(this.props.color || this.props.value || '');
203
+
204
+ if (color !== prevState.color) {
205
+ this.setState({ color });
206
+ }
207
+ }
208
+
209
+ renderCustomPalette() {
210
+ if (!this.props.customPalette) {
211
+ return null;
212
+ }
213
+ return <div style={{ width: '100%', display: 'flex', flexWrap: 'wrap' }}>
214
+ {this.props.customPalette.map(color => <Button
215
+ style={styles.button}
216
+ key={color}
217
+ onClick={() => {
218
+ this.handleChange(color);
219
+ setTimeout(() => this.handleClose(), 300);
220
+ }}
221
+ >
222
+ <div
223
+ style={{ ...styles.iconButton, background: color }}
224
+ />
225
+ </Button>)}
226
+ </div>;
227
+ }
228
+
229
+ render() {
230
+ const style = { ...(this.props.style || {}) };
231
+ style.position = 'relative';
232
+ const { color } = this.state;
233
+
234
+ return <Box
235
+ component="div"
236
+ style={style}
237
+ sx={this.props.sx || undefined}
238
+ className={this.props.className || ''}
239
+ >
240
+ {this.props.noInputField ? null : <TextField
241
+ disabled={this.props.disabled}
242
+ variant="standard"
243
+ id="ar_color_picker_name"
244
+ label={this.props.label || this.props.name}
245
+ value={color || ''}
246
+ margin="dense"
247
+ sx={{
248
+ '&.MuiFormControl-root': styles.textDense,
249
+ width: color ? 'calc(100% - 80px)' : 'calc(100% - 56px)',
250
+ mr: color ? undefined : 1,
251
+ }}
252
+ onChange={e => this.handleChange(e.target.value)}
253
+ />}
254
+ {!this.props.noInputField && color ? <IconButton
255
+ disabled={this.props.disabled}
256
+ onClick={() => this.handleChange('')}
257
+ size="small"
258
+ style={{ ...(this.props.label || this.props.name ? styles.delButton : undefined), ...(color ? undefined : { opacity: 0, cursor: 'default' }) }}
259
+ >
260
+ <IconDelete />
261
+ </IconButton> : null}
262
+ <Box
263
+ component="div"
264
+ onClick={e => !this.props.disabled && this.handleClick(e)}
265
+ title={I18n.t('ra_Select color')}
266
+ sx={{
267
+ ...styles.swatch,
268
+ ...(this.props.disabled ? styles.swatchDisabled : undefined),
269
+ background: color ? undefined : 'transparent',
270
+ border: color ? undefined : '1px dashed #ccc',
271
+ boxSizing: 'border-box',
272
+ marginTop: this.props.noInputField || !(this.props.label || this.props.name) ? 0 : undefined,
273
+ }}
274
+ >
275
+ <div
276
+ style={{
277
+ ...styles.color,
278
+ background: ColorPicker.getColor(color),
279
+ width: this.props.noInputField ? (this.props.barWidth || 16) : this.props.barWidth || 36,
280
+ }}
281
+ />
282
+ </Box>
283
+ {this.state.displayColorPicker && !this.props.disabled ? <Menu
284
+ sx={{
285
+ ...styles.popover,
286
+ '&. MuiMenu-list': styles.popoverList,
287
+ }}
288
+ anchorEl={this.state.anchorEl}
289
+ open={!0}
290
+ onClose={() => this.handleClose()}
291
+ >
292
+ <ChromePicker
293
+ color={this.state.color || undefined}
294
+ onChangeComplete={(_color: { rgb: RGBColor }) => this.handleChange(_color.rgb)}
295
+ styles={{
296
+ default: {
297
+ picker: {
298
+ backgroundColor: this.props.theme?.palette.background.paper || '#888',
299
+ },
300
+ },
301
+ }}
302
+ />
303
+ {color && this.props.noInputField ? <IconButton sx={styles.closeButton} onClick={() => this.handleChange('')}>
304
+ <IconDelete />
305
+ </IconButton> : null}
306
+ <IconButton sx={styles.closeButton} onClick={() => this.handleClose()}>
307
+ <IconClose />
308
+ </IconButton>
309
+ {this.renderCustomPalette()}
310
+ </Menu> : null}
311
+ </Box>;
312
+ }
313
+ }
314
+
315
+ export default ColorPicker;