Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a simple refactoring.
/*
* Easy Paginate 1.0 - jQuery plugin
* written by Alen Grakalic
* http://cssglobe.com/
*
* Copyright (c) 2011 Alen Grakalic (http://cssglobe.com)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* Built for jQuery library
* http://jquery.com
*
*/
(function($) {
$.fn.easyPaginate = function(options){
var defaults = {
step: 4,
delay: 100,
numeric: true,
nextprev: true,
auto:false,
loop:false,
pause:4000,
clickstop:true,
controls: 'pagination',
current: 'current',
item: 'sprite_icones item',
randomstart: false
};
var options = $.extend(defaults, options);
var step = options.step;
var lower, upper;
var children = $(this).children();
var count = children.length;
var obj, next, prev;
var pages = Math.floor(count/step);
var page = (options.randomstart) ? Math.floor(Math.random()*pages)+1 : 1;
var clicked = false;
var stepbutton = function(event) {
clicked = true;
show(event);
}
// varias porquisses... precisa de refactoring!
function show(event) {
if (event && $(event.delegateTarget).hasClass('off')) {
return;
}
if (event && $(event.delegateTarget).hasClass('next')) {
page++;
}
if (event && $(event.delegateTarget).hasClass('prev')) {
page--;
}
clearTimeout(window.carouselTimeout);
lower = ((page-1) * step);
upper = lower+step;
$(children).each(function(i){
var child = $(this);
child.hide();
if(i>=lower && i<upper) {
setTimeout(function() {
child.fadeIn('fast');
},
( i-( Math.floor(i/step) * step) )*options.delay
);
}
if(options.nextprev){
if(upper >= count) {
next.addClass('off');
} else {
next.removeClass('off');
};
if(lower >= 1) {
prev.removeClass('off');
} else {
prev.addClass('off');
};
};
});
$('li','#'+ options.controls).removeClass(options.current);
$('li[data-index="'+page+'"]','#'+ options.controls).addClass(options.current);
if(options.auto){
if(options.clickstop && clicked){}else{ window.carouselTimeout = setTimeout(auto,options.pause); };
};
};
function auto(){
if(options.loop) if(upper >= count){ page=0; show(); }
if(upper < count){ page++; show(); }
};
this.each(function(){
obj = this;
if(count>step){
if((count/step) > pages) pages++;
var ol = $('<ol id="'+ options.controls +'"></ol>').insertAfter(obj);
if(options.nextprev){
prev = $('<li class="sprite_icones arrow prev"></li>')
.appendTo(ol)
.on('click.prev', stepbutton);
};
if(options.numeric){
for(var i=1;i<=pages;i++){
$('<li data-index="'+ i +'" class="'+ options.item + '"></li>')
.appendTo(ol)
.click(function(){
clicked = true;
page = $(this).attr('data-index');
show();
});
};
};
if(options.nextprev){
next = $('<li class="sprite_icones arrow next"></li>')
.appendTo(ol)
.on('click.next', stepbutton);
};
show();
};
});
window.cleanCarouselTimeout = function () {
$(children).fadeIn('fast');
clearTimeout(window.carouselTimeout);
}
};
})(jQuery);
(function ($) {
$.fn.paginator = function (config) {
var items = $(this).children(),
pages = Math.floor(items.length/config.step),
currentPage = 0,
self = this,
$paging = $('<ol id="pagination"/>');
items.each(function () {
$(this).hide();
});
function display (items, currentPage) {
$.each(items, function () {
$(this).hide();
});
var offset = currentPage * pages;
var itemsToDisplay = items.splice(offset, config.step);
$.each(itemsToDisplay, function () {
$(this).fadeIn('fast');
});
}
function pageClick(event) {
$(event.target).parent().children().removeClass('current');
$(event.target).addClass('current');
var page = $(event.target).prop('data-index');
display(items.slice(0), page);
}
function buildPaging() {
for (var i = 0; i < pages; i++) {
var $li = $('<li class="sprite_icones item" />').bind('click', pageClick);
$li.prop('data-index', i);
$paging.append($li);
}
$paging.insertAfter(self);
}
buildPaging();
display(items.slice(0), 0);
}
}(jQuery))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.