@iobroker/json-config 1.0.0

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