|
122 | 122 |
|
123 | 123 | data = parseJSON(file);
|
124 | 124 |
|
| 125 | + // Make sure we have some data |
125 | 126 | if (!data)
|
126 | 127 | {
|
127 | 128 | callback(null);
|
128 | 129 | return;
|
129 | 130 | }
|
| 131 | + |
| 132 | + // Check that there's a captions object |
| 133 | + if (!data.captions) |
| 134 | + { |
| 135 | + this.create(file, callback); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
130 | 139 | this.onProjectLoaded(data, callback);
|
131 | 140 | }
|
132 | 141 | if (WEB)
|
|
202 | 211 | */
|
203 | 212 | p.onProjectLoaded = function(data, callback)
|
204 | 213 | {
|
205 |
| - // No projects file! |
206 |
| - if (!data || !data.captions) |
207 |
| - { |
208 |
| - callback(null); |
209 |
| - return; |
210 |
| - } |
211 | 214 | this.locales = data.captions.locales;
|
212 | 215 | this.assets = data.captions.assets;
|
213 | 216 | this.setLocale('default', function(project){
|
|
223 | 226 | */
|
224 | 227 | p.create = function(file, callback)
|
225 | 228 | {
|
226 |
| - var self = this, |
227 |
| - dir = path.dirname(file); |
| 229 | + var dir = path.dirname(file); |
| 230 | + this.modal.open(dir, |
| 231 | + this.onModalClosed.bind(this, file, dir, callback) |
| 232 | + ); |
| 233 | + }; |
228 | 234 |
|
229 |
| - this.modal.open(dir, function(result){ |
| 235 | + /** |
| 236 | + * When the modal project dialog is closed |
| 237 | + * @method onModalClosed |
| 238 | + * @private |
| 239 | + * @param {string} file The project file to save |
| 240 | + * @param {string} dir Project main path |
| 241 | + * @param {function} callback The result when we're done |
| 242 | + * @param {object} result |
| 243 | + */ |
| 244 | + p.onModalClosed = function(file, dir, callback, result) |
| 245 | + { |
| 246 | + // Failed! |
| 247 | + if (!result) |
| 248 | + { |
| 249 | + callback(null); |
| 250 | + return; |
| 251 | + } |
| 252 | + |
| 253 | + var audioPath = result.audioPath, |
| 254 | + exportPath = result.exportPath, |
| 255 | + cwd = path.join(dir, audioPath), |
| 256 | + self = this; |
| 257 | + |
| 258 | + // Select all OGG file from the audio folder |
| 259 | + glob('**/*.ogg', {cwd:cwd}, function(err, files) { |
230 | 260 |
|
231 |
| - // Failed! |
232 |
| - if (!result) |
| 261 | + if (err) |
233 | 262 | {
|
234 | 263 | callback(null);
|
235 |
| - return; |
| 264 | + throw err; |
236 | 265 | }
|
237 | 266 |
|
238 |
| - var audioPath = result.audioPath, |
239 |
| - exportPath = result.exportPath, |
240 |
| - cwd = path.join(dir, audioPath); |
241 |
| - |
242 |
| - // Select all OGG file from the audio folder |
243 |
| - glob('**/*.ogg', {cwd:cwd}, function(err, files) { |
| 267 | + var assets = []; |
244 | 268 |
|
245 |
| - if (err) |
246 |
| - { |
247 |
| - callback(null); |
248 |
| - throw err; |
249 |
| - } |
250 |
| - |
251 |
| - var assets = []; |
252 |
| - |
253 |
| - _.each(files, function(file){ |
254 |
| - assets.push({ |
255 |
| - id : path.basename(file, '.ogg'), |
256 |
| - src : file |
257 |
| - }); |
| 269 | + _.each(files, function(file){ |
| 270 | + assets.push({ |
| 271 | + id : path.basename(file, '.ogg'), |
| 272 | + src : file |
258 | 273 | });
|
| 274 | + }); |
259 | 275 |
|
260 |
| - // Add an empty export file if there isn't one |
261 |
| - var exportFile = path.join(dir, exportPath); |
262 |
| - if (!fs.existsSync(exportFile)) |
| 276 | + // Add an empty export file if there isn't one |
| 277 | + var exportFile = path.join(dir, exportPath); |
| 278 | + if (!fs.existsSync(exportFile)) |
| 279 | + { |
| 280 | + if (DEBUG) |
263 | 281 | {
|
264 |
| - if (DEBUG) |
265 |
| - { |
266 |
| - console.log("Save empty captions file " + exportFile); |
267 |
| - } |
268 |
| - fs.writeFileSync(exportFile, "{}"); |
| 282 | + console.log("Save empty captions file " + exportFile); |
269 | 283 | }
|
| 284 | + fs.writeFileSync(exportFile, "{}"); |
| 285 | + } |
270 | 286 |
|
271 |
| - // Dummy project file |
272 |
| - var data = { |
273 |
| - "captions" : { |
274 |
| - "assets" : assets, |
275 |
| - "locales" : { |
276 |
| - "default" : { |
277 |
| - "path" : audioPath, |
278 |
| - "export" : exportPath |
279 |
| - } |
| 287 | + // Dummy project file |
| 288 | + var data = { |
| 289 | + "captions" : { |
| 290 | + "assets" : assets, |
| 291 | + "locales" : { |
| 292 | + "default" : { |
| 293 | + "path" : audioPath, |
| 294 | + "export" : exportPath |
280 | 295 | }
|
281 | 296 | }
|
282 |
| - }; |
| 297 | + } |
| 298 | + }; |
283 | 299 |
|
| 300 | + // File already exists, but no captions data |
| 301 | + if (fs.existsSync(file)) |
| 302 | + { |
| 303 | + var projectData = parseJSON(file); |
| 304 | + projectData.captions = data.captions; |
| 305 | + writeJSON(file, projectData); |
| 306 | + } |
| 307 | + else |
| 308 | + { |
284 | 309 | // Write the project to file
|
285 | 310 | writeJSON(file, data);
|
| 311 | + } |
286 | 312 |
|
287 |
| - // Continue with loading the new created project |
288 |
| - self.onProjectLoaded(data, callback); |
289 |
| - }); |
| 313 | + // Continue with loading the new created project |
| 314 | + self.onProjectLoaded(data, callback); |
290 | 315 | });
|
291 | 316 | };
|
292 | 317 |
|
|
0 commit comments