|
62 | 62 | var options = $.extend(defaults, options); |
63 | 63 |
|
64 | 64 | return this.each(function() { |
| 65 | + var $this = $(this); |
| 66 | + if ( $this.data( 'tabslet-init' ) ) return; |
65 | 67 |
|
66 | | - var $this = $(this), _cache_li = [], _cache_div = []; |
| 68 | + var _cache_li = []; |
| 69 | + var _cache_div = []; |
67 | 70 | var _container = options.container ? $(options.container) : $this; |
68 | 71 | var _tabs = _container.find('> div'); |
69 | 72 |
|
|
73 | 76 | // Autorotate |
74 | 77 | var elements = $this.find('> ul > li'), i = options.active - 1; // ungly |
75 | 78 |
|
76 | | - if ( !$this.data( 'tabslet-init' ) ) { |
| 79 | + $this.data( 'tabslet-init', true ); |
77 | 80 |
|
78 | | - $this.data( 'tabslet-init', true ); |
| 81 | + $this.opts = []; |
79 | 82 |
|
80 | | - $this.opts = []; |
| 83 | + $.map( ['mouseevent', 'activeclass', 'attribute', 'animation', 'autorotate', 'deeplinking', 'pauseonhover', 'delay', 'container'], function( val, i ) { |
| 84 | + $this.opts[val] = $this.data(val) || options[val]; |
| 85 | + }); |
81 | 86 |
|
82 | | - $.map( ['mouseevent', 'activeclass', 'attribute', 'animation', 'autorotate', 'deeplinking', 'pauseonhover', 'delay', 'container'], function( val, i ) { |
83 | | - $this.opts[val] = $this.data(val) || options[val]; |
84 | | - }); |
85 | | - |
86 | | - $this.opts['active'] = $this.opts.deeplinking ? deep_link() : ( $this.data('active') || options.active ) |
87 | | - |
88 | | - _tabs.hide(); |
89 | | - |
90 | | - if ( $this.opts.active ) { |
91 | | - _tabs.eq($this.opts.active - 1).show(); |
92 | | - elements.eq($this.opts.active - 1).addClass(options.activeclass); |
93 | | - } |
| 87 | + $this.opts['active'] = $this.opts.deeplinking ? deep_link() : ( $this.data('active') || options.active ) |
94 | 88 |
|
95 | | - var fn = eval( |
| 89 | + _tabs.hide(); |
96 | 90 |
|
97 | | - function(e, tab) { |
98 | | - var _this = tab ? elements.find('a[' + $this.opts.attribute + '="' + tab +'"]').parent() : $(this); |
| 91 | + if ( $this.opts.active ) { |
| 92 | + _tabs.eq($this.opts.active - 1).show(); |
| 93 | + elements.eq($this.opts.active - 1).addClass(options.activeclass); |
| 94 | + } |
99 | 95 |
|
100 | | - _this.trigger('_before'); |
| 96 | + var fn = eval( |
101 | 97 |
|
102 | | - elements.removeClass(options.activeclass); |
103 | | - _this.addClass(options.activeclass); |
104 | | - _tabs.hide(); |
| 98 | + function(e, tab) { |
| 99 | + var _this = tab ? elements.find('a[' + $this.opts.attribute + '="' + tab +'"]').parent() : $(this); |
105 | 100 |
|
106 | | - i = elements.index(_this); |
| 101 | + _this.trigger('_before'); |
107 | 102 |
|
108 | | - var currentTab = tab || _this.find('a').attr($this.opts.attribute); |
| 103 | + elements.removeClass(options.activeclass); |
| 104 | + _this.addClass(options.activeclass); |
| 105 | + _tabs.hide(); |
109 | 106 |
|
110 | | - if ($this.opts.deeplinking) location.hash = currentTab; |
| 107 | + i = elements.index(_this); |
111 | 108 |
|
112 | | - if ($this.opts.animation) { |
| 109 | + var currentTab = tab || _this.find('a').attr($this.opts.attribute); |
113 | 110 |
|
114 | | - _container.find(currentTab).animate( { opacity: 'show' }, 'slow', function() { |
115 | | - _this.trigger('_after'); |
116 | | - }); |
| 111 | + if ($this.opts.deeplinking) location.hash = currentTab; |
117 | 112 |
|
118 | | - } else { |
| 113 | + if ($this.opts.animation) { |
119 | 114 |
|
120 | | - _container.find(currentTab).show(); |
| 115 | + _container.find(currentTab).animate( { opacity: 'show' }, 'slow', function() { |
121 | 116 | _this.trigger('_after'); |
| 117 | + }); |
122 | 118 |
|
123 | | - } |
| 119 | + } else { |
124 | 120 |
|
125 | | - return false; |
| 121 | + _container.find(currentTab).show(); |
| 122 | + _this.trigger('_after'); |
126 | 123 |
|
127 | 124 | } |
128 | 125 |
|
129 | | - ); |
| 126 | + return false; |
130 | 127 |
|
131 | | - var init = eval("elements." + $this.opts.mouseevent + "(fn)"); |
| 128 | + } |
132 | 129 |
|
133 | | - init; |
| 130 | + ); |
134 | 131 |
|
135 | | - var t; |
| 132 | + var init = eval("elements." + $this.opts.mouseevent + "(fn)"); |
136 | 133 |
|
137 | | - var forward = function() { |
| 134 | + init; |
138 | 135 |
|
139 | | - i = ++i % elements.length; // wrap around |
| 136 | + var t; |
140 | 137 |
|
141 | | - $this.opts.mouseevent == 'hover' ? elements.eq(i).trigger('mouseover') : elements.eq(i).click(); |
| 138 | + var forward = function() { |
142 | 139 |
|
143 | | - if ($this.opts.autorotate) { |
| 140 | + i = ++i % elements.length; // wrap around |
144 | 141 |
|
145 | | - clearTimeout(t); |
| 142 | + $this.opts.mouseevent == 'hover' ? elements.eq(i).trigger('mouseover') : elements.eq(i).click(); |
146 | 143 |
|
147 | | - t = setTimeout(forward, $this.opts.delay); |
| 144 | + if ($this.opts.autorotate) { |
148 | 145 |
|
149 | | - $this.mouseover(function () { |
| 146 | + clearTimeout(t); |
150 | 147 |
|
151 | | - if ($this.opts.pauseonhover) clearTimeout(t); |
| 148 | + t = setTimeout(forward, $this.opts.delay); |
152 | 149 |
|
| 150 | + if ($this.opts.pauseonhover) { |
| 151 | + $this.mouseover(function () { |
| 152 | + clearTimeout(t); |
153 | 153 | }); |
154 | | - |
155 | 154 | } |
156 | 155 |
|
157 | 156 | } |
158 | 157 |
|
159 | | - if ($this.opts.autorotate) { |
| 158 | + } |
160 | 159 |
|
161 | | - t = setTimeout(forward, $this.opts.delay); |
| 160 | + if ($this.opts.autorotate) { |
162 | 161 |
|
163 | | - $this.hover(function() { |
| 162 | + t = setTimeout(forward, $this.opts.delay); |
164 | 163 |
|
165 | | - if ($this.opts.pauseonhover) clearTimeout(t); |
| 164 | + $this.hover(function() { |
166 | 165 |
|
167 | | - }, function() { |
| 166 | + if ($this.opts.pauseonhover) clearTimeout(t); |
168 | 167 |
|
169 | | - t = setTimeout(forward, $this.opts.delay); |
| 168 | + }, function() { |
170 | 169 |
|
171 | | - }); |
| 170 | + t = setTimeout(forward, $this.opts.delay); |
172 | 171 |
|
173 | | - if ($this.opts.pauseonhover) $this.on( "mouseleave", function() { clearTimeout(t); t = setTimeout(forward, $this.opts.delay); }); |
| 172 | + }); |
174 | 173 |
|
175 | | - } |
| 174 | + if ($this.opts.pauseonhover) $this.on( "mouseleave", function() { clearTimeout(t); t = setTimeout(forward, $this.opts.delay); }); |
176 | 175 |
|
177 | | - function deep_link() { |
| 176 | + } |
178 | 177 |
|
179 | | - var ids = []; |
| 178 | + function deep_link() { |
180 | 179 |
|
181 | | - elements.find('a').each(function() { ids.push($(this).attr($this.opts.attribute)); }); |
| 180 | + var ids = []; |
182 | 181 |
|
183 | | - var index = $.inArray(location.hash, ids) |
| 182 | + elements.find('a').each(function() { ids.push($(this).attr($this.opts.attribute)); }); |
184 | 183 |
|
185 | | - if (index > -1) { |
| 184 | + var index = $.inArray(location.hash, ids) |
186 | 185 |
|
187 | | - return index + 1 |
| 186 | + if (index > -1) { |
188 | 187 |
|
189 | | - } else { |
| 188 | + return index + 1 |
190 | 189 |
|
191 | | - return ($this.data('active') || options.active) |
| 190 | + } else { |
192 | 191 |
|
193 | | - } |
| 192 | + return ($this.data('active') || options.active) |
194 | 193 |
|
195 | 194 | } |
196 | 195 |
|
197 | | - var move = function(direction) { |
| 196 | + } |
198 | 197 |
|
199 | | - if (direction == 'forward') i = ++i % elements.length; // wrap around |
| 198 | + var move = function(direction) { |
200 | 199 |
|
201 | | - if (direction == 'backward') i = --i % elements.length; // wrap around |
| 200 | + if (direction == 'forward') i = ++i % elements.length; // wrap around |
202 | 201 |
|
203 | | - elements.eq(i).click(); |
| 202 | + if (direction == 'backward') i = --i % elements.length; // wrap around |
204 | 203 |
|
205 | | - } |
| 204 | + elements.eq(i).click(); |
206 | 205 |
|
207 | | - $this.find(options.controls.next).click(function() { |
208 | | - move('forward'); |
209 | | - }); |
| 206 | + } |
210 | 207 |
|
211 | | - $this.find(options.controls.prev).click(function() { |
212 | | - move('backward'); |
213 | | - }); |
| 208 | + $this.find(options.controls.next).click(function() { |
| 209 | + move('forward'); |
| 210 | + }); |
214 | 211 |
|
215 | | - $this.on ('show', function(e, tab) { |
216 | | - fn(e, tab); |
217 | | - }); |
| 212 | + $this.find(options.controls.prev).click(function() { |
| 213 | + move('backward'); |
| 214 | + }); |
218 | 215 |
|
219 | | - $this.on ('next', function() { |
220 | | - move('forward'); |
221 | | - }); |
| 216 | + $this.on ('show', function(e, tab) { |
| 217 | + fn(e, tab); |
| 218 | + }); |
222 | 219 |
|
223 | | - $this.on ('prev', function() { |
224 | | - move('backward'); |
225 | | - }); |
| 220 | + $this.on ('next', function() { |
| 221 | + move('forward'); |
| 222 | + }); |
226 | 223 |
|
227 | | - $this.on ('destroy', function() { |
228 | | - $(this) |
229 | | - .removeData() |
230 | | - .find('> ul li').each( function(i) { |
231 | | - $(this).removeClass(options.activeclass); |
232 | | - }); |
233 | | - _tabs.each( function(i) { |
234 | | - $(this).removeAttr('style').css( 'display', _cache_div[i] ); |
| 224 | + $this.on ('prev', function() { |
| 225 | + move('backward'); |
| 226 | + }); |
| 227 | + |
| 228 | + $this.on ('destroy', function() { |
| 229 | + $(this) |
| 230 | + .removeData() |
| 231 | + .find('> ul li').each( function(i) { |
| 232 | + $(this).removeClass(options.activeclass); |
235 | 233 | }); |
| 234 | + _tabs.each( function(i) { |
| 235 | + $(this).removeAttr('style').css( 'display', _cache_div[i] ); |
236 | 236 | }); |
237 | | - |
238 | | - } |
239 | | - |
| 237 | + }); |
240 | 238 | }); |
241 | 239 |
|
242 | 240 | }; |
|
0 commit comments