actions.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import * as types from './mutation-type'
  2. import api from '../../api/editor'
  3. import Page from '../../model/Page'
  4. import Theme from '../../model/Theme'
  5. import Element from '../../model/Element'
  6. import tools from '../../util/tools'
  7. /**
  8. * 保存页面数据
  9. */
  10. export const saveTheme = ({commit}, theme) => {
  11. if (theme && theme._id) {
  12. return Promise.resolve(api.updateTheme(theme).then((res) => {
  13. commit(types.UPDATE_THEME_SUCCESS, res)
  14. }))
  15. } else {
  16. return Promise.resolve(api.saveTheme(theme).then((res) => {
  17. commit(types.ADD_THEME_SUCCESS, res)
  18. }))
  19. }
  20. }
  21. /**
  22. * 获取用户所有场景主题
  23. * @param commit
  24. */
  25. export const getUserThemeList = ({commit}, type) => {
  26. api.getUserThemeList(type).then((res) => {
  27. commit(types.GET_USER_THEME_LIST, res)
  28. })
  29. }
  30. /**
  31. * 创建场景主题
  32. * @param commit
  33. */
  34. export const createTheme = ({commit}, type) => {
  35. var theme = new Theme({type: type})
  36. commit(types.CREATE_THEME, theme)
  37. commit(types.SET_CUR_EDITOR_THEME, theme)
  38. }
  39. /**
  40. * 设置当前编辑的主题
  41. */
  42. export const setEditorTheme = ({commit}, theme) => {
  43. var newTheme = new Theme(theme)
  44. commit(types.SET_CUR_EDITOR_THEME, newTheme)
  45. }
  46. /**
  47. * 设置当前正在编辑的页面
  48. * @param commit
  49. * @param page
  50. */
  51. export const setEditorPage = ({commit}, page) => {
  52. commit(types.SET_CUR_EDITOR_PAGE, page)
  53. }
  54. /**
  55. * 给主题添加页面
  56. * @param commit
  57. */
  58. export const addPage = ({commit}) => {
  59. var page = new Page()
  60. commit(types.ADD_PAGE, page)
  61. commit(types.SET_CUR_EDITOR_PAGE, page)
  62. }
  63. /**
  64. * 添加页面元素
  65. */
  66. export const addElement = ({commit, state}, data) => {
  67. commit(types.ADD_PIC_ELEMENT, new Element(data))
  68. var list = state.editorPage.elements
  69. var lastIndex = list.length - 1
  70. list[lastIndex]['zindex'] = lastIndex ? list[lastIndex - 1]['zindex'] + 1 : 1
  71. commit(types.SET_CUR_EDITOR_ELEMENT, state.editorPage.elements[lastIndex])
  72. }
  73. /**
  74. * 添加背景图片
  75. */
  76. export const addBGElement = ({commit}, data) => {
  77. var element = new Element(data)
  78. commit(types.SET_BG_ELEMENT, element)
  79. commit(types.SET_CUR_EDITOR_ELEMENT, null)
  80. }
  81. /**
  82. * 保存图片
  83. * @param commit
  84. * @param data
  85. */
  86. export const savePic = ({commit}, data) => {
  87. api.uploadPic(data).then((res) => {
  88. // commit(types.SAVE_PIC, res)
  89. commit(types.PUSH_PIC_LIST, res)
  90. })
  91. }
  92. /**
  93. * 清除背景
  94. * @param commit
  95. */
  96. export const cleanBG = ({commit}) => {
  97. commit(types.CLEAN_BG)
  98. }
  99. export const cleanEle = ({commit}, ele) => {
  100. commit(types.CLEAN_ELE, ele)
  101. }
  102. /**
  103. * 复制页面
  104. * @param commit
  105. */
  106. export const copyPage = ({commit}, data) => {
  107. var page = tools.vue2json(data)
  108. commit(types.ADD_PAGE, page)
  109. }
  110. /**
  111. * 删除页面
  112. * @param commit
  113. */
  114. export const delPage = ({commit}, page) => {
  115. commit(types.DELETE_PAGE, page)
  116. }
  117. export const getPageByThemeId = ({dispatch, commit}, id) => {
  118. api.getPageByThemeId(id).then((res) => {
  119. commit(types.SET_CUR_EDITOR_THEME, res)
  120. commit(types.SET_CUR_EDITOR_PAGE, res.pages[0])
  121. }).then(() => {
  122. dispatch('sortElementsByZindex')
  123. })
  124. }
  125. export const setEditorElement = ({commit}, element) => {
  126. commit(types.SET_CUR_EDITOR_ELEMENT, element)
  127. }
  128. // 删除元素
  129. export const deleteElement = ({commit}, element) => {
  130. commit(types.DELETE_ELEMENT, element)
  131. }
  132. export const deleteSelectedElement = ({commit, state}) => {
  133. commit(types.DELETE_ELEMENT, state.editorElement)
  134. }
  135. export const playAnimate = ({state, commit, getters}) => {
  136. commit(types.PLAY_ANIMATE)
  137. let target = getters['editingElement'] || getters['editingPageElements'] || null
  138. let time = 0
  139. if (target instanceof Array) {
  140. target.forEach(v => {
  141. time = v['animatedName'] && (v['duration'] + v['delay']) > time ? (v['duration'] + v['delay']) : time
  142. })
  143. } else if (target instanceof Object) {
  144. time = (target['duration'] + target['delay'])
  145. }
  146. setTimeout(() => {
  147. commit(types.STOP_ANIMATE, target)
  148. }, time * 1000)
  149. }
  150. export const getPicListByThemeId = ({commit}, _id) => {
  151. api.getPicListByThemeId(_id).then((res) => {
  152. commit(types.FETCH_PIC_LIST, res)
  153. })
  154. }
  155. export const cleanPicList = ({commit}) => {
  156. commit(types.CLEAN_PIC_LIST)
  157. }
  158. export const sortElementsByZindex = ({commit}, location) => {
  159. commit(types.SORTELEMENTS_BY_ZINDEX, location)
  160. }
  161. export const deleteTheme = ({commit}, theme) => {
  162. return Promise.resolve(api.delTheme(theme).then((res) => {
  163. commit(types.DELETE_THEME, theme)
  164. }))
  165. }