File : dialogs/notesDialog/toolbar/NoteDialogToolbarData.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 { ZERO, ONE } from '../../../main/Constants.js';
26
import PredefinedIconData from './PredefinedIconData.js';
27
import EditionButtonData from './EditionButtonData.js';
28
29
/* ------------------------------------------------------------------------------------------------------------------------- */
30
/**
31
This class is a container for the edition buttons data and predefined icons data
32
*/
33
/* ------------------------------------------------------------------------------------------------------------------------- */
34
35
class NoteDialogToolbarData {
36
37
    /**
38
    The additional buttons defined in the TravelNotesDialogXX.json file or a file loaded by the user
39
    @type {Array.<EditionButtonData>}
40
    */
41
42
    #editionButtonsData;
43
44
    /**
45
    A map with the additional icons defined in the TravelNotesDialogXX.json file or a file loaded by the user, ordered by name
46
    @type {Map.<PredefinedIconData>}
47
    */
48
49
    #preDefinedIconsDataMap;
50
51
    /**
52
    The additional icons defined in the TravelNotesDialogXX.json file or a file loaded by the user
53
    @type {Array.<PredefinedIconData>}
54
    */
55
56
    #preDefinedIconsDataArray;
57
58
    /**
59
    The constructor
60
    */
61
62
    constructor ( ) {
63
        Object.freeze ( this );
64
        this.#editionButtonsData = [];
65
        this.#preDefinedIconsDataMap = new Map ( );
66
        this.#preDefinedIconsDataArray = [];
67
    }
68
69
    /**
70
    The additional buttons defined in the TravelNotesDialogXX.json file or a file loaded by the user
71
    @type {Array.<EditionButtonData>}
72
    */
73
74
    get editionButtonsData ( ) { return this.#editionButtonsData; }
75
76
    /**
77
    The additional icons defined in the TravelNotesDialogXX.json file or a file loaded by the user
78
    @type {Array.<PredefinedIconData>}
79
    */
80
81
    get preDefinedIconsData ( ) {
82
        return this.#preDefinedIconsDataArray;
83
    }
84
85
    /**
86
    get and icon from the icon position in the array
87
    @param {Number} index The icon index in the array
88
    @return {PredefinedIconData} The predefinedIconData at the given index
89
    */
90
91
    preDefinedIconDataAt ( index ) {
92
        return this.#preDefinedIconsDataArray [ index ];
93
    }
94
95
    /**
96
    get an icon from the icon name
97
    @param {String} iconName The icon name
98
    @return {PredefinedIconData} The predefinedIconData with the name equal to the given name
99
    */
100
101
    preDefinedIconDataFromName ( iconName ) {
102
        const preDefinedIcon = this.#preDefinedIconsDataMap.get ( iconName );
103
        return preDefinedIcon ? preDefinedIcon.icon : '';
104
    }
105
106
    /**
107
    Load a json file with predefined icons and / or edition buttons
108
    @param {JsonObject} jsonData The file content after JSON.parse ( )
109
    */
110
111
    loadJson ( jsonData ) {
112
        if ( jsonData.editionButtons ) {
113
            jsonData.editionButtons.forEach (
114
                jsonEditionButtonData => {
115
                    try {
116
                        let editionButtonData = new EditionButtonData ( jsonEditionButtonData );
117
                        this.#editionButtonsData.push ( editionButtonData );
118
                    }
119
                    catch ( err ) {
120
                        console.error ( err.message );
121
                    }
122
                }
123
            );
124
        }
125
        if ( jsonData.preDefinedIconsList ) {
126
            jsonData.preDefinedIconsList.forEach (
127
                jsonPredefinedIconData => {
128
                    const predefinedIconData = new PredefinedIconData ( jsonPredefinedIconData );
129
                    this.#preDefinedIconsDataMap.set ( predefinedIconData.name, predefinedIconData );
130
                }
131
            );
132
133
            this.#preDefinedIconsDataArray.length = ZERO;
134
            for ( const element of this.#preDefinedIconsDataMap ) {
135
                this.#preDefinedIconsDataArray.push ( element [ ONE ] );
136
            }
137
            this.#preDefinedIconsDataArray = this.#preDefinedIconsDataArray.sort (
138
                ( first, second ) => first.name.localeCompare ( second.name )
139
            );
140
        }
141
    }
142
}
143
144
/* ------------------------------------------------------------------------------------------------------------------------- */
145
/**
146
The one and only one instance of NoteDialogToolbarData class
147
@type {NoteDialogToolbarData}
148
*/
149
/* ------------------------------------------------------------------------------------------------------------------------- */
150
151
const theNoteDialogToolbarData = new NoteDialogToolbarData ( );
152
153
export default theNoteDialogToolbarData;
154
155
/* --- End of file --------------------------------------------------------------------------------------------------------- */
156