File : dialogs/apiKeysDialog/ApiKeysControl.js

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 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
This class is the ApiKeys control of the ApiKeysDialog
33
*/
34
/* ------------------------------------------------------------------------------------------------------------------------- */
35
36
class ApiKeysControl extends BaseControl {
37
38
    /**
39
    A map to store the rows of the ApiKeyControl object
40
    @type {Map}
41
    */
42
43
    #apiKeysControlRowsMap;
44
45
    /**
46
    The DeleteApiKeyButtonClickEL for the delete buttons
47
    @type {deleteApiKeyButtonClickEL}
48
    */
49
50
    #deleteApiKeyButtonClickEL;
51
52
    /**
53
    The constructor
54
    */
55
56
    constructor ( ) {
57
        super ( );
58
        this.#apiKeysControlRowsMap = new Map ( );
59
        this.#deleteApiKeyButtonClickEL = new DeleteApiKeyButtonClickEL ( this );
60
    }
61
62
    /**
63
    The destructor. Remove event listener and clean the map
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
    Verify that the control don't have empty provider name or provider key
77
    Verify that the control don't have duplicate provider name
78
    @return {Object} an object with rhe validation results
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
    Add an array of ApiKeys to the control.
107
    @param {Array.<ApiKey>} apiKeys An array with the ApiKeys to add
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
    Add a new ApiKey to the control.
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
    Delete a row with an ApiKey in the control
134
    @param {Number} rowObjId The objId of the row to delete
135
    */
136
137
    deleteApiKey ( rowObjId ) {
138
        this.#apiKeysControlRowsMap.get ( rowObjId ).destructor ( );
139
        this.#apiKeysControlRowsMap.delete ( rowObjId );
140
        this.refreshApiKeys ( );
141
    }
142
143
    /**
144
    Remove all elements from the #apiKeysControl and add the existing ApiKeys
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
    Get an array with the ApiKeys in the control
158
    @type {Array.<ApiKey>}
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
    Get a JSON string with the ApiKeys in the control
171
    @type {String}
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
/* --- End of file --------------------------------------------------------------------------------------------------------- */
187