File : dialogs/notesDialog/toolbar/NoteDialogToolbar.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 theNoteDialogToolbarData from './NoteDialogToolbarData.js';
26
import theHTMLElementsFactory from '../../../core/uiLib/HTMLElementsFactory.js';
27
import theTranslator from '../../../core/uiLib/Translator.js';
28
import theHTMLSanitizer from '../../../core/htmlSanitizer/HTMLSanitizer.js';
29
30
import { NOT_FOUND } from '../../../main/Constants.js';
31
32
/* ------------------------------------------------------------------------------------------------------------------------- */
33
/**
34
This class is the toolbar of the NoteDialog
35
*/
36
/* ------------------------------------------------------------------------------------------------------------------------- */
37
38
class NoteDialogToolbar {
39
40
    /**
41
    The toolbar container
42
    @type {HTMLElement}
43
    */
44
45
    #rootHTMLElement;
46
47
    /**
48
    The icon selector
49
    @type {HTMLElement}
50
    */
51
52
    #iconSelect;
53
54
    /**
55
    The open file button
56
    @type {HTMLElement}
57
    */
58
59
    #openFileButton;
60
61
    /**
62
    The editions buttons
63
    @type {Array.<HTMLElement>}
64
    */
65
66
    #editionButtons;
67
68
    /**
69
    Add the icon selector to the toolbar
70
    */
71
72
    #addIconsSelector ( ) {
73
        this.#iconSelect = theHTMLElementsFactory.create (
74
            'select',
75
            {
76
                className : 'TravelNotes-NoteDialog-Select',
77
                id : 'TravelNotes-NoteDialog-IconSelect'
78
            },
79
            this.#rootHTMLElement
80
        );
81
82
        theNoteDialogToolbarData.preDefinedIconsData.forEach (
83
            selectOption => {
84
                this.#iconSelect.add ( theHTMLElementsFactory.create ( 'option', { text : selectOption.name } ) );
85
            }
86
        );
87
        this.#iconSelect.selectedIndex = NOT_FOUND;
88
    }
89
90
    /**
91
    Add the toolbar buttons to the toolbar
92
    */
93
94
    #addToolbarButtons ( ) {
95
        this.#openFileButton = theHTMLElementsFactory.create (
96
            'div',
97
            {
98
                className : 'TravelNotes-BaseDialog-Button',
99
                title : theTranslator.getText ( 'NoteDialogToolbar - Open a configuration file' ),
100
                textContent : '📂'
101
            },
102
            this.#rootHTMLElement
103
        );
104
    }
105
106
    /**
107
    Add the edition buttons to the toolbar
108
    */
109
110
    #addEditionButtons ( ) {
111
        theNoteDialogToolbarData.editionButtonsData.forEach (
112
            editionButtonData => {
113
                const newButton = theHTMLElementsFactory.create (
114
                    'div',
115
                    {
116
                        dataset : {
117
                            HtmlBefore : editionButtonData.htmlBefore,
118
                            HtmlAfter : editionButtonData.htmlAfter
119
                        },
120
                        className : 'TravelNotes-NoteDialog-EditorButton'
121
                    },
122
                    this.#rootHTMLElement
123
                );
124
                theHTMLSanitizer.sanitizeToHtmlElement ( editionButtonData.title, newButton );
125
                this.#editionButtons.push ( newButton );
126
            }
127
        );
128
    }
129
130
    /**
131
    Add elements to the toolbar
132
    */
133
134
    #addToolbarElements ( ) {
135
136
        this.#addIconsSelector ( );
137
        this.#addToolbarButtons ( );
138
        this.#addEditionButtons ( );
139
    }
140
141
    /**
142
    Add the events listeners to the toolbar objects
143
    @param {NoteDialogEventListeners} eventListeners A reference to the eventListeners object of the NoteDialog
144
    */
145
146
    #addEventListeners ( eventListeners ) {
147
        this.#iconSelect.addEventListener ( 'change', eventListeners.iconSelectorChange );
148
        this.#editionButtons.forEach (
149
            button => { button.addEventListener ( 'click', eventListeners.editionButtonsClick ); }
150
        );
151
        this.#openFileButton.addEventListener ( 'click', eventListeners.openCfgFileButtonClick, false );
152
    }
153
154
    /**
155
    Remove event listeners on all htmlElements
156
    @param {NoteDialogEventListeners} eventListeners A reference to the eventListeners object of the NoteDialog
157
    */
158
159
    #removeEventListeners ( eventListeners ) {
160
        this.#iconSelect.removeEventListener ( 'change', eventListeners.iconSelectorChange );
161
        this.#editionButtons.forEach (
162
            button => { button.removeEventListener ( 'click', eventListeners.editionButtonsClick ); }
163
        );
164
        this.#openFileButton.removeEventListener ( 'click', eventListeners.openCfgFileButtonClick, false );
165
    }
166
167
    /**
168
    The constructor
169
    @param {NoteDialogEventListeners} eventListeners A reference to the eventListeners object of the NoteDialog
170
    */
171
172
    constructor ( eventListeners ) {
173
174
        Object.freeze ( this );
175
176
        this.#editionButtons = [];
177
178
        this.#rootHTMLElement = theHTMLElementsFactory.create (
179
            'div',
180
            {
181
                id : 'TravelNotes-NoteDialog-ToolbarDiv'
182
            }
183
        );
184
185
        this.#addToolbarElements ( );
186
        this.#addEventListeners ( eventListeners );
187
    }
188
189
    /**
190
    Destructor. Remove event listeners.
191
    @param {NoteDialogEventListeners} eventListeners A reference to the eventListeners object of the NoteDialog
192
    */
193
194
    destructor ( eventListeners ) {
195
        this.#removeEventListeners ( eventListeners );
196
    }
197
198
    /**
199
    Refresh the toolbar - needed after a file upload.
200
    @param {NoteDialogEventListeners} eventListeners A reference to the eventListeners object of the NoteDialog
201
    */
202
203
    update ( eventListeners ) {
204
        this.#removeEventListeners ( eventListeners );
205
        this.#rootHTMLElement.textContent = '';
206
        this.#editionButtons = [];
207
        this.#addToolbarElements ( );
208
        this.#addEventListeners ( eventListeners );
209
    }
210
211
    /**
212
    The rootHTMLElement of the toolbar
213
    @type {HTMLElement}
214
    */
215
216
    get rootHTMLElement ( ) { return this.#rootHTMLElement;    }
217
218
}
219
220
export default NoteDialogToolbar;
221
222
/* --- End of file --------------------------------------------------------------------------------------------------------- */
223