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.

70 lines
1.6 KiB

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