display error message on file load error

This commit is contained in:
juliandescottes 2017-01-14 21:37:52 +01:00
parent 552d4fa710
commit 8cad3bb607
7 changed files with 67 additions and 18 deletions

View File

@ -82,6 +82,8 @@ var Events = {
PERFORMANCE_REPORT_CHANGED : 'PERFORMANCE_REPORT_CHANGED', PERFORMANCE_REPORT_CHANGED : 'PERFORMANCE_REPORT_CHANGED',
PISKEL_FILE_IMPORT_FAILED : 'PISKEL_FILE_IMPORT_FAILED',
// Tests // Tests
MOUSE_EVENT : 'MOUSE_EVENT', MOUSE_EVENT : 'MOUSE_EVENT',
KEYBOARD_EVENT : 'KEYBOARD_EVENT', KEYBOARD_EVENT : 'KEYBOARD_EVENT',

View File

@ -126,6 +126,7 @@
this.storageService.init(); this.storageService.init();
this.importService = new pskl.service.ImportService(this.piskelController); this.importService = new pskl.service.ImportService(this.piskelController);
this.importService.init();
this.imageUploadService = new pskl.service.ImageUploadService(); this.imageUploadService = new pskl.service.ImageUploadService();
this.imageUploadService.init(); this.imageUploadService.init();

View File

@ -79,9 +79,15 @@
ns.ImportController.prototype.openPiskelFile_ = function (file) { ns.ImportController.prototype.openPiskelFile_ = function (file) {
if (this.isPiskel_(file)) { if (this.isPiskel_(file)) {
pskl.utils.PiskelFileUtils.loadFromFile(file, function (piskel) { pskl.utils.PiskelFileUtils.loadFromFile(file,
pskl.app.piskelController.setPiskel(piskel); // onSuccess
}); function (piskel) {
pskl.app.piskelController.setPiskel(piskel);
},
// onError
function (reason) {
$.publish(Events.PISKEL_FILE_IMPORT_FAILED, [reason]);
});
this.closeDrawer_(); this.closeDrawer_();
} }
}; };

View File

@ -35,7 +35,7 @@
if (isImage) { if (isImage) {
this.readImageFile_(file); this.readImageFile_(file);
} else if (isPiskel) { } else if (isPiskel) {
pskl.utils.PiskelFileUtils.loadFromFile(file, this.onPiskelFileLoaded_); pskl.utils.PiskelFileUtils.loadFromFile(file, this.onPiskelFileLoaded_, this.onPiskelFileError_);
} else if (isPalette) { } else if (isPalette) {
pskl.app.paletteImportService.read(file, this.onPaletteLoaded_.bind(this)); pskl.app.paletteImportService.read(file, this.onPaletteLoaded_.bind(this));
} }
@ -57,6 +57,10 @@
} }
}; };
ns.FileDropperService.prototype.onPiskelFileError_ = function (reason) {
$.publish(Events.PISKEL_FILE_IMPORT_FAILED, [reason]);
};
ns.FileDropperService.prototype.processImageSource_ = function (imageSource) { ns.FileDropperService.prototype.processImageSource_ = function (imageSource) {
this.importedImage_ = new Image(); this.importedImage_ = new Image();
this.importedImage_.onload = this.onImageLoaded_.bind(this); this.importedImage_.onload = this.onImageLoaded_.bind(this);

View File

@ -6,11 +6,25 @@
* @param {!PiskelController} piskelController * @param {!PiskelController} piskelController
* @constructor * @constructor
*/ */
ns.ImportService = ns.ImportService = function (piskelController) {
function (piskelController) {
this.piskelController_ = piskelController; this.piskelController_ = piskelController;
}; };
ns.ImportService.prototype.init = function () {
$.subscribe(Events.PISKEL_FILE_IMPORT_FAILED, this.onPiskelFileImportFailed_);
};
/**
* Called when a piskel load failed event is published. Display an appropriate error message.
* TODO: for some failure reasons, we might want to display a dialog with more details.
*/
ns.ImportService.prototype.onPiskelFileImportFailed_ = function (evt, reason) {
$.publish(Events.SHOW_NOTIFICATION, [{
'content': 'Piskel file import failed (' + reason + ')',
'hideDelay' : 10000
}]);
};
/** /**
* Given an image object and some options, create a new Piskel and open it * Given an image object and some options, create a new Piskel and open it
* for editing. * for editing.

View File

@ -2,6 +2,12 @@
var ns = $.namespace('pskl.utils'); var ns = $.namespace('pskl.utils');
ns.PiskelFileUtils = { ns.PiskelFileUtils = {
FAILURE : {
EMPTY : 'No data found in piskel file',
INVALID : 'Invalid piskel file, contact us on twitter @piskelapp',
DESERIALIZATION : 'Piskel data deserialization failed'
},
/** /**
* Load a piskel from a piskel file. * Load a piskel from a piskel file.
* After deserialization is successful, the provided success callback will be called. * After deserialization is successful, the provided success callback will be called.
@ -29,10 +35,22 @@
}, },
decodePiskelFile : function (rawPiskel, onSuccess, onError) { decodePiskelFile : function (rawPiskel, onSuccess, onError) {
var serializedPiskel = JSON.parse(rawPiskel); var serializedPiskel;
if (rawPiskel.length === 0) {
onError(ns.PiskelFileUtils.FAILURE.EMPTY);
return;
}
try {
serializedPiskel = JSON.parse(rawPiskel);
} catch (e) {
onError(ns.PiskelFileUtils.FAILURE.INVALID);
return;
}
var piskel = serializedPiskel.piskel; var piskel = serializedPiskel.piskel;
pskl.utils.serialization.Deserializer.deserialize(serializedPiskel, function (piskel) { pskl.utils.serialization.Deserializer.deserialize(serializedPiskel, onSuccess, function () {
onSuccess(piskel); onError(ns.PiskelFileUtils.FAILURE.DESERIALIZATION);
}); });
} }
}; };

View File

@ -9,16 +9,20 @@
this.layers_ = []; this.layers_ = [];
}; };
ns.Deserializer.deserialize = function (data, callback) { ns.Deserializer.deserialize = function (data, onSuccess, onError) {
var deserializer; try {
if (data.modelVersion == Constants.MODEL_VERSION) { var deserializer;
deserializer = new ns.Deserializer(data, callback); if (data.modelVersion == Constants.MODEL_VERSION) {
} else if (data.modelVersion == 1) { deserializer = new ns.Deserializer(data, onSuccess);
deserializer = new ns.backward.Deserializer_v1(data, callback); } else if (data.modelVersion == 1) {
} else { deserializer = new ns.backward.Deserializer_v1(data, onSuccess);
deserializer = new ns.backward.Deserializer_v0(data, callback); } else {
deserializer = new ns.backward.Deserializer_v0(data, onSuccess);
}
deserializer.deserialize();
} catch (e) {
onError(e);
} }
deserializer.deserialize();
}; };
ns.Deserializer.prototype.deserialize = function () { ns.Deserializer.prototype.deserialize = function () {