Files
galaxy/static/scripts/packed/ui/pagination.js
T

1 line
2.9 KiB
JavaScript

(function(a){if(typeof define==="function"&&define.amd){define(["jquery"],a)}else{a(jQuery)}}(function(c){function k(p,o){this.numPages=null;this.currPage=0;return this.init(p,o)}k.prototype.DATA_KEY="pagination";k.prototype.defaults={startingPage:0,perPage:20,totalDataSize:null,currDataSize:null};k.prototype.init=function h(o,p){p=p||{};this.$element=o;this.options=jQuery.extend(true,{},this.defaults,p);this.currPage=this.options.startingPage;if(this.options.totalDataSize!==null){this.numPages=Math.ceil(this.options.totalDataSize/this.options.perPage);if(this.currPage>=this.numPages){this.currPage=this.numPages-1}}this.$element.data(k.prototype.DATA_KEY,this);this._render();return this};function n(o){return c(['<li><a href="javascript:void(0);">',o,"</a></li>"].join(""))}k.prototype._render=function f(){if(this.options.totalDataSize===0){return this}if(this.numPages===1){return this}if(this.numPages>0){this._renderPages();this._scrollToActivePage()}else{this._renderPrevNext()}return this};k.prototype._renderPrevNext=function b(){var p=this,q=n("Prev"),o=n("Next"),r=c("<ul/>").addClass("pagination pagination-prev-next");if(this.currPage===0){q.addClass("disabled")}else{q.click(function(){p.prevPage()})}if((this.numPages&&this.currPage===(this.numPages-1))||(this.options.currDataSize&&this.options.currDataSize<this.options.perPage)){o.addClass("disabled")}else{o.click(function(){p.nextPage()})}this.$element.html(r.append([q,o]));return this.$element};k.prototype._renderPages=function a(){var o=this,r=c("<div>").addClass("pagination-scroll-container"),t=c("<ul/>").addClass("pagination pagination-page-list"),s=function(u){o.goToPage(c(this).data("page"))};for(var p=0;p<this.numPages;p+=1){var q=n(p+1).attr("data-page",p).click(s);if(p===this.currPage){q.addClass("active")}t.append(q)}return this.$element.html(r.html(t))};k.prototype._scrollToActivePage=function m(){var q=this.$element.find(".pagination-scroll-container");if(!q.size()){return this}var p=this.$element.find("li.active"),o=q.width()/2;q.scrollLeft(q.scrollLeft()+p.position().left-o);return this};k.prototype.goToPage=function j(o){if(o<=0){o=0}if(this.numPages&&o>=this.numPages){o=this.numPages-1}if(o===this.currPage){return this}this.currPage=o;this.$element.trigger("pagination.page-change",this.currPage);this._render();return this};k.prototype.prevPage=function d(){return this.goToPage(this.currPage-1)};k.prototype.nextPage=function i(){return this.goToPage(this.currPage+1)};k.prototype.page=function g(){return this.currPage};k.create=function l(o,p){return new k(o,p)};jQuery.fn.extend({pagination:function e(p){var o=jQuery.makeArray(arguments).slice(1);if(jQuery.type(p)==="object"){return this.map(function(){k.create(c(this),p);return this})}var r=c(this[0]),s=r.data(k.prototype.DATA_KEY);if(s){if(jQuery.type(p)==="string"){var q=s[p];if(jQuery.type(q)==="function"){return q.apply(s,o)}}else{return s}}return undefined}})}));