-
Notifications
You must be signed in to change notification settings - Fork 8
/
tab_view_controller.js
369 lines (335 loc) · 11.4 KB
/
tab_view_controller.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/// <reference path="../../../config/config.js" />
/// <reference path="../../../utilities/utilities.js" />
/// <reference path="../../../utilities/dom_utilities.js" />
/**
* @typedef {object} TabViewControllerType
* @property {() => boolean} isViewAttached
* @property {() => TabViewControllerType} attachView
* @property {() => TabViewControllerType} detachView
* @property {() => boolean} isViewShowing
* @property {() => TabViewControllerType} showView
* @property {() => TabViewControllerType} hideView
* @property {() => boolean} isListening
* @property {() => TabViewControllerType} startListening
* @property {() => TabViewControllerType} stopListening
* @property {(tab: HTMLElement) => TabViewControllerType} activateTab
*/
/**
* Controller for tab view:
*
* When a tab is clicked, it is activated and the sibling tabs are
* deactivated. The content associated with the selected tablink
* element (specified as a css selector in the element's
* data-tabcontent attribute) is shown. Likewise, the tabcontent
* of sibling tablink elements is hidden.
*
* If a messageBus is supplied to the constructor, then 'tabActivated'
* and 'tabDeactivated' messages are published on the bus.
* The data for the message is the tabcontent selector specified
* in the tablink element's data-tabcontent attribute.
* Your code should expect the a message will be sent for each
* tablink element (specifically, the a tabDeactivated message will be
* sent even if the tab is already deactivated).
* You code should_not_ assume any ordering for how the tabActivated
* and tabDeactivate messages are sent.
*
* const viewController = TabViewController(cssTabContainer, cssTabLink);
* viewController.attachView(); // select DOM under view control
* viewController.startListening(); // attach event handlers to DOM
* viewController.showView(); // show the DOM
* // View is showing
* viewController.hideView(); // hide the DOM
* viewController.stopListening(); // remove event handlers
* viewController.detachView(); // clear references to DOM
*
* @param {string} cssTabContainer
* @param {string} cssTabLinks
* @param {MessageBusType | null} messageBus
* @returns {TabViewControllerType}
*/
function TabViewController(cssTabContainer, cssTabLinks, messageBus = null) {
/** @type {HTMLElement | null} */
let _tabContainer = null;
/** @type {NodeListOf<HTMLElement> | null} */
let _tabLinks = null;
/** @type {string[]} */
let _tabContentSelector = [];
/** @type {HTMLElement[]} */
let _tabContent = [];
/**
* @summary Determine if dom elements have been attached.
* @returns {boolean}
*/
function isViewAttached() {
return ((!!_tabContainer) && (!!_tabLinks));
}
/**
* @summary Bind the controller to the associated DOM elements.
*
* @description
* This uses the css selectors that are passed to the constructor
* to lookup the DOM elements that are used by the controller.
* >> NOTE: attaching more than once is ignored.
*
* @returns {TabViewControllerType} // this controller for fluent chain calling
*/
function attachView() {
if (isViewAttached()) {
console.log("Attempt to attach tab view twice is ignored.");
return self;
}
_tabContainer = document.querySelector(cssTabContainer);
_tabLinks = _tabContainer.querySelectorAll(cssTabLinks);
// collect that tab content associated with each tab
_tabContent = [];
_tabContentSelector = [];
for (let i = 0; i < _tabLinks.length; i += 1) {
// read value of data-tabcontent attribute
_tabContentSelector.push(_tabLinks[i].dataset.tabcontent);
_tabContent.push(document.querySelector(_tabContentSelector[i]))
}
if(_tabLinks.length > 0) {
activateTab(_tabLinks[0]); // select the first tab, hide the others
}
return self;
}
/**
* @summary Unbind the controller from the DOM.
*
* @description
* This releases the DOM elements that are selected
* by the attachView() method.
* >> NOTE: before detaching, the controller must stop listening.
*
* @returns {TabViewControllerType} // this controller for fluent chain calling
*/
function detachView() {
if (isListening()) {
console.log("Attempt to detachView while still listening is ignored.");
return self;
}
_tabContainer = null;
_tabLinks = null;
_tabContent = [];
_tabContentSelector = [];
return self;
}
let _showing = 0;
/**
* @summary Determine if the view is showing.
*
* @returns {boolean} // RET: true if view is showing
* false if view is hidden
*/
function isViewShowing() {
return _showing > 0;
}
/**
* @summary Show/Enable the view.
*
* @description
* Show the attached DOM elements.
*
* >> NOTE: the controller must be attached.
*
* >> NOTE: keeps count of calls to start/stop,
* and balances multiple calls;
*
* @example
* ```
* showView() // true == isViewShowing()
* showView() // true == isViewShowing()
* hideView() // true == isViewShowing()
* hideView() // false == isViewShowing()
* ```
*
* @returns {TabViewControllerType} this controller instance for fluent chain calling
*/
function showView() {
if (!isViewAttached()) {
console.log("Attempt to show a detached view is ignored.");
return self;
}
_showing += 1;
if (1 === _showing) {
show(_tabContainer);
}
return self;
}
/**
* @summary Hide/Disable the view.
*
* @description
* Hide the attached DOM elements.
*
* >> NOTE: the controller must be attached.
*
* >> NOTE: keeps count of calls to start/stop,
* and balances multiple calls;
*
* @example
* ```
* showView() // true == isViewShowing()
* showView() // true == isViewShowing()
* hideView() // true == isViewShowing()
* hideView() // false == isViewShowing()
* ```
*
* @returns {TabViewControllerType} this controller instance for fluent chain calling
*/
function hideView() {
if (!isViewAttached()) {
console.log("Attempt to show a detached view is ignored.");
return self;
}
_showing -= 1;
if (0 === _showing) {
hide(_tabContainer);
}
return self;
}
let _listening = 0;
/**
* @summary Determine if controller is listening for messages and DOM events.
*
* @returns {boolean} true if listening for events,
* false if not listening for events.
*/
function isListening() {
return _listening > 0;
}
/**
* @summary Start listening for DOM events.
* @description
* This adds event listeners to attached dom elements.
*
* >> NOTE: the view must be attached.
*
* >> NOTE: This keeps count of calls to start/stop and balances multiple calls;
*
* @example
* ```
* startListening() // true === isListening()
* startListening() // true === isListening()
* stopListening() // true === isListening()
* stopListening() // false === isListening()
* ```
*
* @returns {TabViewControllerType} this controller instance for fluent chain calling
*/
function startListening() {
if (!isViewAttached()) {
console.log("Attempt to start listening to detached view is ignored.");
return self;
}
_listening += 1;
if (1 === _listening) {
if (_tabLinks) {
_tabLinks.forEach(el => el.addEventListener("click", _onTabClick));
}
}
return self;
}
/**
* @summary Stop listening for DOM events.
* @description
* This removes event listeners from attached dom elements.
*
* >> NOTE: the view must be attached.
*
* >> NOTE: This keeps count of calls to start/stop and balances multiple calls;
*
* @example
* ```
* startListening() // true === isListening()
* startListening() // true === isListening()
* stopListening() // true === isListening()
* stopListening() // false === isListening()
* ```
*
* @returns {TabViewControllerType} this controller instance for fluent chain calling
*/
function stopListening() {
if (!isViewAttached()) {
console.log("Attempt to stop listening to detached view is ignored.");
return self;
}
_listening -= 1;
if (0 === _listening) {
if (_tabLinks) {
_tabLinks.forEach(el => el.removeEventListener("click", _onTabClick));
}
}
return self;
}
/**
* @summary Activate a tab and deactivate the others
*
* @description
* This will activate/show the give tab and
* hide/disable the others.
* The activated tab starts listening and the
* disabled tabs stop listening.
* If a message bus has been provided, then a message
* is published for each tab's new state, so that
* other parts of the app can coordinate their
* behavior if necessary.
* - publish `TAB_ACTIVATED(tabname)` message when a tas is activate
* - publish `TAB_DEACTIVATED(tabname)` message when a tab is deactivated.
*
* @param {HTMLElement} tab
* @returns {TabViewControllerType} this controller instance for fluent chain calling
*/
function activateTab(tab) {
for (let i = 0; i < _tabLinks.length; i += 1) {
const tabLink = _tabLinks[i];
if (tab === tabLink) {
// activate this tab's content
tabLink.classList.add("active");
if (_tabContent[i]) {
show(_tabContent[i]);
}
if (messageBus) {
messageBus.publish(`TAB_ACTIVATED(${_tabContentSelector[i]})`);
}
} else {
// deactivate this tab's content
tabLink.classList.remove("active");
if (_tabContent[i]) {
hide(_tabContent[i]);
}
if (messageBus) {
messageBus.publish(`TAB_DEACTIVATED(${_tabContentSelector[i]})`);
}
}
}
return self;
}
/**
* @summary Event handler when a tab is clicked.
*
* @description
* When a table is clicked then activateTab() is called
* for that tab, which enables it and disables the others.
*
* @param {*} event
*/
function _onTabClick(event) {
// make this tab active and all siblings inactive
activateTab(event.target);
}
/** @type {TabViewControllerType} */
const self = Object.freeze({
"attachView": attachView,
"detachView": detachView,
"isViewAttached": isViewAttached,
"showView": showView,
"hideView": hideView,
"isViewShowing": isViewShowing,
"startListening": startListening,
"stopListening": stopListening,
"isListening": isListening,
"activateTab": activateTab,
});
return self;
}