actions.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. commit(types.PUSH_PIC_LIST, data)
  88. }
  89. /**
  90. * 清除背景
  91. * @param commit
  92. */
  93. export const cleanBG = ({commit}) => {
  94. commit(types.CLEAN_BG)
  95. }
  96. export const cleanEle = ({commit}, ele) => {
  97. commit(types.CLEAN_ELE, ele)
  98. }
  99. /**
  100. * 复制页面
  101. * @param commit
  102. */
  103. export const copyPage = ({commit}, data) => {
  104. var page = tools.vue2json(data)
  105. commit(types.ADD_PAGE, page)
  106. }
  107. /**
  108. * 删除页面
  109. * @param commit
  110. */
  111. export const delPage = ({commit}, page) => {
  112. commit(types.DELETE_PAGE, page)
  113. }
  114. export const getPageByThemeId = ({dispatch, commit}, id) => {
  115. api.getPageByThemeId(id).then((res) => {
  116. commit(types.SET_CUR_EDITOR_THEME, res)
  117. commit(types.SET_CUR_EDITOR_PAGE, res.pages[0])
  118. }).then(() => {
  119. dispatch('sortElementsByZindex')
  120. })
  121. }
  122. export const setEditorElement = ({commit}, element) => {
  123. commit(types.SET_CUR_EDITOR_ELEMENT, element)
  124. }
  125. // 删除元素
  126. export const deleteElement = ({commit}, element) => {
  127. commit(types.DELETE_ELEMENT, element)
  128. }
  129. export const deleteSelectedElement = ({commit, state}) => {
  130. commit(types.DELETE_ELEMENT, state.editorElement)
  131. }
  132. export const playAnimate = ({state, commit, getters}) => {
  133. commit(types.PLAY_ANIMATE)
  134. let target = getters['editingElement'] || getters['editingPageElements'] || null
  135. let time = 0
  136. if (target instanceof Array) {
  137. target.forEach(v => {
  138. time = v['animatedName'] && (v['duration'] + v['delay']) > time ? (v['duration'] + v['delay']) : time
  139. })
  140. } else if (target instanceof Object) {
  141. time = (target['duration'] + target['delay'])
  142. }
  143. setTimeout(() => {
  144. commit(types.STOP_ANIMATE, target)
  145. }, time * 1000)
  146. }
  147. export const getPicListByThemeId = ({commit}, _id) => {
  148. api.getPicListByThemeId(_id).then((res) => {
  149. commit(types.FETCH_PIC_LIST, res)
  150. })
  151. }
  152. export const cleanPicList = ({commit}) => {
  153. commit(types.CLEAN_PIC_LIST)
  154. }
  155. export const sortElementsByZindex = ({commit}, location) => {
  156. commit(types.SORTELEMENTS_BY_ZINDEX, location)
  157. }
  158. export const deleteTheme = ({commit}, theme) => {
  159. return Promise.resolve(api.delTheme(theme).then((res) => {
  160. commit(types.DELETE_THEME, theme)
  161. }))
  162. }