File : roadbook/SaveFileButtonClickEL.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
/* ------------------------------------------------------------------------------------------------------------------------- */
26
/**
27
click event listener for the save button
28
*/
29
/* ------------------------------------------------------------------------------------------------------------------------- */
30
31
class SaveFileButtonClickEL {
32
33
    /**
34
    The constructor
35
    */
36
37
    constructor ( ) {
38
        Object.freeze ( this );
39
    }
40
41
    /**
42
    Event listener method
43
    */
44
45
    handleEvent ( ) {
46
        try {
47
            const fileName =
48
                document.querySelector ( '.TravelNotes-Roadbook-Travel-Header-Name' ).textContent + '-Roadbook.html';
49
50
            // Temporary removing the save button
51
            const buttonsDiv = document.getElementById ( 'TravelNotes-ButtonsDiv' );
52
            const saveButton = buttonsDiv.removeChild ( document.getElementById ( 'TravelNotes-SaveButton' ) );
53
54
            // Saving
55
            const mapFile = window.URL.createObjectURL (
56
                new File (
57
                    [ '<!DOCTYPE html>', document.documentElement.outerHTML ],
58
                    fileName,
59
                    { type : 'text/plain' }
60
                )
61
            );
62
            const anchorElement = document.createElement ( 'a' );
63
            anchorElement.setAttribute ( 'href', mapFile );
64
            anchorElement.setAttribute ( 'download', fileName );
65
            anchorElement.style.display = 'none';
66
            anchorElement.click ( );
67
            window.URL.revokeObjectURL ( mapFile );
68
69
            // Restoring the save button
70
            buttonsDiv.appendChild ( saveButton );
71
        }
72
        catch ( err ) {
73
            if ( err instanceof Error ) {
74
                console.error ( err );
75
            }
76
        }
77
    }
78
}
79
80
export default SaveFileButtonClickEL;
81
82
/* --- End of file --------------------------------------------------------------------------------------------------------- */
83