forked from dcloudio/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegmented-controllers.js
106 lines (93 loc) · 3.23 KB
/
segmented-controllers.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
/**
* segmented-controllers
* @param {type} $
* @param {type} window
* @param {type} document
* @param {type} undefined
* @returns {undefined}
*/
(function($, window, document, name, undefined) {
var CLASS_CONTROL_ITEM = $.className('control-item');
var CLASS_SEGMENTED_CONTROL = $.className('segmented-control');
var CLASS_SEGMENTED_CONTROL_VERTICAL = $.className('segmented-control-vertical');
var CLASS_CONTROL_CONTENT = $.className('control-content');
var CLASS_TAB_BAR = $.className('bar-tab');
var CLASS_TAB_ITEM = $.className('tab-item');
var CLASS_SLIDER_ITEM = $.className('slider-item');
var handle = function(event, target) {
if (target.classList && (target.classList.contains(CLASS_CONTROL_ITEM) || target.classList.contains(CLASS_TAB_ITEM))) {
if (target.parentNode && target.parentNode.classList && target.parentNode.classList.contains(CLASS_SEGMENTED_CONTROL_VERTICAL)) {
//vertical 如果preventDefault会导致无法滚动
} else {
event.preventDefault(); //stop hash change
}
// if (target.hash) {
return target;
// }
}
return false;
};
$.registerTarget({
name: name,
index: 80,
handle: handle,
target: false
});
window.addEventListener('tap', function(e) {
var targetTab = $.targets.tab;
if (!targetTab) {
return;
}
var activeTab;
var activeBodies;
var targetBody;
var className = $.className('active');
var classSelector = '.' + className;
var segmentedControl = targetTab.parentNode;
for (; segmentedControl && segmentedControl !== document; segmentedControl = segmentedControl.parentNode) {
if (segmentedControl.classList.contains(CLASS_SEGMENTED_CONTROL)) {
activeTab = segmentedControl.querySelector(classSelector + '.' + CLASS_CONTROL_ITEM);
break;
} else if (segmentedControl.classList.contains(CLASS_TAB_BAR)) {
activeTab = segmentedControl.querySelector(classSelector + '.' + CLASS_TAB_ITEM);
}
}
if (activeTab) {
activeTab.classList.remove(className);
}
var isLastActive = targetTab === activeTab;
if (targetTab) {
targetTab.classList.add(className);
}
if (!targetTab.hash) {
return;
}
targetBody = document.getElementById(targetTab.hash.replace('#', ''));
if (!targetBody) {
return;
}
if (!targetBody.classList.contains(CLASS_CONTROL_CONTENT)) { //tab bar popover
targetTab.classList[isLastActive ? 'remove' : 'add'](className);
return;
}
if (isLastActive) { //same
return;
}
var parentNode = targetBody.parentNode;
activeBodies = parentNode.querySelectorAll('.' + CLASS_CONTROL_CONTENT + classSelector);
for (var i = 0; i < activeBodies.length; i++) {
var activeBody = activeBodies[i];
activeBody.parentNode === parentNode && activeBody.classList.remove(className);
}
targetBody.classList.add(className);
var contents = [];
var _contents = parentNode.querySelectorAll('.' + CLASS_CONTROL_CONTENT);
for (var i = 0; i < _contents.length; i++) { //查找直属子节点
_contents[i].parentNode === parentNode && (contents.push(_contents[i]));
}
$.trigger(targetBody, $.eventName('shown', name), {
tabNumber: Array.prototype.indexOf.call(contents, targetBody)
});
e.detail && e.detail.gesture.preventDefault(); //fixed hashchange
});
})(mui, window, document, 'tab');