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.

59 lines
1.3 KiB

5 years ago
  1. export const state = () => ({
  2. limit: 10,
  3. page: 1,
  4. projectId: null
  5. })
  6. export const getters = {
  7. offset(state) {
  8. return Math.floor((state.page - 1) / state.limit) * state.limit
  9. },
  10. current(state) {
  11. return (state.page - 1) % state.limit
  12. },
  13. page(state) {
  14. return state.page
  15. },
  16. limit(state) {
  17. return state.limit
  18. }
  19. }
  20. export const mutations = {
  21. updatePage(state, page) {
  22. state.page = page
  23. },
  24. savePage(state) {
  25. const checkpoint = {}
  26. checkpoint[state.projectId] = state.page
  27. localStorage.setItem('checkpoint', JSON.stringify(checkpoint))
  28. },
  29. loadPage(state) {
  30. const checkpoint = JSON.parse(localStorage.getItem('checkpoint')) || {}
  31. state.page = checkpoint[state.projectId] ? checkpoint[state.projectId] : 1
  32. },
  33. setProjectId(state, projectId) {
  34. state.projectId = projectId
  35. }
  36. }
  37. export const actions = {
  38. prevPage({ commit, state }) {
  39. const page = Math.max(state.page - 1, 1)
  40. commit('updatePage', page)
  41. commit('savePage')
  42. },
  43. nextPage({ commit, state }, total) {
  44. const page = Math.min(state.page + 1, total)
  45. commit('updatePage', page)
  46. commit('savePage')
  47. },
  48. movePage({ commit }, newPage) {
  49. commit('updatePage', newPage)
  50. commit('savePage')
  51. },
  52. initPage({ commit }, payload) {
  53. commit('setProjectId', payload.projectId)
  54. commit('loadPage')
  55. }
  56. }