| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 | 1x
1x
1x
24x
24x
24x
1x
1x
5x
1x
4x
4x
4x
1x
1x
2x
2x
6x
1x
5x
1x
4x
1x
3x
4x
4x
1x
3x
1x
1x
1x
1x
1x
1x
1x
15x
| import {
CREATE_PLAYLIST, DROP_DRAG_CONTENTS_ITEM, START_DRAG_CONTENTS_ITEM, DELETE_CONTENT_ITEM,
CHANGE_CONTENT_ITEMS_ORDER_START, CHANGE_CONTENT_ITEMS_ORDER_PROCESS, CHANGE_CONTENT_ITEMS_ORDER_END,
UPDATE_CONTENT_ITEM_TRANSITION, UPDATE_CONTENT_ITEM_DURATION, DISCARD_PLAYLIST
} from '../action/playlist';
import {createContentItem} from "../model-factory/content-item";
const initialState = {
name: '',
contentItems: [],
selectedPreviewItemIndex: 0,
selectedPreviewPreviousItemIndex: -1,
resolution: {},
willBeReordered: {},
blankIndex: -1,
dragContentsItem: {},
};
export function playlist(state = initialState, action) {
const {type, name, resolution, contentsItem, index, uuid, transitionEffect, durationSeconds} = action;
const {contentItems, willBeReordered, blankIndex, dragContentsItem} = state;
switch (type) {
case CREATE_PLAYLIST:
return Object.assign({}, state, {name: name, resolution: resolution});
case START_DRAG_CONTENTS_ITEM:
return Object.assign({}, state, {dragContentsItem: contentsItem});
case DROP_DRAG_CONTENTS_ITEM: {
if (isEmptyObject(dragContentsItem)) {
return state;
}
const contentItem = createContentItem(dragContentsItem, dragContentsItem.durationSeconds);
const newContentItem = [...state.contentItems, contentItem];
return Object.assign({}, state, {contentItems: newContentItem, dragContentsItem: {}});
}
case DELETE_CONTENT_ITEM: {
const newContentItems = [...contentItems.slice(0, index), ...contentItems.slice(index + 1, contentItems.length)];
return Object.assign({}, state, {contentItems: newContentItems});
}
case CHANGE_CONTENT_ITEMS_ORDER_START: {
const willBeReordered = {...contentItems[index]};
return Object.assign({}, state, {willBeReordered, blankIndex: index});
}
case CHANGE_CONTENT_ITEMS_ORDER_PROCESS: {
if (isEmptyObject(willBeReordered)) {
return state;
}
if (blankIndex === index) {
return state;
}
let newContentItems;
if (index < blankIndex) {
newContentItems = [
...contentItems.slice(0, index),
willBeReordered,
...contentItems.slice(index, blankIndex),
...contentItems.slice(blankIndex + 1, contentItems.length)
];
} else {
newContentItems = [
...contentItems.slice(0, blankIndex),
...contentItems.slice(blankIndex + 1, index + 1),
willBeReordered,
...contentItems.slice(index + 1, contentItems.length)
];
}
return Object.assign({}, state, {blankIndex: index, contentItems: newContentItems});
}
case CHANGE_CONTENT_ITEMS_ORDER_END: {
if (isEmptyObject(willBeReordered)) {
return state;
}
return Object.assign({}, state, {blankIndex: -1, willBeReordered: {}});
}
case UPDATE_CONTENT_ITEM_TRANSITION:
contentItems.find(contentItem => contentItem.uuid === uuid).transitionEffect = transitionEffect;
return Object.assign({}, state, {contentItems: [...contentItems]});
case UPDATE_CONTENT_ITEM_DURATION:
contentItems.find(contentItem => contentItem.uuid === uuid).durationSeconds = durationSeconds;
return Object.assign({}, state, {contentItems: [...contentItems]});
case DISCARD_PLAYLIST:
return initialState;
default:
return state;
}
}
const isEmptyObject = (test) => {
return Object.keys(test).length === 0;
}; |