-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.hash-tabs.js
107 lines (92 loc) · 3.11 KB
/
jquery.hash-tabs.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
(function( $ ){
var methods;
methods = {
'init': function (options) {
//noinspection JSValidateTypes
options = $.extend({
selectorTriggers: '.trigger',
selectorTabs: '.tab',
hashPostfix: '_box',
clickCallback: function (targetTab) {},
openCallback: function (targetTab) {}
}, options);
return this.each(function () {
$(this).addClass('hash-tabs-wrapper');
var self = $(this);
var elements;
elements = {
self: self,
triggers: self.find(options.selectorTriggers),
tabs: self.find(options.selectorTabs)
};
elements.triggers.each(function(){
$(this).addClass('hash-trigger-wrapper');
var href = $(this).attr('href');
var index = $(this).index();
if ( !href || href == '#' ) {
$(this).attr('href', '#hash_tab' + index + options.hashPostfix);
$(elements.tabs.get(index)).attr('id', 'hash_tab' + index);
}
});
elements.tabs.each(function(){
$(this).addClass('hash-tab-wrapper');
var id = $(this).attr('id');
var index = $(this).index();
if ( !id ) {
var href = $(elements.triggers.get(index)).attr('href');
href = href.replace("#", "");
href = href.replace(options.hashPostfix, "");
$(this).attr('id', href);
}
});
if (window.location.hash) {
var hash = window.location.hash;
var href = "[href='" + hash + "']";
if (elements.triggers.filter(href).length > 0) {
elements.triggers.filter(href).hashTabs('switch', elements, options);
} else {
elements.triggers.first().hashTabs('switch', elements, options);
}
} else {
elements.triggers.first().hashTabs('switch', elements, options);
}
elements.triggers.on('click', function () {
var index = $(this).index();
options.clickCallback.call(this, elements.tabs.get(index));
$(this).hashTabs('switch', elements, options);
});
});
},
'switch': function (elements, options) {
return this.each(function () {
if ( $(this).parents('.hash-tab-wrapper') ) {
$(this).parents('.hash-tab-wrapper').each(function(){
var id = $(this).attr('id');
var href = "[href^='#" + id + "']";
$('.trigger' + href).trigger('click');
});
}
var hash = $(this).attr('href');
var href = "[href='" + hash + "']";
if (hash) {
elements.triggers.removeClass("active");
elements.tabs.removeClass("active");
elements.triggers.filter(href).addClass("active");
hash = hash.replace(options.hashPostfix, "");
elements.tabs.filter(hash).addClass("active");
options.openCallback.call(this, elements.tabs.filter(hash));
}
});
}
};
$.fn.hashTabs = function ( method ) {
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Метод ' + method + ' в jQuery.hashTabs не существует' );
return false;
}
};
})( jQuery );