1
|
|
|
GLSR.tabs = function( options ) { |
|
|
|
|
2
|
|
|
this.options = x.extend( {}, this.defaults, options ); |
|
|
|
|
3
|
|
|
this.active = document.querySelector( 'input[name=_active_tab]' ); |
4
|
|
|
this.referrer = document.querySelector( 'input[name=_wp_http_referer]' ); |
5
|
|
|
this.tabs = document.querySelectorAll( this.options.tabSelector ); |
6
|
|
|
this.views = document.querySelectorAll( this.options.viewSelector ); |
7
|
|
|
if( !this.active || !this.referrer || !this.tabs || !this.views )return; |
|
|
|
|
8
|
|
|
this.init(); |
9
|
|
|
}; |
10
|
|
|
|
11
|
|
|
GLSR.tabs.prototype = { |
|
|
|
|
12
|
|
|
|
13
|
|
|
defaults: { |
14
|
|
|
tabSelector: '.glsr-nav-tab', |
15
|
|
|
viewSelector: '.glsr-nav-view', |
16
|
|
|
}, |
17
|
|
|
|
18
|
|
|
/** @return void */ |
19
|
|
|
init: function() { |
20
|
|
|
[].forEach.call( this.tabs, function( tab, index ) { |
21
|
|
|
var active = location.hash ? tab.getAttribute( 'href' ).slice(1) === location.hash.slice(2) : index === 0; |
22
|
|
|
if( active ) { |
23
|
|
|
this.setTab( tab ); |
24
|
|
|
} |
25
|
|
|
tab.addEventListener( 'click', this.onClick.bind( this )); |
26
|
|
|
tab.addEventListener( 'touchend', this.onClick.bind( this )); |
27
|
|
|
}.bind( this )); |
28
|
|
|
}, |
29
|
|
|
|
30
|
|
|
/** @return string */ |
31
|
|
|
getAction: function( bool ) { |
32
|
|
|
return bool ? 'add' : 'remove'; |
33
|
|
|
}, |
34
|
|
|
|
35
|
|
|
/** @return void */ |
36
|
|
|
onClick: function( ev ) { |
37
|
|
|
ev.preventDefault(); |
38
|
|
|
ev.target.blur(); |
39
|
|
|
this.setTab( ev.target ); |
40
|
|
|
location.hash = '!' + ev.target.getAttribute( 'href' ).slice(1); |
41
|
|
|
}, |
42
|
|
|
|
43
|
|
|
/** @return void */ |
44
|
|
|
setReferrer: function( index ) { |
45
|
|
|
var referrerUrl = this.referrer.value.split('#')[0] + '#!' + this.views[index].id; |
46
|
|
|
this.referrer.value = referrerUrl; |
47
|
|
|
}, |
48
|
|
|
|
49
|
|
|
/** @return void */ |
50
|
|
|
setTab: function( el ) { |
51
|
|
|
[].forEach.call( this.tabs, function( tab, index ) { |
52
|
|
|
var action = this.getAction( tab === el ); |
53
|
|
|
if( action === 'add' ) { |
54
|
|
|
this.active.value = this.views[index].id; |
55
|
|
|
this.setReferrer( index ); |
56
|
|
|
this.setView( index ); |
57
|
|
|
} |
58
|
|
|
tab.classList[action]( 'nav-tab-active' ); |
59
|
|
|
}.bind( this )); |
60
|
|
|
}, |
61
|
|
|
|
62
|
|
|
/** @return void */ |
63
|
|
|
setView: function( idx ) { |
64
|
|
|
[].forEach.call( this.views, function( view, index ) { |
65
|
|
|
var action = this.getAction( index !== idx ); |
66
|
|
|
view.classList[action]( 'ui-tabs-hide' ); |
67
|
|
|
}.bind( this )); |
68
|
|
|
}, |
69
|
|
|
}; |
70
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.