Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.31 KB | None | 0 0
  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.  
  13. var methods = {
  14. init: function(options) {
  15. var o = $.extend({
  16. items: 1,
  17. itemsOnPage: 1,
  18. pages: 0,
  19. displayedPages: 5,
  20. edges: 2,
  21. currentPage: 0,
  22. hrefTextPrefix: '#page-',
  23. hrefTextSuffix: '',
  24. prevText: 'Prev',
  25. nextText: 'Next',
  26. ellipseText: '…',
  27. cssStyle: 'light-theme',
  28. labelMap: [],
  29. selectOnClick: true,
  30. nextAtFront: false,
  31. invertPageOrder: false,
  32. useStartEdge : true,
  33. useEndEdge : true,
  34. onPageClick: function(pageNumber, event) {
  35. // Callback triggered when a page is clicked
  36. // Page number is given as an optional parameter
  37. },
  38. onInit: function() {
  39. // Callback triggered immediately after initialization
  40. }
  41. }, options || {});
  42.  
  43. var self = this;
  44.  
  45. o.pages = o.pages ? o.pages : Math.ceil(o.items / o.itemsOnPage) ? Math.ceil(o.items / o.itemsOnPage) : 1;
  46. if (o.currentPage)
  47. o.currentPage = o.currentPage - 1;
  48. else
  49. o.currentPage = !o.invertPageOrder ? 0 : o.pages - 1;
  50. o.halfDisplayed = o.displayedPages / 2;
  51.  
  52. this.each(function() {
  53. self.addClass(o.cssStyle + ' simple-pagination').data('pagination', o);
  54. methods._draw.call(self);
  55. });
  56.  
  57. o.onInit();
  58.  
  59. return this;
  60. },
  61.  
  62. selectPage: function(page) {
  63. methods._selectPage.call(this, page - 1);
  64. return this;
  65. },
  66.  
  67. prevPage: function() {
  68. var o = this.data('pagination');
  69. if (!o.invertPageOrder) {
  70. if (o.currentPage > 0) {
  71. methods._selectPage.call(this, o.currentPage - 1);
  72. }
  73. } else {
  74. if (o.currentPage < o.pages - 1) {
  75. methods._selectPage.call(this, o.currentPage + 1);
  76. }
  77. }
  78. return this;
  79. },
  80.  
  81. nextPage: function() {
  82. var o = this.data('pagination');
  83. if (!o.invertPageOrder) {
  84. if (o.currentPage < o.pages - 1) {
  85. methods._selectPage.call(this, o.currentPage + 1);
  86. }
  87. } else {
  88. if (o.currentPage > 0) {
  89. methods._selectPage.call(this, o.currentPage - 1);
  90. }
  91. }
  92. return this;
  93. },
  94.  
  95. getPagesCount: function() {
  96. return this.data('pagination').pages;
  97. },
  98.  
  99. getCurrentPage: function () {
  100. return this.data('pagination').currentPage + 1;
  101. },
  102.  
  103. destroy: function(){
  104. this.empty();
  105. return this;
  106. },
  107.  
  108. drawPage: function (page) {
  109. var o = this.data('pagination');
  110. o.currentPage = page - 1;
  111. this.data('pagination', o);
  112. methods._draw.call(this);
  113. return this;
  114. },
  115.  
  116. redraw: function(){
  117. methods._draw.call(this);
  118. return this;
  119. },
  120.  
  121. disable: function(){
  122. var o = this.data('pagination');
  123. o.disabled = true;
  124. this.data('pagination', o);
  125. methods._draw.call(this);
  126. return this;
  127. },
  128.  
  129. enable: function(){
  130. var o = this.data('pagination');
  131. o.disabled = false;
  132. this.data('pagination', o);
  133. methods._draw.call(this);
  134. return this;
  135. },
  136.  
  137. updateItems: function (newItems) {
  138. var o = this.data('pagination');
  139. o.items = newItems;
  140. o.pages = methods._getPages(o);
  141. this.data('pagination', o);
  142. methods._draw.call(this);
  143. },
  144.  
  145. updateItemsOnPage: function (itemsOnPage) {
  146. var o = this.data('pagination');
  147. o.itemsOnPage = itemsOnPage;
  148. o.pages = methods._getPages(o);
  149. this.data('pagination', o);
  150. methods._selectPage.call(this, 0);
  151. return this;
  152. },
  153.  
  154. _draw: function() {
  155. var o = this.data('pagination'),
  156. interval = methods._getInterval(o),
  157. i,
  158. tagName;
  159.  
  160. methods.destroy.call(this);
  161.  
  162. tagName = (typeof this.prop === 'function') ? this.prop('tagName') : this.attr('tagName');
  163.  
  164. var $panel = tagName === 'UL' ? this : $('<ul></ul>').appendTo(this);
  165.  
  166. // Generate Prev link
  167. if (o.prevText) {
  168. methods._appendItem.call(this, !o.invertPageOrder ? o.currentPage - 1 : o.currentPage + 1, {text: o.prevText, classes: 'prev'});
  169. }
  170.  
  171. // Generate Next link (if option set for at front)
  172. if (o.nextText && o.nextAtFront) {
  173. methods._appendItem.call(this, !o.invertPageOrder ? o.currentPage + 1 : o.currentPage - 1, {text: o.nextText, classes: 'next'});
  174. }
  175.  
  176. // Generate start edges
  177. if (!o.invertPageOrder) {
  178. if (interval.start > 0 && o.edges > 0) {
  179. if(o.useStartEdge) {
  180. var end = Math.min(o.edges, interval.start);
  181. for (i = 0; i < end; i++) {
  182. methods._appendItem.call(this, i);
  183. }
  184. }
  185. if (o.edges < interval.start && (interval.start - o.edges != 1)) {
  186. $panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
  187. } else if (interval.start - o.edges == 1) {
  188. methods._appendItem.call(this, o.edges);
  189. }
  190. }
  191. } else {
  192. if (interval.end < o.pages && o.edges > 0) {
  193. if(o.useStartEdge) {
  194. var begin = Math.max(o.pages - o.edges, interval.end);
  195. for (i = o.pages - 1; i >= begin; i--) {
  196. methods._appendItem.call(this, i);
  197. }
  198. }
  199.  
  200. if (o.pages - o.edges > interval.end && (o.pages - o.edges - interval.end != 1)) {
  201. $panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
  202. } else if (o.pages - o.edges - interval.end == 1) {
  203. methods._appendItem.call(this, interval.end);
  204. }
  205. }
  206. }
  207.  
  208. // Generate interval links
  209. if (!o.invertPageOrder) {
  210. for (i = interval.start; i < interval.end; i++) {
  211. methods._appendItem.call(this, i);
  212. }
  213. } else {
  214. for (i = interval.end - 1; i >= interval.start; i--) {
  215. methods._appendItem.call(this, i);
  216. }
  217. }
  218.  
  219. // Generate end edges
  220. if (!o.invertPageOrder) {
  221. if (interval.end < o.pages && o.edges > 0) {
  222. if (o.pages - o.edges > interval.end && (o.pages - o.edges - interval.end != 1)) {
  223. $panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
  224. } else if (o.pages - o.edges - interval.end == 1) {
  225. methods._appendItem.call(this, interval.end);
  226. }
  227. if(o.useEndEdge) {
  228. var begin = Math.max(o.pages - o.edges, interval.end);
  229. for (i = begin; i < o.pages; i++) {
  230. methods._appendItem.call(this, i);
  231. }
  232. }
  233. }
  234. } else {
  235. if (interval.start > 0 && o.edges > 0) {
  236. if (o.edges < interval.start && (interval.start - o.edges != 1)) {
  237. $panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
  238. } else if (interval.start - o.edges == 1) {
  239. methods._appendItem.call(this, o.edges);
  240. }
  241.  
  242. if(o.useEndEdge) {
  243. var end = Math.min(o.edges, interval.start);
  244. for (i = end - 1; i >= 0; i--) {
  245. methods._appendItem.call(this, i);
  246. }
  247. }
  248. }
  249. }
  250.  
  251. // Generate Next link (unless option is set for at front)
  252. if (o.nextText && !o.nextAtFront) {
  253. methods._appendItem.call(this, !o.invertPageOrder ? o.currentPage + 1 : o.currentPage - 1, {text: o.nextText, classes: 'next'});
  254. }
  255. },
  256.  
  257. _getPages: function(o) {
  258. var pages = Math.ceil(o.items / o.itemsOnPage);
  259. return pages || 1;
  260. },
  261.  
  262. _getInterval: function(o) {
  263. return {
  264. start: Math.ceil(o.currentPage > o.halfDisplayed ? Math.max(Math.min(o.currentPage - o.halfDisplayed, (o.pages - o.displayedPages)), 0) : 0),
  265. end: Math.ceil(o.currentPage > o.halfDisplayed ? Math.min(o.currentPage + o.halfDisplayed, o.pages) : Math.min(o.displayedPages, o.pages))
  266. };
  267. },
  268.  
  269. _appendItem: function(pageIndex, opts) {
  270. var self = this, options, $link, o = self.data('pagination'), $linkWrapper = $('<li></li>'), $ul = self.find('ul');
  271.  
  272. pageIndex = pageIndex < 0 ? 0 : (pageIndex < o.pages ? pageIndex : o.pages - 1);
  273.  
  274. options = {
  275. text: pageIndex + 1,
  276. classes: ''
  277. };
  278.  
  279. if (o.labelMap.length && o.labelMap[pageIndex]) {
  280. options.text = o.labelMap[pageIndex];
  281. }
  282.  
  283. options = $.extend(options, opts || {});
  284.  
  285. if (pageIndex == o.currentPage || o.disabled) {
  286. if (o.disabled) {
  287. $linkWrapper.addClass('disabled');
  288. } else {
  289. $linkWrapper.addClass('active');
  290. }
  291. $link = $('<span class="current">' + (options.text) + '</span>');
  292. } else {
  293. $link = $('<a href="' + o.hrefTextPrefix + (pageIndex + 1) + o.hrefTextSuffix + '" class="page-link">' + (options.text) + '</a>');
  294. $link.click(function(event){
  295. return methods._selectPage.call(self, pageIndex, event);
  296. });
  297. }
  298.  
  299. if (options.classes) {
  300. $link.addClass(options.classes);
  301. }
  302.  
  303. $linkWrapper.append($link);
  304.  
  305. if ($ul.length) {
  306. $ul.append($linkWrapper);
  307. } else {
  308. self.append($linkWrapper);
  309. }
  310. },
  311.  
  312. _selectPage: function(pageIndex, event) {
  313. var o = this.data('pagination');
  314. o.currentPage = pageIndex;
  315. if (o.selectOnClick) {
  316. methods._draw.call(this);
  317. }
  318. return o.onPageClick(pageIndex + 1, event);
  319. }
  320.  
  321. };
  322.  
  323. $.fn.pagination = function(method) {
  324.  
  325. // Method calling logic
  326. if (methods[method] && method.charAt(0) != '_') {
  327. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  328. } else if (typeof method === 'object' || !method) {
  329. return methods.init.apply(this, arguments);
  330. } else {
  331. $.error('Method ' + method + ' does not exist on jQuery.pagination');
  332. }
  333.  
  334. };
  335.  
  336. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement