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 theConfig from '../../data/Config.js'; |
26 | import theHTMLElementsFactory from '../../core/uiLib/HTMLElementsFactory.js'; |
27 | import theHTMLSanitizer from '../../core/htmlSanitizer/HTMLSanitizer.js'; |
28 | |
29 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
30 | /** |
31 | This class show a message on the screen |
32 | |
33 | See theErrorsUI for the one and only one instance of this class |
34 | */ |
35 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
36 | |
37 | class ErrorsUI { |
38 | |
39 | /** |
40 | The container |
41 | @type {HTMLElement} |
42 | */ |
43 | |
44 | #mainHTMLElement; |
45 | |
46 | /** |
47 | The message area |
48 | @type {HTMLElement} |
49 | */ |
50 | |
51 | #messageHTMLElement; |
52 | |
53 | /** |
54 | A timerId for the close UI timer |
55 | @type {Number} |
56 | */ |
57 | |
58 | #timerId; |
59 | |
60 | /** |
61 | The hide help input |
62 | @type {HTMLElement} |
63 | */ |
64 | |
65 | #hideHelpInput; |
66 | |
67 | /** |
68 | The hide help container |
69 | @type {HTMLElement} |
70 | */ |
71 | |
72 | #hideHelpHTMLElement; |
73 | |
74 | /** |
75 | The error level. Must be 'Info', 'Help', 'Warning' or 'Error' |
76 | @type {String} |
77 | */ |
78 | |
79 | #currentErrorLevel; |
80 | |
81 | /** |
82 | Hide the help window |
83 | */ |
84 | |
85 | #hide ( ) { |
86 | if ( this.#timerId ) { |
87 | clearTimeout ( this.#timerId ); |
88 | this.#timerId = null; |
89 | } |
90 | this.#mainHTMLElement.classList.remove ( 'TravelNotes-ErrorsUI-' + this.#currentErrorLevel ); |
91 | this.#mainHTMLElement.classList.add ( 'TravelNotes-Hidden' ); |
92 | this.#hideHelpHTMLElement.classList.add ( 'TravelNotes-Hidden' ); |
93 | this.#messageHTMLElement.textContent = ''; |
94 | } |
95 | |
96 | /** |
97 | This method show the windows |
98 | @param {String} message The message to be displayed |
99 | @param {String} errorLevel The tpe of window to display |
100 | */ |
101 | |
102 | #show ( message, errorLevel ) { |
103 | this.#currentErrorLevel = errorLevel; |
104 | if ( |
105 | ( ! theConfig.errorsUI [ 'show' + this.#currentErrorLevel ] ) |
106 | || |
107 | ( 'Help' === errorLevel && this.#hideHelpInput.checked ) |
108 | ) { |
109 | return; |
110 | } |
111 | if ( this.#timerId ) { |
112 | clearTimeout ( this.#timerId ); |
113 | this.#timerId = null; |
114 | } |
115 | this.#messageHTMLElement.textContent = ''; |
116 | theHTMLSanitizer.sanitizeToHtmlElement ( message, this.#messageHTMLElement ); |
117 | this.#mainHTMLElement.classList.add ( 'TravelNotes-ErrorsUI-' + this.#currentErrorLevel ); |
118 | this.#mainHTMLElement.classList.remove ( 'TravelNotes-Hidden' ); |
119 | |
120 | let timeOutDuration = theConfig.errorsUI.timeOut; |
121 | if ( 'Help' === this.#currentErrorLevel ) { |
122 | this.#hideHelpHTMLElement.classList.remove ( 'TravelNotes-Hidden' ); |
123 | timeOutDuration = theConfig.errorsUI.helpTimeOut; |
124 | } |
125 | this.#timerId = setTimeout ( ( ) => this.#hide ( ), timeOutDuration ); |
126 | } |
127 | |
128 | /** |
129 | The constructor |
130 | */ |
131 | |
132 | constructor ( ) { |
133 | Object.freeze ( this ); |
134 | } |
135 | |
136 | /** |
137 | creates the user interface |
138 | */ |
139 | |
140 | createUI ( ) { |
141 | if ( this.#mainHTMLElement ) { |
142 | return; |
143 | } |
144 | this.#mainHTMLElement = theHTMLElementsFactory.create ( |
145 | 'div', |
146 | { |
147 | id : 'TravelNotes-ErrorsUI', |
148 | className : 'TravelNotes-Hidden' |
149 | }, |
150 | document.body |
151 | ); |
152 | const headerDiv = theHTMLElementsFactory.create ( 'div', null, this.#mainHTMLElement ); |
153 | theHTMLElementsFactory.create ( |
154 | 'span', |
155 | { |
156 | id : 'TravelNotes-ErrorsUI-CancelButton', |
157 | textContent : '❌' |
158 | }, |
159 | headerDiv |
160 | ) |
161 | .addEventListener ( 'click', ( ) => this.#hide ( ), false ); |
162 | this.#messageHTMLElement = theHTMLElementsFactory.create ( |
163 | 'div', |
164 | { |
165 | id : 'TravelNotes-ErrorsUI-Message' |
166 | }, |
167 | this.#mainHTMLElement |
168 | ); |
169 | this.#hideHelpHTMLElement = theHTMLElementsFactory.create ( |
170 | 'div', |
171 | { |
172 | id : 'TravelNotes-ErrorsUI-HelpInputDiv', |
173 | className : 'TravelNotes-Hidden' |
174 | }, |
175 | this.#mainHTMLElement |
176 | ); |
177 | this.#hideHelpInput = theHTMLElementsFactory.create ( |
178 | 'input', |
179 | { |
180 | id : 'TravelNotes-ErrorsUI-HelpInput', |
181 | type : 'checkbox', |
182 | checked : ! theConfig.errorsUI.showHelp |
183 | }, |
184 | this.#hideHelpHTMLElement |
185 | ); |
186 | theHTMLElementsFactory.create ( |
187 | 'label', |
188 | { |
189 | id : 'TravelNotes-ErrorsUI-HelpInputLabel', |
190 | htmlFor : 'TravelNotes-ErrorsUI-HelpInput', |
191 | textContent : 'Don\'t show help again' |
192 | |
193 | // not possible to translate. We need the ErrorsUI for loading translations! |
194 | // textContent : theTranslator.getText ( 'ErrorUI - Dont show again' ) |
195 | }, |
196 | this.#hideHelpHTMLElement |
197 | ); |
198 | } |
199 | |
200 | /** |
201 | Show an error message ( a white text on a red background ) |
202 | |
203 | See theConfig.errorsUI.showError to disable or enable the error messages |
204 | @param {String} error The error message to display |
205 | */ |
206 | |
207 | showError ( error ) { this.#show ( error, 'Error' ); } |
208 | |
209 | /** |
210 | Show an warning message ( a black text on an orange background ) |
211 | |
212 | See theConfig.errorsUI.showWarning to disable or enable the warning messages |
213 | @param {String} warning The warning message to display |
214 | */ |
215 | |
216 | showWarning ( warning ) { this.#show ( warning, 'Warning' ); } |
217 | |
218 | /** |
219 | Show an info message ( a black text on a white background ) |
220 | @see theConfig.errorsUI.showInfo to disable or enable the info messages |
221 | @param {String} info The info message to display |
222 | */ |
223 | |
224 | showInfo ( info ) { this.#show ( info, 'Info' ); } |
225 | |
226 | /** |
227 | Show a help message ( a black text on a white background ) |
228 | |
229 | See theConfig.errorsUI.showHelp to disable or enable the help messages and the |
230 | checkbox in the UI to disable the help |
231 | @param {String} help The help message to display |
232 | */ |
233 | |
234 | showHelp ( help ) { this.#show ( help, 'Help' ); } |
235 | |
236 | } |
237 | |
238 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
239 | /** |
240 | The one and only one instance of ErrorsUI class |
241 | @type {ErrorsUI} |
242 | */ |
243 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
244 | |
245 | const theErrorsUI = new ErrorsUI ( ); |
246 | |
247 | export default theErrorsUI; |
248 | |
249 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
250 |