Skip to content

Commit ae4a652

Browse files
Improvements in the js code
1 parent 1186bfe commit ae4a652

3 files changed

Lines changed: 98 additions & 98 deletions

File tree

demo/javascripts/vendor/jquery.tabslet.min.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@
3737
3838
*/
3939

40-
!function($,window,undefined){"use strict";$.fn.tabslet=function(options){var defaults={mouseevent:"click",activeclass:"active",attribute:"href",animation:!1,autorotate:!1,deeplinking:!1,pauseonhover:!0,delay:2e3,active:1,container:!1,controls:{prev:".prev",next:".next"}},options=$.extend(defaults,options);return this.each(function(){function deep_link(){var t=[];elements.find("a").each(function(){t.push($(this).attr($this.opts.attribute))});var e=$.inArray(location.hash,t);return e>-1?e+1:$this.data("active")||options.active}var $this=$(this),_cache_li=[],_cache_div=[],_container=options.container?$(options.container):$this,_tabs=_container.find("> div");_tabs.each(function(){_cache_div.push($(this).css("display"))});var elements=$this.find("> ul > li"),i=options.active-1;if(!$this.data("tabslet-init")){$this.data("tabslet-init",!0),$this.opts=[],$.map(["mouseevent","activeclass","attribute","animation","autorotate","deeplinking","pauseonhover","delay","container"],function(t){$this.opts[t]=$this.data(t)||options[t]}),$this.opts.active=$this.opts.deeplinking?deep_link():$this.data("active")||options.active,_tabs.hide(),$this.opts.active&&(_tabs.eq($this.opts.active-1).show(),elements.eq($this.opts.active-1).addClass(options.activeclass));var fn=eval(function(t,e){var s=e?elements.find("a["+$this.opts.attribute+'="'+e+'"]').parent():$(this);s.trigger("_before"),elements.removeClass(options.activeclass),s.addClass(options.activeclass),_tabs.hide(),i=elements.index(s);var o=e||s.find("a").attr($this.opts.attribute);return $this.opts.deeplinking&&(location.hash=o),$this.opts.animation?_container.find(o).animate({opacity:"show"},"slow",function(){s.trigger("_after")}):(_container.find(o).show(),s.trigger("_after")),!1}),init=eval("elements."+$this.opts.mouseevent+"(fn)"),t,forward=function(){i=++i%elements.length,"hover"==$this.opts.mouseevent?elements.eq(i).trigger("mouseover"):elements.eq(i).click(),$this.opts.autorotate&&(clearTimeout(t),t=setTimeout(forward,$this.opts.delay),$this.mouseover(function(){$this.opts.pauseonhover&&clearTimeout(t)}))};$this.opts.autorotate&&(t=setTimeout(forward,$this.opts.delay),$this.hover(function(){$this.opts.pauseonhover&&clearTimeout(t)},function(){t=setTimeout(forward,$this.opts.delay)}),$this.opts.pauseonhover&&$this.on("mouseleave",function(){clearTimeout(t),t=setTimeout(forward,$this.opts.delay)}));var move=function(t){"forward"==t&&(i=++i%elements.length),"backward"==t&&(i=--i%elements.length),elements.eq(i).click()};$this.find(options.controls.next).click(function(){move("forward")}),$this.find(options.controls.prev).click(function(){move("backward")}),$this.on("show",function(t,e){fn(t,e)}),$this.on("next",function(){move("forward")}),$this.on("prev",function(){move("backward")}),$this.on("destroy",function(){$(this).removeData().find("> ul li").each(function(){$(this).removeClass(options.activeclass)}),_tabs.each(function(t){$(this).removeAttr("style").css("display",_cache_div[t])})})}})},$(document).ready(function(){$('[data-toggle="tabslet"]').tabslet()})}(jQuery);
40+
41+
(function($,window,undefined){"use strict";$.fn.tabslet=function(options){var defaults={mouseevent:"click",activeclass:"active",attribute:"href",animation:false,autorotate:false,deeplinking:false,pauseonhover:true,delay:2e3,active:1,container:false,controls:{prev:".prev",next:".next"}};var options=$.extend(defaults,options);return this.each(function(){var $this=$(this);if($this.data("tabslet-init"))return;var _cache_li=[];var _cache_div=[];var _container=options.container?$(options.container):$this;var _tabs=_container.find("> div");_tabs.each(function(){_cache_div.push($(this).css("display"))});var elements=$this.find("> ul > li"),i=options.active-1;$this.data("tabslet-init",true);$this.opts=[];$.map(["mouseevent","activeclass","attribute","animation","autorotate","deeplinking","pauseonhover","delay","container"],function(val,i){$this.opts[val]=$this.data(val)||options[val]});$this.opts["active"]=$this.opts.deeplinking?deep_link():$this.data("active")||options.active;_tabs.hide();if($this.opts.active){_tabs.eq($this.opts.active-1).show();elements.eq($this.opts.active-1).addClass(options.activeclass)}var fn=eval(function(e,tab){var _this=tab?elements.find("a["+$this.opts.attribute+'="'+tab+'"]').parent():$(this);_this.trigger("_before");elements.removeClass(options.activeclass);_this.addClass(options.activeclass);_tabs.hide();i=elements.index(_this);var currentTab=tab||_this.find("a").attr($this.opts.attribute);if($this.opts.deeplinking)location.hash=currentTab;if($this.opts.animation){_container.find(currentTab).animate({opacity:"show"},"slow",function(){_this.trigger("_after")})}else{_container.find(currentTab).show();_this.trigger("_after")}return false});var init=eval("elements."+$this.opts.mouseevent+"(fn)");init;var t;var forward=function(){i=++i%elements.length;$this.opts.mouseevent=="hover"?elements.eq(i).trigger("mouseover"):elements.eq(i).click();if($this.opts.autorotate){clearTimeout(t);t=setTimeout(forward,$this.opts.delay);if($this.opts.pauseonhover){$this.mouseover(function(){clearTimeout(t)})}}};if($this.opts.autorotate){t=setTimeout(forward,$this.opts.delay);$this.hover(function(){if($this.opts.pauseonhover)clearTimeout(t)},function(){t=setTimeout(forward,$this.opts.delay)});if($this.opts.pauseonhover)$this.on("mouseleave",function(){clearTimeout(t);t=setTimeout(forward,$this.opts.delay)})}function deep_link(){var ids=[];elements.find("a").each(function(){ids.push($(this).attr($this.opts.attribute))});var index=$.inArray(location.hash,ids);if(index>-1){return index+1}else{return $this.data("active")||options.active}}var move=function(direction){if(direction=="forward")i=++i%elements.length;if(direction=="backward")i=--i%elements.length;elements.eq(i).click()};$this.find(options.controls.next).click(function(){move("forward")});$this.find(options.controls.prev).click(function(){move("backward")});$this.on("show",function(e,tab){fn(e,tab)});$this.on("next",function(){move("forward")});$this.on("prev",function(){move("backward")});$this.on("destroy",function(){$(this).removeData().find("> ul li").each(function(i){$(this).removeClass(options.activeclass)});_tabs.each(function(i){$(this).removeAttr("style").css("display",_cache_div[i])})})})};$(document).ready(function(){$('[data-toggle="tabslet"]').tabslet()})})(jQuery);

jquery.tabslet.js

Lines changed: 94 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@
6262
var options = $.extend(defaults, options);
6363

6464
return this.each(function() {
65+
var $this = $(this);
66+
if ( $this.data( 'tabslet-init' ) ) return;
6567

66-
var $this = $(this), _cache_li = [], _cache_div = [];
68+
var _cache_li = [];
69+
var _cache_div = [];
6770
var _container = options.container ? $(options.container) : $this;
6871
var _tabs = _container.find('> div');
6972

@@ -73,170 +76,165 @@
7376
// Autorotate
7477
var elements = $this.find('> ul > li'), i = options.active - 1; // ungly
7578

76-
if ( !$this.data( 'tabslet-init' ) ) {
79+
$this.data( 'tabslet-init', true );
7780

78-
$this.data( 'tabslet-init', true );
81+
$this.opts = [];
7982

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+
});
8186

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 )
9488

95-
var fn = eval(
89+
_tabs.hide();
9690

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+
}
9995

100-
_this.trigger('_before');
96+
var fn = eval(
10197

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);
105100

106-
i = elements.index(_this);
101+
_this.trigger('_before');
107102

108-
var currentTab = tab || _this.find('a').attr($this.opts.attribute);
103+
elements.removeClass(options.activeclass);
104+
_this.addClass(options.activeclass);
105+
_tabs.hide();
109106

110-
if ($this.opts.deeplinking) location.hash = currentTab;
107+
i = elements.index(_this);
111108

112-
if ($this.opts.animation) {
109+
var currentTab = tab || _this.find('a').attr($this.opts.attribute);
113110

114-
_container.find(currentTab).animate( { opacity: 'show' }, 'slow', function() {
115-
_this.trigger('_after');
116-
});
111+
if ($this.opts.deeplinking) location.hash = currentTab;
117112

118-
} else {
113+
if ($this.opts.animation) {
119114

120-
_container.find(currentTab).show();
115+
_container.find(currentTab).animate( { opacity: 'show' }, 'slow', function() {
121116
_this.trigger('_after');
117+
});
122118

123-
}
119+
} else {
124120

125-
return false;
121+
_container.find(currentTab).show();
122+
_this.trigger('_after');
126123

127124
}
128125

129-
);
126+
return false;
130127

131-
var init = eval("elements." + $this.opts.mouseevent + "(fn)");
128+
}
132129

133-
init;
130+
);
134131

135-
var t;
132+
var init = eval("elements." + $this.opts.mouseevent + "(fn)");
136133

137-
var forward = function() {
134+
init;
138135

139-
i = ++i % elements.length; // wrap around
136+
var t;
140137

141-
$this.opts.mouseevent == 'hover' ? elements.eq(i).trigger('mouseover') : elements.eq(i).click();
138+
var forward = function() {
142139

143-
if ($this.opts.autorotate) {
140+
i = ++i % elements.length; // wrap around
144141

145-
clearTimeout(t);
142+
$this.opts.mouseevent == 'hover' ? elements.eq(i).trigger('mouseover') : elements.eq(i).click();
146143

147-
t = setTimeout(forward, $this.opts.delay);
144+
if ($this.opts.autorotate) {
148145

149-
$this.mouseover(function () {
146+
clearTimeout(t);
150147

151-
if ($this.opts.pauseonhover) clearTimeout(t);
148+
t = setTimeout(forward, $this.opts.delay);
152149

150+
if ($this.opts.pauseonhover) {
151+
$this.mouseover(function () {
152+
clearTimeout(t);
153153
});
154-
155154
}
156155

157156
}
158157

159-
if ($this.opts.autorotate) {
158+
}
160159

161-
t = setTimeout(forward, $this.opts.delay);
160+
if ($this.opts.autorotate) {
162161

163-
$this.hover(function() {
162+
t = setTimeout(forward, $this.opts.delay);
164163

165-
if ($this.opts.pauseonhover) clearTimeout(t);
164+
$this.hover(function() {
166165

167-
}, function() {
166+
if ($this.opts.pauseonhover) clearTimeout(t);
168167

169-
t = setTimeout(forward, $this.opts.delay);
168+
}, function() {
170169

171-
});
170+
t = setTimeout(forward, $this.opts.delay);
172171

173-
if ($this.opts.pauseonhover) $this.on( "mouseleave", function() { clearTimeout(t); t = setTimeout(forward, $this.opts.delay); });
172+
});
174173

175-
}
174+
if ($this.opts.pauseonhover) $this.on( "mouseleave", function() { clearTimeout(t); t = setTimeout(forward, $this.opts.delay); });
176175

177-
function deep_link() {
176+
}
178177

179-
var ids = [];
178+
function deep_link() {
180179

181-
elements.find('a').each(function() { ids.push($(this).attr($this.opts.attribute)); });
180+
var ids = [];
182181

183-
var index = $.inArray(location.hash, ids)
182+
elements.find('a').each(function() { ids.push($(this).attr($this.opts.attribute)); });
184183

185-
if (index > -1) {
184+
var index = $.inArray(location.hash, ids)
186185

187-
return index + 1
186+
if (index > -1) {
188187

189-
} else {
188+
return index + 1
190189

191-
return ($this.data('active') || options.active)
190+
} else {
192191

193-
}
192+
return ($this.data('active') || options.active)
194193

195194
}
196195

197-
var move = function(direction) {
196+
}
198197

199-
if (direction == 'forward') i = ++i % elements.length; // wrap around
198+
var move = function(direction) {
200199

201-
if (direction == 'backward') i = --i % elements.length; // wrap around
200+
if (direction == 'forward') i = ++i % elements.length; // wrap around
202201

203-
elements.eq(i).click();
202+
if (direction == 'backward') i = --i % elements.length; // wrap around
204203

205-
}
204+
elements.eq(i).click();
206205

207-
$this.find(options.controls.next).click(function() {
208-
move('forward');
209-
});
206+
}
210207

211-
$this.find(options.controls.prev).click(function() {
212-
move('backward');
213-
});
208+
$this.find(options.controls.next).click(function() {
209+
move('forward');
210+
});
214211

215-
$this.on ('show', function(e, tab) {
216-
fn(e, tab);
217-
});
212+
$this.find(options.controls.prev).click(function() {
213+
move('backward');
214+
});
218215

219-
$this.on ('next', function() {
220-
move('forward');
221-
});
216+
$this.on ('show', function(e, tab) {
217+
fn(e, tab);
218+
});
222219

223-
$this.on ('prev', function() {
224-
move('backward');
225-
});
220+
$this.on ('next', function() {
221+
move('forward');
222+
});
226223

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);
235233
});
234+
_tabs.each( function(i) {
235+
$(this).removeAttr('style').css( 'display', _cache_div[i] );
236236
});
237-
238-
}
239-
237+
});
240238
});
241239

242240
};

0 commit comments

Comments
 (0)