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 theHTMLSanitizer from '../core/htmlSanitizer/HTMLSanitizer.js'; |
27 | |
28 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
29 | /** |
30 | Class used to overload theConfig with the contains of theTravelNotesConfig.json file and |
31 | finally freeze the config. |
32 | |
33 | See theConfig for the one and only one instance of this class |
34 | */ |
35 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
36 | |
37 | class ConfigOverloader { |
38 | |
39 | /** |
40 | copy the properties between two objects |
41 | |
42 | This method: |
43 | - search recursively all target properties |
44 | - foreach found property, search the same property in source |
45 | - copy the property value from source to target if found |
46 | - search recursively all sources properties |
47 | - foreach found property search the same property in target |
48 | - copy the property value from source to target |
49 | So: |
50 | - if a property is missing in the user config, the property is selected from the default config |
51 | - if a property is in the user config but missing in the default config, the property is also added (and reminder |
52 | that the user can have more dashChoices than the default config ) |
53 | - if a property is changed in the user config, the property is adapted |
54 | @param {Object} source The source object |
55 | @param {Object} target The target object |
56 | */ |
57 | |
58 | #copyObjectTo ( source, target ) { |
59 | if ( ( 'object' !== typeof source ) || ( 'object' !== typeof target ) ) { |
60 | return; |
61 | } |
62 | |
63 | // iteration on target. |
64 | for ( const property in target ) { |
65 | if ( 'object' === typeof target [ property ] ) { |
66 | this.#copyObjectTo ( source [ property ], target [ property ] ); |
67 | } |
68 | else if ( typeof ( source [ property ] ) === typeof ( target [ property ] ) ) { |
69 | if ( 'string' === typeof ( target [ property ] ) ) { |
70 | if ( 'color' === property ) { |
71 | target [ property ] = theHTMLSanitizer.sanitizeToColor ( source [ property ] ) |
72 | || |
73 | target [ property ]; |
74 | } |
75 | else if ( 'url' === property ) { |
76 | target [ property ] = theHTMLSanitizer.sanitizeToUrl ( source [ property ] ).url; |
77 | } |
78 | else { |
79 | target [ property ] = |
80 | theHTMLSanitizer.sanitizeToJsString ( source [ property ] ); |
81 | } |
82 | } |
83 | else { |
84 | target [ property ] = source [ property ] || target [ property ]; |
85 | } |
86 | } |
87 | } |
88 | |
89 | // iteration on source |
90 | for ( const property in source ) { |
91 | if ( 'object' === typeof source [ property ] ) { |
92 | if ( '[object Array]' === Object.prototype.toString.call ( source [ property ] ) ) { |
93 | target [ property ] = target [ property ] || []; |
94 | } |
95 | else { |
96 | target [ property ] = target [ property ] || {}; |
97 | } |
98 | this.#copyObjectTo ( source [ property ], target [ property ] ); |
99 | } |
100 | else if ( 'string' === typeof ( target.property ) ) { |
101 | target [ property ] = |
102 | theHTMLSanitizer.sanitizeToHtmlString ( source [ property ], [] ).htmlString; |
103 | } |
104 | else { |
105 | target [ property ] = source [ property ]; |
106 | } |
107 | } |
108 | } |
109 | |
110 | /** |
111 | Freeze an object recursively |
112 | @param {Object} object The object to freeze |
113 | */ |
114 | |
115 | #freeze ( object ) { |
116 | for ( const property in object ) { |
117 | if ( 'object' === typeof object [ property ] ) { |
118 | this.#freeze ( object [ property ] ); |
119 | } |
120 | } |
121 | Object.freeze ( object ); |
122 | } |
123 | |
124 | /** |
125 | The constructor |
126 | */ |
127 | |
128 | constructor ( ) { |
129 | Object.freeze ( this ); |
130 | } |
131 | |
132 | /** |
133 | Overload the default config with another config. The config can be overloaded only once! |
134 | @param {JsonObject} source The object from witch theConfig will be overloaded |
135 | ( = the contains of the TravelNotesConfig.json file ) |
136 | */ |
137 | |
138 | overload ( source ) { |
139 | this.#copyObjectTo ( source, theConfig ); |
140 | this.#freeze ( theConfig ); |
141 | } |
142 | } |
143 | |
144 | export default ConfigOverloader; |
145 | |
146 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
147 |