@iobroker/json-config 6.17.6 → 6.17.13

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
package/src/Utils.jsx ADDED
@@ -0,0 +1,1683 @@
1
+ /**
2
+ * Copyright 2018-2023 Denis Haev <dogafox@gmail.com>
3
+ *
4
+ * MIT License
5
+ *
6
+ * */
7
+ import React from 'react';
8
+ import { Utils as _Utils, I18n } from '@iobroker/adapter-react-v5';
9
+
10
+ const NAMESPACE = 'material';
11
+ const days = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
12
+ const months = ['Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
13
+ const QUALITY_BITS = {
14
+ 0x00: '0x00 - good',
15
+
16
+ 0x01: '0x01 - general problem',
17
+ 0x02: '0x02 - no connection problem',
18
+
19
+ 0x10: '0x10 - substitute value from controller',
20
+ 0x20: '0x20 - substitute initial value',
21
+ 0x40: '0x40 - substitute value from device or instance',
22
+ 0x80: '0x80 - substitute value from sensor',
23
+
24
+ 0x11: '0x11 - general problem by instance',
25
+ 0x41: '0x41 - general problem by device',
26
+ 0x81: '0x81 - general problem by sensor',
27
+
28
+ 0x12: '0x12 - instance not connected',
29
+ 0x42: '0x42 - device not connected',
30
+ 0x82: '0x82 - sensor not connected',
31
+
32
+ 0x44: '0x44 - device reports error',
33
+ 0x84: '0x84 - sensor reports error',
34
+ };
35
+ const SIGNATURES = {
36
+ JVBERi0: 'pdf',
37
+ R0lGODdh: 'gif',
38
+ R0lGODlh: 'gif',
39
+ iVBORw0KGgo: 'png',
40
+ '/9j/': 'jpg',
41
+ PHN2Zw: 'svg',
42
+ Qk1: 'bmp',
43
+ AAABAA: 'ico', // 00 00 01 00 according to https://en.wikipedia.org/wiki/List_of_file_signatures
44
+ };
45
+
46
+ class Utils {
47
+ static namespace = NAMESPACE;
48
+
49
+ static INSTANCES = 'instances';
50
+
51
+ static dateFormat = ['DD', 'MM'];
52
+
53
+ static FORBIDDEN_CHARS = /[^._\-/ :!#$%&()+=@^{}|~\p{Ll}\p{Lu}\p{Nd}]+/gu;
54
+
55
+ /**
56
+ * Capitalize words.
57
+ * @param {string | undefined} name
58
+ * @returns {string}
59
+ */
60
+ static CapitalWords(name) {
61
+ return (name || '').split(/[\s_]/)
62
+ .filter(item => item)
63
+ .map(word => (word ? word[0].toUpperCase() + word.substring(1).toLowerCase() : ''))
64
+ .join(' ');
65
+ }
66
+
67
+ static formatSeconds(seconds) {
68
+ const days_ = Math.floor(seconds / (3600 * 24));
69
+ seconds %= 3600 * 24;
70
+ let hours = Math.floor(seconds / 3600);
71
+ if (hours < 10) {
72
+ hours = `0${hours}`;
73
+ }
74
+ seconds %= 3600;
75
+ let minutes = Math.floor(seconds / 60);
76
+ if (minutes < 10) {
77
+ minutes = `0${minutes}`;
78
+ }
79
+ seconds %= 60;
80
+ seconds = Math.floor(seconds);
81
+ if (seconds < 10) {
82
+ seconds = `0${seconds}`;
83
+ }
84
+ let text = '';
85
+ if (days_) {
86
+ text += `${days_} ${I18n.t('ra_daysShortText')} `;
87
+ }
88
+ text += `${hours}:${minutes}:${seconds}`;
89
+
90
+ return text;
91
+ }
92
+
93
+ /**
94
+ * Get the name of the object by id from the name or description.
95
+ * @param {Record<string, ioBroker.Object>} objects
96
+ * @param {string} id
97
+ * @param {{ name: any; } | ioBroker.Languages | null} settings
98
+ * @param {{ language?: ioBroker.Languages; }} options
99
+ * @param {boolean} [isDesc] Set to true to get the description.
100
+ * @returns {string}
101
+ */
102
+ static getObjectName(objects, id, settings, options, isDesc) {
103
+ const item = objects[id];
104
+ let text;
105
+ const attr = isDesc ? 'desc' : 'name';
106
+
107
+ if (typeof settings === 'string' && !options) {
108
+ options = { language: settings };
109
+ settings = null;
110
+ }
111
+
112
+ options = options || {};
113
+ if (!options.language) {
114
+ options.language = (objects['system.config'] && objects['system.config'].common && objects['system.config'].common.language) || window.sysLang || 'en';
115
+ }
116
+ if (settings && settings.name) {
117
+ text = settings.name;
118
+ if (typeof text === 'object') {
119
+ text = text[options.language] || text.en;
120
+ }
121
+ } else if (item && item.common && item.common[attr]) {
122
+ text = item.common[attr];
123
+ if (attr !== 'desc' && !text && item.common.desc) {
124
+ text = item.common.desc;
125
+ }
126
+ if (typeof text === 'object') {
127
+ text = text[options.language] || text.en || text.de || text.ru || '';
128
+ }
129
+ text = (text || '').toString().replace(/[_.]/g, ' ');
130
+
131
+ if (text === text.toUpperCase()) {
132
+ text = text[0] + text.substring(1).toLowerCase();
133
+ }
134
+ } else {
135
+ const pos = id.lastIndexOf('.');
136
+ text = id.substring(pos + 1).replace(/[_.]/g, ' ');
137
+ text = Utils.CapitalWords(text);
138
+ }
139
+
140
+ return text.trim();
141
+ }
142
+
143
+ /**
144
+ * Get the name of the object from the name or description.
145
+ * @param {ioBroker.PartialObject} obj
146
+ * @param {{ name: any; } | ioBroker.Languages | null } settings or language
147
+ * @param {{ language?: ioBroker.Languages; } } options
148
+ * @param {boolean} [isDesc] Set to true to get the description.
149
+ * @param {boolean} [noTrim] Allow to use spaces in name (by edit)
150
+ * @returns {string}
151
+ */
152
+ static getObjectNameFromObj(obj, settings, options, isDesc, noTrim) {
153
+ const item = obj;
154
+ let text = (obj && obj._id) || '';
155
+ const attr = isDesc ? 'desc' : 'name';
156
+
157
+ if (typeof settings === 'string' && !options) {
158
+ options = { language: settings };
159
+ settings = null;
160
+ }
161
+
162
+ options = options || {};
163
+
164
+ if (settings && settings.name) {
165
+ text = settings.name;
166
+ if (typeof text === 'object') {
167
+ text = text[options.language] || text.en;
168
+ }
169
+ } else if (item && item.common && item.common[attr]) {
170
+ text = item.common[attr];
171
+ if (attr !== 'desc' && !text && item.common.desc) {
172
+ text = item.common.desc;
173
+ }
174
+ if (typeof text === 'object') {
175
+ text = text[options.language] || text.en;
176
+ }
177
+ text = (text || '').toString().replace(/[_.]/g, ' ');
178
+
179
+ if (text === text.toUpperCase()) {
180
+ text = text[0] + text.substring(1).toLowerCase();
181
+ }
182
+ }
183
+ return noTrim ? text : text.trim();
184
+ }
185
+
186
+ /**
187
+ * @param {ioBroker.PartialObject | ioBroker.ObjectCommon} obj
188
+ * @param {string} forEnumId
189
+ * @param {{ user: string; }} options
190
+ * @returns {string | null}
191
+ */
192
+ static getSettingsOrder(obj, forEnumId, options) {
193
+ if (obj && Object.prototype.hasOwnProperty.call(obj, 'common')) {
194
+ obj = obj.common;
195
+ }
196
+ let settings;
197
+ if (obj && obj.custom) {
198
+ settings = (obj.custom || {})[NAMESPACE];
199
+ const user = options.user || 'admin';
200
+ if (settings && settings[user]) {
201
+ if (forEnumId) {
202
+ if (settings[user].subOrder && settings[user].subOrder[forEnumId]) {
203
+ return JSON.parse(JSON.stringify(settings[user].subOrder[forEnumId]));
204
+ }
205
+ } else if (settings[user].order) {
206
+ return JSON.parse(JSON.stringify(settings[user].order));
207
+ }
208
+ }
209
+ }
210
+ return null;
211
+ }
212
+
213
+ /**
214
+ * @param {ioBroker.PartialObject | ioBroker.ObjectCommon} obj
215
+ * @param {string} forEnumId
216
+ * @param {{ user: string; }} options
217
+ */
218
+ static getSettingsCustomURLs(obj, forEnumId, options) {
219
+ if (obj && Object.prototype.hasOwnProperty.call(obj, 'common')) {
220
+ obj = obj.common;
221
+ }
222
+ let settings;
223
+ if (obj && obj.custom) {
224
+ settings = (obj.custom || {})[NAMESPACE];
225
+ const user = options.user || 'admin';
226
+ if (settings && settings[user]) {
227
+ if (forEnumId) {
228
+ if (settings[user].subURLs && settings[user].subURLs[forEnumId]) {
229
+ return JSON.parse(JSON.stringify(settings[user].subURLs[forEnumId]));
230
+ }
231
+ } else if (settings[user].URLs) {
232
+ return JSON.parse(JSON.stringify(settings[user].URLs));
233
+ }
234
+ }
235
+ }
236
+ return null;
237
+ }
238
+
239
+ /**
240
+ * Reorder the array items in list between source and dest.
241
+ * @param {Iterable<any> | ArrayLike<any>} list
242
+ * @param {number} source
243
+ * @param {number} dest
244
+ */
245
+ static reorder(list, source, dest) {
246
+ const result = Array.from(list);
247
+ const [removed] = result.splice(source, 1);
248
+ result.splice(dest, 0, removed);
249
+ return result;
250
+ }
251
+
252
+ /**
253
+ * @param {any} obj
254
+ * @param {{ id: any; user: any; name: any; icon: any; color: any; language: ioBroker.Languages; }} options
255
+ * @param {boolean} [defaultEnabling]
256
+ */
257
+ static getSettings(obj, options, defaultEnabling) {
258
+ let settings;
259
+ const id = (obj && obj._id) || (options && options.id);
260
+ if (obj && Object.prototype.hasOwnProperty.call(obj, 'common')) {
261
+ obj = obj.common;
262
+ }
263
+ if (obj?.custom) {
264
+ settings = obj.custom;
265
+ settings = settings[NAMESPACE] && settings[NAMESPACE][options.user || 'admin'] ? JSON.parse(JSON.stringify(settings[NAMESPACE][options.user || 'admin'])) : { enabled: true };
266
+ } else {
267
+ settings = { enabled: defaultEnabling === undefined ? true : defaultEnabling, useCustom: false };
268
+ }
269
+
270
+ if (!Object.prototype.hasOwnProperty.call(settings, 'enabled')) {
271
+ settings.enabled = defaultEnabling === undefined ? true : defaultEnabling;
272
+ }
273
+
274
+ // if (false && settings.useCommon) {
275
+ // if (obj.color) settings.color = obj.color;
276
+ // if (obj.icon) settings.icon = obj.icon;
277
+ // if (obj.name) settings.name = obj.name;
278
+ // } else {
279
+ if (options) {
280
+ if (!settings.name && options.name) settings.name = options.name;
281
+ if (!settings.icon && options.icon) settings.icon = options.icon;
282
+ if (!settings.color && options.color) settings.color = options.color;
283
+ }
284
+
285
+ if (obj) {
286
+ if (!settings.color && obj.color) settings.color = obj.color;
287
+ if (!settings.icon && obj.icon) settings.icon = obj.icon;
288
+ if (!settings.name && obj.name) settings.name = obj.name;
289
+ }
290
+ // }
291
+
292
+ if (typeof settings.name === 'object') {
293
+ settings.name = settings.name[options.language] || settings.name.en;
294
+
295
+ settings.name = (settings.name || '').toString().replace(/_/g, ' ');
296
+
297
+ if (settings.name === settings.name.toUpperCase()) {
298
+ settings.name = settings.name[0] + settings.name.substring(1).toLowerCase();
299
+ }
300
+ }
301
+ if (!settings.name && id) {
302
+ const pos = id.lastIndexOf('.');
303
+ settings.name = id.substring(pos + 1).replace(/[_.]/g, ' ');
304
+ settings.name = (settings.name || '').toString().replace(/_/g, ' ');
305
+ settings.name = Utils.CapitalWords(settings.name);
306
+ }
307
+
308
+ return settings;
309
+ }
310
+
311
+ /**
312
+ * @param {any} obj
313
+ * @param {any} settings
314
+ * @param {{ user: any; language: ioBroker.Languages; }} options
315
+ */
316
+ static setSettings(obj, settings, options) {
317
+ if (obj) {
318
+ obj.common = obj.common || {};
319
+ obj.common.custom = obj.common.custom || {};
320
+ obj.common.custom[NAMESPACE] = obj.common.custom[NAMESPACE] || {};
321
+ obj.common.custom[NAMESPACE][options.user || 'admin'] = settings;
322
+ const s = obj.common.custom[NAMESPACE][options.user || 'admin'];
323
+ if (s.useCommon) {
324
+ if (s.color !== undefined) {
325
+ obj.common.color = s.color;
326
+ delete s.color;
327
+ }
328
+ if (s.icon !== undefined) {
329
+ obj.common.icon = s.icon;
330
+ delete s.icon;
331
+ }
332
+ if (s.name !== undefined) {
333
+ if (typeof obj.common.name !== 'object') {
334
+ obj.common.name = {};
335
+ obj.common.name[options.language] = s.name;
336
+ } else {
337
+ obj.common.name[options.language] = s.name;
338
+ }
339
+ delete s.name;
340
+ }
341
+ }
342
+
343
+ return true;
344
+ }
345
+
346
+ return false;
347
+ }
348
+
349
+ /**
350
+ * Get the icon for the given settings.
351
+ * @param {{ icon: string | undefined; name: string | undefined; prefix: string | undefined}} settings
352
+ * @param {any} style
353
+ * @returns {JSX.Element | null}
354
+ */
355
+ static getIcon(settings, style) {
356
+ if (settings && settings.icon) {
357
+ // If UTF-8 icon
358
+ if (settings.icon.length <= 2) {
359
+ return <span style={style || {}}>{settings.icon}</span>;
360
+ }
361
+ if (settings.icon.startsWith('data:image')) {
362
+ return <img alt={settings.name} src={settings.icon} style={style || {}} />;
363
+ }
364
+ // maybe later some changes for a second type
365
+ return <img alt={settings.name} src={(settings.prefix || '') + settings.icon} style={style || {}} />;
366
+ }
367
+ return null;
368
+ }
369
+
370
+ /**
371
+ * Get the icon for the given object.
372
+ * @param {string} id
373
+ * @param {{ common: { icon: any; }; }} obj
374
+ * @returns {string | null}
375
+ */
376
+ static getObjectIcon(id, obj) {
377
+ // If id is Object
378
+ if (typeof id === 'object') {
379
+ obj = id;
380
+ id = obj._id;
381
+ }
382
+
383
+ if (obj && obj.common && obj.common.icon) {
384
+ let icon = obj.common.icon;
385
+ // If UTF-8 icon
386
+ if (typeof icon === 'string' && icon.length <= 2) {
387
+ return icon;
388
+ }
389
+ if (icon.startsWith('data:image')) {
390
+ return icon;
391
+ }
392
+
393
+ const parts = id.split('.');
394
+ if (parts[0] === 'system') {
395
+ icon = `adapter/${parts[2]}${icon.startsWith('/') ? '' : '/'}${icon}`;
396
+ } else {
397
+ icon = `adapter/${parts[0]}${icon.startsWith('/') ? '' : '/'}${icon}`;
398
+ }
399
+
400
+ if (window.location.pathname.match(/adapter\/[^/]+\/[^/]+\.html/)) {
401
+ icon = `../../${icon}`;
402
+ } else if (window.location.pathname.match(/material\/[.\d]+/)) {
403
+ icon = `../../${icon}`;
404
+ } else if (window.location.pathname.match(/material\//)) {
405
+ icon = `../${icon}`;
406
+ }
407
+ return icon;
408
+ }
409
+
410
+ return null;
411
+ }
412
+
413
+ /**
414
+ * Splits CamelCase into words.
415
+ * @param {string | undefined} text
416
+ * @returns {string}
417
+ */
418
+ static splitCamelCase(text) {
419
+ // if (false && text !== text.toUpperCase()) {
420
+ // const words = text.split(/\s+/);
421
+ // for (let i = 0; i < words.length; i++) {
422
+ // const word = words[i];
423
+ // if (word.toLowerCase() !== word && word.toUpperCase() !== word) {
424
+ // let z = 0;
425
+ // const ww = [];
426
+ // let start = 0;
427
+ // while (z < word.length) {
428
+ // if (word[z].match(/[A-ZÜÄÖА-Я]/)) {
429
+ // ww.push(word.substring(start, z));
430
+ // start = z;
431
+ // }
432
+ // z++;
433
+ // }
434
+ // if (start !== z) {
435
+ // ww.push(word.substring(start, z));
436
+ // }
437
+ // for (let k = 0; k < ww.length; k++) {
438
+ // words.splice(i + k, 0, ww[k]);
439
+ // }
440
+ // i += ww.length;
441
+ // }
442
+ // }
443
+ //
444
+ // return words.map(w => {
445
+ // w = w.trim();
446
+ // if (w) {
447
+ // return w[0].toUpperCase() + w.substring(1).toLowerCase();
448
+ // }
449
+ // return '';
450
+ // }).join(' ');
451
+ // }
452
+ return Utils.CapitalWords(text);
453
+ }
454
+
455
+ /**
456
+ * Check if the given color is bright.
457
+ * https://stackoverflow.com/questions/35969656/how-can-i-generate-the-opposite-color-according-to-current-color
458
+ * @param {string | null | undefined} color
459
+ * @param {boolean} [defaultValue]
460
+ * @returns {boolean}
461
+ */
462
+ static isUseBright(color, defaultValue) {
463
+ if (color === null || color === undefined || color === '') {
464
+ return defaultValue === undefined ? true : defaultValue;
465
+ }
466
+ color = color.toString();
467
+ if (color.startsWith('#')) {
468
+ color = color.slice(1);
469
+ }
470
+ let r;
471
+ let g;
472
+ let b;
473
+
474
+ const rgb = color.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
475
+ if (rgb && rgb.length === 4) {
476
+ r = parseInt(rgb[1], 10);
477
+ g = parseInt(rgb[2], 10);
478
+ b = parseInt(rgb[3], 10);
479
+ } else {
480
+ // convert 3-digit hex to 6-digits.
481
+ if (color.length === 3) {
482
+ color = color[0] + color[0] + color[1] + color[1] + color[2] + color[2];
483
+ }
484
+ // remove alfa channel
485
+ if (color.length === 8) {
486
+ color = color.substring(0, 6);
487
+ } else if (color.length !== 6) {
488
+ return false;
489
+ }
490
+
491
+ r = parseInt(color.slice(0, 2), 16);
492
+ g = parseInt(color.slice(2, 4), 16);
493
+ b = parseInt(color.slice(4, 6), 16);
494
+ }
495
+
496
+ // http://stackoverflow.com/a/3943023/112731
497
+ return (r * 0.299 + g * 0.587 + b * 0.114) <= 186;
498
+ }
499
+
500
+ /**
501
+ * Get the time string in the format 00:00.
502
+ * @param {string | number} seconds
503
+ */
504
+ static getTimeString(seconds) {
505
+ seconds = parseFloat(seconds);
506
+ if (Number.isNaN(seconds)) {
507
+ return '--:--';
508
+ }
509
+ const hours = Math.floor(seconds / 3600);
510
+ let minutes = Math.floor((seconds % 3600) / 60);
511
+ let secs = seconds % 60;
512
+ if (hours) {
513
+ if (minutes < 10) {
514
+ minutes = `0${minutes}`;
515
+ }
516
+ if (secs < 10) {
517
+ secs = `0${secs}`;
518
+ }
519
+ return `${hours}:${minutes}:${secs}`;
520
+ }
521
+
522
+ if (secs < 10) {
523
+ secs = `0${secs}`;
524
+ }
525
+ return `${minutes}:${secs}`;
526
+ }
527
+
528
+ /**
529
+ * Gets the wind direction with the given angle (degrees).
530
+ * @param {number} angle in degrees.
531
+ * @returns {string | undefined}
532
+ */
533
+ static getWindDirection(angle) {
534
+ if (angle >= 0 && angle < 11.25) {
535
+ return 'N';
536
+ }
537
+ if (angle >= 11.25 && angle < 33.75) {
538
+ return 'NNE';
539
+ }
540
+ if (angle >= 33.75 && angle < 56.25) {
541
+ return 'NE';
542
+ }
543
+ if (angle >= 56.25 && angle < 78.75) {
544
+ return 'ENE';
545
+ }
546
+ if (angle >= 78.75 && angle < 101.25) {
547
+ return 'E';
548
+ }
549
+ if (angle >= 101.25 && angle < 123.75) {
550
+ return 'ESE';
551
+ }
552
+ if (angle >= 123.75 && angle < 146.25) {
553
+ return 'SE';
554
+ }
555
+ if (angle >= 146.25 && angle < 168.75) {
556
+ return 'SSE';
557
+ }
558
+ if (angle >= 168.75 && angle < 191.25) {
559
+ return 'S';
560
+ }
561
+ if (angle >= 191.25 && angle < 213.75) {
562
+ return 'SSW';
563
+ }
564
+ if (angle >= 213.75 && angle < 236.25) {
565
+ return 'SW';
566
+ }
567
+ if (angle >= 236.25 && angle < 258.75) {
568
+ return 'WSW';
569
+ }
570
+ if (angle >= 258.75 && angle < 281.25) {
571
+ return 'W';
572
+ }
573
+ if (angle >= 281.25 && angle < 303.75) {
574
+ return 'WNW';
575
+ }
576
+ if (angle >= 303.75 && angle < 326.25) {
577
+ return 'NW';
578
+ }
579
+ if (angle >= 326.25 && angle < 348.75) {
580
+ return 'NNW';
581
+ }
582
+ // if (angle >= 348.75) {
583
+ return 'N';
584
+ }
585
+
586
+ /**
587
+ * Pad the given number with a zero if it's not 2 digits long.
588
+ * @param {string | number} num
589
+ */
590
+ static padding(num) {
591
+ if (typeof num === 'string') {
592
+ if (num.length < 2) {
593
+ return `0${num}`;
594
+ }
595
+ return num;
596
+ }
597
+ if (num < 10) {
598
+ return `0${num}`;
599
+ }
600
+ return num;
601
+ }
602
+
603
+ /**
604
+ * Sets the date format.
605
+ * @param {string} format
606
+ */
607
+ static setDataFormat(format) {
608
+ if (format) {
609
+ Utils.dateFormat = format.toUpperCase().split(/[.-/]/);
610
+ Utils.dateFormat.splice(Utils.dateFormat.indexOf('YYYY'), 1);
611
+ }
612
+ }
613
+
614
+ /**
615
+ * Converts the date to a string.
616
+ * @param {string | number | Date} now
617
+ * @returns {string}
618
+ */
619
+ static date2string(now) {
620
+ if (typeof now === 'string') {
621
+ now = now.trim();
622
+ if (!now) return '';
623
+ // only letters
624
+ if (now.match(/^[\w\s]+$/)) {
625
+ // Day of the week
626
+ return now;
627
+ }
628
+ const m = now.match(/(\d{1,4})[-./](\d{1,2})[-./](\d{1,4})/);
629
+ if (m) {
630
+ const a = [parseInt(m[1], 10), parseInt(m[2], 10), parseInt(m[3], 10)];
631
+ const year = a.find(y => y > 31);
632
+ a.splice(a.indexOf(year), 1);
633
+ const day = a.find(mm => mm > 12);
634
+ if (day) {
635
+ a.splice(a.indexOf(day), 1);
636
+ now = new Date(year, a[0] - 1, day);
637
+ } else if (Utils.dateFormat[0][0] === 'M' && Utils.dateFormat[1][0] === 'D') {
638
+ // MM DD
639
+ now = new Date(year, a[0] - 1, a[1]);
640
+ if (Math.abs(now.getTime - Date.now()) > 3600000 * 24 * 10) {
641
+ now = new Date(year, a[1] - 1, a[0]);
642
+ }
643
+ } else if (Utils.dateFormat[0][0] === 'D' && Utils.dateFormat[1][0] === 'M') {
644
+ // DD MM
645
+ now = new Date(year, a[1] - 1, a[0]);
646
+ if (Math.abs(now.getTime - Date.now()) > 3600000 * 24 * 10) {
647
+ now = new Date(year, a[0] - 1, a[1]);
648
+ }
649
+ } else {
650
+ now = new Date(now);
651
+ }
652
+ } else {
653
+ now = new Date(now);
654
+ }
655
+ } else {
656
+ now = new Date(now);
657
+ }
658
+
659
+ let date = I18n.t(`ra_dow_${days[now.getDay()]}`).replace('ra_dow_', '');
660
+ date += `. ${now.getDate()} ${I18n.t(`ra_month_${months[now.getMonth()]}`).replace('ra_month_', '')}`;
661
+ return date;
662
+ }
663
+
664
+ /**
665
+ * Render a text as a link.
666
+ * @param {string} text
667
+ * @returns {string | JSX.Element[]}
668
+ */
669
+ static renderTextWithA(text) {
670
+ let m = text.match(/<a [^<]+<\/a>|<br\/?>|<b>[^<]+<\/b>|<i>[^<]+<\/i>/);
671
+ if (m) {
672
+ const result = [];
673
+ let key = 1;
674
+ do {
675
+ const start = text.substring(0, m.index);
676
+ text = text.substring(m.index + m[0].length);
677
+ start && result.push(<span key={`a${key++}`}>{start}</span>);
678
+
679
+ if (m[0].startsWith('<b>')) {
680
+ result.push(<b key={`a${key++}`}>{m[0].substring(3, m[0].length - 4)}</b>);
681
+ } else if (m[0].startsWith('<i>')) {
682
+ result.push(<i key={`a${key++}`}>{m[0].substring(3, m[0].length - 4)}</i>);
683
+ } else if (m[0].startsWith('<br')) {
684
+ result.push(<br key={`a${key++}`} />);
685
+ } else {
686
+ const href = m[0].match(/href="([^"]+)"/) || m[0].match(/href='([^']+)'/);
687
+ const target = m[0].match(/target="([^"]+)"/) || m[0].match(/target='([^']+)'/);
688
+ const rel = m[0].match(/rel="([^"]+)"/) || m[0].match(/rel='([^']+)'/);
689
+ const title = m[0].match(/>([^<]*)</);
690
+
691
+ // eslint-disable-next-line
692
+ result.push(<a
693
+ key={`a${key++}`}
694
+ href={href ? href[1] : ''}
695
+ target={target ? target[1] : '_blank'}
696
+ rel={rel ? rel[1] : ''}
697
+ style={{ color: 'inherit' }}
698
+ >
699
+ {title ? title[1] : ''}
700
+ </a>);
701
+ }
702
+
703
+ m = text && text.match(/<a [^<]+<\/a>|<br\/?>|<b>[^<]+<\/b>|<i>[^<]+<\/i>/);
704
+ if (!m) {
705
+ text && result.push(<span key={`a${key++}`}>{text}</span>);
706
+ }
707
+ } while (m);
708
+
709
+ return result;
710
+ }
711
+
712
+ return text;
713
+ }
714
+
715
+ /**
716
+ * Get the smart name of the given state.
717
+ * @param {Record<string, ioBroker.StateObject> | ioBroker.StateObject} states
718
+ * @param {string} id
719
+ * @param {string} instanceId
720
+ * @param {boolean} [noCommon]
721
+ */
722
+ static getSmartName(states, id, instanceId, noCommon) {
723
+ if (!id) {
724
+ if (!noCommon) {
725
+ if (!states.common) {
726
+ return states.smartName;
727
+ }
728
+ if (states && !states.common) {
729
+ return states.smartName;
730
+ }
731
+ return states.common.smartName;
732
+ }
733
+ if (states && !states.common) {
734
+ return states.smartName;
735
+ }
736
+ return states?.common?.custom && states.common.custom[instanceId] ?
737
+ states.common.custom[instanceId].smartName : undefined;
738
+ }
739
+ if (!noCommon) {
740
+ return states[id].common.smartName;
741
+ }
742
+
743
+ return (states[id] &&
744
+ states[id].common?.custom &&
745
+ states[id].common.custom[instanceId]) ?
746
+ states[id].common.custom[instanceId].smartName || null : null;
747
+ }
748
+
749
+ /**
750
+ * Get the smart name from a state.
751
+ * @param {ioBroker.StateObject} obj
752
+ * @param {string} instanceId
753
+ * @param {boolean} [noCommon]
754
+ */
755
+ static getSmartNameFromObj(obj, instanceId, noCommon) {
756
+ if (!noCommon) {
757
+ if (!obj.common) {
758
+ return obj.smartName;
759
+ }
760
+ if (obj && !obj.common) {
761
+ return obj.smartName;
762
+ }
763
+ return obj.common.smartName;
764
+ }
765
+ if (obj && !obj.common) {
766
+ return obj.smartName;
767
+ }
768
+
769
+ return obj?.common?.custom && obj.common.custom[instanceId] ?
770
+ obj.common.custom[instanceId].smartName : undefined;
771
+ }
772
+
773
+ /**
774
+ * Enable smart name for a state.
775
+ * @param {ioBroker.StateObject} obj
776
+ * @param {string} instanceId
777
+ * @param {boolean} [noCommon]
778
+ */
779
+ static enableSmartName(obj, instanceId, noCommon) {
780
+ if (noCommon) {
781
+ obj.common.custom = obj.common.custom || {};
782
+ obj.common.custom[instanceId] = obj.common.custom[instanceId] || {};
783
+ obj.common.custom[instanceId].smartName = {};
784
+ } else {
785
+ obj.common.smartName = {};
786
+ }
787
+ }
788
+
789
+ /**
790
+ * Completely remove smart name from a state.
791
+ * @param {ioBroker.StateObject} obj
792
+ * @param {string | number} instanceId
793
+ * @param {boolean} [noCommon]
794
+ */
795
+ static removeSmartName(obj, instanceId, noCommon) {
796
+ if (noCommon) {
797
+ if (obj.common && obj.common.custom && obj.common.custom[instanceId]) {
798
+ obj.common.custom[instanceId] = null;
799
+ }
800
+ } else {
801
+ obj.common.smartName = null;
802
+ }
803
+ }
804
+
805
+ /**
806
+ * Update the smartname of a state.
807
+ * @param {ioBroker.StateObject} obj
808
+ * @param {string} newSmartName
809
+ * @param {string | undefined} byON
810
+ * @param {string | undefined} smartType
811
+ * @param {string} instanceId
812
+ * @param {boolean} [noCommon]
813
+ */
814
+ static updateSmartName(obj, newSmartName, byON, smartType, instanceId, noCommon) {
815
+ const language = I18n.getLanguage();
816
+
817
+ // convert the old format
818
+ if (typeof obj.common.smartName === 'string') {
819
+ const nnn = obj.common.smartName;
820
+ obj.common.smartName = {};
821
+ obj.common.smartName[language] = nnn;
822
+ }
823
+
824
+ // convert the old settings
825
+ if (obj.native && obj.native.byON) {
826
+ delete obj.native.byON;
827
+ let _smartName = obj.common.smartName;
828
+
829
+ if (!_smartName || typeof _smartName !== 'object') {
830
+ _smartName = { en: _smartName };
831
+ _smartName[language] = _smartName.en;
832
+ }
833
+ obj.common.smartName = _smartName;
834
+ }
835
+ if (smartType !== undefined) {
836
+ if (noCommon) {
837
+ obj.common.custom = obj.common.custom || {};
838
+ obj.common.custom[instanceId] = obj.common.custom[instanceId] || {};
839
+ obj.common.custom[instanceId].smartName = obj.common.custom[instanceId].smartName || {};
840
+ if (!smartType) {
841
+ delete obj.common.custom[instanceId].smartName.smartType;
842
+ } else {
843
+ obj.common.custom[instanceId].smartName.smartType = smartType;
844
+ }
845
+ } else {
846
+ obj.common.smartName = obj.common.smartName || {};
847
+ if (!smartType) {
848
+ delete obj.common.smartName.smartType;
849
+ } else {
850
+ obj.common.smartName.smartType = smartType;
851
+ }
852
+ }
853
+ }
854
+
855
+ if (byON !== undefined) {
856
+ if (noCommon) {
857
+ obj.common.custom = obj.common.custom || {};
858
+ obj.common.custom[instanceId] = obj.common.custom[instanceId] || {};
859
+ obj.common.custom[instanceId].smartName = obj.common.custom[instanceId].smartName || {};
860
+ obj.common.custom[instanceId].smartName.byON = byON;
861
+ } else {
862
+ obj.common.smartName = obj.common.smartName || {};
863
+ obj.common.smartName.byON = byON;
864
+ }
865
+ }
866
+
867
+ if (newSmartName !== undefined) {
868
+ let smartName;
869
+ if (noCommon) {
870
+ obj.common.custom = obj.common.custom || {};
871
+ obj.common.custom[instanceId] = obj.common.custom[instanceId] || {};
872
+ obj.common.custom[instanceId].smartName = obj.common.custom[instanceId].smartName || {};
873
+ smartName = obj.common.custom[instanceId].smartName;
874
+ } else {
875
+ obj.common.smartName = obj.common.smartName || {};
876
+ smartName = obj.common.smartName;
877
+ }
878
+ smartName[language] = newSmartName;
879
+
880
+ // If smart name deleted
881
+ if (smartName && (!smartName[language] ||
882
+ (smartName[language] === obj.common.name &&
883
+ (!obj.common.role || obj.common.role.includes('button'))))) {
884
+ delete smartName[language];
885
+ let empty = true;
886
+ // Check if the structure has any definitions
887
+ for (const key in smartName) {
888
+ if (Object.prototype.hasOwnProperty.call(smartName, key)) {
889
+ empty = false;
890
+ break;
891
+ }
892
+ }
893
+ // If empty => delete smartName completely
894
+ if (empty) {
895
+ if (noCommon) {
896
+ if (obj.common.custom[instanceId].smartName.byON === undefined) {
897
+ delete obj.common.custom[instanceId];
898
+ } else {
899
+ delete obj.common.custom[instanceId].en;
900
+ delete obj.common.custom[instanceId].de;
901
+ delete obj.common.custom[instanceId].ru;
902
+ delete obj.common.custom[instanceId].nl;
903
+ delete obj.common.custom[instanceId].pl;
904
+ delete obj.common.custom[instanceId].it;
905
+ delete obj.common.custom[instanceId].fr;
906
+ delete obj.common.custom[instanceId].pt;
907
+ delete obj.common.custom[instanceId].es;
908
+ delete obj.common.custom[instanceId].uk;
909
+ delete obj.common.custom[instanceId]['zh-cn'];
910
+ }
911
+ } else if (obj.common.smartName.byON !== undefined) {
912
+ delete obj.common.smartName.en;
913
+ delete obj.common.smartName.de;
914
+ delete obj.common.smartName.ru;
915
+ delete obj.common.smartName.nl;
916
+ delete obj.common.smartName.pl;
917
+ delete obj.common.smartName.it;
918
+ delete obj.common.smartName.fr;
919
+ delete obj.common.smartName.pt;
920
+ delete obj.common.smartName.es;
921
+ delete obj.common.smartName.uk;
922
+ delete obj.common.smartName['zh-cn'];
923
+ } else {
924
+ obj.common.smartName = null;
925
+ }
926
+ }
927
+ }
928
+ }
929
+ }
930
+
931
+ /**
932
+ * Disable the smart name of a state.
933
+ * @param {ioBroker.StateObject} obj
934
+ * @param {string} instanceId
935
+ * @param {boolean} [noCommon]
936
+ */
937
+ static disableSmartName(obj, instanceId, noCommon) {
938
+ if (noCommon) {
939
+ obj.common.custom = obj.common.custom || {};
940
+ obj.common.custom[instanceId] = obj.common.custom[instanceId] || {};
941
+ obj.common.custom[instanceId].smartName = false;
942
+ } else {
943
+ obj.common.smartName = false;
944
+ }
945
+ }
946
+
947
+ /**
948
+ * Copy text to the clipboard.
949
+ * @param {string} text
950
+ * @param {Event} [e]
951
+ */
952
+ static copyToClipboard(text, e) {
953
+ e && e.stopPropagation();
954
+ e && e.preventDefault();
955
+ return _Utils.copyToClipboard(text);
956
+ }
957
+
958
+ /**
959
+ * Gets the extension of a file name.
960
+ * @param {string | null} [fileName] the file name.
961
+ * @returns {string | null} The extension in lower case.
962
+ */
963
+ static getFileExtension(fileName) {
964
+ const pos = (fileName || '').lastIndexOf('.');
965
+ if (pos !== -1) {
966
+ return fileName.substring(pos + 1).toLowerCase();
967
+ }
968
+ return null;
969
+ }
970
+
971
+ /**
972
+ * Format number of bytes as a string with B, KB, MB or GB.
973
+ * The base for all calculations is 1024.
974
+ * @param {number} bytes The number of bytes.
975
+ * @returns {string} The formatted string (e.g. '723.5 KB')
976
+ */
977
+ static formatBytes(bytes) {
978
+ if (Math.abs(bytes) < 1024) {
979
+ return `${bytes} B`;
980
+ }
981
+
982
+ const units = ['KB', 'MB', 'GB'];
983
+ // const units = ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
984
+ let u = -1;
985
+
986
+ do {
987
+ bytes /= 1024;
988
+ ++u;
989
+ } while (Math.abs(bytes) >= 1024 && u < units.length - 1);
990
+
991
+ return `${bytes.toFixed(1)} ${units[u]}`;
992
+ }
993
+
994
+ /**
995
+ * Invert the given color according to a theme type to get the inverted text color for background
996
+ * @param {string} color Color in the format '#rrggbb' or '#rgb' (or without a hash)
997
+ * @param {string} themeType theme type
998
+ * @param {string} invert dark theme has light color in control or light theme has light color in control
999
+ * @returns {string | undefined}
1000
+ */
1001
+ static getInvertedColor(color, themeType, invert) {
1002
+ if (!color) {
1003
+ return undefined;
1004
+ }
1005
+ const invertedColor = Utils.invertColor(color, true);
1006
+ if (invertedColor === '#FFFFFF' && (themeType === 'dark' || (invert && themeType === 'light'))) {
1007
+ return '#DDD';
1008
+ }
1009
+ if (invertedColor === '#000000' && (themeType === 'light' || (invert && themeType === 'dark'))) {
1010
+ return '#222';
1011
+ }
1012
+
1013
+ return undefined;
1014
+ }
1015
+
1016
+ // Big thanks to: https://stackoverflow.com/questions/35969656/how-can-i-generate-the-opposite-color-according-to-current-color
1017
+ /**
1018
+ * Invert the given color
1019
+ * @param {string} hex Color in the format '#rrggbb' or '#rgb' (or without hash)
1020
+ * @param {boolean} bw Set to black or white.
1021
+ * @returns {string}
1022
+ */
1023
+ static invertColor(hex, bw) {
1024
+ if (hex === undefined || hex === null || hex === '' || typeof hex !== 'string') {
1025
+ return '';
1026
+ }
1027
+ if (hex.startsWith('rgba')) {
1028
+ const m = hex.match(/rgba?\((\d+),\s*(\d+),\s*(\d+),\s*([.\d]+)\)/);
1029
+ if (m) {
1030
+ hex = parseInt(m[1], 10).toString(16).padStart(2, '0') +
1031
+ parseInt(m[2], 10).toString(16).padStart(2, '0') +
1032
+ parseInt(m[2], 10).toString(16).padStart(2, '0');
1033
+ }
1034
+ } else if (hex.startsWith('rgb')) {
1035
+ const m = hex.match(/rgb?\((\d+),\s*(\d+),\s*(\d+)\)/);
1036
+ if (m) {
1037
+ hex = parseInt(m[1], 10).toString(16).padStart(2, '0') +
1038
+ parseInt(m[2], 10).toString(16).padStart(2, '0') +
1039
+ parseInt(m[2], 10).toString(16).padStart(2, '0');
1040
+ }
1041
+ } else if (hex.startsWith('#')) {
1042
+ hex = hex.slice(1);
1043
+ }
1044
+ // convert 3-digit hex to 6-digits.
1045
+ if (hex.length === 3) {
1046
+ hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
1047
+ }
1048
+ let alfa = null;
1049
+ if (hex.length === 8) {
1050
+ alfa = hex.substring(6, 8);
1051
+ hex = hex.substring(0, 6);
1052
+ } else if (hex.length !== 6) {
1053
+ console.warn(`Cannot invert color: ${hex}`);
1054
+ return hex;
1055
+ }
1056
+ let r = parseInt(hex.slice(0, 2), 16);
1057
+ let g = parseInt(hex.slice(2, 4), 16);
1058
+ let b = parseInt(hex.slice(4, 6), 16);
1059
+
1060
+ if (bw) {
1061
+ // http://stackoverflow.com/a/3943023/112731
1062
+ return (r * 0.299 + g * 0.587 + b * 0.114) > 186
1063
+ ? `#000000${alfa || ''}`
1064
+ : `#FFFFFF${alfa || ''}`;
1065
+ }
1066
+ // invert color components
1067
+ r = (255 - r).toString(16);
1068
+ g = (255 - g).toString(16);
1069
+ b = (255 - b).toString(16);
1070
+ // pad each with zeros and return
1071
+ return `#${r.padStart(2, '0')}${g.padStart(2, '0')}${b.padStart(2, '0')}${alfa || ''}`;
1072
+ }
1073
+
1074
+ /**
1075
+ * Convert RGB to array [r, g, b]
1076
+ * @param {string} hex Color in the format '#rrggbb' or '#rgb' (or without hash) or rgb(r,g,b) or rgba(r,g,b,a)
1077
+ * @returns {Array<number>} Array with 3 elements [r, g, b]
1078
+ */
1079
+ static color2rgb(hex) {
1080
+ if (hex === undefined || hex === null || hex === '' || typeof hex !== 'string') {
1081
+ return '';
1082
+ }
1083
+ if (hex.startsWith('rgba')) {
1084
+ const m = hex.match(/rgba?\((\d+),\s*(\d+),\s*(\d+),\s*([.\d]+)\)/);
1085
+ if (m) {
1086
+ hex = parseInt(m[1], 10).toString(16).padStart(2, '0') +
1087
+ parseInt(m[2], 10).toString(16).padStart(2, '0') +
1088
+ parseInt(m[2], 10).toString(16).padStart(2, '0');
1089
+ }
1090
+ } else if (hex.startsWith('rgb')) {
1091
+ const m = hex.match(/rgb?\((\d+),\s*(\d+),\s*(\d+)\)/);
1092
+ if (m) {
1093
+ hex = parseInt(m[1], 10).toString(16).padStart(2, '0') +
1094
+ parseInt(m[2], 10).toString(16).padStart(2, '0') +
1095
+ parseInt(m[2], 10).toString(16).padStart(2, '0');
1096
+ }
1097
+ } else if (hex.startsWith('#')) {
1098
+ hex = hex.slice(1);
1099
+ }
1100
+ // convert 3-digit hex to 6-digits.
1101
+ if (hex.length === 3) {
1102
+ hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
1103
+ }
1104
+ if (hex.length !== 6 && hex.length !== 8) {
1105
+ console.warn(`Cannot invert color: ${hex}`);
1106
+ return false;
1107
+ }
1108
+
1109
+ return [
1110
+ parseInt(hex.slice(0, 2), 16),
1111
+ parseInt(hex.slice(2, 4), 16),
1112
+ parseInt(hex.slice(4, 6), 16),
1113
+ ];
1114
+ }
1115
+
1116
+ // Big thanks to: https://github.com/antimatter15/rgb-lab
1117
+ /**
1118
+ * Convert RGB to LAB
1119
+ * @param {Array<number>} rgb color in format [r,g,b]
1120
+ * @returns {Array<number>} lab color in format [l,a,b]
1121
+ */
1122
+ static rgb2lab(rgb) {
1123
+ let r = rgb[0] / 255;
1124
+ let g = rgb[1] / 255;
1125
+ let b = rgb[2] / 255;
1126
+
1127
+ r = (r > 0.04045) ? ((r + 0.055) / 1.055) ** 2.4 : r / 12.92;
1128
+ g = (g > 0.04045) ? ((g + 0.055) / 1.055) ** 2.4 : g / 12.92;
1129
+ b = (b > 0.04045) ? ((b + 0.055) / 1.055) ** 2.4 : b / 12.92;
1130
+
1131
+ let x = (r * 0.4124 + g * 0.3576 + b * 0.1805) / 0.95047;
1132
+ let y = (r * 0.2126 + g * 0.7152 + b * 0.0722); /* / 1.00000; */
1133
+ let z = (r * 0.0193 + g * 0.1192 + b * 0.9505) / 1.08883;
1134
+
1135
+ x = (x > 0.008856) ? x ** 0.33333333 : (7.787 * x) + 0.137931; // 16 / 116;
1136
+ y = (y > 0.008856) ? y ** 0.33333333 : (7.787 * y) + 0.137931; // 16 / 116;
1137
+ z = (z > 0.008856) ? z ** 0.33333333 : (7.787 * z) + 0.137931; // 16 / 116;
1138
+
1139
+ return [(116 * y) - 16, 500 * (x - y), 200 * (y - z)];
1140
+ }
1141
+
1142
+ /**
1143
+ * Calculate the distance between two colors in LAB color space in the range 0-100^2
1144
+ * If distance is less than 1000, the colors are similar
1145
+ * @param {string} color1 Color in the format '#rrggbb' or '#rgb' (or without hash) or rgb(r,g,b) or rgba(r,g,b,a)
1146
+ * @param {string} color2 Color in the format '#rrggbb' or '#rgb' (or without hash) or rgb(r,g,b) or rgba(r,g,b,a)
1147
+ * @returns {number} distance in the range 0-100^2
1148
+ */
1149
+ static colorDistance(color1, color2) {
1150
+ const lab1 = Utils.rgb2lab(Utils.color2rgb(color1));
1151
+ const lab2 = Utils.rgb2lab(Utils.color2rgb(color2));
1152
+ const dltL = lab1[0] - lab2[0];
1153
+ const dltA = lab1[1] - lab2[1];
1154
+ const dltB = lab1[2] - lab2[2];
1155
+ const c1 = Math.sqrt(lab1[1] * lab1[1] + lab1[2] * lab1[2]);
1156
+ const c2 = Math.sqrt(lab2[1] * lab2[1] + lab2[2] * lab2[2]);
1157
+ const dltC = c1 - c2;
1158
+ let dltH = dltA * dltA + dltB * dltB - dltC * dltC;
1159
+ dltH = dltH < 0 ? 0 : Math.sqrt(dltH);
1160
+ const sc = 1.0 + 0.045 * c1;
1161
+ const sh = 1.0 + 0.015 * c1;
1162
+ const dltLKlsl = dltL;
1163
+ const dltCkcsc = dltC / sc;
1164
+ const dltHkhsh = dltH / sh;
1165
+ const i = dltLKlsl * dltLKlsl + dltCkcsc * dltCkcsc + dltHkhsh * dltHkhsh;
1166
+ return i < 0 ? 0 : i;
1167
+ }
1168
+
1169
+ // https://github.com/lukeed/clsx/blob/master/src/index.js
1170
+ // License
1171
+ // MIT © Luke Edwards
1172
+ /**
1173
+ * @private
1174
+ * @param {any} mix
1175
+ * @returns {string}
1176
+ */
1177
+ static _toVal(mix) {
1178
+ let y;
1179
+ let str = '';
1180
+
1181
+ if (typeof mix === 'string' || typeof mix === 'number') {
1182
+ str += mix;
1183
+ } else if (typeof mix === 'object') {
1184
+ if (Array.isArray(mix)) {
1185
+ for (let k = 0; k < mix.length; k++) {
1186
+ if (mix[k]) {
1187
+ y = Utils._toVal(mix[k]);
1188
+ if (y) {
1189
+ str && (str += ' ');
1190
+ str += y;
1191
+ }
1192
+ }
1193
+ }
1194
+ } else {
1195
+ for (const k in mix) {
1196
+ if (mix[k]) {
1197
+ str && (str += ' ');
1198
+ str += k;
1199
+ }
1200
+ }
1201
+ }
1202
+ }
1203
+
1204
+ return str;
1205
+ }
1206
+
1207
+ // https://github.com/lukeed/clsx/blob/master/src/index.js
1208
+ // License
1209
+ // MIT © Luke Edwards
1210
+ /**
1211
+ * Convert any object to a string with its values.
1212
+ * @returns {string}
1213
+ */
1214
+ static clsx() {
1215
+ let i = 0;
1216
+ let tmp;
1217
+ let x;
1218
+ let str = '';
1219
+ while (i < arguments.length) {
1220
+ // eslint-disable-next-line prefer-rest-params
1221
+ tmp = arguments[i++];
1222
+ if (tmp) {
1223
+ x = Utils._toVal(tmp);
1224
+ if (x) {
1225
+ str && (str += ' ');
1226
+ str += x;
1227
+ }
1228
+ }
1229
+ }
1230
+ return str;
1231
+ }
1232
+
1233
+ /**
1234
+ * Get the current theme name (either from local storage or the browser settings).
1235
+ * @param {string} [themeName]
1236
+ * @returns {string}
1237
+ */
1238
+ static getThemeName(themeName = '') {
1239
+ if (window.vendorPrefix && window.vendorPrefix !== '@@vendorPrefix@@') {
1240
+ return window.vendorPrefix;
1241
+ }
1242
+
1243
+ return themeName || ((window._localStorage || window.localStorage).getItem('App.themeName') ?
1244
+ (window._localStorage || window.localStorage).getItem('App.themeName') : window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'colored');
1245
+ }
1246
+
1247
+ /**
1248
+ * Get the type of theme.
1249
+ * @param {string} [themeName]
1250
+ * @returns {'dark' | 'light'}
1251
+ */
1252
+ static getThemeType(themeName = '') {
1253
+ if (window.vendorPrefix && window.vendorPrefix !== '@@vendorPrefix@@') {
1254
+ return 'light';
1255
+ }
1256
+
1257
+ themeName = themeName || (window._localStorage || window.localStorage).getItem('App.themeName');
1258
+ return themeName === 'dark' || themeName === 'blue' ? 'dark' : 'light';
1259
+ }
1260
+
1261
+ /**
1262
+ * Set the theme name and theme type.
1263
+ * @param {string} themeName
1264
+ */
1265
+ static setThemeName(themeName) {
1266
+ if (window.vendorPrefix && window.vendorPrefix !== '@@vendorPrefix@@') {
1267
+ return; // ignore
1268
+ }
1269
+ (window._localStorage || window.localStorage).setItem('App.themeName', themeName);
1270
+ (window._localStorage || window.localStorage).setItem('App.theme', themeName === 'dark' || themeName === 'blue' ? 'dark' : 'light');
1271
+ }
1272
+
1273
+ /**
1274
+ * Toggle the theme name between 'dark' and 'colored'.
1275
+ * @param {string | null} themeName
1276
+ * @returns {string} the new theme name.
1277
+ */
1278
+ static toggleTheme(themeName) {
1279
+ if (window.vendorPrefix && window.vendorPrefix !== '@@vendorPrefix@@') {
1280
+ return window.vendorPrefix;
1281
+ }
1282
+ themeName = themeName || (window._localStorage || window.localStorage).getItem('App.themeName');
1283
+
1284
+ // dark => blue => colored => light => dark
1285
+ const themes = Utils.getThemeNames();
1286
+ const pos = themes.indexOf(themeName);
1287
+ let newTheme;
1288
+ if (pos !== -1) {
1289
+ newTheme = themes[(pos + 1) % themes.length];
1290
+ } else {
1291
+ newTheme = themes[0];
1292
+ }
1293
+ Utils.setThemeName(newTheme);
1294
+
1295
+ return newTheme;
1296
+ }
1297
+
1298
+ /**
1299
+ * Get the list of themes
1300
+ * @returns {array<string>} list of possible themes
1301
+ */
1302
+ static getThemeNames() {
1303
+ if (window.vendorPrefix && window.vendorPrefix !== '@@vendorPrefix@@') {
1304
+ return [window.vendorPrefix];
1305
+ }
1306
+
1307
+ return ['light', 'dark', 'blue', 'colored'];
1308
+ }
1309
+
1310
+ /**
1311
+ * Parse a query string into its parts.
1312
+ * @param {string} query
1313
+ * @returns {Record<string, string | boolean | number>}
1314
+ */
1315
+ static parseQuery(query) {
1316
+ query = (query || '').toString().replace(/^\?/, '');
1317
+ /** @type {Record<string, string | boolean | number>} */
1318
+ const result = {};
1319
+ query.split('&').forEach(part => {
1320
+ part = part.trim();
1321
+ if (part) {
1322
+ const parts = part.split('=');
1323
+ const attr = decodeURIComponent(parts[0]).trim();
1324
+ if (parts.length > 1) {
1325
+ result[attr] = decodeURIComponent(parts[1]);
1326
+ if (result[attr] === 'true') {
1327
+ result[attr] = true;
1328
+ } else if (result[attr] === 'false') {
1329
+ result[attr] = false;
1330
+ } else {
1331
+ const f = parseFloat(result[attr]);
1332
+ if (f.toString() === result[attr]) {
1333
+ result[attr] = f;
1334
+ }
1335
+ }
1336
+ } else {
1337
+ result[attr] = true;
1338
+ }
1339
+ }
1340
+ });
1341
+ return result;
1342
+ }
1343
+
1344
+ /**
1345
+ * Returns parent ID.
1346
+ * @param {string} id
1347
+ * @returns {string | null} parent ID or null if no parent
1348
+ */
1349
+ static getParentId(id) {
1350
+ const p = (id || '').toString().split('.');
1351
+ if (p.length > 1) {
1352
+ p.pop();
1353
+ return p.join('.');
1354
+ }
1355
+
1356
+ return null;
1357
+ }
1358
+
1359
+ static formatDate(dateObj, dateFormat) {
1360
+ // format could be DD.MM.YYYY, YYYY.MM.DD or MM/DD/YYYY
1361
+
1362
+ if (!dateObj) {
1363
+ return '';
1364
+ }
1365
+
1366
+ let text;
1367
+ let mm = dateObj.getMonth() + 1;
1368
+ if (mm < 10) {
1369
+ mm = `0${mm}`;
1370
+ }
1371
+
1372
+ let dd = dateObj.getDate();
1373
+ if (dd < 10) {
1374
+ dd = `0${dd}`;
1375
+ }
1376
+
1377
+ if (dateFormat === 'MM/DD/YYYY') {
1378
+ text = `${mm}/${dd}/${dateObj.getFullYear()}`;
1379
+ } else {
1380
+ text = `${dateObj.getFullYear()}-${mm}-${dd}`;
1381
+ }
1382
+
1383
+ // time
1384
+ let v = dateObj.getHours();
1385
+ if (v < 10) {
1386
+ text += ` 0${v}`;
1387
+ } else {
1388
+ text += ` ${v}`;
1389
+ }
1390
+ v = dateObj.getMinutes();
1391
+ if (v < 10) {
1392
+ text += `:0${v}`;
1393
+ } else {
1394
+ text += `:${v}`;
1395
+ }
1396
+
1397
+ v = dateObj.getSeconds();
1398
+ if (v < 10) {
1399
+ text += `:0${v}`;
1400
+ } else {
1401
+ text += `:${v}`;
1402
+ }
1403
+
1404
+ v = dateObj.getMilliseconds();
1405
+ if (v < 10) {
1406
+ text += `.00${v}`;
1407
+ } else if (v < 100) {
1408
+ text += `.0${v}`;
1409
+ } else {
1410
+ text += `.${v}`;
1411
+ }
1412
+
1413
+ return text;
1414
+ }
1415
+
1416
+ static formatTime(seconds) {
1417
+ if (seconds) {
1418
+ seconds = Math.round(seconds);
1419
+ const d = Math.floor(seconds / (3600 * 24));
1420
+ const h = Math.floor((seconds % (3600 * 24)) / 3600);
1421
+ const m = Math.floor((seconds % 3600) / 60);
1422
+ const s = seconds % 60;
1423
+ if (d) {
1424
+ return `${d}.${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`;
1425
+ }
1426
+ if (h) {
1427
+ return `${h}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`;
1428
+ }
1429
+
1430
+ return `0:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`;
1431
+ }
1432
+ return '0:00:00';
1433
+ }
1434
+
1435
+ static MDtext2link(text) {
1436
+ const m = text.match(/\d+\.\)\s/);
1437
+ if (m) {
1438
+ text = text.replace(m[0], m[0].replace(/\s/, '&nbsp;'));
1439
+ }
1440
+
1441
+ return text.replace(/[^a-zA-Zа-яА-Я0-9]/g, '').trim().replace(/\s/g, '').toLowerCase();
1442
+ }
1443
+
1444
+ static openLink(url, target) {
1445
+ // replace IPv6 Address with [ipv6]:port
1446
+ url = url.replace(/\/\/([0-9a-f]*:[0-9a-f]*:[0-9a-f]*:[0-9a-f]*:[0-9a-f]*:[0-9a-f]*)(:\d+)?\//i, '//[$1]$2/');
1447
+
1448
+ if (target === 'this') {
1449
+ window.location = url;
1450
+ } else {
1451
+ window.open(url, target || '_blank');
1452
+ }
1453
+ }
1454
+
1455
+ static MDgetTitle(text) {
1456
+ const result = Utils.extractHeader(text);
1457
+ const header = result.header;
1458
+ let body = result.body;
1459
+ if (!header.title) {
1460
+ // remove {docsify-bla}
1461
+ body = body.replace(/{[^}]*}/g, '');
1462
+ body = body.trim();
1463
+ const lines = body.replace(/\r/g, '').split('\n');
1464
+ for (let i = 0; i < lines.length; i++) {
1465
+ if (lines[i].startsWith('# ')) {
1466
+ return lines[i].substring(2).trim();
1467
+ }
1468
+ }
1469
+ return '';
1470
+ }
1471
+
1472
+ return header.title;
1473
+ }
1474
+
1475
+ static MDextractHeader(text) {
1476
+ const attrs = {};
1477
+ if (text.substring(0, 3) === '---') {
1478
+ const pos = text.substring(3).indexOf('\n---');
1479
+ if (pos !== -1) {
1480
+ const _header = text.substring(3, pos + 3);
1481
+ const lines = _header.replace(/\r/g, '').split('\n');
1482
+ lines.forEach(line => {
1483
+ if (!line.trim()) {
1484
+ return;
1485
+ }
1486
+ const pos_ = line.indexOf(':');
1487
+ if (pos_ !== -1) {
1488
+ const attr = line.substring(0, pos_).trim();
1489
+ attrs[attr] = line.substring(pos_ + 1).trim();
1490
+ attrs[attr] = attrs[attr].replace(/^['"]|['"]$/g, '');
1491
+ if (attrs[attr] === 'true') {
1492
+ attrs[attr] = true;
1493
+ } else if (attrs[attr] === 'false') {
1494
+ attrs[attr] = false;
1495
+ } else if (parseFloat(attrs[attr]).toString() === attrs[attr]) {
1496
+ attrs[attr] = parseFloat(attrs[attr]);
1497
+ }
1498
+ } else {
1499
+ attrs[line.trim()] = true;
1500
+ }
1501
+ });
1502
+ text = text.substring(pos + 7);
1503
+ }
1504
+ }
1505
+ return { header: attrs, body: text };
1506
+ }
1507
+
1508
+ static MDremoveDocsify(text) {
1509
+ const m = text.match(/{docsify-[^}]*}/g);
1510
+ if (m) {
1511
+ m.forEach(doc => text = text.replace(doc, ''));
1512
+ }
1513
+ return text;
1514
+ }
1515
+
1516
+ /**
1517
+ * Generate the json file on the file for download.
1518
+ * @param {string} filename file name
1519
+ * @param {Record<string, unknown>} json file data
1520
+ * @returns {object} json structure (not stringified)
1521
+ */
1522
+ static generateFile(filename, json) {
1523
+ const el = document.createElement('a');
1524
+ el.setAttribute('href', `data:application/json;charset=utf-8,${encodeURIComponent(JSON.stringify(json, null, 2))}`);
1525
+ el.setAttribute('download', filename);
1526
+
1527
+ el.style.display = 'none';
1528
+ document.body.appendChild(el);
1529
+
1530
+ el.click();
1531
+
1532
+ document.body.removeChild(el);
1533
+ }
1534
+
1535
+ /**
1536
+ * Convert quality code into text
1537
+ * @param {number} quality code
1538
+ * @returns {array<string>} lines that decode quality
1539
+ */
1540
+ static quality2text(quality) {
1541
+ // eslint-disable-next-line no-bitwise
1542
+ const custom = quality & 0xFFFF0000;
1543
+ const text = QUALITY_BITS[quality];
1544
+ let result;
1545
+ if (text) {
1546
+ result = [text];
1547
+ // eslint-disable-next-line no-bitwise
1548
+ } else if (quality & 0x01) {
1549
+ // eslint-disable-next-line no-bitwise
1550
+ result = [QUALITY_BITS[0x01], `0x${(quality & (0xFFFF & ~1)).toString(16)}`];
1551
+ // eslint-disable-next-line no-bitwise
1552
+ } else if (quality & 0x02) {
1553
+ // eslint-disable-next-line no-bitwise
1554
+ result = [QUALITY_BITS[0x02], `0x${(quality & (0xFFFF & ~2)).toString(16)}`];
1555
+ } else {
1556
+ result = [`0x${quality.toString(16)}`];
1557
+ }
1558
+ if (custom) {
1559
+ // eslint-disable-next-line no-bitwise
1560
+ result.push(`0x${(custom >> 16).toString(16).toUpperCase()}`);
1561
+ }
1562
+ return result;
1563
+ }
1564
+
1565
+ /**
1566
+ * Deep copy object
1567
+ * @param {object} object
1568
+ * @returns {object}
1569
+ */
1570
+ static clone(object) {
1571
+ return JSON.parse(JSON.stringify(object));
1572
+ }
1573
+
1574
+ /**
1575
+ * Get states of object
1576
+ * @param {object} obj
1577
+ * @returns {object} states as an object in form {"value1": "label1", "value2": "label2"} or null
1578
+ */
1579
+ static getStates(obj) {
1580
+ let states = obj?.common?.states;
1581
+ if (states) {
1582
+ if (typeof states === 'string' && states[0] === '{') {
1583
+ try {
1584
+ states = JSON.parse(states);
1585
+ } catch (ex) {
1586
+ console.error(`Cannot parse states: ${states}`);
1587
+ states = null;
1588
+ }
1589
+ } else if (typeof states === 'string') {
1590
+ // if old format val1:text1;val2:text2
1591
+ const parts = states.split(';');
1592
+ states = {};
1593
+ for (let p = 0; p < parts.length; p++) {
1594
+ const s = parts[p].split(':');
1595
+ states[s[0]] = s[1];
1596
+ }
1597
+ } else if (Array.isArray(states)) {
1598
+ const result = {};
1599
+ if (obj.common.type === 'number') {
1600
+ states.forEach((value, key) => result[key] = value);
1601
+ } else if (obj.common.type === 'string') {
1602
+ states.forEach(value => result[value] = value);
1603
+ } else if (obj.common.type === 'boolean') {
1604
+ result.false = states[0];
1605
+ result.true = states[1];
1606
+ }
1607
+
1608
+ return result;
1609
+ }
1610
+ }
1611
+
1612
+ return states;
1613
+ }
1614
+
1615
+ /**
1616
+ * Get svg file as text
1617
+ * @param {string} url URL of SVG file
1618
+ * @returns {object} Promise with "data:image..."
1619
+ */
1620
+ static getSvg(url) {
1621
+ return fetch(url)
1622
+ .then(response => response.blob())
1623
+ .then(blob => new Promise(resolve => {
1624
+ const reader = new FileReader();
1625
+ // eslint-disable-next-line func-names
1626
+ reader.onload = function () { // do not optimize this function. "this" is important.
1627
+ resolve(this.result);
1628
+ };
1629
+ reader.readAsDataURL(blob);
1630
+ }));
1631
+ }
1632
+
1633
+ /**
1634
+ * Detect file extension by its content
1635
+ * @param {string} base64 Base64 encoded binary file
1636
+ * @returns {string} Detected extension, like 'jpg'
1637
+ */
1638
+ static detectMimeType(base64) {
1639
+ const signature = Object.keys(SIGNATURES).find(s => base64.startsWith(s));
1640
+ return signature ? SIGNATURES[signature] : null;
1641
+ }
1642
+
1643
+ /**
1644
+ * Check if configured repository is the stable repository
1645
+ *
1646
+ * @param {string | string[]} activeRepo current configured repository or multi repository
1647
+ * @return {boolean}
1648
+ */
1649
+ static isStableRepository(activeRepo) {
1650
+ return !!((
1651
+ typeof activeRepo === 'string' &&
1652
+ activeRepo.toLowerCase().startsWith('stable')
1653
+ )
1654
+ ||
1655
+ (
1656
+ activeRepo &&
1657
+ typeof activeRepo !== 'string' &&
1658
+ activeRepo.find(r => r.toLowerCase().startsWith('stable'))
1659
+ ));
1660
+ }
1661
+
1662
+ /**
1663
+ * Check if given string is an integer
1664
+ *
1665
+ * @param {string} str string to check
1666
+ * @return {boolean}
1667
+ */
1668
+ static isStringInteger(str) {
1669
+ return parseInt(str).toString() === str;
1670
+ }
1671
+
1672
+ /**
1673
+ * Check if the date is valid
1674
+ *
1675
+ * @param {Date} date
1676
+ * @return {boolean}
1677
+ */
1678
+ static isValidDate(date) {
1679
+ return date instanceof Date && !isNaN(date);
1680
+ }
1681
+ }
1682
+
1683
+ export default Utils;