bjui-pagination.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*!
  2. * B-JUI v1.2 (http://b-jui.com)
  3. * Git@OSC (http://git.oschina.net/xknaan/B-JUI)
  4. * Copyright 2014 K'naan (xknaan@163.com).
  5. * Licensed under Apache (http://www.apache.org/licenses/LICENSE-2.0)
  6. */
  7. /* ========================================================================
  8. * B-JUI: bjui-pagination.js v1.2
  9. * @author K'naan (xknaan@163.com)
  10. * -- Modified from dwz.pagination.js (author:ZhangHuihua@msn.com)
  11. * http://git.oschina.net/xknaan/B-JUI/blob/master/BJUI/js/bjui-pagination.js
  12. * ========================================================================
  13. * Copyright 2014 K'naan.
  14. * Licensed under Apache (http://www.apache.org/licenses/LICENSE-2.0)
  15. * ======================================================================== */
  16. +function ($) {
  17. 'use strict';
  18. // PAGINATION CLASS DEFINITION
  19. // ======================
  20. var Pagination = function(element, options) {
  21. this.$element = $(element)
  22. this.options = options
  23. this.tools = this.TOOLS()
  24. }
  25. Pagination.DEFAULTS = {
  26. first : 'li.j-first',
  27. prev : 'li.j-prev',
  28. next : 'li.j-next',
  29. last : 'li.j-last',
  30. nums : 'li.j-num > a',
  31. jump : 'li.jumpto',
  32. pageNumFrag : '<li class="#liClass#"><a href="javascript:;">#pageNum#</a></li>',
  33. total : 0,
  34. pageSize : 10,
  35. pageNum : 10,
  36. pageCurrent : 1,
  37. callback : function() { return false }
  38. }
  39. Pagination.prototype.TOOLS = function() {
  40. var that = this
  41. var options = this.options
  42. var tools = {
  43. pageNums: function() {
  44. return Math.ceil(options.total / options.pageSize)
  45. },
  46. getInterval: function() {
  47. var ne_half = Math.ceil(options.pageNum / 2)
  48. var pn = this.pageNums()
  49. var upper_limit = pn - options.pageNum
  50. var start = this.getCurrentPage() > ne_half ? Math.max(Math.min(this.getCurrentPage() - ne_half, upper_limit), 0) : 0
  51. var end = this.getCurrentPage() > ne_half ? Math.min(this.getCurrentPage() + ne_half, pn) : Math.min(options.pageNum, pn)
  52. return {start: start + 1, end:end + 1}
  53. },
  54. getCurrentPage: function() {
  55. var pageCurrent = parseInt(options.pageCurrent)
  56. return (isNaN(pageCurrent)) ? 1 : pageCurrent
  57. },
  58. hasPrev: function() {
  59. return this.getCurrentPage() > 1
  60. },
  61. hasNext: function() {
  62. return this.getCurrentPage() < this.pageNums()
  63. }
  64. }
  65. return tools
  66. }
  67. Pagination.prototype.init = function() {
  68. if (BJUI.ui.clientPaging && !this.getClientPaging()) this.setClientPaging({pageCurrent:this.options.pageCurrent, pageSize:this.options.pageSize})
  69. var that = this
  70. var options = this.options
  71. var tools = this.tools
  72. var interval = tools.getInterval()
  73. var pageNumFrag = ''
  74. var pagination = FRAG.pagination
  75. var pr = BJUI.regional.pagination
  76. for (var i = interval.start; i < interval.end; i++) {
  77. pageNumFrag += options.pageNumFrag.replaceAll('#pageNum#', i).replaceAll('#liClass#', i == tools.getCurrentPage() ? 'selected j-num' : 'j-num')
  78. }
  79. pagination =
  80. pagination
  81. .replaceAll('#pageNumFrag#', pageNumFrag)
  82. .replaceAll('#pageCurrent#', tools.getCurrentPage())
  83. .replaceAll('#first#', pr.first)
  84. .replaceAll('#last#', pr.last)
  85. .replaceAll('#prev#', pr.prev)
  86. .replaceAll('#next#', pr.next)
  87. .replaceAll('#jumpto#', pr.jumpto)
  88. .replaceAll('#jump#', pr.jump)
  89. this.$element.html(pagination)
  90. var $first = this.$element.find(options.first)
  91. var $prev = this.$element.find(options.prev)
  92. var $next = this.$element.find(options.next)
  93. var $last = this.$element.find(options.last)
  94. if (tools.hasPrev()){
  95. $first.add($prev).find('> span').hide()
  96. _bindEvent($prev, tools.getCurrentPage() - 1)
  97. _bindEvent($first, 1)
  98. } else {
  99. $first.add($prev).addClass('disabled').find('> a').hide()
  100. }
  101. if (tools.hasNext()) {
  102. $next.add($last).find('> span').hide()
  103. _bindEvent($next, tools.getCurrentPage() + 1)
  104. _bindEvent($last, tools.pageNums())
  105. } else {
  106. $next.add($last).addClass('disabled').find('> a').hide()
  107. }
  108. this.$element.find(options.nums).each(function(i) {
  109. _bindEvent($(this), i + interval.start)
  110. })
  111. this.$element.find(options.jump).each(function() {
  112. var $inputBox = $(this).find(':text')
  113. var $button = $(this).find('.goto')
  114. $button.on('click', function() {
  115. var pageCurrent = $inputBox.val(), pagingInfo = {pageCurrent:pageCurrent, pageSize:options.pageSize}
  116. if (pageCurrent && pageCurrent.isPositiveInteger()) {
  117. that.setClientPaging(pagingInfo)
  118. $(this).bjuiajax('pageCallback', pagingInfo, that.$element.closest('.bjui-layout'))
  119. }
  120. })
  121. $inputBox.keyup(function(e) {
  122. if (e.keyCode == BJUI.keyCode.ENTER) $button.trigger('click')
  123. })
  124. })
  125. function _bindEvent($target, pageCurrent) {
  126. $target.on('click', function(e) {
  127. var pagingInfo = {pageCurrent:pageCurrent, pageSize:that.options.pageSize}
  128. that.setClientPaging(pagingInfo)
  129. $(this).bjuiajax('pageCallback', pagingInfo, that.$element.closest('.bjui-layout'))
  130. e.preventDefault()
  131. })
  132. }
  133. }
  134. Pagination.prototype.changePagesize = function() {
  135. var that = this, pageSize = that.$element.val(), pagingInfo = {pageSize:pageSize}
  136. if (!isNaN(pageSize)) {
  137. that.setClientPaging(pagingInfo)
  138. that.$element.bjuiajax('pageCallback', pagingInfo, that.$element.closest('.bjui-layout'))
  139. }
  140. }
  141. Pagination.prototype.orderBy = function(options) {
  142. var that = this
  143. that.$element.css({cursor:'pointer'}).click(function() {
  144. var orderField = $(this).data('orderField')
  145. var orderDirection = $(this).data('orderDirection')
  146. var orderInfo = {orderField:orderField, orderDirection:orderDirection}
  147. that.setClientPaging(orderInfo)
  148. $(this).bjuiajax('pageCallback', orderInfo, that.$element.closest('.bjui-layout'))
  149. })
  150. }
  151. Pagination.prototype.destroy = function() {
  152. this.$element.removeData('bjui.pagination').empty()
  153. }
  154. Pagination.prototype.getTarget = function() {
  155. var that = this, $target
  156. if (that.$element.closest('.bjui-layout').length) $target = that.$element.closest('.bjui-layout')
  157. else if (that.$element.closest('.navtab-panel').length) $target = $.CurrentNavtab
  158. else $target = $.CurrentDialog
  159. return $target
  160. }
  161. Pagination.prototype.getClientPaging = function() {
  162. return this.getTarget().data('bjui.clientPaging')
  163. }
  164. Pagination.prototype.setClientPaging = function(clientPaging) {
  165. if (BJUI.ui.clientPaging) {
  166. var $target = this.getTarget()
  167. $target.data('bjui.clientPaging', $.extend({}, $target.data('bjui.clientPaging') || {}, clientPaging))
  168. }
  169. }
  170. Pagination.prototype.setClientOrder = function(clientOrder) {
  171. if (BJUI.ui.clientPaging) {
  172. var clientPaging = this.getClientPaging()
  173. if (!clientPaging || !clientPaging.orderField) this.setClientPaging(clientOrder)
  174. }
  175. }
  176. Pagination.prototype.setPagingAndOrderby = function($target) {
  177. var clientPaging = $target.data('bjui.clientPaging')
  178. $target.find('[data-toggle="pagination"]')
  179. .pagination('destroy')
  180. .pagination(clientPaging)
  181. if (clientPaging.pageSize)
  182. $target.find('select[data-toggle-change="changepagesize"]').selectpicker('val', clientPaging.pageSize)
  183. if (clientPaging.orderField)
  184. $target.find('th[data-order-field="'+ clientPaging.orderField +'"]').addClass(clientPaging.orderDirection).siblings().removeClass('asc desc')
  185. }
  186. // PAGINATION PLUGIN DEFINITION
  187. // =======================
  188. function Plugin(option) {
  189. var args = arguments
  190. var property = option
  191. return this.each(function () {
  192. var $this = $(this)
  193. var options = $.extend({}, Pagination.DEFAULTS, $this.data(), typeof option == 'object' && option)
  194. var data = $this.data('bjui.pagination')
  195. if (!data) $this.data('bjui.pagination', (data = new Pagination(this, options)))
  196. if (typeof property == 'string' && $.isFunction(data[property])) {
  197. [].shift.apply(args)
  198. if (!args) data[property]()
  199. else data[property].apply(data, args)
  200. } else {
  201. data.init()
  202. }
  203. })
  204. }
  205. var old = $.fn.pagination
  206. $.fn.pagination = Plugin
  207. $.fn.pagination.Constructor = Pagination
  208. // PAGINATION NO CONFLICT
  209. // =================
  210. $.fn.pagination.noConflict = function () {
  211. $.fn.pagination = old
  212. return this
  213. }
  214. // PAGINATION DATA-API
  215. // ==============
  216. $(document).on(BJUI.eventType.initUI, function(e) {
  217. var $this = $(e.target).find('[data-toggle="pagination"]')
  218. if (!$this.length) return
  219. Plugin.call($this)
  220. })
  221. $(document).on('change.bjui.pagination.data-api', 'select[data-toggle-change="changepagesize"]', function(e) {
  222. var $this = $(this)
  223. var options = $this.data()
  224. Plugin.call($this, 'changePagesize')
  225. e.preventDefault()
  226. })
  227. }(jQuery);