File : dialogs/notesDialog/eventListeners/IconSelectorChangeEL.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
import theTranslator from '../../../core/uiLib/Translator.js';
26
import theNoteDialogToolbarData from '../toolbar/NoteDialogToolbarData.js';
27
import MapIconFromOsmFactory from '../../../core/mapIcon/MapIconFromOsmFactory.js';
28
29
/* ------------------------------------------------------------------------------------------------------------------------- */
30
/**
31
change event listener for the icon selector
32
*/
33
/* ------------------------------------------------------------------------------------------------------------------------- */
34
35
class IconSelectorChangeEL {
36
37
    /**
38
    A reference to the NoteDialog object
39
    @type {NoteDialog}
40
    */
41
42
    #noteDialog;
43
44
    /**
45
    Helper method for the onIconSelectChange mehod
46
    @param {Object} noteData An object with the note properties to update
47
    */
48
49
    #updatePreviewAndControls ( noteData )    {
50
        this.#noteDialog.setControlsValues ( noteData );
51
        this.#noteDialog.updatePreview ( noteData );
52
    }
53
54
    /**
55
    Svg Map icon creation
56
    */
57
58
    #onMapIcon ( ) {
59
        const mapIconData = this.#noteDialog.mapIconData;
60
        if ( ! mapIconData.route ) {
61
            this.#noteDialog.showError (
62
                theTranslator.getText ( 'IconSelectorChangeEL - not possible to create a SVG icon for a travel note' )
63
            );
64
            return;
65
        }
66
67
        this.#noteDialog.hideError ( );
68
        this.#noteDialog.showWait ( );
69
        new MapIconFromOsmFactory ( ).getIconAndAdressWithPromise ( mapIconData )
70
            .then (
71
                noteData => {
72
                    this.#noteDialog.hideWait ( );
73
                    this.#updatePreviewAndControls ( noteData );
74
                }
75
            )
76
            .catch (
77
                err => {
78
                    if ( err instanceof Error ) {
79
                        console.error ( err );
80
                    }
81
                    this.#noteDialog.hideWait ( );
82
                    this.#noteDialog.showError (
83
                        theTranslator.getText ( 'IconSelectorChangeEL - an error occurs when creating the SVG icon' )
84
                    );
85
                }
86
            );
87
    }
88
89
    /**
90
    The constructor
91
    @param {NoteDialog} noteDialog A reference to the Notedialog object
92
    */
93
94
    constructor ( noteDialog ) {
95
        Object.freeze ( this );
96
        this.#noteDialog = noteDialog;
97
    }
98
99
    /**
100
    Event listener method
101
    @param {Event} changeEvent The event to handle
102
    */
103
104
    handleEvent ( changeEvent ) {
105
        changeEvent.stopPropagation ( );
106
        const preDefinedIcon = theNoteDialogToolbarData.preDefinedIconDataAt ( changeEvent.target.selectedIndex );
107
        if ( 'SvgIcon' === preDefinedIcon.icon ) {
108
            this.#onMapIcon ( );
109
            return;
110
        }
111
        this.#updatePreviewAndControls (
112
            {
113
                iconContent : preDefinedIcon.icon,
114
                iconHeight : preDefinedIcon.height,
115
                iconWidth : preDefinedIcon.width,
116
                tooltipContent : preDefinedIcon.tooltip
117
            }
118
        );
119
        this.#noteDialog.setAddress ( );
120
    }
121
}
122
123
export default IconSelectorChangeEL;
124
125
/* --- End of file --------------------------------------------------------------------------------------------------------- */
126