PageSourceSearch

https://blog.packagecloud.io/assets/js/jquery.simplePagination.js?1752470250

js packagecloud.io collected 2026-09-24 15:22:20 UTC 7,636 bytes, 267 lines download raw bytes

1/**
2* simplePagination.js v1.6
3* A simple jQuery pagination plugin.
4* http://flaviusmatis.github.com/simplePagination.js/
5*
6* Copyright 2012, Flavius Matis
7* Released under the MIT license.
8* http://flaviusmatis.github.com/license.html
9*/
10
11(function($){
12  var methods = {
13    init: function(options) {
14      var o = $.extend({
15        items: 1,
16        itemsOnPage: 1,
17        pages: 0,
18        displayedPages: 1,
19        edges: 2,
20        currentPage: 1,
21        hrefTextPrefix: '#page-',
22        hrefTextSuffix: '',
23        prevText: 'Prev',
24        nextText: 'Next',
25        ellipseText: '…',
26        cssStyle: 'light-theme',
27        labelMap: [],
28        selectOnClick: true,
29        onPageClick: function(pageNumber, event) {
30          // Callback triggered when a page is clicked
31          // Page number is given as an optional parameter
32        },
33        onInit: function() {
34          // Callback triggered immediately after initialization
35        }
36      }, options || {});
37
38      var self = this;
39
40      o.pages = o.pages ? o.pages : Math.ceil(o.items / o.itemsOnPage) ? Math.ceil(o.items / o.itemsOnPage) : 1;
41      o.currentPage = o.currentPage - 1;
42      o.halfDisplayed = o.displayedPages / 2;
43
44      this.each(function() {
45        self.addClass(o.cssStyle + ' simple-pagination').data('pagination', o);
46        methods._draw.call(self);
47      });
48
49      o.onInit();
50
51      return this;
52    },
53
54    selectPage: function(page) {
55      methods._selectPage.call(this, page - 1);
56      return this;
57    },
58
59    prevPage: function() {
60      var o = this.data('pagination');
61      if (o.currentPage > 0) {
62        methods._selectPage.call(this, o.currentPage - 1);
63      }
64      return this;
65    },
66
67    nextPage: function() {
68      var o = this.data('pagination');
69      if (o.currentPage < o.pages - 1) {
70        methods._selectPage.call(this, o.currentPage + 1);
71      }
72      return this;
73    },
74
75    getPagesCount: function() {
76      return this.data('pagination').pages;
77    },
78
79    getCurrentPage: function () {
80      return this.data('pagination').currentPage + 1;
81    },
82
83    destroy: function(){
84      this.empty();
85      return this;
86    },
87
88    drawPage: function (page) {
89      var o = this.data('pagination');
90      o.currentPage = page - 1;
91      this.data('pagination', o);
92      methods._draw.call(this);
93      return this;
94    },
95
96    redraw: function(){
97      methods._draw.call(this);
98      return this;
99    },
100
101    disable: function(){
102      var o = this.data('pagination');
103      o.disabled = true;
104      this.data('pagination', o);
105      methods._draw.call(this);
106      return this;
107    },
108
109    enable: function(){
110      var o = this.data('pagination');
111      o.disabled = false;
112      this.data('pagination', o);
113      methods._draw.call(this);
114      return this;
115    },
116
117    updateItems: function (newItems) {
118      var o = this.data('pagination');
119      o.items = newItems;
120      o.pages = methods._getPages(o);
121      this.data('pagination', o);
122      methods._draw.call(this);
123    },
124
125    updateItemsOnPage: function (itemsOnPage) {
126      var o = this.data('pagination');
127      o.itemsOnPage = itemsOnPage;
128      o.pages = methods._getPages(o);
129      this.data('pagination', o);
130      methods._selectPage.call(this, 0);
131      return this;
132    },
133
134    _draw: function() {
135      var  o = this.data('pagination'),
136        interval = methods._getInterval(o),
137        i,
138        tagName;
139
140      methods.destroy.call(this);
141      
142      tagName = (typeof this.prop === 'function') ? this.prop('tagName') : this.attr('tagName');
143
144      var $panel = tagName === 'UL' ? this : $('<ul></ul>').appendTo(this);
145
146      // Generate Prev link
147      if (o.prevText) {
148        methods._appendItem.call(this, o.currentPage - 1, {text: o.prevText, classes: 'prev'});
149      }
150
151      // Generate start edges
152      if (interval.start > 0 && o.edges > 0) {
153        var end = Math.min(o.edges, interval.start);
154        for (i = 0; i < end; i++) {
155          methods._appendItem.call(this, i);
156        }
157        if (o.edges < interval.start && (interval.start - o.edges != 1)) {
158          $panel.append('<li class="disabled"><span class="ellipse">
158' + o.ellipseText + '</span></li>');
159        } else if (interval.start - o.edges == 1) {
160          methods._appendItem.call(this, o.edges);
161        }
162      }
163
164      // Generate interval links
165      for (i = interval.start; i < interval.end; i++) {
166        methods._appendItem.call(this, i);
167      }
168
169      // Generate end edges
170      if (interval.end < o.pages && o.edges > 0) {
171        if (o.pages - o.edges > interval.end && (o.pages - o.edges - interval.end != 1)) {
172          $panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
173        } else if (o.pages - o.edges - interval.end == 1) {
174          methods._appendItem.call(this, interval.end++);
175        }
176        var begin = Math.max(o.pages - o.edges, interval.end);
177        for (i = begin; i < o.pages; i++) {
178          methods._appendItem.call(this, i);
179        }
180      }
181
182      // Generate Next link
183      if (o.nextText) {
184        methods._appendItem.call(this, o.currentPage + 1, {text: o.nextText, classes: 'next'});
185      }
186    },
187
188    _getPages: function(o) {
189      var pages = Math.ceil(o.items / o.itemsOnPage);
190      return pages || 1;
191    },
192
193    _getInterval: function(o) {
194      return {
195        start: Math.ceil(o.currentPage > o.halfDisplayed ? Math.max(Math.min(o.currentPage - o.halfDisplayed, (o.pages - o.displayedPages)), 0) : 0),
196        end: Math.ceil(o.currentPage > o.halfDisplayed ? Math.min(o.currentPage + o.halfDisplayed, o.pages) : Math.min(o.displayedPages, o.pages))
197      };
198    },
199
200    _appendItem: function(pageIndex, opts) {
201      var self = this, options, $link, o = self.data('pagination'), $linkWrapper = $('<li></li>'), $ul = self.find('ul');
202
203      pageIndex = pageIndex < 0 ? 0 : (pageIndex < o.pages ? pageIndex : o.pages - 1);
204
205      options = {
206        text: pageIndex + 1,
207        classes: ''
208      };
209
210      if (o.labelMap.length && o.labelMap[pageIndex]) {
211        options.text = o.labelMap[pageIndex];
212      }
213
214      options = $.extend(options, opts || {});
215
216      if (pageIndex == o.currentPage || o.disabled) {
217        if (o.disabled) {
218          $linkWrapper.addClass('disabled');
219        } else {
220          $linkWrapper.addClass('active');
221        }
222        $link = $('<span class="current">' + (options.text) + '</span>');
223      } else {
224        $link = $('<a href="' + o.hrefTextPrefix + (pageIndex + 1) + o.hrefTextSuffix + '" class="page-link">' + (options.text) + '</a>');
225        $link.click(function(event){
226          return methods._selectPage.call(self, pageIndex, event);
227        });
228      }
229
230      if (options.classes) {
231        $link.addClass(options.classes);
232      }
233
234      $linkWrapper.append($link);
235
236      if ($ul.length) {
237        $ul.append($linkWrapper);
238      } else {
239        self.append($linkWrapper);
240      }
241    },
242
243    _selectPage: function(pageIndex, event) {
244      var o = this.data('pagination');
245      o.currentPage = pageIndex;
246      if (o.selectOnClick) {
247        methods._draw.call(this);
248      }
249      return o.onPageClick(pageIndex + 1, event);
250    }
251
252  };
253
254  $.fn.pagination = function(method) {
255
256    // Method calling logic
257    if (methods[method] && method.charAt(0) != '_') {
258      return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
259    } else if (typeof method === 'object' || !method) {
260      return methods.init.apply(this, arguments);
261    } else {
262      $.error('Method ' +  method + ' does not exist on jQuery.pagination');
263    }
264
265  };
266
267})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.