mutations.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import * as types from './mutation-type'
  2. import app from '../../util/appConst'
  3. import Element from '../../model/Element'
  4. const mutations = {
  5. [types.SET_CUR_EDITOR_ELEMENT](state, data) {
  6. state.editorElement = data
  7. },
  8. [types.ADD_PIC_ELEMENT](state, data) {
  9. state.editorPage.elements.push(new Element(data))
  10. },
  11. [types.ADD_PAGE_QUESTION](state, data) {
  12. state.editorPage.questions = data
  13. },
  14. [types.SET_BG_ELEMENT](state, data) {
  15. let haveBG = false
  16. state.editorPage.elements.findIndex((value, index, arr) => {
  17. if (value.type === 'bg') {
  18. haveBG = true
  19. value.imgSrc = data.imgSrc
  20. value.width = data.width
  21. value.height = data.height
  22. }
  23. })
  24. if (!haveBG) {
  25. state.editorPage.elements.push(new Element(data))
  26. }
  27. },
  28. // 播放动画
  29. [types.PLAY_ANIMATE](state) {
  30. let elements = state.editorPage.elements
  31. let editingElement = state.editorElement
  32. if (editingElement && editingElement.animatedName) {
  33. // 如存在有动画的选择元素
  34. editingElement.playing = true
  35. } else if (!editingElement) {
  36. // 不存在被选择的元素
  37. elements.forEach(v => {
  38. v.playing = true
  39. })
  40. }
  41. },
  42. // 停止播放动画
  43. [types.STOP_ANIMATE](state, data) {
  44. if (data instanceof Array) {
  45. // 该页元素
  46. data.forEach(v => {
  47. v['playing'] = false
  48. })
  49. } else if (data instanceof Object) {
  50. // 单个元素
  51. data['playing'] = false
  52. } else {
  53. // 不传参情况
  54. state['editorPage']['elements'].forEach(v => {
  55. v['playing'] = false
  56. })
  57. }
  58. },
  59. [types.ADD_PAGE](state, page) {
  60. state.editorTheme.pages.push(page);
  61. },
  62. [types.ADD_PAGE_POSITION](state, page) {
  63. state.editorTheme.pages.splice(page.position, 0, page);
  64. },
  65. [types.DELETE_PAGE](state, data) {
  66. state.editorTheme.pages.findIndex((value, index, arr) => {
  67. if (value === data) {
  68. state.editorTheme.pages.splice(index, 1)
  69. }
  70. })
  71. },
  72. [types.SET_CUR_EDITOR_PAGE](state, data) {
  73. state.editorPage = data
  74. },
  75. [types.GET_USER_THEME_LIST](state, data) {
  76. state.themeList = data
  77. },
  78. [types.SET_CUR_EDITOR_THEME](state, data) {
  79. state.editorTheme = data
  80. },
  81. [types.UPDATE_THEME_DES](state, { title, description, canvasHeight }) {
  82. state.editorTheme.title = title
  83. state.editorTheme.description = description
  84. state.editorTheme.canvasHeight = canvasHeight
  85. },
  86. [types.DELETE_ELEMENT](state, data) {
  87. state.editorPage.elements.findIndex((value, index, arr) => {
  88. if (value === data) {
  89. state.editorPage.elements.splice(index, 1)
  90. state.editorElement = null
  91. }
  92. })
  93. },
  94. [types.DELETE_ELEMENT_QID](state, questionId) {
  95. var elements = state.editorPage.elements;
  96. var newElements = [];
  97. elements.findIndex((value, index, arr) => {
  98. if (value.questionId !== questionId) {
  99. newElements.push(value);
  100. }
  101. })
  102. state.editorPage.elements = newElements;
  103. state.editorElement = null
  104. },
  105. [types.CREATE_THEME](state, data) {
  106. state.themeList.push(data)
  107. },
  108. [types.ADD_THEME_SUCCESS](state, data) {
  109. state.editorTheme._id = data._id
  110. },
  111. [types.UPDATE_THEME_SUCCESS](state, data) {
  112. },
  113. [types.SAVE_PIC](state, data) {
  114. state.editorElement.imgSrc = app.APP_MALL_API_URL + data.filePath
  115. },
  116. [types.GET_PAGE_THEMEID](state, data) {
  117. state.editorPage = data
  118. },
  119. [types.CLEAN_BG](state) {
  120. state.editorPage.elements.findIndex((value, index, arr) => {
  121. if (value && value.type === 'bg') {
  122. state.editorPage.elements.splice(index, 1)
  123. }
  124. })
  125. },
  126. [types.CLEAN_ELE](state, ele) {
  127. state.editorPage.elements.splice(state.editorPage.elements.indexOf(ele), 1)
  128. },
  129. [types.FETCH_PIC_LIST](state, picList) {
  130. state.picList = picList
  131. },
  132. [types.PUSH_PIC_LIST](state, ele) {
  133. state.picList.push(ele)
  134. },
  135. [types.CLEAN_PIC_LIST](state) {
  136. state.picList = []
  137. },
  138. [types.SORTELEMENTS](state, data) {
  139. let element = state.editorPage.elements[data.start]
  140. let end = parseInt(data.end)
  141. if (end !== -1) {
  142. state.editorPage.elements.splice(data.start, 1)
  143. if (end >= state.editorPage.elements.length) {
  144. state.editorPage.elements.push(element)
  145. } else {
  146. state.editorPage.elements.splice(end, 0, element)
  147. }
  148. state.editorPage.elements.map((value, index, arr) => {
  149. value.zindex = index + 1
  150. })
  151. }
  152. },
  153. [types.DELETE_THEME](state, data) {
  154. state.themeList.findIndex((value, index, arr) => {
  155. if (value === data) {
  156. state.themeList.splice(index, 1)
  157. }
  158. })
  159. },
  160. [types.SORTELEMENTS_BY_ZINDEX](state, data) {
  161. state.editorPage.elements.sort((a, b) => a['zindex'] - b['zindex'])
  162. state.editorPage.elements.forEach((v, i, arr) => {
  163. arr[i]['zindex'] = i + 1
  164. })
  165. }
  166. }
  167. export default mutations