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 BaseControl from '../../controls/baseControl/BaseControl.js'; |
26 | import ApiKeyControlRow from './ApiKeyControlRow.js'; |
27 | import ApiKey from '../../containers/ApiKey.js'; |
28 | import DeleteApiKeyButtonClickEL from './DeleteApiKeyButtonClickEL.js'; |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | class ApiKeysControl extends BaseControl { |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | #apiKeysControlRowsMap; |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | #deleteApiKeyButtonClickEL; |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | constructor ( ) { |
57 | super ( ); |
58 | this.#apiKeysControlRowsMap = new Map ( ); |
59 | this.#deleteApiKeyButtonClickEL = new DeleteApiKeyButtonClickEL ( this ); |
60 | } |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | destructor ( ) { |
67 | this.#apiKeysControlRowsMap.forEach ( |
68 | apiKeysControlRow => apiKeysControlRow.destructor ( ) |
69 | ); |
70 | this.#apiKeysControlRowsMap.clear ( ); |
71 | this.#deleteApiKeyButtonClickEL.destructor ( ); |
72 | this.#deleteApiKeyButtonClickEL = null; |
73 | } |
74 | |
75 | |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | haveEmptyOrDuplicateValues ( ) { |
82 | let haveEmpty = false; |
83 | const providersNames = []; |
84 | this.#apiKeysControlRowsMap.forEach ( |
85 | apiKeyControl => { |
86 | haveEmpty = |
87 | haveEmpty || |
88 | '' === apiKeyControl.providerName |
89 | || |
90 | '' === apiKeyControl.providerKey; |
91 | providersNames.push ( apiKeyControl.providerName ); |
92 | } |
93 | ); |
94 | let haveDuplicate = false; |
95 | providersNames.forEach ( |
96 | providerName => { |
97 | haveDuplicate = |
98 | haveDuplicate || |
99 | providersNames.indexOf ( providerName ) !== providersNames.lastIndexOf ( providerName ); |
100 | } |
101 | ); |
102 | return { haveEmpty : haveEmpty, haveDuplicate : haveDuplicate }; |
103 | } |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | |
110 | addApiKeys ( apiKeys ) { |
111 | this.#apiKeysControlRowsMap.clear ( ); |
112 | apiKeys.forEach ( |
113 | apiKey => { |
114 | const apiKeyControl = new ApiKeyControlRow ( apiKey, this.#deleteApiKeyButtonClickEL ); |
115 | this.#apiKeysControlRowsMap.set ( apiKeyControl.objId, apiKeyControl ); |
116 | } |
117 | ); |
118 | this.refreshApiKeys ( ); |
119 | } |
120 | |
121 | |
122 | |
123 | |
124 | |
125 | newApiKey ( ) { |
126 | const apiKey = new ApiKey ( ); |
127 | const apiKeyControlRow = new ApiKeyControlRow ( apiKey, this.#deleteApiKeyButtonClickEL ); |
128 | this.#apiKeysControlRowsMap.set ( apiKeyControlRow.objId, apiKeyControlRow ); |
129 | this.refreshApiKeys ( ); |
130 | } |
131 | |
132 | |
133 | |
134 | |
135 | |
136 | |
137 | deleteApiKey ( rowObjId ) { |
138 | this.#apiKeysControlRowsMap.get ( rowObjId ).destructor ( ); |
139 | this.#apiKeysControlRowsMap.delete ( rowObjId ); |
140 | this.refreshApiKeys ( ); |
141 | } |
142 | |
143 | |
144 | |
145 | |
146 | |
147 | refreshApiKeys ( ) { |
148 | while ( this.controlHTMLElement.firstChild ) { |
149 | this.controlHTMLElement.removeChild ( this.controlHTMLElement.firstChild ); |
150 | } |
151 | this.#apiKeysControlRowsMap.forEach ( |
152 | apiKeyControlRow => { this.controlHTMLElement.appendChild ( apiKeyControlRow.HTMLElement ); } |
153 | ); |
154 | } |
155 | |
156 | |
157 | |
158 | |
159 | |
160 | |
161 | get apiKeys ( ) { |
162 | const apiKeys = []; |
163 | this.#apiKeysControlRowsMap.forEach ( |
164 | apiKeyControl => apiKeys.push ( apiKeyControl.apiKey ) |
165 | ); |
166 | return apiKeys; |
167 | } |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | |
174 | get apiKeysJSON ( ) { |
175 | const apiKeys = []; |
176 | this.#apiKeysControlRowsMap.forEach ( |
177 | apiKeyControl => apiKeys.push ( apiKeyControl.apiKey.jsonObject ) |
178 | ); |
179 | return JSON.stringify ( apiKeys ); |
180 | |
181 | } |
182 | } |
183 | |
184 | export default ApiKeysControl; |
185 | |
186 | |
187 | |