|
| 1 | +/** @file redux actions and reducers for project animation metadata. */ |
| 2 | + |
| 3 | +import _ from '../lodash'; |
| 4 | +import utils from '../utils'; |
| 5 | +import {animations as animationsApi} from '../clientApi'; |
| 6 | +import {reportError} from './errorDialogStackModule'; |
| 7 | + |
| 8 | +export const ADD_ANIMATION_AT = 'ADD_ANIMATION_AT'; |
| 9 | +const DELETE_ANIMATION = 'DELETE_ANIMATION'; |
| 10 | +const SET_ANIMATION_NAME = 'SET_ANIMATION_NAME'; |
| 11 | +const SET_INITIAL_ANIMATION_METADATA = 'SET_INITIAL_ANIMATION_METADATA'; |
| 12 | + |
| 13 | + |
| 14 | +export default function animations(state, action) { |
| 15 | + state = state || []; |
| 16 | + |
| 17 | + switch (action.type) { |
| 18 | + |
| 19 | + case ADD_ANIMATION_AT: |
| 20 | + return [].concat( |
| 21 | + state.slice(0, action.index), |
| 22 | + action.animationProps, |
| 23 | + state.slice(action.index)); |
| 24 | + |
| 25 | + case DELETE_ANIMATION: |
| 26 | + return state.filter(function (animation) { |
| 27 | + return animation.key !== action.animationKey; |
| 28 | + }); |
| 29 | + |
| 30 | + case SET_INITIAL_ANIMATION_METADATA: |
| 31 | + return action.metadata; |
| 32 | + |
| 33 | + case SET_ANIMATION_NAME: |
| 34 | + return state.map(function (animState) { |
| 35 | + return animation(animState, action); |
| 36 | + }); |
| 37 | + |
| 38 | + default: |
| 39 | + return state; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +function animation(state, action) { |
| 44 | + state = state || { key: utils.createUuid() }; |
| 45 | + |
| 46 | + switch (action.type) { |
| 47 | + case SET_ANIMATION_NAME: |
| 48 | + if (state.key === action.animationKey) { |
| 49 | + return _.assign({}, state, { |
| 50 | + name: action.name |
| 51 | + }); |
| 52 | + } |
| 53 | + return state; |
| 54 | + |
| 55 | + default: |
| 56 | + return state; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Push full animation metadata into the store, usually on first load |
| 62 | + * from the sources API. |
| 63 | + * |
| 64 | + * @param {Object} metadata |
| 65 | + * @returns {{type: ActionType, metadata: Object}} |
| 66 | + */ |
| 67 | +export function setInitialAnimationMetadata(metadata) { |
| 68 | + return { |
| 69 | + type: SET_INITIAL_ANIMATION_METADATA, |
| 70 | + metadata: metadata |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +export function addAnimation(animationProps) { |
| 75 | + // TODO: Validate animationProps? |
| 76 | + return function (dispatch, getState) { |
| 77 | + dispatch({ |
| 78 | + type: ADD_ANIMATION_AT, |
| 79 | + index: getState().animations.length, |
| 80 | + animationProps: animationProps |
| 81 | + }); |
| 82 | + // TODO: Save project after adding an animation? |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +export function cloneAnimation(animationKey) { |
| 87 | + return function (dispatch, getState) { |
| 88 | + var animations = getState().animations; |
| 89 | + |
| 90 | + var onCloneError = function (errorMessage) { |
| 91 | + dispatch(reportError( |
| 92 | + 'Error copying object ' + animationKey + ': ' + errorMessage)); |
| 93 | + }; |
| 94 | + |
| 95 | + // Track down the source animation and its index in the collection |
| 96 | + var sourceIndex = animations.map(function (a) { return a.key; }).indexOf(animationKey); |
| 97 | + if (sourceIndex < 0) { |
| 98 | + onCloneError('Animation not found'); |
| 99 | + return; |
| 100 | + } |
| 101 | + var sourceAnimation = animations[sourceIndex]; |
| 102 | + var newAnimationKey = utils.createUuid(); |
| 103 | + |
| 104 | + animationsApi.ajax( |
| 105 | + 'PUT', |
| 106 | + newAnimationKey + '.png?src=' + animationKey + '.png', |
| 107 | + function success(xhr) { |
| 108 | + try { |
| 109 | + var response = JSON.parse(xhr.responseText); |
| 110 | + dispatch({ |
| 111 | + type: ADD_ANIMATION_AT, |
| 112 | + index: sourceIndex + 1, |
| 113 | + animationProps: _.assign({}, sourceAnimation, { |
| 114 | + key: newAnimationKey, |
| 115 | + name: sourceAnimation.name + '_copy', // TODO: better generated names |
| 116 | + version: response.versionId |
| 117 | + }) |
| 118 | + }); |
| 119 | + } catch (e) { |
| 120 | + onCloneError(e.message); |
| 121 | + } |
| 122 | + }, |
| 123 | + function error(xhr) { |
| 124 | + onCloneError(xhr.status + ' ' + xhr.statusText); |
| 125 | + }); |
| 126 | + }; |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Delete the specified animation from the project. |
| 131 | + * @param {string} animationKey |
| 132 | + * @returns {function} |
| 133 | + */ |
| 134 | +export function deleteAnimation(animationKey) { |
| 135 | + return function (dispatch) { |
| 136 | + animationsApi.ajax( |
| 137 | + 'DELETE', |
| 138 | + animationKey + '.png', |
| 139 | + function success() { |
| 140 | + dispatch({ |
| 141 | + type: DELETE_ANIMATION, |
| 142 | + animationKey: animationKey |
| 143 | + }); |
| 144 | + // TODO: Save project after deleting an animation? |
| 145 | + }, |
| 146 | + function error(xhr) { |
| 147 | + dispatch(reportError( |
| 148 | + 'Error deleting object ' + animationKey + ': ' + |
| 149 | + xhr.status + ' ' + xhr.statusText)); |
| 150 | + }); |
| 151 | + }; |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * Set the display name of the specified animation. |
| 156 | + * @param {string} animationKey |
| 157 | + * @param {string} name |
| 158 | + * @returns {{type: ActionType, animationKey: string, name: string}} |
| 159 | + */ |
| 160 | +export function setAnimationName(animationKey, name) { |
| 161 | + return { |
| 162 | + type: SET_ANIMATION_NAME, |
| 163 | + animationKey: animationKey, |
| 164 | + name: name |
| 165 | + }; |
| 166 | +} |
0 commit comments