File : toolbars/baseToolbar/ToolbarItem.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
Simple container for toolbar buttons content
28
*/
29
/* ------------------------------------------------------------------------------------------------------------------------- */
30
31
class ToolbarItem {
32
33
    /**
34
    The text displayed on the toolbar button
35
    @type {String}
36
    */
37
38
    #textContent;
39
40
    /**
41
    The tooltip text of the toolbar button
42
    @type {String}
43
    */
44
45
    #title;
46
47
    /**
48
    The action to be performed when the user click on the button. Can be a function or a link
49
    @type {function|String}
50
    */
51
52
    #action;
53
54
    /**
55
    The constructor
56
    @param {String|function} textContent The text displayed on the toolbar button
57
    @param {String} title The tooltip text of the toolbar button
58
    @param {function|String} action The action to be performed when the user click on the button. Can be a function or a link
59
    */
60
61
    constructor ( textContent, title, action ) {
62
        this.#textContent = textContent;
63
        this.#textContent = 'function' === typeof ( textContent ) ? textContent ( ) : textContent;
64
        this.#title = title;
65
        this.#action = action;
66
    }
67
68
    /**
69
    The text displayed on the toolbar button
70
    @type {String}
71
    */
72
73
    get textContent ( ) { return this.#textContent; }
74
75
    /**
76
    The tooltip text of the toolbar button
77
    @type {String}
78
    */
79
80
    get title ( ) { return this.#title; }
81
82
    /**
83
    Execute the action registered for the item
84
    */
85
86
    doAction ( ) {
87
        switch ( typeof ( this.#action ) ) {
88
        case 'string' :
89
            {
90
                const linkElement = document.createElement ( 'a' );
91
                linkElement.href = this.#action;
92
                linkElement.target = '_blank';
93
                linkElement.click ( );
94
            }
95
            break;
96
        case 'function' :
97
            this.#action ( );
98
            break;
99
        default :
100
            break;
101
        }
102
    }
103
}
104
105
export default ToolbarItem;
106
107
/* --- End of file --------------------------------------------------------------------------------------------------------- */
108