Skip to content

Commit 00ad28a

Browse files
classes: implement ProjectData class.
1 parent 76a2f20 commit 00ad28a

File tree

1 file changed

+293
-0
lines changed

1 file changed

+293
-0
lines changed

src/classes/project-data.js

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
/**
2+
* Copyright (c) 2018-present, Renderforest, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the license found in the
6+
* LICENSE file in the root directory.
7+
*/
8+
9+
const PackageJson = require('../../package.json')
10+
11+
class ProjectData {
12+
/**
13+
* @constructor
14+
* @param {Object} projectDataJson
15+
*/
16+
constructor (projectDataJson) {
17+
this.projectDataJson = projectDataJson
18+
this.setGenerator()
19+
}
20+
21+
/**
22+
* @description Set the generator.
23+
*/
24+
setGenerator () {
25+
this.projectDataJson.data['generator'] = ProjectData.generator
26+
}
27+
28+
/**
29+
* @returns {Object}
30+
* @description Get the raw data.
31+
*/
32+
getRawData () {
33+
return Object.assign({}, this.projectDataJson)
34+
}
35+
36+
/**
37+
* @returns {number}
38+
* @description Get the project id.
39+
*/
40+
getProjectId () {
41+
return this.projectDataJson.projectId
42+
}
43+
44+
/**
45+
* @returns {number}
46+
* @description Get the template id.
47+
*/
48+
getTemplateId () {
49+
return this.projectDataJson.data['templateId']
50+
}
51+
52+
/**
53+
* @returns {boolean}
54+
* @description Check whether is equalizer or not.
55+
*/
56+
isEqualizer () {
57+
return this.projectDataJson.data['equalizer']
58+
}
59+
60+
/**
61+
* @returns {boolean}
62+
* @description Check whether is lego or not.
63+
*/
64+
isLego () {
65+
return this.projectDataJson.data['isLego']
66+
}
67+
68+
/**
69+
* @returns {boolean}
70+
* @description Get the project muteMusic property.
71+
*/
72+
getMuteMusic () {
73+
return this.projectDataJson.data['muteMusic']
74+
}
75+
76+
/**
77+
*
78+
* @param {boolean} muteMusic
79+
* @description Set the project muteMusic property.
80+
*/
81+
setMuteMusic (muteMusic) {
82+
this.projectDataJson.data['muteMusic'] = muteMusic
83+
}
84+
85+
/**
86+
* @returns {Array}
87+
* @description Get the project colors.
88+
*/
89+
getProjectColors () {
90+
return this.projectDataJson.data['projectColors']
91+
}
92+
93+
/**
94+
* @param {Array} projectColors
95+
* @description Set the project colors.
96+
*/
97+
setProjectColors (projectColors) {
98+
this.projectDataJson.data['projectColors'] = projectColors
99+
}
100+
101+
/**
102+
* @returns {Object}
103+
* @description Get the project theme.
104+
*/
105+
getTheme () {
106+
return {
107+
themeVariableName: this.projectDataJson.data['themeVariableName'],
108+
themeVariableValue: this.projectDataJson.data['themeVariableValue']
109+
}
110+
}
111+
112+
/**
113+
* @param {Object} payload
114+
* @param {string} payload.themeVariableName
115+
* @param {string} payload.themeVariableValue
116+
* @description Set the project theme.
117+
*/
118+
setTheme (payload) {
119+
this.projectDataJson.data['themeVariableName'] = payload.themeVariableName
120+
this.projectDataJson.data['themeVariableValue'] = payload.themeVariableValue
121+
}
122+
123+
/**
124+
* @returns {Array}
125+
* @description Get the project sounds.
126+
*/
127+
getSounds () {
128+
return this.projectDataJson.data['sounds']
129+
}
130+
131+
/**
132+
* @param {Array} sounds
133+
* @description Set the project sounds.
134+
*/
135+
setSounds (sounds) {
136+
this.projectDataJson.data['sounds'] = sounds
137+
}
138+
139+
/**
140+
* @returns {string}
141+
* @description Get the project title.
142+
*/
143+
getTitle () {
144+
return this.projectDataJson.data['title']
145+
}
146+
147+
/**
148+
* @returns {Array}
149+
* @description Get screens (add methods on screens & screen areas).
150+
*/
151+
getScreens () {
152+
const screens = this.projectDataJson.data['screens'] || []
153+
return screens.map((screen) => {
154+
return this.constructScreen(screen)
155+
})
156+
}
157+
158+
/**
159+
* @param {Object} screen
160+
* @returns {Object}
161+
* @description Construct screen.
162+
*/
163+
constructScreen (screen) {
164+
const {
165+
id, characterBasedDuration, compositionName, duration, extraVideoSecond, gifBigPath, gifPath, gifThumbnailPath,
166+
hidden, iconAdjustable, isFull, maxDuration, order, path, tags, title, type, areas
167+
} = screen
168+
169+
return {
170+
id,
171+
characterBasedDuration,
172+
compositionName,
173+
duration,
174+
extraVideoSecond,
175+
gifBigPath,
176+
gifPath,
177+
gifThumbnailPath,
178+
hidden,
179+
iconAdjustable,
180+
isFull,
181+
maxDuration,
182+
order,
183+
path,
184+
tags,
185+
title,
186+
type,
187+
getAreas: () => {
188+
return areas.map((area) => {
189+
return this.constructArea(area)
190+
})
191+
}
192+
}
193+
}
194+
195+
/**
196+
* @param {Object} area
197+
* @returns {Object}
198+
* @description Construct area.
199+
*/
200+
constructArea (area) {
201+
const {
202+
id, fileName, height, width, value, cords, title, wordCount, originalHeight, originalWidth, order, type,
203+
mimeType, webpPath, fileType, thumbnailPath, imageCropParams, videoCropParams
204+
} = area
205+
206+
const result = { id, height, width, value, cords, title, wordCount, order, type }
207+
208+
if (area.type === 'text') {
209+
result.setText = (text) => {
210+
area.value = text
211+
}
212+
}
213+
214+
if (area.type === 'image') {
215+
result.fileName = fileName
216+
result.originalHeight = originalHeight
217+
result.originalWidth = originalWidth
218+
result.mimeType = mimeType
219+
result.webpPath = webpPath
220+
result.fileType = fileType
221+
result.thumbnailPath = thumbnailPath
222+
result.imageCropParams = imageCropParams
223+
result.setImage = (image) => {
224+
ProjectData.setAreaImage(area, image)
225+
}
226+
}
227+
228+
if (area.type === 'video') {
229+
result.fileName = fileName
230+
result.originalHeight = originalHeight
231+
result.originalWidth = originalWidth
232+
result.mimeType = mimeType
233+
result.webpPath = webpPath
234+
result.fileType = fileType
235+
result.thumbnailPath = thumbnailPath
236+
result.videoCropParams = videoCropParams
237+
result.setVideo = (video) => {
238+
ProjectData.setAreaVideo(area, video)
239+
}
240+
result.setImage = (image) => {
241+
ProjectData.setAreaImage(area, image)
242+
}
243+
}
244+
245+
return result
246+
}
247+
248+
/**
249+
* @param {Object} area
250+
* @param {{fileName, mime, filePath, webpPath, fileType, thumbnailPath, imageCropParams}} image
251+
* @param {{transform, top, left, width, height}} image.imageCropParams
252+
* @description Set image on area.
253+
*/
254+
static setAreaImage (area, image) {
255+
const { fileName, mime, filePath, webpPath, fileType, thumbnailPath, imageCropParams } = image
256+
const { transform, top, left, width, height } = imageCropParams
257+
258+
area.fileName = fileName
259+
area.mimeType = mime
260+
area.value = filePath
261+
area.webpPath = webpPath
262+
area.fileType = fileType
263+
area.thumbnailPath = thumbnailPath
264+
area.imageCropParams = {
265+
transform: `rotate(${transform}deg)`,
266+
top: `${top}px`,
267+
left: `${left}px`,
268+
width,
269+
height
270+
}
271+
}
272+
273+
/**
274+
* @param {Object} area
275+
* @param {{fileName, mime, filePath, webpPath, fileType, videoCropParams}} video
276+
* @param {{duration, end, mime, start, stockFootageId, thumbnail, thumbnailVideo, videoVoiceTreatment}} video.videoCropParams
277+
* @description Set video on area.
278+
*/
279+
static setAreaVideo (area, video) {
280+
const { fileName, mime, filePath, webpPath, fileType, videoCropParams } = video
281+
282+
area.fileName = fileName
283+
area.mimeType = mime
284+
area.value = filePath
285+
area.webpPath = webpPath
286+
area.fileType = fileType
287+
area.videoCropParams = videoCropParams
288+
}
289+
}
290+
291+
ProjectData.generator = `renderforest/sdk-node/${PackageJson.version}`
292+
293+
module.exports = ProjectData

0 commit comments

Comments
 (0)