Advertisement
Guest User

Untitled

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