| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | import theTranslator from '../../core/uiLib/Translator.js'; |
| 26 | import theConfig from '../../data/Config.js'; |
| 27 | import theHTMLElementsFactory from '../../core/uiLib/HTMLElementsFactory.js'; |
| 28 | import RestoreFromUnsecureFileButtonClickEL from './RestoreFromUnsecureFileButtonClickEL.js'; |
| 29 | import ReloadFromServerButtonClickEL from './ReloadFromServerButtonClickEL.js'; |
| 30 | import RestoreFromSecureFileButtonClickEL from './RestoreFromSecureFileButtonClickEL.js'; |
| 31 | import SaveToSecureFileButtonClickEL from './SaveToSecureFileButtonClickEL.js'; |
| 32 | import SaveToUnsecureFileButtonClickEL from './SaveToUnsecureFileButtonClickEL.js'; |
| 33 | import NewApiKeyButtonClickEL from './NewApiKeyButtonClickEL.js'; |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | class ApiKeysDialogToolbar { |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | #apiKeysDialog; |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | #apiKeysControl; |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | #toolbarHTMLElement; |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | #reloadKeysFromServerButton; |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | #saveKeysToSecureFileButton; |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | #restoreKeysFromSecureFileButton; |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | #newApiKeyButton; |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | #saveKeysToUnsecureFileButton; |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | #restoreKeysFromUnsecureFileButton; |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | #haveApiKeysFile; |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | #reloadFromServerButtonClickEL; |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | #saveToSecureFileButtonClickEL; |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | #restoreFromSecureFileButtonClickEL; |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | #newApiKeyButtonClickEL; |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | #saveToUnsecureFileButtonClickEL; |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | #restoreFromUnsecureFileButtonClickEL; |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | #createReloadKeysFromServerButton ( ) { |
| 160 | this.#reloadKeysFromServerButton = theHTMLElementsFactory.create ( |
| 161 | 'div', |
| 162 | { |
| 163 | className : 'TravelNotes-BaseDialog-Button', |
| 164 | title : theTranslator.getText ( 'ApiKeysDialogToolbar - Reload from server' ), |
| 165 | textContent : '🔄' |
| 166 | }, |
| 167 | this.#toolbarHTMLElement |
| 168 | ); |
| 169 | this.#reloadFromServerButtonClickEL = |
| 170 | new ReloadFromServerButtonClickEL ( this.#apiKeysDialog ); |
| 171 | this.#reloadKeysFromServerButton.addEventListener ( |
| 172 | 'click', |
| 173 | this.#reloadFromServerButtonClickEL, |
| 174 | false |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | #createSaveKeysToSecureFileButton ( ) { |
| 183 | this.#saveKeysToSecureFileButton = theHTMLElementsFactory.create ( |
| 184 | 'div', |
| 185 | { |
| 186 | className : 'TravelNotes-BaseDialog-Button', |
| 187 | title : theTranslator.getText ( 'ApiKeysDialogToolbar - Save to file' ), |
| 188 | textContent : '💾' |
| 189 | }, |
| 190 | this.#toolbarHTMLElement |
| 191 | ); |
| 192 | this.#saveToSecureFileButtonClickEL = |
| 193 | new SaveToSecureFileButtonClickEL ( this.#apiKeysDialog ); |
| 194 | this.#saveKeysToSecureFileButton.addEventListener ( |
| 195 | 'click', |
| 196 | this.#saveToSecureFileButtonClickEL, |
| 197 | false |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | #createRestoreKeysFromSecureFileButton ( ) { |
| 206 | this.#restoreKeysFromSecureFileButton = theHTMLElementsFactory.create ( |
| 207 | 'div', |
| 208 | { |
| 209 | className : 'TravelNotes-BaseDialog-Button', |
| 210 | title : theTranslator.getText ( 'ApiKeysDialogToolbar - Open file' ), |
| 211 | textContent : '📂' |
| 212 | }, |
| 213 | this.#toolbarHTMLElement |
| 214 | ); |
| 215 | this.#restoreFromSecureFileButtonClickEL = |
| 216 | new RestoreFromSecureFileButtonClickEL ( this.#apiKeysDialog ); |
| 217 | this.#restoreKeysFromSecureFileButton.addEventListener ( |
| 218 | 'click', |
| 219 | this.#restoreFromSecureFileButtonClickEL, |
| 220 | false |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | #createNewApiKeyButton ( ) { |
| 229 | this.#newApiKeyButton = theHTMLElementsFactory.create ( |
| 230 | 'div', |
| 231 | { |
| 232 | className : 'TravelNotes-BaseDialog-Button', |
| 233 | title : theTranslator.getText ( 'ApiKeysDialogToolbar - new api key' ), |
| 234 | textContent : '+' |
| 235 | }, |
| 236 | this.#toolbarHTMLElement |
| 237 | ); |
| 238 | this.#newApiKeyButtonClickEL = |
| 239 | new NewApiKeyButtonClickEL ( this.#apiKeysControl ); |
| 240 | this.#newApiKeyButton.addEventListener ( |
| 241 | 'click', |
| 242 | this.#newApiKeyButtonClickEL, |
| 243 | false |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | #createSaveKeysToUnsecureFileButton ( ) { |
| 252 | this.#saveKeysToUnsecureFileButton = theHTMLElementsFactory.create ( |
| 253 | 'div', |
| 254 | { |
| 255 | className : 'TravelNotes-BaseDialog-Button TravelNotes-ApiKeysDialog-AtRightButton', |
| 256 | title : theTranslator.getText ( 'ApiKeysDialogToolbar - Save to json file' ), |
| 257 | textContent : '💾' |
| 258 | }, |
| 259 | this.#toolbarHTMLElement |
| 260 | ); |
| 261 | this.#saveToUnsecureFileButtonClickEL = |
| 262 | new SaveToUnsecureFileButtonClickEL ( this.#apiKeysDialog ); |
| 263 | this.#saveKeysToUnsecureFileButton.addEventListener ( |
| 264 | 'click', |
| 265 | this.#saveToUnsecureFileButtonClickEL, |
| 266 | false |
| 267 | ); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | #createRestoreKeysFromUnsecureFileButton ( ) { |
| 275 | this.#restoreKeysFromUnsecureFileButton = theHTMLElementsFactory.create ( |
| 276 | 'div', |
| 277 | { |
| 278 | className : 'TravelNotes-BaseDialog-Button', |
| 279 | title : theTranslator.getText ( 'ApiKeysDialogToolbar - Open json file' ), |
| 280 | textContent : '📂' |
| 281 | }, |
| 282 | this.#toolbarHTMLElement |
| 283 | ); |
| 284 | this.#restoreFromUnsecureFileButtonClickEL = |
| 285 | new RestoreFromUnsecureFileButtonClickEL ( this.#apiKeysDialog ); |
| 286 | this.#restoreKeysFromUnsecureFileButton.addEventListener ( |
| 287 | 'click', |
| 288 | this.#restoreFromUnsecureFileButtonClickEL, |
| 289 | false |
| 290 | ); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | #addToolbarButtons ( ) { |
| 298 | if ( window?.crypto?.subtle?.importKey && window.isSecureContext ) { |
| 299 | if ( this.#haveApiKeysFile ) { |
| 300 | this.#createReloadKeysFromServerButton ( ); |
| 301 | } |
| 302 | this.#createSaveKeysToSecureFileButton ( ); |
| 303 | this.#createRestoreKeysFromSecureFileButton ( ); |
| 304 | } |
| 305 | |
| 306 | this.#createNewApiKeyButton ( ); |
| 307 | |
| 308 | if ( theConfig.ApiKeysDialog.haveUnsecureButtons ) { |
| 309 | this.#createSaveKeysToUnsecureFileButton ( ); |
| 310 | this.#createRestoreKeysFromUnsecureFileButton ( ); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | constructor ( apiKeysDialog, apiKeysControl, haveApiKeysFile ) { |
| 322 | this.#apiKeysDialog = apiKeysDialog; |
| 323 | this.#apiKeysControl = apiKeysControl; |
| 324 | this.#haveApiKeysFile = haveApiKeysFile; |
| 325 | this.#toolbarHTMLElement = theHTMLElementsFactory.create ( |
| 326 | 'div', |
| 327 | { |
| 328 | id : 'TravelNotes-ApiKeysDialog-ToolbarDiv' |
| 329 | } |
| 330 | ); |
| 331 | |
| 332 | this.#addToolbarButtons ( ); |
| 333 | Object.freeze ( this ); |
| 334 | } |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | destructor ( ) { |
| 341 | if ( this.#reloadKeysFromServerButton ) { |
| 342 | this.#reloadKeysFromServerButton.removeEventListener ( |
| 343 | 'click', |
| 344 | this.#reloadFromServerButtonClickEL, |
| 345 | false |
| 346 | ); |
| 347 | this.#reloadFromServerButtonClickEL.destructor ( ); |
| 348 | this.#reloadFromServerButtonClickEL = null; |
| 349 | } |
| 350 | |
| 351 | if ( this.#saveKeysToSecureFileButton ) { |
| 352 | this.#saveKeysToSecureFileButton.removeEventListener ( |
| 353 | 'click', |
| 354 | this.#saveToSecureFileButtonClickEL, |
| 355 | false |
| 356 | ); |
| 357 | this.#saveToSecureFileButtonClickEL.destructor ( ); |
| 358 | this.#saveToSecureFileButtonClickEL = null; |
| 359 | } |
| 360 | |
| 361 | if ( this.#restoreKeysFromSecureFileButton ) { |
| 362 | this.#restoreKeysFromSecureFileButton.removeEventListener ( |
| 363 | 'click', |
| 364 | this.#restoreFromSecureFileButtonClickEL, |
| 365 | false |
| 366 | ); |
| 367 | this.#restoreFromSecureFileButtonClickEL.destructor ( ); |
| 368 | this.#restoreFromSecureFileButtonClickEL = null; |
| 369 | } |
| 370 | |
| 371 | if ( this.#newApiKeyButton ) { |
| 372 | this.#newApiKeyButton.removeEventListener ( |
| 373 | 'click', |
| 374 | this.#newApiKeyButtonClickEL, |
| 375 | false |
| 376 | ); |
| 377 | this.#newApiKeyButtonClickEL.destructor ( ); |
| 378 | this.#newApiKeyButtonClickEL = null; |
| 379 | } |
| 380 | |
| 381 | if ( this.#saveKeysToUnsecureFileButton ) { |
| 382 | this.#saveKeysToUnsecureFileButton.removeEventListener ( |
| 383 | 'click', |
| 384 | this.#saveToUnsecureFileButtonClickEL, |
| 385 | false |
| 386 | ); |
| 387 | this.#saveToUnsecureFileButtonClickEL.destructor ( ); |
| 388 | this.#saveToUnsecureFileButtonClickEL = null; |
| 389 | } |
| 390 | |
| 391 | if ( this.#restoreKeysFromUnsecureFileButton ) { |
| 392 | this.#restoreKeysFromUnsecureFileButton.removeEventListener ( |
| 393 | 'click', |
| 394 | this.#restoreFromUnsecureFileButtonClickEL, |
| 395 | false |
| 396 | ); |
| 397 | this.#restoreFromUnsecureFileButtonClickEL.destructor ( ); |
| 398 | this.#restoreFromUnsecureFileButtonClickEL = null; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | get toolbarHTMLElement ( ) { |
| 408 | return this.#toolbarHTMLElement; |
| 409 | } |
| 410 | |
| 411 | } |
| 412 | |
| 413 | export default ApiKeysDialogToolbar; |
| 414 | |
| 415 | |
| 416 | |