-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTabNavItem.js
More file actions
211 lines (177 loc) · 6.12 KB
/
Copy pathTabNavItem.js
File metadata and controls
211 lines (177 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import Logger from "../../Utils/LoggerByDefault";
import Helper from "../../Utils/Helper";
import Control from "../Control";
import SelectorID from "../../Utils/SelectorID";
var logger = Logger.getLogger("tabnav");
/**
* Objet représentant un item de navigation tertiaire
* @typedef {Object} TabNavItemOptions
* @property {String} label - Label du bouton
* @property {String|HTMLElement} [content] - Contenu lié au bouton
* @property {String} [title] - Titre du bouton
* @property {String} [icon] - Classe CSS de l'icône du bouton
* @property {Function} [onOpen] - Callback appelé à l'ouverture de l'onglet
* @property {Function} [onClose] - Callback appelé à la fermeture de l'onglet
*/
/**
* @classdesc
* Classe gérant une navigation tertiaire avec onglets.
*
* @alias TabNavItem
* @module TabNavItem
* @extends {Control}
*/
class TabNavItem extends Control {
/**
* Constructeur du TabNavItem.
* @constructor
* @param {TabNavItemOptions} options Options du constructeur
*/
constructor (options = {}) {
super(options);
// Initialisation du composant
this._initialize(options);
// Création du conteneur
this._initContainer(options);
// Initialisation des événements
this._initEvents(options);
}
/**
* @param {TabNavItemOptions} options Options de construction
*/
_initialize (options) {
super._initialize(options);
this.tabNavItemClass = "GPF-tabnav";
// UID utilisé dans la suite
this._uid = options.id || SelectorID.generate();
this.set("selected", false);
this.selectors = {
OPEN_TAB : "tab:open",
CLOSE_TAB : "tab:close",
};
this.onOpen = typeof options.onOpen === "function" ? options.onOpen : () => { };
this.onClose = typeof options.onClose === "function" ? options.onClose : () => { };
}
/**
* Crée le conteneur principal de la navigation.
* @protected
* @param {TabNavItemOptions} options Options de construction
* @returns {HTMLElement} Élément div de navigation
*/
_initContainer (options) {
super._initContainer(options);
if (!options) {
return;
}
// const btnId = Helper.getUid("tabNavLink");
const btnId = Helper.addUID("tabNavLink", this._uid);
// const contentId = Helper.getUid("tabNavContent");
const contentId = Helper.addUID("tabNavContent", this._uid);
// Création de la puce
this.element = document.createElement("li");
this.element.className = `fr-nav__item fr-tabnav__item ${this.tabNavItemClass}__item`;
this.element.setAttribute("role", "presentation");
// Création du bouton
this.button = document.createElement("button");
this.button.type = "button";
this.button.id = btnId;
this.button.setAttribute("role", "tab");
this.button.setAttribute("aria-controls", contentId);
this.button.setAttribute("aria-selected", "false");
this.button.innerHTML = options.label;
let buttonClasses = `fr-nav__link fr-tabnav__link ${this.tabNavItemClass}__link`;
if (options.icon) {
buttonClasses += ` ${options.icon} fr-btn--icon-left ${this.tabNavItemClass}__link-btn`;
}
this.button.className = buttonClasses;
if (options.title) {
this.button.title = options.title;
}
this.element.appendChild(this.button);
// Création du contenu lié à l'onglet
this.content = document.createElement("div");
this.content.id = contentId;
this.content.setAttribute("aria-labelledby", btnId);
this.content.setAttribute("role", "tabpanel");
this.content.className = "fr-tabnav__panel fr-hidden GPelementHidden";
if (typeof options.content === "string") {
this.content.innerHTML = options.content;
} else if (options.content instanceof HTMLElement) {
this.content.appendChild(options.content);
}
return this.element;
}
/**
* @param {TabNavItemOptions} options Options du constructeur
*/
_initEvents (options) {
super._initEvents(options);
this.addEventListener(this.selectors.OPEN_TAB, this.onOpen);
this.addEventListener(this.selectors.CLOSE_TAB, this.onClose);
}
/**
* Retourne l'élément principal de la navigation.
* @returns {HTMLElement} Élément div de navigation
*/
getElement () {
return this.element;
}
/**
* Retourne le bouton de l'item.
* @returns {HTMLButtonElement} Élément div de navigation
*/
getButton () {
return this.button;
}
/**
* Retourne le contenu de l'item.
*
* Attention, cela est différent du conteneur dans lequel il se trouve
* @returns {HTMLElement} Élément div de navigation
*/
getContent () {
return this.content;
}
/**
* Affiche le contenu.
*/
show () {
this.content.classList.remove("fr-hidden", "GPelementHidden");
this.dispatchEvent(this.selectors.OPEN_TAB);
}
/**
* Masque le contenu.
*/
hide () {
this.content.classList.add("fr-hidden", "GPelementHidden");
this.dispatchEvent(this.selectors.CLOSE_TAB);
}
/**
* Sélectionne ou déselectionne l'item
* @param {Boolean} bool Vrai si l'élément doit être sélectionné.
*/
setSelected (bool) {
this.set("selected", bool);
if (bool) {
this.button.ariaSelected = true;
// Affiche le contenu
this.show();
} else {
this.button.ariaSelected = false;
// Cache le contenu
this.hide();
}
}
/**
* Vérifie si l'élément est sélectionné
* @return {Boolean} Vrai si l'élément est sélectionné.
*/
isSelected () {
return this.get("selected");
}
}
export default TabNavItem;
// Expose TabNavItem as ol.control.TabNavItem (for a build bundle)
if (window.ol && window.ol.control) {
window.ol.control.TabNavItem = TabNavItem;
}