forked from binary-com/binary-static
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu_content.js
More file actions
158 lines (139 loc) · 5.59 KB
/
menu_content.js
File metadata and controls
158 lines (139 loc) · 5.59 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
var MenuContent = (function () {
var listeners_events = [];
var that = {
init: function (_menu_containers) {
_menu_containers.filter(':not(.follow-default)').delegate('.tm-a,.tm-a-2', 'click', function (event) {
event.preventDefault();
var target = $(event.target);
var tab_id = target.parents('li:first').attr('id');
if (tab_id)
{
var tab_container = target.parents('.tm-ul');
var selected_tab =
// find previously active tab
tab_container.find('.tm-a,.tm-a-2')
// remove previously active tab
.removeClass('a-active').end()
// unwrap previously active tab
.find('.menu-wrap-a .tm-a').unwrap().unwrap()
// go back to selected target
.end().end()
// set active class to it
.addClass('a-active')
// set active class to its parent as well
.parents('.tm-li').addClass('active').removeClass('hover').find('.tm-li-2').addClass('active').end()
// wrap it
.find('.tm-a').wrap('<span class="menu-wrap-a"><span class="menu-wrap-b"></span></span>').end()
// remove previously active parent
.siblings().removeClass('active').find('.tm-li-2').removeClass('active').end()
.end().end();
// replace span to a, to make it clickable for real
var span_tm_a = tab_container.find('span.tm-a');
span_tm_a.replaceWith('<a href="#" class="'+span_tm_a.attr('class')+'">'+span_tm_a.html()+'</a>');
var menu_li = selected_tab.parents('li');
var sub_menu_selected = menu_li.find('.tm-ul-2 .a-active');
var selected_tab_id = menu_li.attr('id');
if (!sub_menu_selected.length)
{
sub_menu_selected = menu_li.find('.tm-a-2:first').addClass('a-active');
if (sub_menu_selected.length)
{
selected_tab = sub_menu_selected;
selected_tab_id = sub_menu_selected.parents('li').attr('id');
selected_content = $('#'+selected_tab_id+'-content').removeClass('invisible');
}
else
{
selected_tab_id = menu_li.attr('id');
}
}
var selected_content = $('#'+selected_tab_id+'-content')
// show selected tab content
.removeClass('invisible')
// and hide the rest
.siblings(':not(.sticky)').addClass('invisible').end();
that.push_to_listeners({
'id': selected_tab_id,
'target': selected_tab,
'content': selected_content,
'menu': menu_li.parents('ul.tm-ul'),
'event': event
});
}
return false;
});
},
push_to_listeners: function (info)
{
// push to listeners events
for (var i=0; i<listeners_events.length; i++)
{
listeners_events[i](info);
}
},
listen_click: function (callback)
{
if (typeof callback != 'function')
{
return false;
}
listeners_events.push(callback);
},
find_selected_tab: function (menu_id)
{
var menu = $('#'+menu_id);
var selected_tab = menu.find('.a-active').parents('.tm-li');
if (!selected_tab.length)
{
selected_tab = menu.find('.active');
}
return selected_tab;
},
is_tab_selected: function (tab)
{
return tab.hasClass('active');
},
hide_tab: function (tab)
{
tab.addClass('invisible').find('.menu-wrap-a .tm-a').unwrap().unwrap();
$('#'+tab.attr('id')+'-content').addClass('invisible');
},
show_tab: function (tab)
{
tab.removeClass('invisible');
},
trigger: function (id)
{
var tab_id = id['tab_id'];
var content_id = id['content_id'];
if (!tab_id && typeof content_id != 'undefined') {
var matched = content_id.match(/^(.+)-content$/);
if (matched && matched[1]) {
tab_id = matched[1];
}
}
if (!tab_id)
{
return false;
}
var tab_to_trigger = $('#'+tab_id);
if (!tab_to_trigger.size() || tab_to_trigger.hasClass('invisible'))
{
return false;
}
else
{
var tab = tab_to_trigger.find('.tm-a');
if (tab.size())
{
return tab.trigger('click');
}
else
{
return tab_to_trigger.find('.tm-a-2').trigger('click');
}
}
}
};
return that;
})();