/
nav-collapse.js
121 lines (99 loc) · 4.52 KB
/
nav-collapse.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
/**
*------------------------------------------------------------------------------
* @package T3 Framework for Joomla!
*------------------------------------------------------------------------------
* @copyright Copyright (C) 2004-2013 JoomlArt.com. All Rights Reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* @authors JoomlArt, JoomlaBamboo, (contribute to this project at github
* & Google group to become co-author)
* @Google group: https://groups.google.com/forum/#!forum/t3fw
* @Link: http://t3-framework.org
*------------------------------------------------------------------------------
*/
jQuery(document).ready(function ($) {
// clone the collapse menu from mainnav (.t3-navbar)
$('.t3-navbar').each(function(){
var $navwrapper = $(this),
$menu = null,
$placeholder = null;
if ($navwrapper.find('.t3-megamenu').length) {
// clone for megamenu
$menu = $navwrapper.find('ul.level0').clone(),
$placeholder = $navwrapper.prev('.navbar-collapse');
if(!$placeholder.length){
//get the empty one
$placeholder = $navwrapper.closest('.container, .t3-mainnav').find('.navbar-collapse:empty');
}
var lis = $menu.find('li[data-id]'),
liactive = lis.filter('.current');
// clean class
lis.removeClass('mega dropdown mega-align-left mega-align-right mega-align-center mega-align-adjust');
// rebuild
lis.each(function () {
// get firstchild - a or span
var $li = $(this),
$child = $li.find('>:first-child');
if ($child[0].nodeName == 'DIV') {
$child.find('>:first-child').prependTo($li);
$child.remove();
}
// remove caret
if($li.data('hidewcol')){
$child.find('.caret').remove();
$child.nextAll().remove();
return; //that is all for this item
}
// find subnav and inject into one ul
var subul = $li.find('ul.level' + $li.data('level'));
if (subul.length) {
// create subnav
$ul = $('<ul class="level' + $li.data('level') + ' dropdown-menu">');
subul.each(function () {
// check if the ul not in a hide when collapsed column
if ($(this).parents('.mega-col-nav').data('hidewcol')) return ;
$(this).find('>li').appendTo($ul);
});
if ($ul.children().length) {
$ul.appendTo($li);
}
}
// remove all child div
$li.find('>div').remove();
// clean caret if there was no real submenu
if(!$li.children('ul').length){
$child.find('.caret').remove();
}
var divider = $li.hasClass('divider');
// clear all attributes
// $li.removeAttr('class');
for (var x in $li.data()) {
$li.removeAttr('data-' + x)
}
$child.removeAttr('class');
for (var x in $child.data()) {
$child.removeAttr('data-' + x)
}
if(divider){
$li.addClass('divider');
}
});
// update class current
liactive.addClass('current active');
} else {
// clone for bootstrap menu
$menu = $navwrapper.find ('ul.nav').clone();
$placeholder = $('.t3-navbar-collapse:empty, .navbar-collapse:empty').eq(0);
}
//so we have all structure, add standard bootstrap class
$menu.find ('a[data-toggle="dropdown"]').removeAttr('data-toggle').removeAttr('data-target');
$menu
.find('> li > ul.dropdown-menu')
.prev().attr('data-toggle', 'dropdown').attr('data-target', '#')
.parent('li')
.addClass(function(){
return 'dropdown' + ($(this).data('level') > 1 ? ' dropdown-submenu' : '');
});
// inject into .t3-navbar-collapse
$menu.appendTo ($placeholder);
});
});