1 | /* |
2 | Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/ |
3 | |
4 | This program is free software; |
5 | you can redistribute it and/or modify it under the terms of the |
6 | GNU General Public License as published by the Free Software Foundation; |
7 | either version 3 of the License, or any later version. |
8 | |
9 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software |
16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
17 | */ |
18 | /* |
19 | Changes: |
20 | - v4.0.0: |
21 | - created from v3.6.0 |
22 | Doc reviewed 202208 |
23 | */ |
24 | |
25 | import ModalBaseDialog from '../baseDialog/ModalBaseDialog.js'; |
26 | import ApiKeysDialogToolbar from './ApiKeysDialogToolbar.js'; |
27 | import theTranslator from '../../core/uiLib/Translator.js'; |
28 | import ApiKeysControl from './ApiKeysControl.js'; |
29 | import theErrorsUI from '../../uis/errorsUI/ErrorsUI.js'; |
30 | |
31 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
32 | /** |
33 | This class is the ApiKeys dialog |
34 | */ |
35 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
36 | |
37 | class ApiKeysDialog extends ModalBaseDialog { |
38 | |
39 | /** |
40 | The dialog toolbar |
41 | @type {ApiKeysDialogToolbar} |
42 | */ |
43 | |
44 | #toolbar; |
45 | |
46 | /** |
47 | A div that contains the ApiKeysControl |
48 | @type {HTMLElement} |
49 | */ |
50 | |
51 | #apiKeysControl; |
52 | |
53 | /** |
54 | An array with the existing ApiKeys |
55 | @type {Array.<ApiKey>} |
56 | */ |
57 | |
58 | #apiKeys; |
59 | |
60 | /** |
61 | A boolean indicating when a ApiKey file was found on the server |
62 | @type {Boolean} |
63 | */ |
64 | |
65 | #haveApiKeysFile; |
66 | |
67 | /** |
68 | The constructor |
69 | @param {Array.<ApiKey>} apiKeys An array with the existing ApiKeys |
70 | @param {Boolean} haveApiKeysFile A boolean indicating when a ApiKey file was found on the server |
71 | */ |
72 | |
73 | constructor ( apiKeys, haveApiKeysFile ) { |
74 | super ( ); |
75 | this.#apiKeys = apiKeys; |
76 | this.#haveApiKeysFile = haveApiKeysFile; |
77 | } |
78 | |
79 | /** |
80 | Create all the controls needed for the dialog. |
81 | Overload of the base class createContentHTML |
82 | */ |
83 | |
84 | createContentHTML ( ) { |
85 | this.#apiKeysControl = new ApiKeysControl ( ); |
86 | this.#toolbar = new ApiKeysDialogToolbar ( this, this.#apiKeysControl, this.#haveApiKeysFile ); |
87 | this.#apiKeysControl.addApiKeys ( this.#apiKeys ); |
88 | |
89 | } |
90 | |
91 | /** |
92 | Remove all events listeners on the toolbar and controls, so all references to the dialog are released. |
93 | */ |
94 | |
95 | #destructor ( ) { |
96 | this.#apiKeysControl.destructor ( ); |
97 | this.#toolbar.destructor ( ); |
98 | this.#toolbar = null; |
99 | } |
100 | |
101 | /** |
102 | Validate the ApiKeys. Each ApiKey must have a not empty name and a not empty key. |
103 | Duplicate ApiKey names are not allowed |
104 | @return {Boolean} true when all the keys are valid and not duplicated |
105 | */ |
106 | |
107 | validateApiKeys ( ) { |
108 | this.hideError ( ); |
109 | const HaveEmptyOrDuplicate = this.#apiKeysControl.haveEmptyOrDuplicateValues ( ); |
110 | if ( HaveEmptyOrDuplicate.haveEmpty ) { |
111 | this.showError ( |
112 | theTranslator.getText ( 'ApiKeysDialog - empty api key name or value' ) |
113 | ); |
114 | return false; |
115 | } |
116 | else if ( HaveEmptyOrDuplicate.haveDuplicate ) { |
117 | this.showError ( |
118 | theTranslator.getText ( 'ApiKeysDialog - duplicate api key name found' ) |
119 | ); |
120 | return false; |
121 | } |
122 | return true; |
123 | } |
124 | |
125 | /** |
126 | Overload of the BaseDialog.show ( ) method. |
127 | */ |
128 | |
129 | show ( ) { |
130 | theErrorsUI.showHelp ( |
131 | '<p>' + theTranslator.getText ( 'Help - Complete the ApiKeys1' ) + '</p>' + |
132 | '<p>' + theTranslator.getText ( 'Help - Complete the ApiKeys2' ) + '</p>' + |
133 | '<p>' + theTranslator.getText ( 'Help - Complete the ApiKeys3' ) + '</p>' |
134 | ); |
135 | return super.show ( ); |
136 | } |
137 | |
138 | /** |
139 | Overload of the BaseDialog.canClose ( ) method. |
140 | @return {Boolean} true when all the ApiKeys have a name and a value and there are no duplicate keys name |
141 | */ |
142 | |
143 | canClose ( ) { |
144 | return this.validateApiKeys ( ); |
145 | } |
146 | |
147 | /** |
148 | Overload of the BaseDialog.onCancel ( ) method. Called when the cancel button is clicked |
149 | */ |
150 | |
151 | onCancel ( ) { |
152 | this.#destructor ( ); |
153 | super.onCancel ( ); |
154 | } |
155 | |
156 | /** |
157 | Overload of the BaseDialog.onOk ( ) method. Called when the Ok button is clicked |
158 | */ |
159 | |
160 | onOk ( ) { |
161 | if ( super.onOk ( this.#apiKeysControl.apiKeys ) ) { |
162 | this.#destructor ( ); |
163 | } |
164 | } |
165 | |
166 | /** |
167 | The dialog title. Overload of the BaseDialog.title property |
168 | @type {String} |
169 | */ |
170 | |
171 | get title ( ) { return theTranslator.getText ( 'ApiKeysDialog - api keys' ); } |
172 | |
173 | /** |
174 | An HTMLElement that have to be added as toolbar for the dialog. |
175 | Overload of the BaseDialog.toolbarHTMLElement property |
176 | @type {HTMLElement} |
177 | */ |
178 | |
179 | get toolbarHTMLElement ( ) { |
180 | return this.#toolbar.toolbarHTMLElement; |
181 | } |
182 | |
183 | /** |
184 | An array with the HTMLElements that have to be added in the content of the dialog. |
185 | @type {Array.<HTMLElement>} |
186 | */ |
187 | |
188 | get contentHTMLElements ( ) { |
189 | return [ this.#apiKeysControl.controlHTMLElement ]; |
190 | } |
191 | |
192 | /** |
193 | Get a JSON string with the ApiKeys in the control |
194 | @type {String} |
195 | */ |
196 | |
197 | get apiKeysJSON ( ) { return this.#apiKeysControl.apiKeysJSON; } |
198 | |
199 | /** |
200 | Add an array of ApiKeys to the dialog. |
201 | @param {Array.<ApiKey>} apiKeys An array with the ApiKeys to add |
202 | */ |
203 | |
204 | addApiKeys ( apiKeys ) { |
205 | this.#apiKeysControl.addApiKeys ( apiKeys ); |
206 | } |
207 | } |
208 | |
209 | export default ApiKeysDialog; |
210 | |
211 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
212 |