vue-paginate.js 17 KB

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