Skip to content

Instantly share code, notes, and snippets.

@6r1d
Created November 11, 2013 04:18
Show Gist options
  • Save 6r1d/7407790 to your computer and use it in GitHub Desktop.
Save 6r1d/7407790 to your computer and use it in GitHub Desktop.
Pagination debug
Backbone.View.extend({
// root view DOM-element
tagName: "div",
// events for root view DOM-element
events: {
"click .go_back": "back",
"click .go_forward": "forward",
"click .active a": "fire",
"click a.page": "change_page"
},
// This function is executed
// when view is created
initialize: function(options) {
var self = this;
if (options === undefined) {
options = {};
}
this.options = options;
},
// proceeds to one page back
back: function() {
if (this.options.position > this.options.first_page) {
this.set_page(parseInt(this.options.position) - 1);
}
},
// proceeds to one page forward
forward: function() {
if (this.options.position < this.options.last_page) {
this.set_page(parseInt(this.options.position) + 1);
}
},
// shows current page value
fire: function() {
this.set_page(this.options.position);
},
// page click event handler
change_page: function(e) {
this.set_page($(e.target).html());
},
// sets current page value
set_page: function(page) {
this.options.position = page;
this.$el.find("li.active a").html(page);
this.trigger('page_change', {
"page": page,
"limit": this.options.record_limit
});
this.render();
},
// renders page list:
render: function() {
var pages,
options = this.options,
page_list = [],
last_page_length;
switch (options.mode) {
case "centered":
// * using usual centered mode
page_list = this.centered_pagination(
options.first_page,
options.last_page,
parseInt(options.position),
options.radius
);
break;
case "advanced":
// * using advanced mode
page_list = this.advanced_pagination(
options.first_page,
options.last_page,
parseInt(options.position),
options.edge_radius,
options.position_radius
);
break;
}
// Render page template
pages = JST.bootstrap_pagination_plist({
"pages": page_list,
"current": this.options.position
});
// Replace element contents with new pages
this.$el.html("").append(pages).find("li");
},
// ### Basic centered pagination page list generator
centered_pagination: function(first_page, last_page, position, radius) {
// * first_page - page, where page list starts
// * last_page - page, where page list ends
// * position - active page
// * radius - amount of pages around current
// For testing, please use:
//
// var test_pagination = new window.structuredUI.Pagination(), i;
// for (i = -10; i <= 10; i++) {
// console.log(test_pagination.centered_pagination(-10, 10, i, 2));
// }
var pages = [],
i,
range_gen = function(start, end, current) {
var pages = [];
for (i = start; i < end; i++) {
pages.push(i);
}
return pages;
};
// Page list generation, where:
switch (true) {
// - page is located near the center
case (position - radius > first_page && position + radius <= last_page):
pages = range_gen(position - radius, position + radius, position);
break;
// - page is located near the left corner
case (position >= first_page && position <= first_page + radius):
pages = range_gen(first_page, first_page + (radius * 2), position);
break;
// - page is located near the right corner
case (position >= last_page - radius && position <= last_page):
pages = range_gen(last_page - (radius * 2) + 1, last_page + 1, position);
break;
}
return (pages);
},
// ### Advanced pagination page list generator
advanced_pagination: function(first_page, last_page, position, edge_radius, position_radius) {
// * first_page - page, where page list starts
// * last_page - page, where page list ends
// * position - current page in pagination list
// * edge_radius - radius from edges
// * position_radius - radius around current page
// For testing, please use:
//
// var test_pagination = new window.structuredUI.Pagination(), i;
// for (i = -10; i <= 10; i++) {
// console.log(test_pagination.advanced_pagination(-10, 10, i, 2, 2));
// }
return Array.apply(null, new Array(last_page + 1 - first_page)).map(function(e, i, a) {
return Math.min(
Math.min(
Math.max(1 + i - edge_radius, 0),
Math.max(a.length - i - edge_radius, 0)
),
Math.max(Math.abs(i - (position - first_page)) - position_radius, 0)
);
}).map(function(e, i, a) {
return e * Math.max(a[i - 1], a[i + 1]) > 0 ? '...' : (i + first_page).toString();
}).filter(function(e, i, a) {
return e != '.' || a[i - 1] != '...';
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment