forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabs.js
More file actions
159 lines (126 loc) · 4.1 KB
/
tabs.js
File metadata and controls
159 lines (126 loc) · 4.1 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
/**
* MUI CSS/JS tabs module
* @module tabs
*/
'use strict';
var jqLite = require('./lib/jqLite'),
util = require('./lib/util'),
attrKey = 'data-mui-toggle',
attrSelector = '[' + attrKey + '="tab"]',
controlsAttrKey = 'data-mui-controls',
activeClass = 'mui--is-active',
showstartKey = 'mui.tabs.showstart',
showendKey = 'mui.tabs.showend',
hidestartKey = 'mui.tabs.hidestart',
hideendKey = 'mui.tabs.hideend';
/**
* Initialize the toggle element
* @param {Element} toggleEl - The toggle element.
*/
function initialize(toggleEl) {
// check flag
if (toggleEl._muiTabs === true) return;
else toggleEl._muiTabs = true;
// attach click handler
jqLite.on(toggleEl, 'click', clickHandler);
}
/**
* Handle clicks on the toggle element.
* @param {Event} ev - The DOM event.
*/
function clickHandler(ev) {
// only left clicks
if (ev.button !== 0) return;
var toggleEl = this;
// exit if toggle element is disabled
if (toggleEl.getAttribute('disabled') !== null) return;
activateTab(toggleEl);
}
/**
* Activate the tab controlled by the toggle element.
* @param {Element} toggleEl - The toggle element.
*/
function activateTab(currToggleEl) {
var currTabEl = currToggleEl.parentNode,
currPaneId = currToggleEl.getAttribute(controlsAttrKey),
currPaneEl = document.getElementById(currPaneId),
prevTabEl,
prevPaneEl,
prevPaneId,
prevToggleEl,
currData,
prevData,
ev1,
ev2,
cssSelector;
// exit if already active
if (jqLite.hasClass(currTabEl, activeClass)) return;
// raise error if pane doesn't exist
if (!currPaneEl) util.raiseError('Tab pane "' + currPaneId + '" not found');
// get previous pane
prevPaneEl = getActiveSibling(currPaneEl);
prevPaneId = prevPaneEl.id;
// get previous toggle and tab elements
cssSelector = '[' + controlsAttrKey + '="' + prevPaneId + '"]';
prevToggleEl = document.querySelectorAll(cssSelector)[0];
prevTabEl = prevToggleEl.parentNode;
// define event data
currData = {paneId: currPaneId, relatedPaneId: prevPaneId};
prevData = {paneId: prevPaneId, relatedPaneId: currPaneId};
// dispatch 'hidestart', 'showstart' events
ev1 = util.dispatchEvent(prevToggleEl, hidestartKey, true, true, prevData);
ev2 = util.dispatchEvent(currToggleEl, showstartKey, true, true, currData);
// let events bubble
setTimeout(function() {
// exit if either event was canceled
if (ev1.defaultPrevented || ev2.defaultPrevented) return;
// de-activate previous
if (prevTabEl) jqLite.removeClass(prevTabEl, activeClass);
if (prevPaneEl) jqLite.removeClass(prevPaneEl, activeClass);
// activate current
jqLite.addClass(currTabEl, activeClass);
jqLite.addClass(currPaneEl, activeClass);
// dispatch 'hideend', 'showend' events
util.dispatchEvent(prevToggleEl, hideendKey, true, false, prevData);
util.dispatchEvent(currToggleEl, showendKey, true, false, currData);
}, 0);
}
/**
* Get previous active sibling.
* @param {Element} el - The anchor element.
*/
function getActiveSibling(el) {
var elList = el.parentNode.children,
q = elList.length,
activeEl = null,
tmpEl;
while (q-- && !activeEl) {
tmpEl = elList[q];
if (tmpEl !== el && jqLite.hasClass(tmpEl, activeClass)) activeEl = tmpEl
}
return activeEl;
}
/** Define module API */
module.exports = {
/** Initialize module listeners */
initListeners: function() {
// markup elements available when method is called
var elList = document.querySelectorAll(attrSelector);
for (var i=elList.length - 1; i >= 0; i--) initialize(elList[i]);
// TODO: listen for new elements
util.onNodeInserted(function(el) {
if (el.getAttribute(attrKey) === 'tab') initialize(el);
});
},
/** External API */
api: {
activate: function(paneId) {
var cssSelector = '[' + controlsAttrKey + '=' + paneId + ']',
toggleEl = document.querySelectorAll(cssSelector);
if (!toggleEl.length) {
util.raiseError('Tab control for pane "' + paneId + '" not found');
}
activateTab(toggleEl[0]);
}
}
};