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 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
26 | /** |
27 | HTMLElements factory |
28 | See theHTMLElementsFactory for the one and only one instance of this class |
29 | @hideconstructor |
30 | */ |
31 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
32 | |
33 | class HTMLElementsFactory { |
34 | |
35 | /** |
36 | #addProperties |
37 | This method add the properties to the created element |
38 | @param {HTMLElement} element The HTMLElement for witch the properties will be added |
39 | @param {Object} properties An object with properties to add to the HTMLElement |
40 | */ |
41 | |
42 | #addProperties ( element, properties ) { |
43 | for ( const property in properties ) { |
44 | try { |
45 | if ( 'dataset' === property ) { |
46 | const datasetObject = properties.dataset; |
47 | for ( const valueName in datasetObject ) { |
48 | element.dataset [ 'tan' + valueName ] = datasetObject [ valueName ]; |
49 | } |
50 | } |
51 | else { |
52 | element [ property ] = properties [ property ]; |
53 | } |
54 | } |
55 | catch ( err ) { |
56 | if ( err instanceof Error ) { |
57 | console.error ( err ); |
58 | } |
59 | } |
60 | } |
61 | if ( element.target ) { |
62 | element.rel = 'noopener noreferrer'; |
63 | } |
64 | } |
65 | |
66 | /** |
67 | constructor |
68 | */ |
69 | |
70 | constructor ( ) { |
71 | Object.freeze ( this ); |
72 | } |
73 | |
74 | /** |
75 | Create an HTMLElement Object |
76 | @param {String} tagName the tag of the HTMLElement to create |
77 | @param {?object} properties An object with properties to add to the HTMLElement |
78 | @param {?HTMLElement} parentNode The parent node to witch the HTMLElement will be attached |
79 | @return {HTMLElement} the created HTMLElement |
80 | */ |
81 | |
82 | create ( tagName, properties, parentNode ) { |
83 | let element = null; |
84 | if ( 'text' === tagName.toLowerCase ( ) ) { |
85 | element = document.createTextNode ( properties.value || '' ); |
86 | } |
87 | else { |
88 | if ( 'select' === tagName.toLowerCase ( ) ) { |
89 | element = document.createElement ( tagName ); |
90 | } |
91 | else { |
92 | element = Object.seal ( document.createElement ( tagName ) ); |
93 | } |
94 | if ( properties ) { |
95 | this.#addProperties ( element, properties ); |
96 | } |
97 | } |
98 | if ( parentNode ) { |
99 | parentNode.appendChild ( element ); |
100 | } |
101 | return element; |
102 | } |
103 | } |
104 | |
105 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
106 | /** |
107 | The one and only one instance of HTMLElementsFactory class |
108 | @type {HTMLElementsFactory} |
109 | */ |
110 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
111 | |
112 | const theHTMLElementsFactory = new HTMLElementsFactory ( ); |
113 | |
114 | export default theHTMLElementsFactory; |
115 | |
116 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
117 |