@@ -8,19 +8,17 @@ License: MIT License (see MIT-LICENSE)
8
8
Note: If you change or improve on this script, please let us know by
9
9
emailing the author (above) with a link to your demo page.
10
10
------------------------------------------------------------------------------*/
11
- function TabInterface ( el , i ) {
11
+ function TabInterface ( _cabinet , _i ) {
12
12
// Public Properties
13
13
this . Version = '0.4.3' ; // version
14
14
15
15
// Private Properties
16
16
var
17
- _i = i , // incrementor
18
- _cabinet = el , // the "cabinet" element (container)
19
17
_active = false , // ID of the active "folder"
20
18
// the tab list
21
19
_index = document . createElement ( 'ul' ) ,
22
20
// prototype elements
23
- _els = {
21
+ _cabinets = {
24
22
div : document . createElement ( 'div' ) ,
25
23
li : document . createElement ( 'li' )
26
24
} ;
@@ -34,18 +32,18 @@ function TabInterface( el, i ){
34
32
arr , folder , tab , heading ;
35
33
36
34
// set the id
37
- _id = el . getAttribute ( 'id' ) || 'folder-' + _i ;
38
- if ( ! el . getAttribute ( 'id' ) ) el . setAttribute ( 'id' , _id ) ;
35
+ _id = _cabinet . getAttribute ( 'id' ) || 'folder-' + _i ;
36
+ if ( ! _cabinet . getAttribute ( 'id' ) ) _cabinet . setAttribute ( 'id' , _id ) ;
39
37
40
38
// set the ARIA roles
41
- el . parentNode . setAttribute ( 'role' , 'application' ) ;
42
- el . setAttribute ( 'role' , 'presentation' ) ;
39
+ _cabinet . parentNode . setAttribute ( 'role' , 'application' ) ;
40
+ _cabinet . setAttribute ( 'role' , 'presentation' ) ;
43
41
_index . setAttribute ( 'role' , 'tablist' ) ;
44
- _els . div . setAttribute ( 'role' , 'tabpanel' ) ;
45
- _els . div . setAttribute ( 'aria-hidden' , 'true' ) ;
46
- _els . li . setAttribute ( 'role' , 'tab' ) ;
47
- _els . li . setAttribute ( 'aria-selected' , 'false' ) ;
48
- _els . li . setAttribute ( 'tabindex' , '-1' ) ;
42
+ _cabinets . div . setAttribute ( 'role' , 'tabpanel' ) ;
43
+ _cabinets . div . setAttribute ( 'aria-hidden' , 'true' ) ;
44
+ _cabinets . li . setAttribute ( 'role' , 'tab' ) ;
45
+ _cabinets . li . setAttribute ( 'aria-selected' , 'false' ) ;
46
+ _cabinets . li . setAttribute ( 'tabindex' , '-1' ) ;
49
47
50
48
// trim whitespace
51
49
node = _cabinet . firstChild ;
@@ -65,26 +63,29 @@ function TabInterface( el, i ){
65
63
}
66
64
}
67
65
66
+ // flip it on
67
+ addClassName ( _cabinet , 'tabbed-on' ) ;
68
+ removeClassName ( _cabinet , 'tabbed' ) ;
68
69
// establish the folders
69
70
rexp = new RegExp ( '<(' + _tag + ')' , 'ig' ) ;
70
71
arr = _cabinet . innerHTML . replace ( rexp , "||||<$1" ) . split ( '||||' ) ;
71
72
arr . shift ( ) ;
72
73
_cabinet . innerHTML = '' ;
73
- removeClassName ( _cabinet , 'tabbed' ) ;
74
- addClassName ( _cabinet , 'tabbed-on ' ) ;
74
+ // add the index
75
+ addClassName ( _index , 'index ' ) ;
75
76
_cabinet . appendChild ( _index ) ;
77
+ // re-insert the chunks
76
78
for ( i = 0 , len = arr . length ; i < len ; i ++ ) {
77
79
// build the div
78
- folder = _els . div . cloneNode ( true ) ;
80
+ folder = _cabinets . div . cloneNode ( true ) ;
79
81
addClassName ( folder , 'folder' ) ;
80
82
folder . setAttribute ( 'id' , _id + '-' + i ) ;
81
83
folder . setAttribute ( 'aria-labelledby' , _id + '-' + i + '-tab' ) ;
82
84
folder . innerHTML = arr [ i ] ;
83
85
_cabinet . appendChild ( folder ) ;
84
86
// build the tab
85
- tab = _els . li . cloneNode ( true ) ;
86
- tab . folder = folder . getAttribute ( 'id' ) ;
87
- tab . setAttribute ( 'id' , tab . folder + '-tab' ) ;
87
+ tab = _cabinets . li . cloneNode ( true ) ;
88
+ tab . setAttribute ( 'id' , _id + '-' + i + '-tab' ) ;
88
89
tab . onclick = swap ; // set the action
89
90
tab . onkeydown = moveFocus ; // add the keyboard control
90
91
heading = folder . getElementsByTagName ( _tag ) [ 0 ] ;
@@ -102,11 +103,9 @@ function TabInterface( el, i ){
102
103
tab . setAttribute ( 'aria-selected' , 'true' ) ;
103
104
tab . setAttribute ( 'tabindex' , '0' ) ;
104
105
_active = folder . getAttribute ( 'id' ) ;
105
- addClassName ( tab , 'active-tab ' ) ;
106
+ addClassName ( tab , 'active' ) ;
106
107
}
107
108
}
108
- // add the index
109
- addClassName ( _index , 'tab-list' ) ;
110
109
}
111
110
function swap ( e )
112
111
{
@@ -116,14 +115,14 @@ function TabInterface( el, i ){
116
115
old_folder = document . getElementById ( _active ) ,
117
116
new_folder ;
118
117
tab = getTab ( tab ) ;
119
- new_folder = document . getElementById ( tab . folder ) ;
120
- removeClassName ( document . getElementById ( _active + '-tab' ) , 'active-tab ' ) ;
118
+ new_folder = document . getElementById ( tab . getAttribute ( 'id' ) . replace ( '-tab' , '' ) ) ;
119
+ removeClassName ( document . getElementById ( _active + '-tab' ) , 'active' ) ;
121
120
removeClassName ( old_folder , 'visible' ) ;
122
121
old_folder . setAttribute ( 'aria-hidden' , 'true' ) ;
123
- addClassName ( tab , 'active-tab ' ) ;
122
+ addClassName ( tab , 'active' ) ;
124
123
addClassName ( new_folder , 'visible' ) ;
125
124
new_folder . setAttribute ( 'aria-hidden' , 'false' ) ;
126
- _active = tab . folder ;
125
+ _active = new_folder . getAttribute ( 'id' ) ;
127
126
}
128
127
function addClassName ( e , c )
129
128
{
0 commit comments