/content/admin/js/plugins/jquery.simplePagination.js

https://bitbucket.org/emagid/pinktie · JavaScript · 336 lines · 269 code · 48 blank · 19 comment · 84 complexity · 7f5d9c15738aa031ab114aa5c924fd48 MD5 · raw file

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