123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657 |
- (function ($) {
- "use strict";
-
-
- var BootstrapPaginator = function (element, options) {
- this.init(element, options);
- },
- old = null;
- BootstrapPaginator.prototype = {
-
- init: function (element, options) {
- this.$element = $(element);
- var version = (options && options.bootstrapMajorVersion) ? options.bootstrapMajorVersion : $.fn.bootstrapPaginator.defaults.bootstrapMajorVersion,
- id = this.$element.attr("id");
- if (version === 2 && !this.$element.is("div")) {
- throw "in Bootstrap version 2 the pagination must be a div element. Or if you are using Bootstrap pagination 3. Please specify it in bootstrapMajorVersion in the option";
- } else if (version > 2 && !this.$element.is("ul")) {
- throw "in Bootstrap version 3 the pagination root item must be an ul element."
- }
- this.currentPage = 1;
- this.lastPage = 1;
- this.setOptions(options);
- this.initialized = true;
- },
-
- setOptions: function (options) {
- this.options = $.extend({}, (this.options || $.fn.bootstrapPaginator.defaults), options);
- this.totalPages = parseInt(this.options.totalPages, 10);
- this.numberOfPages = parseInt(this.options.numberOfPages, 10);
-
- if (options && typeof (options.currentPage) !== 'undefined') {
- this.setCurrentPage(options.currentPage);
- }
- this.listen();
-
- this.render();
- if (!this.initialized && this.lastPage !== this.currentPage) {
- this.$element.trigger("page-changed", [this.lastPage, this.currentPage]);
- }
- },
-
- listen: function () {
- this.$element.off("page-clicked");
- this.$element.off("page-changed");
- if (typeof (this.options.onPageClicked) === "function") {
- this.$element.bind("page-clicked", this.options.onPageClicked);
- }
- if (typeof (this.options.onPageChanged) === "function") {
- this.$element.on("page-changed", this.options.onPageChanged);
- }
- this.$element.bind("page-clicked", this.onPageClicked);
- },
-
- destroy: function () {
- this.$element.off("page-clicked");
- this.$element.off("page-changed");
- this.$element.removeData('bootstrapPaginator');
- this.$element.empty();
- },
-
- show: function (page) {
- this.setCurrentPage(page);
- this.render();
- if (this.lastPage !== this.currentPage) {
- this.$element.trigger("page-changed", [this.lastPage, this.currentPage]);
- }
- },
-
- showNext: function () {
- var pages = this.getPages();
- if (pages.next) {
- this.show(pages.next);
- }
- },
-
- showPrevious: function () {
- var pages = this.getPages();
- if (pages.prev) {
- this.show(pages.prev);
- }
- },
-
- showFirst: function () {
- var pages = this.getPages();
- if (pages.first) {
- this.show(pages.first);
- }
- },
-
- showLast: function () {
- var pages = this.getPages();
- if (pages.last) {
- this.show(pages.last);
- }
- },
-
- onPageItemClicked: function (event) {
- var type = event.data.type,
- page = event.data.page;
- this.$element.trigger("page-clicked", [event, type, page]);
- },
- onPageClicked: function (event, originalEvent, type, page) {
-
- var currentTarget = $(event.currentTarget);
- switch (type) {
- case "first":
- currentTarget.bootstrapPaginator("showFirst");
- break;
- case "prev":
- currentTarget.bootstrapPaginator("showPrevious");
- break;
- case "next":
- currentTarget.bootstrapPaginator("showNext");
- break;
- case "last":
- currentTarget.bootstrapPaginator("showLast");
- break;
- case "page":
- currentTarget.bootstrapPaginator("show", page);
- break;
- }
- },
-
- render: function () {
-
- var containerClass = this.getValueFromOption(this.options.containerClass, this.$element),
- size = this.options.size || "normal",
- alignment = this.options.alignment || "left",
- pages = this.getPages(),
- listContainer = this.options.bootstrapMajorVersion === 2 ? $("<ul></ul>") : this.$element,
- listContainerClass = this.options.bootstrapMajorVersion === 2 ? this.getValueFromOption(this.options.listContainerClass, listContainer) : null,
- first = null,
- prev = null,
- next = null,
- last = null,
- p = null,
- i = 0;
- this.$element.prop("class", "");
- this.$element.addClass("pagination");
- switch (size.toLowerCase()) {
- case "large":
- case "small":
- case "mini":
- this.$element.addClass($.fn.bootstrapPaginator.sizeArray[this.options.bootstrapMajorVersion][size.toLowerCase()]);
- break;
- default:
- break;
- }
- if (this.options.bootstrapMajorVersion === 2) {
- switch (alignment.toLowerCase()) {
- case "center":
- this.$element.addClass("pagination-centered");
- break;
- case "right":
- this.$element.addClass("pagination-right");
- break;
- default:
- break;
- }
- }
- this.$element.addClass(containerClass);
-
- this.$element.empty();
- if (this.options.bootstrapMajorVersion === 2) {
- this.$element.append(listContainer);
- listContainer.addClass(listContainerClass);
- }
-
- this.pageRef = [];
- if (pages.first) {
- first = this.buildPageItem("first", pages.first);
- if (first) {
- listContainer.append(first);
- }
- }
- if (pages.prev) {
- prev = this.buildPageItem("prev", pages.prev);
- if (prev) {
- listContainer.append(prev);
- }
- }
- for (i = 0; i < pages.length; i = i + 1) {
- p = this.buildPageItem("page", pages[i]);
- if (p) {
- listContainer.append(p);
- }
- }
- if (pages.next) {
- next = this.buildPageItem("next", pages.next);
- if (next) {
- listContainer.append(next);
- }
- }
- if (pages.last) {
- last = this.buildPageItem("last", pages.last);
- if (last) {
- listContainer.append(last);
- }
- }
- },
-
- buildPageItem: function (type, page) {
- var itemContainer = $("<li></li>"),
- itemContent = $("<a></a>"),
- text = "",
- title = "",
- itemContainerClass = this.options.itemContainerClass(type, page, this.currentPage),
- itemContentClass = this.getValueFromOption(this.options.itemContentClass, type, page, this.currentPage),
- tooltipOpts = null;
- switch (type) {
- case "first":
- if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
- text = this.options.itemTexts(type, page, this.currentPage);
- title = this.options.tooltipTitles(type, page, this.currentPage);
- break;
- case "last":
- if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
- text = this.options.itemTexts(type, page, this.currentPage);
- title = this.options.tooltipTitles(type, page, this.currentPage);
- break;
- case "prev":
- if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
- text = this.options.itemTexts(type, page, this.currentPage);
- title = this.options.tooltipTitles(type, page, this.currentPage);
- break;
- case "next":
- if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
- text = this.options.itemTexts(type, page, this.currentPage);
- title = this.options.tooltipTitles(type, page, this.currentPage);
- break;
- case "page":
- if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
- text = this.options.itemTexts(type, page, this.currentPage);
- title = this.options.tooltipTitles(type, page, this.currentPage);
- break;
- }
- itemContainer.addClass(itemContainerClass).append(itemContent);
- itemContent.addClass(itemContentClass).html(text).on("click", null, {type: type, page: page}, $.proxy(this.onPageItemClicked, this));
- if (this.options.pageUrl) {
- itemContent.attr("href", this.getValueFromOption(this.options.pageUrl, type, page, this.currentPage));
- }
- if (this.options.useBootstrapTooltip) {
- tooltipOpts = $.extend({}, this.options.bootstrapTooltipOptions, {title: title});
- itemContent.tooltip(tooltipOpts);
- } else {
- itemContent.attr("title", title);
- }
- return itemContainer;
- },
- setCurrentPage: function (page) {
- if (page > this.totalPages || page < 1) {
- throw "Page out of range";
- }
- this.lastPage = this.currentPage;
- this.currentPage = parseInt(page, 10);
- },
-
- getPages: function () {
- var totalPages = this.totalPages,
- pageStart = (this.currentPage % this.numberOfPages === 0) ? (parseInt(this.currentPage / this.numberOfPages, 10) - 1) * this.numberOfPages + 1 : parseInt(this.currentPage / this.numberOfPages, 10) * this.numberOfPages + 1,
- output = [],
- i = 0,
- counter = 0;
- pageStart = pageStart < 1 ? 1 : pageStart;
- for (i = pageStart, counter = 0; counter < this.numberOfPages && i <= totalPages; i = i + 1, counter = counter + 1) {
- output.push(i);
- }
- output.first = 1;
- if (this.currentPage > 1) {
- output.prev = this.currentPage - 1;
- } else {
- output.prev = 1;
- }
- if (this.currentPage < totalPages) {
- output.next = this.currentPage + 1;
- } else {
- output.next = totalPages;
- }
- output.last = totalPages;
- output.current = this.currentPage;
- output.total = totalPages;
- output.numberOfPages = this.options.numberOfPages;
- return output;
- },
-
- getValueFromOption: function (value) {
- var output = null,
- args = Array.prototype.slice.call(arguments, 1);
- if (typeof value === 'function') {
- output = value.apply(this, args);
- } else {
- output = value;
- }
- return output;
- }
- };
-
- old = $.fn.bootstrapPaginator;
- $.fn.bootstrapPaginator = function (option) {
- var args = arguments,
- result = null;
- $(this).each(function (index, item) {
- var $this = $(item),
- data = $this.data('bootstrapPaginator'),
- options = (typeof option !== 'object') ? null : option;
- if (!data) {
- data = new BootstrapPaginator(this, options);
- $this = $(data.$element);
- $this.data('bootstrapPaginator', data);
- return;
- }
- if (typeof option === 'string') {
- if (data[option]) {
- result = data[option].apply(data, Array.prototype.slice.call(args, 1));
- } else {
- throw "Method " + option + " does not exist";
- }
- } else {
- result = data.setOptions(option);
- }
- });
- return result;
- };
- $.fn.bootstrapPaginator.sizeArray = {
- "2": {
- "large": "pagination-large",
- "small": "pagination-small",
- "mini": "pagination-mini"
- },
- "3": {
- "large": "pagination-lg",
- "small": "pagination-sm",
- "mini": ""
- }
- };
- $.fn.bootstrapPaginator.defaults = {
- containerClass: "",
- size: "normal",
- alignment: "left",
- bootstrapMajorVersion: 2,
- listContainerClass: "",
- itemContainerClass: function (type, page, current) {
- return (page === current) ? "active" : "";
- },
- itemContentClass: function (type, page, current) {
- return "";
- },
- currentPage: 1,
- numberOfPages: 5,
- totalPages: 1,
- pageUrl: function (type, page, current) {
- return null;
- },
- onPageClicked: null,
- onPageChanged: null,
- useBootstrapTooltip: false,
- shouldShowPage: function (type, page, current) {
- var result = true;
- switch (type) {
- case "first":
- result = (current !== 1);
- break;
- case "prev":
- result = (current !== 1);
- break;
- case "next":
- result = (current !== this.totalPages);
- break;
- case "last":
- result = (current !== this.totalPages);
- break;
- case "page":
- result = true;
- break;
- }
- return result;
- },
- itemTexts: function (type, page, current) {
- switch (type) {
- case "first":
- return "<<";
- case "prev":
- return "<";
- case "next":
- return ">";
- case "last":
- return ">>";
- case "page":
- return page;
- }
- },
- tooltipTitles: function (type, page, current) {
- switch (type) {
- case "first":
- return "Go to first page";
- case "prev":
- return "Go to previous page";
- case "next":
- return "Go to next page";
- case "last":
- return "Go to last page";
- case "page":
- return (page === current) ? "Current page is " + page : "Go to page " + page;
- }
- },
- bootstrapTooltipOptions: {
- animation: true,
- html: true,
- placement: 'top',
- selector: false,
- title: "",
- container: false
- }
- };
- $.fn.bootstrapPaginator.Constructor = BootstrapPaginator;
- }(window.jQuery));
|