Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jquery.jqpagination.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*!
  2. * jqPagination, a jQuery pagination plugin (obviously)
  3. * Version: 1.4 (26th July 2013)
  4. *
  5. * Copyright (C) 2013 Ben Everard
  6. *
  7. * http://beneverard.github.com/jqPagination
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. (function ($) {
  24. "use strict";
  25. $.jqPagination = function (el, options) {
  26. // To avoid scope issues, use 'base' instead of 'this'
  27. // to reference this class from internal events and functions.
  28. var base = this;
  29. // Access to jQuery and DOM versions of element
  30. base.$el = $(el);
  31. base.el = el;
  32. // get input jQuery object
  33. base.$input = base.$el.find('input');
  34. // Add a reverse reference to the DOM object
  35. base.$el.data("jqPagination", base);
  36. base.init = function () {
  37. base.options = $.extend({}, $.jqPagination.defaultOptions, options);
  38. // if the user hasn't provided a max page number in the options try and find
  39. // the data attribute for it, if that cannot be found, use one as a max page number
  40. if (base.options.max_page === null) {
  41. if (base.$input.data('max-page') !== undefined) {
  42. base.options.max_page = base.$input.data('max-page');
  43. } else {
  44. base.options.max_page = 1;
  45. }
  46. }
  47. // if the current-page data attribute is specified this takes priority
  48. // over the options passed in, so long as it's a number
  49. if (base.$input.data('current-page') !== undefined && base.isNumber(base.$input.data('current-page'))) {
  50. base.options.current_page = base.$input.data('current-page');
  51. }
  52. // remove the readonly attribute as JavaScript must be working by now ;-)
  53. base.$input.removeAttr('readonly');
  54. // set the initial input value
  55. // pass true to prevent paged callback form being fired
  56. base.updateInput(true);
  57. //***************
  58. // BIND EVENTS
  59. base.$input.on('focus.jqPagination mouseup.jqPagination', function (event) {
  60. // if event === focus, select all text...
  61. if (event.type === 'focus') {
  62. var current_page = parseInt(base.options.current_page, 10);
  63. $(this).val(current_page).select();
  64. }
  65. // if event === mouse up, return false. Fixes Chrome bug
  66. if (event.type === 'mouseup') {
  67. return false;
  68. }
  69. });
  70. base.$input.on('blur.jqPagination keydown.jqPagination', function (event) {
  71. var $self = $(this),
  72. current_page = parseInt(base.options.current_page, 10);
  73. // if the user hits escape revert the input back to the original value
  74. if (event.keyCode === 27) {
  75. $self.val(current_page);
  76. $self.blur();
  77. }
  78. // if the user hits enter, trigger blur event but DO NOT set the page value
  79. if (event.keyCode === 13) {
  80. $self.blur();
  81. }
  82. // only set the page is the event is focusout.. aka blur
  83. if (event.type === 'blur') {
  84. base.setPage($self.val());
  85. }
  86. });
  87. base.$el.on('click.jqPagination', 'a', function (event) {
  88. var $self = $(this);
  89. // we don't want To Do anything if we've clicked a disabled link
  90. // return false so we stop normal link action btu also drop out of this event
  91. if ($self.hasClass('disabled')) {
  92. return false;
  93. }
  94. // for mac + windows (read: other), maintain the cmd + ctrl click for new tab
  95. if (!event.metaKey && !event.ctrlKey) {
  96. event.preventDefault();
  97. base.setPage($self.data('action'));
  98. }
  99. });
  100. };
  101. base.setPage = function (page, prevent_paged) {
  102. // return current_page value if getting instead of setting
  103. if (page === undefined) {
  104. return base.options.current_page;
  105. }
  106. var current_page = parseInt(base.options.current_page, 10),
  107. max_page = parseInt(base.options.max_page, 10);
  108. if (isNaN(parseInt(page, 10))) {
  109. switch (page) {
  110. case 'first':
  111. page = 1;
  112. break;
  113. case 'prev':
  114. case 'previous':
  115. page = current_page - 1;
  116. break;
  117. case 'next':
  118. page = current_page + 1;
  119. break;
  120. case 'last':
  121. page = max_page;
  122. break;
  123. }
  124. }
  125. page = parseInt(page, 10);
  126. // reject any invalid page requests
  127. if (isNaN(page) || page < 1 || page > max_page) {
  128. // update the input element
  129. base.setInputValue(current_page);
  130. return false;
  131. }
  132. // update current page options
  133. base.options.current_page = page;
  134. base.$input.data('current-page', page);
  135. // update the input element
  136. base.updateInput( prevent_paged );
  137. };
  138. base.setMaxPage = function (max_page, prevent_paged) {
  139. // return the max_page value if getting instead of setting
  140. if (max_page === undefined) {
  141. return base.options.max_page;
  142. }
  143. // ignore if max_page is not a number
  144. if (!base.isNumber(max_page)) {
  145. console.error('jqPagination: max_page is not a number');
  146. return false;
  147. }
  148. // ignore if max_page is less than the current_page
  149. if (max_page < base.options.current_page) {
  150. console.error('jqPagination: max_page lower than current_page');
  151. return false;
  152. }
  153. // set max_page options
  154. base.options.max_page = max_page;
  155. base.$input.data('max-page', max_page);
  156. // update the input element
  157. base.updateInput( prevent_paged );
  158. };
  159. // ATTN this isn't really the correct name is it?
  160. base.updateInput = function (prevent_paged) {
  161. var current_page = parseInt(base.options.current_page, 10);
  162. // set the input value
  163. base.setInputValue(current_page);
  164. // set the link href attributes
  165. base.setLinks(current_page);
  166. // we may want to prevent the paged callback from being fired
  167. if (prevent_paged !== true) {
  168. // fire the callback function with the current page
  169. base.options.paged(current_page);
  170. }
  171. };
  172. base.setInputValue = function (page) {
  173. var page_string = base.options.page_string,
  174. max_page = base.options.max_page;
  175. // this looks horrible :-(
  176. page_string = page_string
  177. .replace("{current_page}", page)
  178. .replace("{max_page}", max_page);
  179. base.$input.val(page_string);
  180. };
  181. base.isNumber = function(n) {
  182. return !isNaN(parseFloat(n)) && isFinite(n);
  183. };
  184. base.setLinks = function (page) {
  185. var link_string = base.options.link_string,
  186. current_page = parseInt(base.options.current_page, 10),
  187. max_page = parseInt(base.options.max_page, 10);
  188. if (link_string !== '') {
  189. // set initial page numbers + make sure the page numbers aren't out of range
  190. var previous = current_page - 1;
  191. if (previous < 1) {
  192. previous = 1;
  193. }
  194. var next = current_page + 1;
  195. if (next > max_page) {
  196. next = max_page;
  197. }
  198. // apply each page number to the link string, set it back to the element href attribute
  199. base.$el.find('a.first').attr('href', link_string.replace('{page_number}', '1'));
  200. base.$el.find('a.prev, a.previous').attr('href', link_string.replace('{page_number}', previous));
  201. base.$el.find('a.next').attr('href', link_string.replace('{page_number}', next));
  202. base.$el.find('a.last').attr('href', link_string.replace('{page_number}', max_page));
  203. }
  204. // set disable class on appropriate links
  205. base.$el.find('a').removeClass('disabled');
  206. if (current_page === max_page) {
  207. base.$el.find('.next, .last').addClass('disabled');
  208. }
  209. if (current_page === 1) {
  210. base.$el.find('.previous, .first').addClass('disabled');
  211. }
  212. };
  213. base.callMethod = function (method, key, value) {
  214. switch (method.toLowerCase()) {
  215. case 'option':
  216. // if we're getting, immediately return the value
  217. if ( value === undefined && typeof key !== "object" ) {
  218. return base.options[key];
  219. }
  220. // set default object to trigger the paged event (legacy opperation)
  221. var options = {'trigger': true},
  222. result = false;
  223. // if the key passed in is an object
  224. if($.isPlainObject(key) && !value){
  225. $.extend(options, key)
  226. }
  227. else{ // make the key value pair part of the default object
  228. options[key] = value;
  229. }
  230. var prevent_paged = (options.trigger === false);
  231. // if current_page property is set call setPage
  232. if(options.current_page !== undefined){
  233. result = base.setPage(options.current_page, prevent_paged);
  234. }
  235. // if max_page property is set call setMaxPage
  236. if(options.max_page !== undefined){
  237. result = base.setMaxPage(options.max_page, prevent_paged);
  238. }
  239. // if we've not got a result fire an error and return false
  240. if( result === false ) console.error('jqPagination: cannot get / set option ' + key);
  241. return result;
  242. break;
  243. case 'destroy':
  244. base.$el
  245. .off('.jqPagination')
  246. .find('*')
  247. .off('.jqPagination');
  248. break;
  249. default:
  250. // the function name must not exist
  251. console.error('jqPagination: method "' + method + '" does not exist');
  252. return false;
  253. }
  254. };
  255. // Run initializer
  256. base.init();
  257. };
  258. $.jqPagination.defaultOptions = {
  259. current_page : 1,
  260. link_string : '',
  261. max_page : null,
  262. page_string : 'Page {current_page} of {max_page}',
  263. paged : function () {}
  264. };
  265. $.fn.jqPagination = function () {
  266. // get any function parameters
  267. var self = this,
  268. $self = $(self),
  269. args = Array.prototype.slice.call(arguments),
  270. result = false;
  271. // if the first argument is a string call the desired function
  272. // note: we can only do this to a single element, and not a collection of elements
  273. if (typeof args[0] === 'string') {
  274. // if we're getting, we can only get value for the first pagination element
  275. if (args[2] === undefined) {
  276. result = $self.first().data('jqPagination').callMethod(args[0], args[1]);
  277. } else {
  278. // if we're setting, set values for all pagination elements
  279. $self.each(function(){
  280. result = $(this).data('jqPagination').callMethod(args[0], args[1], args[2]);
  281. });
  282. }
  283. return result;
  284. }
  285. // if we're not dealing with a method, initialise plugin
  286. self.each(function () {
  287. (new $.jqPagination(this, args[0]));
  288. });
  289. };
  290. })(jQuery);
  291. // polyfill, provide a fallback if the console doesn't exist
  292. if (!console) {
  293. var console = {},
  294. func = function () { return false; };
  295. console.log = func;
  296. console.info = func;
  297. console.warn = func;
  298. console.error = func;
  299. }