This repository has been archived by the owner on Jan 31, 2018. It is now read-only.
/
project.js
440 lines (373 loc) · 12.4 KB
/
project.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* This Source Code Form is subject to the terms of the MIT license
* If a copy of the MIT license was not distributed with this file, you can
* obtain one at https://raw.github.com/mozilla/butter/master/LICENSE */
define( [ "core/eventmanager", "core/media", "util/sanitizer" ],
function( EventManager, Media, Sanitizer ) {
var __butterStorage = window.localStorage;
function Project( butter ) {
var _this = this,
_id, _name, _template, _author, _description, _dataObject,
_publishUrl, _iframeUrl, _remixedFrom,
// Whether or not a save to server is required (project data has changed)
_isDirty = false,
// Whether or not a backup to storage is required (project data has changed)
_needsBackup = false,
// Whether or not the project is saved to the db and published.
// The notion of "saving" to consumers of this code is unware of
// the save vs. publish distinction. As such, we use isSaved externally
// and isPublished internally, where Publish follows Save and is
// more correct.
_isPublished = false,
// How often to backup data in ms. If 0, no backups are done.
_backupIntervalMS = butter.config.value( "backupInterval" )|0,
// Interval for backups, starts first time user clicks Save.
_backupInterval = -1,
_thumbnail = location.protocol + "//" + location.host + "/resources/icons/fb-logo.png";
function invalidate() {
// Project is dirty, needs save, backup
_isDirty = true;
_needsBackup = true;
// If the project has an id (if it was saved), start backups again
// since they may have been stopped if LocalStorage size limits were
// exceeded.
if ( _id ) {
startBackups();
}
// Let consumers know that the project changed
_this.dispatch( "projectchanged" );
}
// Manage access to project properties. Some we only want
// to be read (and managed by db/butter), others we want to
// affect save logic.
Object.defineProperties( _this, {
"id": {
get: function() {
return _id;
},
enumerable: true
},
"name": {
get: function() {
return _name;
},
set: function( value ) {
if ( value !== _name ) {
_name = value;
invalidate();
}
},
enumerable: true
},
"template": {
get: function() {
return _template;
},
set: function( value ) {
if ( value !== _template ) {
_template = value;
invalidate();
}
},
enumerable: true
},
"author": {
get: function() {
return _author;
},
set: function( value ) {
if ( value !== _author ) {
_author = value;
invalidate();
}
},
enumerable: true
},
"description": {
get: function() {
return _description;
},
set: function( value ) {
if ( value !== _description ) {
_description = value;
invalidate();
}
},
enumerable: true
},
"thumbnail": {
set: function( val ) {
_thumbnail = val;
invalidate();
},
get: function() {
return _thumbnail;
},
enumerable: true
},
"data": {
get: function() {
// Memoize value, since it doesn't always change
if ( !_dataObject || _isDirty ) {
var exportJSONMedia = [];
for ( var i = 0; i < butter.media.length; ++i ) {
exportJSONMedia.push( butter.media[ i ].json );
}
_dataObject = {
targets: butter.serializeTargets(),
media: exportJSONMedia
};
}
return _dataObject;
},
enumerable: true
},
"publishUrl": {
get: function() {
return _publishUrl;
},
enumerable: true
},
"previewUrl": {
get: function() {
return _publishUrl + "?preview=true";
},
enumerable: true
},
"iframeUrl": {
get: function() {
return _iframeUrl;
},
enumerable: true
},
// Have changes made it to the db and been published?
"isSaved": {
get: function() {
return _isPublished && !_isDirty;
},
enumerable: true
}
});
EventManager.extend( _this );
// Once saved data is loaded, and media is ready, we start to care about
// the app's data states changing, and want to track.
butter.listen( "mediaready", function mediaReady() {
butter.unlisten( "mediaready", mediaReady );
// Listen for changes in the project data so we know when to save.
[ "mediacontentchanged",
"mediaclipadded",
"mediaclipremoved",
"mediatargetchanged",
"trackadded",
"trackremoved",
"tracktargetchanged",
"trackeventadded",
"trackeventremoved",
"trackeventupdated"
].forEach( function( event ) {
butter.listen( event, invalidate );
});
});
function startBackups() {
if ( _backupInterval === -1 && _backupIntervalMS > 0 ) {
_needsBackup = true;
_backupInterval = setInterval( backupData, _backupIntervalMS );
// Do a backup now so we don't miss anything
backupData();
}
}
// Import project data from JSON (i.e., created with project.export())
_this.import = function( json ) {
var oldTarget, targets, targetData,
mediaData, media, m, i, l;
// If JSON, convert to Object
if ( typeof json === "string" ) {
try {
json = JSON.parse( json );
} catch( e ) {
return;
}
}
if ( json.projectID ) {
_id = json.projectID;
_isPublished = true;
}
if ( json.name ) {
// replace HTML entities ("&", etc), possibly introduced by
// templating rules being applied to project metadata, with
// their plain form counterparts ("&", etc).
_name = Sanitizer.reconstituteHTML( json.name );
}
if ( json.template ) {
_template = json.template;
}
if ( json.author ) {
_author = json.author;
}
if ( json.description ) {
_description = json.description;
}
if ( json.thumbnail ) {
_thumbnail = json.thumbnail;
}
if ( json.publishUrl ) {
_publishUrl = json.publishUrl;
}
if ( json.iframeUrl ) {
_iframeUrl = json.iframeUrl;
}
if ( json.remixedFrom ) {
_remixedFrom = json.remixedFrom;
}
targets = json.targets;
if ( targets && Array.isArray( targets ) ) {
for ( i = 0, l = targets.length; i < l; ++i ) {
targetData = targets[ i ];
oldTarget = butter.getTargetByType( "elementID", targetData.element );
// Only add target if it's not already added.
if ( !oldTarget ) {
butter.addTarget( targetData );
} else {
// If it was already added, just update its json.
oldTarget.json = targetData;
}
}
} else if ( console ) {
console.warn( "Ignored imported target data. Must be in an Array." );
}
media = json.media;
if ( media && Array.isArray( media ) ) {
for ( i = 0, l = media.length; i < l; ++i ) {
mediaData = media[ i ];
m = butter.getMediaByType( "target", mediaData.target );
if ( !m ) {
m = new Media();
m.json = mediaData;
butter.addMedia( m );
} else {
m.json = mediaData;
}
}
} else if ( console ) {
console.warn( "Ignored imported media data. Must be in an Array." );
}
// If this is a restored backup, restart backups now (vs. on first save)
// since the user indicated they want it.
if( json.backupDate ) {
startBackups();
}
};
// Export project data as JSON string (e.g., for use with project.import())
_this.export = function() {
return JSON.stringify( _this.data );
};
// Expose backupData() to make testing possible
var backupData = _this.backupData = function() {
// If the project isn't different from last time, or if it's known
// to not fit in storage, don't bother trying.
if ( !_needsBackup ) {
return;
}
// Save everything but the project id
var data = _this.data;
data.name = _name;
data.template = _template;
data.author = _author;
data.description = _description;
data.thumbnail = _thumbnail;
data.backupDate = Date.now();
try {
__butterStorage.setItem( "butter-backup-project", JSON.stringify( data ) );
_needsBackup = false;
} catch ( e ) {
// Deal with QUOTA_EXCEEDED_ERR when localStorage is full.
// Stop the backup loop because we know we can't save anymore until the
// user changes something about the project.
clearInterval( _backupInterval );
_backupInterval = -1;
// Purge the saved project, since it won't be complete.
__butterStorage.removeItem( "butter-backup-project" );
console.warn( "Warning: Popcorn Maker LocalStorage quota exceeded. Stopping automatic backup. Will be restarted when project changes again." );
}
};
// Save and Publish a project. Saving only happens if project data needs
// to be saved (i.e., it has been changed since last save, or was never
// saved before).
_this.save = function( callback ) {
if ( !callback ) {
callback = function() {};
}
// Don't save if there is nothing new to save.
if ( _this.isSaved ) {
callback({ error: "okay" });
return;
}
var projectData = {
id: _id,
name: _name,
template: _template,
author: _author,
description: _description,
thumbnail: _thumbnail,
data: _this.data,
remixedFrom: _remixedFrom
};
// Save to local storage first in case network is down.
backupData();
// Save to db, then publish
butter.cornfield.save( _id, projectData, function( e ) {
if ( e.error === "okay" ) {
// Since we've now fully saved, blow away autosave backup
_isDirty = false;
__butterStorage.removeItem( "butter-backup-project" );
// Start keeping backups in storage, if not already started
startBackups();
// Keep any URLs generated from store in sync with the project.
if ( e.project ) {
_thumbnail = e.project.thumbnail;
}
// If this was a first save, grab id generated by server and store
if ( !_id ) {
_id = e.projectId;
}
// Now Publish and get URLs for embed
butter.cornfield.publish( _id, function( e ) {
if ( e.error === "okay" ) {
// Save + Publish is OK
_isPublished = true;
_publishUrl = e.publishUrl;
_iframeUrl = e.iframeUrl;
}
// Let consumers know that the project is now saved;
_this.dispatch( "projectsaved" );
callback( e );
});
} else {
callback( e );
}
});
};
}
// Check for an existing project that was autosaved but not saved.
// Returns project backup data as JS object if found, otherwise null.
// NOTE: caller must create a new Project object and call import.
Project.checkForBackup = function( butter, callback ) {
// See if we already have a project autosaved from another session.
var projectBackup, backupDate;
// For testing purposes, we can skip backup recovery
if ( butter.config.value( "recover" ) === "purge" ) {
callback( null, null );
return;
}
try {
projectBackup = __butterStorage.getItem( "butter-backup-project" );
projectBackup = JSON.parse( projectBackup );
// Delete since user can save if he/she wants.
__butterStorage.removeItem( "butter-backup-project" );
if ( projectBackup ) {
backupDate = projectBackup.backupDate;
}
} catch( e ) { }
callback( projectBackup, backupDate );
};
return Project;
});