File : dialogs/passwordDialog/PasswordDialog.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 theTranslator from '../../core/uiLib/Translator.js';
26
import ModalBaseDialog from '../baseDialog/ModalBaseDialog.js';
27
import PasswordControl from '../../controls/passwordControl/PasswordControl.js';
28
29
/* ------------------------------------------------------------------------------------------------------------------------- */
30
/**
31
This class is the password dialog
32
*/
33
/* ------------------------------------------------------------------------------------------------------------------------- */
34
35
class PasswordDialog extends ModalBaseDialog {
36
37
    /**
38
    the password control
39
    @type {PasswordControl}
40
    */
41
42
    #passwordControl;
43
44
    /**
45
    the verifyPassword constructor parameter
46
    @type {Boolean}
47
    */
48
49
    #verifyPassword;
50
51
    /**
52
    The minimal length for the password
53
    @type {String}
54
    */
55
56
    // eslint-disable-next-line no-magic-numbers
57
    static get #PSWD_MIN_LENGTH ( ) { return 12; }
58
59
    /**
60
    The constructor
61
    @param {Boolean} verifyPassword When true the password must be conform to the password rules
62
    */
63
64
    constructor ( verifyPassword ) {
65
        super ( );
66
        this.#verifyPassword = verifyPassword;
67
    }
68
69
    /**
70
    Remove event listeners
71
    */
72
73
    #destructor ( ) {
74
        this.#passwordControl.destructor ( );
75
    }
76
77
    /**
78
    Create all the controls needed for the dialog.
79
    Overload of the base class createContentHTML
80
    */
81
82
    createContentHTML ( ) {
83
        this.#passwordControl = new PasswordControl ( );
84
    }
85
86
    /**
87
    Overload of the ModalBaseDialog.show ( ) method.
88
    */
89
90
    show ( ) {
91
        const showPromise = super.show ( );
92
        this.#passwordControl.focus ( );
93
        return showPromise;
94
    }
95
96
    /**
97
    Overload of the BaseDialog.canClose ( ) method.
98
    @return {Boolean} true when the password is correct, false otherwise
99
    */
100
101
    canClose ( ) {
102
        this.hideError ( );
103
        if ( this.#verifyPassword ) {
104
            if (
105
                ( this.#passwordControl.value.length < PasswordDialog.#PSWD_MIN_LENGTH )
106
                ||
107
                ! this.#passwordControl.value.match ( /[0-9]+/ )
108
                ||
109
                ! this.#passwordControl.value.match ( /[a-z]+/ )
110
                ||
111
                ! this.#passwordControl.value.match ( /[A-Z]+/ )
112
                ||
113
                ! this.#passwordControl.value.match ( /[^0-9a-zA-Z]/ )
114
            ) {
115
                this.showError (
116
                    '<p>' + theTranslator.getText ( 'PasswordDialog - Password rules1' ) + '</p><ul>' +
117
                    '<li>' + theTranslator.getText ( 'PasswordDialog - Password rules2' ) + '</li>' +
118
                    '<li>' + theTranslator.getText ( 'PasswordDialog - Password rules3' ) + '</li>' +
119
                    '<li>' + theTranslator.getText ( 'PasswordDialog - Password rules4' ) + '</li>' +
120
                    '<li>' + theTranslator.getText ( 'PasswordDialog - Password rules5' ) + '</li>' +
121
                    '<li>' + theTranslator.getText ( 'PasswordDialog - Password rules6' ) + '</li></ul>'
122
                );
123
                this.#passwordControl.focus ( );
124
                return false;
125
            }
126
        }
127
        return true;
128
    }
129
130
    /**
131
    Overload of the BaseDialog.onCancel ( ) method.
132
    */
133
134
    onCancel ( ) {
135
        this.#destructor ( );
136
        super.onCancel ( );
137
    }
138
139
    /**
140
    Overload of the BaseDialog.onOk ( ) method.
141
    */
142
143
    onOk ( ) {
144
        if ( super.onOk ( new window.TextEncoder ( ).encode ( this.#passwordControl.value ) ) ) {
145
            this.#destructor ( );
146
        }
147
    }
148
149
    /**
150
    An array with the HTMLElements that have to be added in the content of the dialog.
151
    @type {Array.<HTMLElement>}
152
    */
153
154
    get contentHTMLElements ( ) { return [ this.#passwordControl.controlHTMLElement ]; }
155
156
    /**
157
    The dialog title. Overload of the BaseDialog.title property
158
    @type {String}
159
    */
160
161
    get title ( ) { return theTranslator.getText ( 'PasswordDialog - password' ); }
162
163
}
164
165
export default PasswordDialog;
166
167
/* --- End of file --------------------------------------------------------------------------------------------------------- */
168