bootstrap-paginator.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /**
  2. * bootstrap-paginator.js v0.5
  3. * --
  4. * Copyright 2013 Yun Lai <lyonlai1984@gmail.com>
  5. * --
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. (function ($) {
  19. "use strict"; // jshint ;_;
  20. /* Paginator PUBLIC CLASS DEFINITION
  21. * ================================= */
  22. /**
  23. * Boostrap Paginator Constructor
  24. *
  25. * @param element element of the paginator
  26. * @param options the options to config the paginator
  27. *
  28. * */
  29. var BootstrapPaginator = function (element, options) {
  30. this.init(element, options);
  31. },
  32. old = null;
  33. BootstrapPaginator.prototype = {
  34. /**
  35. * Initialization function of the paginator, accepting an element and the options as parameters
  36. *
  37. * @param element element of the paginator
  38. * @param options the options to config the paginator
  39. *
  40. * */
  41. init: function (element, options) {
  42. this.$element = $(element);
  43. var version = (options && options.bootstrapMajorVersion) ? options.bootstrapMajorVersion : $.fn.bootstrapPaginator.defaults.bootstrapMajorVersion,
  44. id = this.$element.attr("id");
  45. if (version === 2 && !this.$element.is("div")) {
  46. throw "in Bootstrap version 2 the pagination must be a div element. Or if you are using Bootstrap pagination 3. Please specify it in bootstrapMajorVersion in the option";
  47. } else if (version > 2 && !this.$element.is("ul")) {
  48. throw "in Bootstrap version 3 the pagination root item must be an ul element."
  49. }
  50. this.currentPage = 1;
  51. this.lastPage = 1;
  52. this.setOptions(options);
  53. this.initialized = true;
  54. },
  55. /**
  56. * Update the properties of the paginator element
  57. *
  58. * @param options options to config the paginator
  59. * */
  60. setOptions: function (options) {
  61. this.options = $.extend({}, (this.options || $.fn.bootstrapPaginator.defaults), options);
  62. this.totalPages = parseInt(this.options.totalPages, 10); //setup the total pages property.
  63. this.numberOfPages = parseInt(this.options.numberOfPages, 10); //setup the numberOfPages to be shown
  64. //move the set current page after the setting of total pages. otherwise it will cause out of page exception.
  65. if (options && typeof (options.currentPage) !== 'undefined') {
  66. this.setCurrentPage(options.currentPage);
  67. }
  68. this.listen();
  69. //render the paginator
  70. this.render();
  71. if (!this.initialized && this.lastPage !== this.currentPage) {
  72. this.$element.trigger("page-changed", [this.lastPage, this.currentPage]);
  73. }
  74. },
  75. /**
  76. * Sets up the events listeners. Currently the pageclicked and pagechanged events are linked if available.
  77. *
  78. * */
  79. listen: function () {
  80. this.$element.off("page-clicked");
  81. this.$element.off("page-changed");// unload the events for the element
  82. if (typeof (this.options.onPageClicked) === "function") {
  83. this.$element.bind("page-clicked", this.options.onPageClicked);
  84. }
  85. if (typeof (this.options.onPageChanged) === "function") {
  86. this.$element.on("page-changed", this.options.onPageChanged);
  87. }
  88. this.$element.bind("page-clicked", this.onPageClicked);
  89. },
  90. /**
  91. *
  92. * Destroys the paginator element, it unload the event first, then empty the content inside.
  93. *
  94. * */
  95. destroy: function () {
  96. this.$element.off("page-clicked");
  97. this.$element.off("page-changed");
  98. this.$element.removeData('bootstrapPaginator');
  99. this.$element.empty();
  100. },
  101. /**
  102. * Shows the page
  103. *
  104. * */
  105. show: function (page) {
  106. this.setCurrentPage(page);
  107. this.render();
  108. if (this.lastPage !== this.currentPage) {
  109. this.$element.trigger("page-changed", [this.lastPage, this.currentPage]);
  110. }
  111. },
  112. /**
  113. * Shows the next page
  114. *
  115. * */
  116. showNext: function () {
  117. var pages = this.getPages();
  118. if (pages.next) {
  119. this.show(pages.next);
  120. }
  121. },
  122. /**
  123. * Shows the previous page
  124. *
  125. * */
  126. showPrevious: function () {
  127. var pages = this.getPages();
  128. if (pages.prev) {
  129. this.show(pages.prev);
  130. }
  131. },
  132. /**
  133. * Shows the first page
  134. *
  135. * */
  136. showFirst: function () {
  137. var pages = this.getPages();
  138. if (pages.first) {
  139. this.show(pages.first);
  140. }
  141. },
  142. /**
  143. * Shows the last page
  144. *
  145. * */
  146. showLast: function () {
  147. var pages = this.getPages();
  148. if (pages.last) {
  149. this.show(pages.last);
  150. }
  151. },
  152. /**
  153. * Internal on page item click handler, when the page item is clicked, change the current page to the corresponding page and
  154. * trigger the pageclick event for the listeners.
  155. *
  156. *
  157. * */
  158. onPageItemClicked: function (event) {
  159. var type = event.data.type,
  160. page = event.data.page;
  161. this.$element.trigger("page-clicked", [event, type, page]);
  162. },
  163. onPageClicked: function (event, originalEvent, type, page) {
  164. //show the corresponding page and retrieve the newly built item related to the page clicked before for the event return
  165. var currentTarget = $(event.currentTarget);
  166. switch (type) {
  167. case "first":
  168. currentTarget.bootstrapPaginator("showFirst");
  169. break;
  170. case "prev":
  171. currentTarget.bootstrapPaginator("showPrevious");
  172. break;
  173. case "next":
  174. currentTarget.bootstrapPaginator("showNext");
  175. break;
  176. case "last":
  177. currentTarget.bootstrapPaginator("showLast");
  178. break;
  179. case "page":
  180. currentTarget.bootstrapPaginator("show", page);
  181. break;
  182. }
  183. },
  184. /**
  185. * Renders the paginator according to the internal properties and the settings.
  186. *
  187. *
  188. * */
  189. render: function () {
  190. //fetch the container class and add them to the container
  191. var containerClass = this.getValueFromOption(this.options.containerClass, this.$element),
  192. size = this.options.size || "normal",
  193. alignment = this.options.alignment || "left",
  194. pages = this.getPages(),
  195. listContainer = this.options.bootstrapMajorVersion === 2 ? $("<ul></ul>") : this.$element,
  196. listContainerClass = this.options.bootstrapMajorVersion === 2 ? this.getValueFromOption(this.options.listContainerClass, listContainer) : null,
  197. first = null,
  198. prev = null,
  199. next = null,
  200. last = null,
  201. p = null,
  202. i = 0;
  203. this.$element.prop("class", "");
  204. this.$element.addClass("pagination");
  205. switch (size.toLowerCase()) {
  206. case "large":
  207. case "small":
  208. case "mini":
  209. this.$element.addClass($.fn.bootstrapPaginator.sizeArray[this.options.bootstrapMajorVersion][size.toLowerCase()]);
  210. break;
  211. default:
  212. break;
  213. }
  214. if (this.options.bootstrapMajorVersion === 2) {
  215. switch (alignment.toLowerCase()) {
  216. case "center":
  217. this.$element.addClass("pagination-centered");
  218. break;
  219. case "right":
  220. this.$element.addClass("pagination-right");
  221. break;
  222. default:
  223. break;
  224. }
  225. }
  226. this.$element.addClass(containerClass);
  227. //empty the outter most container then add the listContainer inside.
  228. this.$element.empty();
  229. if (this.options.bootstrapMajorVersion === 2) {
  230. this.$element.append(listContainer);
  231. listContainer.addClass(listContainerClass);
  232. }
  233. //update the page element reference
  234. this.pageRef = [];
  235. if (pages.first) {//if the there is first page element
  236. first = this.buildPageItem("first", pages.first);
  237. if (first) {
  238. listContainer.append(first);
  239. }
  240. }
  241. if (pages.prev) {//if the there is previous page element
  242. prev = this.buildPageItem("prev", pages.prev);
  243. if (prev) {
  244. listContainer.append(prev);
  245. }
  246. }
  247. for (i = 0; i < pages.length; i = i + 1) {//fill the numeric pages.
  248. p = this.buildPageItem("page", pages[i]);
  249. if (p) {
  250. listContainer.append(p);
  251. }
  252. }
  253. if (pages.next) {//if there is next page
  254. next = this.buildPageItem("next", pages.next);
  255. if (next) {
  256. listContainer.append(next);
  257. }
  258. }
  259. if (pages.last) {//if there is last page
  260. last = this.buildPageItem("last", pages.last);
  261. if (last) {
  262. listContainer.append(last);
  263. }
  264. }
  265. },
  266. /**
  267. *
  268. * Creates a page item base on the type and page number given.
  269. *
  270. * @param page page number
  271. * @param type type of the page, whether it is the first, prev, page, next, last
  272. *
  273. * @return Object the constructed page element
  274. * */
  275. buildPageItem: function (type, page) {
  276. var itemContainer = $("<li></li>"),//creates the item container
  277. itemContent = $("<a></a>"),//creates the item content
  278. text = "",
  279. title = "",
  280. itemContainerClass = this.options.itemContainerClass(type, page, this.currentPage),
  281. itemContentClass = this.getValueFromOption(this.options.itemContentClass, type, page, this.currentPage),
  282. tooltipOpts = null;
  283. switch (type) {
  284. case "first":
  285. if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
  286. text = this.options.itemTexts(type, page, this.currentPage);
  287. title = this.options.tooltipTitles(type, page, this.currentPage);
  288. break;
  289. case "last":
  290. if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
  291. text = this.options.itemTexts(type, page, this.currentPage);
  292. title = this.options.tooltipTitles(type, page, this.currentPage);
  293. break;
  294. case "prev":
  295. if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
  296. text = this.options.itemTexts(type, page, this.currentPage);
  297. title = this.options.tooltipTitles(type, page, this.currentPage);
  298. break;
  299. case "next":
  300. if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
  301. text = this.options.itemTexts(type, page, this.currentPage);
  302. title = this.options.tooltipTitles(type, page, this.currentPage);
  303. break;
  304. case "page":
  305. if (!this.getValueFromOption(this.options.shouldShowPage, type, page, this.currentPage)) { return; }
  306. text = this.options.itemTexts(type, page, this.currentPage);
  307. title = this.options.tooltipTitles(type, page, this.currentPage);
  308. break;
  309. }
  310. itemContainer.addClass(itemContainerClass).append(itemContent);
  311. itemContent.addClass(itemContentClass).html(text).on("click", null, {type: type, page: page}, $.proxy(this.onPageItemClicked, this));
  312. if (this.options.pageUrl) {
  313. itemContent.attr("href", this.getValueFromOption(this.options.pageUrl, type, page, this.currentPage));
  314. }
  315. if (this.options.useBootstrapTooltip) {
  316. tooltipOpts = $.extend({}, this.options.bootstrapTooltipOptions, {title: title});
  317. itemContent.tooltip(tooltipOpts);
  318. } else {
  319. itemContent.attr("title", title);
  320. }
  321. return itemContainer;
  322. },
  323. setCurrentPage: function (page) {
  324. if (page > this.totalPages || page < 1) {// if the current page is out of range, throw exception.
  325. throw "Page out of range";
  326. }
  327. this.lastPage = this.currentPage;
  328. this.currentPage = parseInt(page, 10);
  329. },
  330. /**
  331. * Gets an array that represents the current status of the page object. Numeric pages can be access via array mode. length attributes describes how many numeric pages are there. First, previous, next and last page can be accessed via attributes first, prev, next and last. Current attribute marks the current page within the pages.
  332. *
  333. * @return object output objects that has first, prev, next, last and also the number of pages in between.
  334. * */
  335. getPages: function () {
  336. var totalPages = this.totalPages,// get or calculate the total pages via the total records
  337. pageStart = (this.currentPage % this.numberOfPages === 0) ? (parseInt(this.currentPage / this.numberOfPages, 10) - 1) * this.numberOfPages + 1 : parseInt(this.currentPage / this.numberOfPages, 10) * this.numberOfPages + 1,//calculates the start page.
  338. output = [],
  339. i = 0,
  340. counter = 0;
  341. pageStart = pageStart < 1 ? 1 : pageStart;//check the range of the page start to see if its less than 1.
  342. for (i = pageStart, counter = 0; counter < this.numberOfPages && i <= totalPages; i = i + 1, counter = counter + 1) {//fill the pages
  343. output.push(i);
  344. }
  345. output.first = 1;//add the first when the current page leaves the 1st page.
  346. if (this.currentPage > 1) {// add the previous when the current page leaves the 1st page
  347. output.prev = this.currentPage - 1;
  348. } else {
  349. output.prev = 1;
  350. }
  351. if (this.currentPage < totalPages) {// add the next page when the current page doesn't reach the last page
  352. output.next = this.currentPage + 1;
  353. } else {
  354. output.next = totalPages;
  355. }
  356. output.last = totalPages;// add the last page when the current page doesn't reach the last page
  357. output.current = this.currentPage;//mark the current page.
  358. output.total = totalPages;
  359. output.numberOfPages = this.options.numberOfPages;
  360. return output;
  361. },
  362. /**
  363. * Gets the value from the options, this is made to handle the situation where value is the return value of a function.
  364. *
  365. * @return mixed value that depends on the type of parameters, if the given parameter is a function, then the evaluated result is returned. Otherwise the parameter itself will get returned.
  366. * */
  367. getValueFromOption: function (value) {
  368. var output = null,
  369. args = Array.prototype.slice.call(arguments, 1);
  370. if (typeof value === 'function') {
  371. output = value.apply(this, args);
  372. } else {
  373. output = value;
  374. }
  375. return output;
  376. }
  377. };
  378. /* TYPEAHEAD PLUGIN DEFINITION
  379. * =========================== */
  380. old = $.fn.bootstrapPaginator;
  381. $.fn.bootstrapPaginator = function (option) {
  382. var args = arguments,
  383. result = null;
  384. $(this).each(function (index, item) {
  385. var $this = $(item),
  386. data = $this.data('bootstrapPaginator'),
  387. options = (typeof option !== 'object') ? null : option;
  388. if (!data) {
  389. data = new BootstrapPaginator(this, options);
  390. $this = $(data.$element);
  391. $this.data('bootstrapPaginator', data);
  392. return;
  393. }
  394. if (typeof option === 'string') {
  395. if (data[option]) {
  396. result = data[option].apply(data, Array.prototype.slice.call(args, 1));
  397. } else {
  398. throw "Method " + option + " does not exist";
  399. }
  400. } else {
  401. result = data.setOptions(option);
  402. }
  403. });
  404. return result;
  405. };
  406. $.fn.bootstrapPaginator.sizeArray = {
  407. "2": {
  408. "large": "pagination-large",
  409. "small": "pagination-small",
  410. "mini": "pagination-mini"
  411. },
  412. "3": {
  413. "large": "pagination-lg",
  414. "small": "pagination-sm",
  415. "mini": ""
  416. }
  417. };
  418. $.fn.bootstrapPaginator.defaults = {
  419. containerClass: "",
  420. size: "normal",
  421. alignment: "left",
  422. bootstrapMajorVersion: 2,
  423. listContainerClass: "",
  424. itemContainerClass: function (type, page, current) {
  425. return (page === current) ? "active" : "";
  426. },
  427. itemContentClass: function (type, page, current) {
  428. return "";
  429. },
  430. currentPage: 1,
  431. numberOfPages: 5,
  432. totalPages: 1,
  433. pageUrl: function (type, page, current) {
  434. return null;
  435. },
  436. onPageClicked: null,
  437. onPageChanged: null,
  438. useBootstrapTooltip: false,
  439. shouldShowPage: function (type, page, current) {
  440. var result = true;
  441. switch (type) {
  442. case "first":
  443. result = (current !== 1);
  444. break;
  445. case "prev":
  446. result = (current !== 1);
  447. break;
  448. case "next":
  449. result = (current !== this.totalPages);
  450. break;
  451. case "last":
  452. result = (current !== this.totalPages);
  453. break;
  454. case "page":
  455. result = true;
  456. break;
  457. }
  458. return result;
  459. },
  460. itemTexts: function (type, page, current) {
  461. switch (type) {
  462. case "first":
  463. return "&lt;&lt;";
  464. case "prev":
  465. return "&lt;";
  466. case "next":
  467. return "&gt;";
  468. case "last":
  469. return "&gt;&gt;";
  470. case "page":
  471. return page;
  472. }
  473. },
  474. tooltipTitles: function (type, page, current) {
  475. switch (type) {
  476. case "first":
  477. return "Go to first page";
  478. case "prev":
  479. return "Go to previous page";
  480. case "next":
  481. return "Go to next page";
  482. case "last":
  483. return "Go to last page";
  484. case "page":
  485. return (page === current) ? "Current page is " + page : "Go to page " + page;
  486. }
  487. },
  488. bootstrapTooltipOptions: {
  489. animation: true,
  490. html: true,
  491. placement: 'top',
  492. selector: false,
  493. title: "",
  494. container: false
  495. }
  496. };
  497. $.fn.bootstrapPaginator.Constructor = BootstrapPaginator;
  498. }(window.jQuery));