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.

136 lines
3.5 KiB

  1. import LabelService from '@/services/label.service'
  2. export const state = () => ({
  3. items: [],
  4. selected: [],
  5. loading: false
  6. })
  7. export const getters = {
  8. isLabelSelected(state) {
  9. return state.selected.length > 0
  10. },
  11. shortkeys() {
  12. return '0123456789abcdefghijklmnopqrstuvwxyz'.split('')
  13. }
  14. }
  15. export const mutations = {
  16. setLabelList(state, payload) {
  17. state.items = payload
  18. },
  19. addLabel(state, label) {
  20. state.items.unshift(label)
  21. },
  22. deleteLabel(state, labelId) {
  23. state.items = state.items.filter(item => item.id !== labelId)
  24. },
  25. updateSelected(state, selected) {
  26. state.selected = selected
  27. },
  28. updateLabel(state, label) {
  29. const item = state.items.find(item => item.id === label.id)
  30. Object.assign(item, label)
  31. },
  32. resetSelected(state) {
  33. state.selected = []
  34. },
  35. setLoading(state, payload) {
  36. state.loading = payload
  37. }
  38. }
  39. export const actions = {
  40. getLabelList({ commit }, payload) {
  41. commit('setLoading', true)
  42. return LabelService.getLabelList(payload.projectId)
  43. .then((response) => {
  44. commit('setLabelList', response.data)
  45. })
  46. .catch((error) => {
  47. alert(error)
  48. })
  49. .finally(() => {
  50. commit('setLoading', false)
  51. })
  52. },
  53. createLabel({ commit }, data) {
  54. return LabelService.addLabel(data.projectId, data)
  55. .then((response) => {
  56. commit('addLabel', response.data)
  57. })
  58. },
  59. updateLabel({ commit }, data) {
  60. LabelService.updateLabel(data.projectId, data.id, data)
  61. .then((response) => {
  62. commit('updateLabel', response.data)
  63. })
  64. .catch((error) => {
  65. alert(error)
  66. })
  67. },
  68. deleteLabel({ commit, state }, projectId) {
  69. for (const label of state.selected) {
  70. LabelService.deleteLabel(projectId, label.id)
  71. .then((response) => {
  72. commit('deleteLabel', label.id)
  73. })
  74. .catch((error) => {
  75. alert(error)
  76. })
  77. }
  78. commit('resetSelected')
  79. },
  80. importLabels({ commit }, payload) {
  81. commit('setLoading', true)
  82. const formData = new FormData()
  83. formData.append('file', payload.file)
  84. const reader = new FileReader()
  85. reader.onload = (e) => {
  86. const labels = JSON.parse(e.target.result)
  87. for (const label of labels) {
  88. LabelService.addLabel(payload.projectId, label)
  89. .then((response) => {
  90. commit('addLabel', response.data)
  91. })
  92. }
  93. }
  94. reader.readAsText(payload.file)
  95. commit('setLoading', false)
  96. },
  97. exportLabels({ commit }, payload) {
  98. commit('setLoading', true)
  99. LabelService.getLabelList(payload.projectId)
  100. .then((response) => {
  101. const url = window.URL.createObjectURL(new Blob([JSON.stringify(response.data)]))
  102. const link = document.createElement('a')
  103. link.href = url
  104. link.setAttribute('download', `project_${payload.projectId}_labels.json`)
  105. document.body.appendChild(link)
  106. link.click()
  107. })
  108. .catch((error) => {
  109. alert(error)
  110. })
  111. .finally(() => {
  112. commit('setLoading', false)
  113. })
  114. },
  115. uploadLabel({ commit, dispatch }, data) {
  116. commit('setLoading', true)
  117. const formData = new FormData()
  118. formData.append('file', data.file)
  119. const config = {
  120. headers: {
  121. 'Content-Type': 'multipart/form-data'
  122. }
  123. }
  124. return LabelService.uploadFile(data.projectId, formData, config)
  125. .then((response) => {
  126. dispatch('getLabelList', data)
  127. })
  128. .finally(() => {
  129. commit('setLoading', false)
  130. })
  131. }
  132. }