test-paginate.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /**
  2. * vue-paginate v3.6.0
  3. * (c) 2018 Taha Shashtari
  4. * @license MIT
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  8. typeof define === 'function' && define.amd ? define(factory) :
  9. (global.VuePaginate = factory());
  10. }(this, function () { 'use strict';
  11. var warn = function () {}
  12. var formatComponentName
  13. var hasConsole = typeof console !== 'undefined'
  14. warn = function (msg, vm, type) {
  15. if ( type === void 0 ) type = 'error';
  16. if (hasConsole) {
  17. console[type]("[vue-paginate]: " + msg + " " + (
  18. vm ? formatLocation(formatComponentName(vm)) : ''
  19. ))
  20. }
  21. }
  22. formatComponentName = function (vm) {
  23. if (vm.$root === vm) {
  24. return 'root instance'
  25. }
  26. var name = vm._isVue
  27. ? vm.$options.name || vm.$options._componentTag
  28. : vm.name
  29. return (
  30. (name ? ("component <" + name + ">") : "anonymous component") +
  31. (vm._isVue && vm.$options.__file ? (" at " + (vm.$options.__file)) : '')
  32. )
  33. }
  34. var formatLocation = function (str) {
  35. if (str === 'anonymous component') {
  36. str += " - use the \"name\" option for better debugging messages."
  37. }
  38. return ("\n(found in " + str + ")")
  39. }
  40. var Paginate = {
  41. name: 'paginate',
  42. props: {
  43. name: {
  44. type: String,
  45. required: true
  46. },
  47. list: {
  48. type: Array,
  49. required: true
  50. },
  51. per: {
  52. type: Number,
  53. default: 3,
  54. validator: function validator (value) {
  55. return value > 0
  56. }
  57. },
  58. tag: {
  59. type: String,
  60. default: 'ul'
  61. },
  62. container: {
  63. type: Object,
  64. default: null
  65. }
  66. },
  67. data: function data () {
  68. return {
  69. initialListSize: this.list.length
  70. }
  71. },
  72. computed: {
  73. parent: function parent () {
  74. return this.container ? this.container : this.$parent
  75. },
  76. currentPage: {
  77. get: function get () {
  78. if (this.parent.paginate[this.name]) {
  79. return this.parent.paginate[this.name].page
  80. }
  81. },
  82. set: function set (page) {
  83. this.parent.paginate[this.name].page = page
  84. }
  85. },
  86. pageItemsCount: function pageItemsCount () {
  87. var numOfItems = this.list.length
  88. var first = this.currentPage * this.per + 1
  89. var last = Math.min((this.currentPage * this.per) + this.per, numOfItems)
  90. return (first + "-" + last + " of " + numOfItems)
  91. },
  92. lastPage: function lastPage () {
  93. return Math.ceil(this.list.length / this.per)
  94. }
  95. },
  96. mounted: function mounted () {
  97. if (this.per <= 0) {
  98. warn(("<paginate name=\"" + (this.name) + "\"> 'per' prop can't be 0 or less."), this.parent)
  99. }
  100. if (!this.parent.paginate[this.name]) {
  101. warn(("'" + (this.name) + "' is not registered in 'paginate' array."), this.parent)
  102. return
  103. }
  104. this.paginateList()
  105. },
  106. watch: {
  107. currentPage: function currentPage () {
  108. this.paginateList()
  109. },
  110. list: function list () {
  111. if (this.currentPage >= this.lastPage) {
  112. this.currentPage = this.lastPage - 1
  113. }
  114. this.paginateList()
  115. },
  116. per: function per () {
  117. this.currentPage = 0
  118. this.paginateList()
  119. }
  120. },
  121. methods: {
  122. paginateList: function paginateList () {
  123. var index = this.currentPage * this.per
  124. var paginatedList = this.list.slice(index, index + this.per)
  125. this.parent.paginate[this.name].list = paginatedList
  126. },
  127. goToPage: function goToPage (page) {
  128. var lastPage = Math.ceil(this.list.length / this.per)
  129. if (page > lastPage) {
  130. warn(("You cannot go to page " + page + ". The last page is " + lastPage + "."), this.parent)
  131. return
  132. }
  133. this.currentPage = page - 1
  134. }
  135. },
  136. render: function render (h) {
  137. return h(this.tag, {}, this.$slots.default)
  138. }
  139. }
  140. var LEFT_ARROW = '«'
  141. var RIGHT_ARROW = '»'
  142. var ELLIPSES = '…'
  143. var LimitedLinksGenerator = function LimitedLinksGenerator (listOfPages, currentPage, limit) {
  144. this.listOfPages = listOfPages
  145. this.lastPage = listOfPages.length - 1
  146. this.currentPage = currentPage === this.lastPage
  147. ? this.lastPage - 1
  148. : currentPage
  149. this.limit = limit
  150. };
  151. LimitedLinksGenerator.prototype.generate = function generate () {
  152. var firstHalf = this._buildFirstHalf()
  153. var secondHalf = this._buildSecondHalf()
  154. return firstHalf.concat( secondHalf)
  155. };
  156. LimitedLinksGenerator.prototype._buildFirstHalf = function _buildFirstHalf () {
  157. var firstHalf = this._allPagesButLast()
  158. .slice(
  159. this._currentChunkIndex(),
  160. this._currentChunkIndex() + this.limit
  161. )
  162. // Add backward ellipses with first page if needed
  163. if (this.currentPage >= this.limit) {
  164. firstHalf.unshift(ELLIPSES)
  165. firstHalf.unshift(0)
  166. }
  167. // Add ellipses if needed
  168. if (this.lastPage - this.limit > this._currentChunkIndex()) {
  169. firstHalf.push(ELLIPSES)
  170. }
  171. return firstHalf
  172. };
  173. LimitedLinksGenerator.prototype._buildSecondHalf = function _buildSecondHalf () {
  174. var secondHalf = [this.lastPage]
  175. return secondHalf
  176. };
  177. LimitedLinksGenerator.prototype._currentChunkIndex = function _currentChunkIndex () {
  178. var currentChunk = Math.floor(this.currentPage / this.limit)
  179. return currentChunk * this.limit
  180. };
  181. LimitedLinksGenerator.prototype._allPagesButLast = function _allPagesButLast () {
  182. var this$1 = this;
  183. return this.listOfPages.filter(function (n) { return n !== this$1.lastPage; })
  184. };
  185. var PaginateLinks = {
  186. name: 'paginate-links',
  187. props: {
  188. for: {
  189. type: String,
  190. required: true
  191. },
  192. limit: {
  193. type: Number,
  194. default: 0
  195. },
  196. simple: {
  197. type: Object,
  198. default: null,
  199. validator: function validator (obj) {
  200. return obj.prev && obj.next
  201. }
  202. },
  203. stepLinks: {
  204. type: Object,
  205. default: function () {
  206. return {
  207. prev: LEFT_ARROW,
  208. next: RIGHT_ARROW
  209. }
  210. },
  211. validator: function validator$1 (obj) {
  212. return obj.prev && obj.next
  213. }
  214. },
  215. showStepLinks: {
  216. type: Boolean
  217. },
  218. hideSinglePage: {
  219. type: Boolean
  220. },
  221. classes: {
  222. type: Object,
  223. default: null
  224. },
  225. async: {
  226. type: Boolean,
  227. default: false
  228. },
  229. container: {
  230. type: Object,
  231. default: null
  232. }
  233. },
  234. data: function data () {
  235. return {
  236. listOfPages: [],
  237. numberOfPages: 0,
  238. target: null
  239. }
  240. },
  241. computed: {
  242. parent: function parent () {
  243. return this.container ? this.container.el : this.$parent
  244. },
  245. state: function state () {
  246. return this.container ? this.container.state : this.$parent.paginate[this.for]
  247. },
  248. currentPage: {
  249. get: function get () {
  250. if (this.state) {
  251. return this.state.page
  252. }
  253. },
  254. set: function set (page) {
  255. this.state.page = page
  256. }
  257. }
  258. },
  259. mounted: function mounted () {
  260. var this$1 = this;
  261. if (this.simple && this.limit) {
  262. warn(("<paginate-links for=\"" + (this.for) + "\"> 'simple' and 'limit' props can't be used at the same time. In this case, 'simple' will take precedence, and 'limit' will be ignored."), this.parent, 'warn')
  263. }
  264. if (this.simple && !this.simple.next) {
  265. warn(("<paginate-links for=\"" + (this.for) + "\"> 'simple' prop doesn't contain 'next' value."), this.parent)
  266. }
  267. if (this.simple && !this.simple.prev) {
  268. warn(("<paginate-links for=\"" + (this.for) + "\"> 'simple' prop doesn't contain 'prev' value."), this.parent)
  269. }
  270. if (this.stepLinks && !this.stepLinks.next) {
  271. warn(("<paginate-links for=\"" + (this.for) + "\"> 'step-links' prop doesn't contain 'next' value."), this.parent)
  272. }
  273. if (this.stepLinks && !this.stepLinks.prev) {
  274. warn(("<paginate-links for=\"" + (this.for) + "\"> 'step-links' prop doesn't contain 'prev' value."), this.parent)
  275. }
  276. this.$nextTick(function () {
  277. this$1.updateListOfPages()
  278. })
  279. },
  280. watch: {
  281. 'state': {
  282. handler: function handler () {
  283. this.updateListOfPages()
  284. },
  285. deep: true
  286. },
  287. currentPage: function currentPage (toPage, fromPage) {
  288. this.$emit('change', toPage + 1, fromPage + 1)
  289. }
  290. },
  291. methods: {
  292. updateListOfPages: function updateListOfPages () {
  293. this.target = getTargetPaginateComponent(this.parent.$children, this.for)
  294. if (!this.target) {
  295. if (this.async) { return }
  296. warn(("<paginate-links for=\"" + (this.for) + "\"> can't be used without its companion <paginate name=\"" + (this.for) + "\">"), this.parent)
  297. warn("To fix that issue you may need to use :async=\"true\" on <paginate-links> component to allow for asyncronous rendering", this.parent, 'warn')
  298. return
  299. }
  300. this.numberOfPages = Math.ceil(this.target.list.length / this.target.per)
  301. this.listOfPages = getListOfPageNumbers(this.numberOfPages)
  302. }
  303. },
  304. render: function render (h) {
  305. var this$1 = this;
  306. if (!this.target && this.async) { return null }
  307. var links = this.simple
  308. ? getSimpleLinks(this, h)
  309. : this.limit > 1
  310. ? getLimitedLinks(this, h)
  311. : getFullLinks(this, h)
  312. if (this.hideSinglePage && this.numberOfPages <= 1) {
  313. return null
  314. }
  315. var el = h('ul', {
  316. class: ['paginate-links', this.for]
  317. }, links)
  318. if (this.classes) {
  319. this.$nextTick(function () {
  320. addAdditionalClasses(el.elm, this$1.classes)
  321. })
  322. }
  323. return el
  324. }
  325. }
  326. function getFullLinks (vm, h) {
  327. var allLinks = vm.showStepLinks
  328. ? [vm.stepLinks.prev ].concat( vm.listOfPages, [vm.stepLinks.next])
  329. : vm.listOfPages
  330. return allLinks.map(function (link) {
  331. var data = {
  332. on: {
  333. click: function (e) {
  334. e.preventDefault()
  335. vm.currentPage = getTargetPageForLink(
  336. link,
  337. vm.limit,
  338. vm.currentPage,
  339. vm.listOfPages,
  340. vm.stepLinks
  341. )
  342. }
  343. }
  344. }
  345. var liClasses = getClassesForLink(
  346. link,
  347. vm.currentPage,
  348. vm.listOfPages.length - 1,
  349. vm.stepLinks
  350. )
  351. var linkText = link === vm.stepLinks.next || link === vm.stepLinks.prev
  352. ? link
  353. : link + 1 // it means it's a number
  354. return h('li', { class: liClasses }, [h('a', data, linkText)])
  355. })
  356. }
  357. function getLimitedLinks (vm, h) {
  358. var limitedLinks = new LimitedLinksGenerator(
  359. vm.listOfPages,
  360. vm.currentPage,
  361. vm.limit,
  362. vm.stepLinks
  363. ).generate()
  364. limitedLinks = vm.showStepLinks
  365. ? [vm.stepLinks.prev ].concat( limitedLinks, [vm.stepLinks.next])
  366. : limitedLinks
  367. var limitedLinksMetadata = getLimitedLinksMetadata(limitedLinks)
  368. return limitedLinks.map(function (link, index) {
  369. var data = {
  370. on: {
  371. click: function (e) {
  372. e.preventDefault()
  373. vm.currentPage = getTargetPageForLink(
  374. link,
  375. vm.limit,
  376. vm.currentPage,
  377. vm.listOfPages,
  378. vm.stepLinks,
  379. limitedLinksMetadata[index]
  380. )
  381. }
  382. }
  383. }
  384. var liClasses = getClassesForLink(
  385. link,
  386. vm.currentPage,
  387. vm.listOfPages.length - 1,
  388. vm.stepLinks
  389. )
  390. // If the link is a number,
  391. // then incremented by 1 (since it's 0 based).
  392. // otherwise, do nothing (so, it's a symbol).
  393. var text = (link === parseInt(link, 10)) ? link + 1 : link
  394. return h('li', { class: liClasses }, [h('a', data, text)])
  395. })
  396. }
  397. function getSimpleLinks (vm, h) {
  398. var lastPage = vm.listOfPages.length - 1
  399. var prevData = {
  400. on: {
  401. click: function (e) {
  402. e.preventDefault()
  403. if (vm.currentPage > 0) { vm.currentPage -= 1 }
  404. }
  405. }
  406. }
  407. var nextData = {
  408. on: {
  409. click: function (e) {
  410. e.preventDefault()
  411. if (vm.currentPage < lastPage) { vm.currentPage += 1 }
  412. }
  413. }
  414. }
  415. var nextListData = { class: ['next', vm.currentPage >= lastPage ? 'disabled' : ''] }
  416. var prevListData = { class: ['prev', vm.currentPage <= 0 ? 'disabled' : ''] }
  417. var prevLink = h('li', prevListData, [h('a', prevData, vm.simple.prev)])
  418. var nextLink = h('li', nextListData, [h('a', nextData, vm.simple.next)])
  419. return [prevLink, nextLink]
  420. }
  421. function getTargetPaginateComponent (children, targetName) {
  422. return children
  423. .filter(function (child) { return (child.$vnode.componentOptions.tag === 'paginate'); })
  424. .find(function (child) { return child.name === targetName; })
  425. }
  426. function getListOfPageNumbers (numberOfPages) {
  427. // converts number of pages into an array
  428. // that contains each individual page number
  429. // For Example: 4 => [0, 1, 2, 3]
  430. return Array.apply(null, { length: numberOfPages })
  431. .map(function (val, index) { return index; })
  432. }
  433. function getClassesForLink(link, currentPage, lastPage, ref) {
  434. var prev = ref.prev;
  435. var next = ref.next;
  436. var liClass = []
  437. if (link === prev) {
  438. liClass.push('left-arrow')
  439. } else if (link === next) {
  440. liClass.push('right-arrow')
  441. } else if (link === ELLIPSES) {
  442. liClass.push('ellipses')
  443. } else {
  444. liClass.push('number')
  445. }
  446. if (link === currentPage) {
  447. liClass.push('active')
  448. }
  449. if (link === prev && currentPage <= 0) {
  450. liClass.push('disabled')
  451. } else if (link === next && currentPage >= lastPage) {
  452. liClass.push('disabled')
  453. }
  454. return liClass
  455. }
  456. function getTargetPageForLink (link, limit, currentPage, listOfPages, ref, metaData) {
  457. var prev = ref.prev;
  458. var next = ref.next;
  459. if ( metaData === void 0 ) metaData = null;
  460. var currentChunk = Math.floor(currentPage / limit)
  461. if (link === prev) {
  462. return (currentPage - 1) < 0 ? 0 : currentPage - 1
  463. } else if (link === next) {
  464. return (currentPage + 1 > listOfPages.length - 1)
  465. ? listOfPages.length - 1
  466. : currentPage + 1
  467. } else if (metaData && metaData === 'right-ellipses') {
  468. return (currentChunk + 1) * limit
  469. } else if (metaData && metaData === 'left-ellipses') {
  470. var chunkContent = listOfPages.slice(currentChunk * limit, currentChunk * limit + limit)
  471. var isLastPage = currentPage === listOfPages.length - 1
  472. if (isLastPage && chunkContent.length === 1) {
  473. currentChunk--
  474. }
  475. return (currentChunk - 1) * limit + limit - 1
  476. }
  477. // which is number
  478. return link
  479. }
  480. /**
  481. * Mainly used here to check whether the displayed
  482. * ellipses is for showing previous or next links
  483. */
  484. function getLimitedLinksMetadata (limitedLinks) {
  485. return limitedLinks.map(function (link, index) {
  486. if (link === ELLIPSES && limitedLinks[index - 1] === 0) {
  487. return 'left-ellipses'
  488. } else if (link === ELLIPSES && limitedLinks[index - 1] !== 0) {
  489. return 'right-ellipses'
  490. }
  491. return link
  492. })
  493. }
  494. function addAdditionalClasses (linksContainer, classes) {
  495. Object.keys(classes).forEach(function (selector) {
  496. if (selector === 'ul') {
  497. var selectorValue = classes['ul']
  498. if (Array.isArray(selectorValue)) {
  499. selectorValue.forEach(function (c) { return linksContainer.classList.add(c); })
  500. } else {
  501. linksContainer.classList.add(selectorValue)
  502. }
  503. }
  504. linksContainer.querySelectorAll(selector).forEach(function (node) {
  505. var selectorValue = classes[selector]
  506. if (Array.isArray(selectorValue)) {
  507. selectorValue.forEach(function (c) { return node.classList.add(c); })
  508. } else {
  509. node.classList.add(selectorValue)
  510. }
  511. })
  512. })
  513. }
  514. function paginateDataGenerator (listNames) {
  515. if ( listNames === void 0 ) listNames = [];
  516. return listNames.reduce(function (curr, listName) {
  517. curr[listName] = {
  518. list: [],
  519. page: 0
  520. }
  521. return curr
  522. }, {})
  523. }
  524. var vuePaginate = {}
  525. vuePaginate.install = function (Vue) {
  526. Vue.mixin({
  527. created: function created () {
  528. if (this.paginate !== 'undefined' && this.paginate instanceof Array) {
  529. this.paginate = paginateDataGenerator(this.paginate)
  530. }
  531. },
  532. methods: {
  533. paginated: function paginated (listName) {
  534. if (!this.paginate || !this.paginate[listName]) {
  535. warn(("'" + listName + "' is not registered in 'paginate' array."), this)
  536. return
  537. }
  538. return this.paginate[listName].list
  539. }
  540. }
  541. })
  542. Vue.component('paginate', Paginate)
  543. Vue.component('paginate-links', PaginateLinks)
  544. }
  545. if (typeof window !== 'undefined' && window.Vue) {
  546. window.Vue.use(vuePaginate)
  547. }
  548. return vuePaginate;
  549. }));