| 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 FileLoader from '../../core/FileLoader.js'; | 
| 26 | import theErrorsUI from '../../uis/errorsUI/ErrorsUI.js'; | 
| 27 | import { NOT_FOUND, ZERO } from '../../main/Constants.js'; | 
| 28 | import theConfig from '../../data/Config.js'; | 
| 29 | |
| 30 | /* ------------------------------------------------------------------------------------------------------------------------- */ | 
| 31 | /** | 
| 32 | change event listener for the input associated to the import button | 
| 33 | */ | 
| 34 | /* ------------------------------------------------------------------------------------------------------------------------- */ | 
| 35 | |
| 36 | class ImportInputChangeEL { | 
| 37 | |
| 38 |     /** | 
| 39 |     The constructor | 
| 40 |     */ | 
| 41 | |
| 42 |     constructor ( ) { | 
| 43 |         Object.freeze ( this ); | 
| 44 | } | 
| 45 | |
| 46 |     /** | 
| 47 |     Event listener method | 
| 48 |     @param {Event} changeEvent The event to handle | 
| 49 |     */ | 
| 50 | |
| 51 |     handleEvent ( changeEvent ) { | 
| 52 | changeEvent.stopPropagation ( ); | 
| 53 | const fileName = changeEvent.target.files [ ZERO ].name; | 
| 54 | const fileExtension = fileName.split ( '.' ) | 
| 55 | .pop ( ) | 
| 56 | .toLowerCase ( ); | 
| 57 | const fileReader = new FileReader ( ); | 
| 58 |         fileReader.onload = ( ) => { | 
| 59 |             try { | 
| 60 | if ( NOT_FOUND !== theConfig.files.openTaN.indexOf ( fileExtension ) ) { | 
| 61 | new FileLoader ( ).mergeLocalTrvFile ( fileReader.result ); | 
| 62 | } | 
| 63 | else if ( NOT_FOUND !== theConfig.files.openGpx.indexOf ( fileExtension ) ) { | 
| 64 | new FileLoader ( ).mergeLocalGpxFile ( fileReader.result ); | 
| 65 | } | 
| 66 | } | 
| 67 |             catch ( err ) { | 
| 68 |                 if ( err instanceof Error ) { | 
| 69 | console.error ( err ); | 
| 70 | theErrorsUI.showError ( | 
| 71 |                         'An error occurs when reading the file ' + | 
| 72 | fileName | 
| 73 | ); | 
| 74 | } | 
| 75 | } | 
| 76 | }; | 
| 77 | |
| 78 | fileReader.readAsText ( changeEvent.target.files [ ZERO ] ); | 
| 79 | } | 
| 80 | } | 
| 81 | |
| 82 | export default ImportInputChangeEL; | 
| 83 | |
| 84 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ | 
| 85 |