2013-07-22 02:23:17 +00:00
|
|
|
'use strict';
|
2013-08-31 03:08:19 +00:00
|
|
|
define(
|
|
|
|
[
|
|
|
|
'Commands/CommandController',
|
|
|
|
'Commands/CommandCollection',
|
|
|
|
'Shared/Messenger'],
|
|
|
|
function(CommandController, CommandCollection, Messenger) {
|
|
|
|
|
|
|
|
var actioneer = Marionette.AppRouter.extend({
|
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
this.trackedCommands = [];
|
|
|
|
CommandCollection.fetch();
|
|
|
|
this.listenTo(CommandCollection, 'sync', this._handleCommands);
|
|
|
|
},
|
|
|
|
|
2013-07-22 02:23:17 +00:00
|
|
|
ExecuteCommand: function (options) {
|
|
|
|
options.iconClass = this._getIconClass(options.element);
|
|
|
|
|
2013-08-31 03:08:19 +00:00
|
|
|
if (options.button) {
|
|
|
|
options.button.addClass('disable');
|
|
|
|
}
|
|
|
|
|
2013-07-22 02:23:17 +00:00
|
|
|
this._setSpinnerOnElement(options);
|
|
|
|
|
|
|
|
var promise = CommandController.Execute(options.command, options.properties);
|
2013-08-31 03:08:19 +00:00
|
|
|
this._showStartMessage(options, promise);
|
2013-07-22 02:23:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
SaveModel: function (options) {
|
|
|
|
options.iconClass = this._getIconClass(options.element);
|
|
|
|
|
2013-07-22 02:52:53 +00:00
|
|
|
this._showStartMessage(options);
|
2013-07-22 02:23:17 +00:00
|
|
|
this._setSpinnerOnElement(options);
|
|
|
|
|
2013-07-22 02:52:53 +00:00
|
|
|
var promise = options.context.model.save();
|
2013-07-22 02:23:17 +00:00
|
|
|
this._handlePromise(promise, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
_handlePromise: function (promise, options) {
|
|
|
|
promise.done(function () {
|
2013-08-31 03:08:19 +00:00
|
|
|
self._onSuccess(options);
|
2013-07-22 02:23:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
promise.fail(function (ajaxOptions) {
|
|
|
|
if (ajaxOptions.readyState === 0 || ajaxOptions.status === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-31 03:08:19 +00:00
|
|
|
self._onError(options);
|
2013-07-22 02:23:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
promise.always(function () {
|
2013-08-31 03:08:19 +00:00
|
|
|
self._onComplete(options);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleCommands: function () {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
_.each(this.trackedCommands, function (trackedCommand){
|
|
|
|
if (trackedCommand.completed === true) {
|
|
|
|
return;
|
|
|
|
}
|
2013-07-22 02:23:17 +00:00
|
|
|
|
2013-08-31 03:08:19 +00:00
|
|
|
var options = trackedCommand.options;
|
|
|
|
var command = CommandCollection.find({ 'id': trackedCommand.id });
|
|
|
|
|
|
|
|
if (!command) {
|
2013-08-31 16:12:37 +00:00
|
|
|
trackedCommand.completed = true;
|
|
|
|
|
|
|
|
self._onError(options, trackedCommand.id);
|
|
|
|
self._onComplete(options);
|
2013-08-31 03:08:19 +00:00
|
|
|
return;
|
2013-07-22 02:23:17 +00:00
|
|
|
}
|
|
|
|
|
2013-08-31 03:08:19 +00:00
|
|
|
if (command.get('state') === 'completed') {
|
|
|
|
trackedCommand.completed = true;
|
|
|
|
|
|
|
|
self._onSuccess(options, command.get('id'));
|
|
|
|
self._onComplete(options);
|
2013-08-31 16:12:37 +00:00
|
|
|
return;
|
2013-07-22 02:23:17 +00:00
|
|
|
}
|
|
|
|
|
2013-08-31 03:08:19 +00:00
|
|
|
if (command.get('state') === 'failed') {
|
|
|
|
trackedCommand.completed = true;
|
|
|
|
|
|
|
|
self._onError(options, command.get('id'));
|
|
|
|
self._onComplete(options);
|
2013-07-22 02:23:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_getIconClass: function(element) {
|
|
|
|
return element.attr('class').match(/(?:^|\s)icon\-.+?(?:$|\s)/)[0];
|
|
|
|
},
|
|
|
|
|
|
|
|
_setSpinnerOnElement: function (options) {
|
2013-08-31 03:08:19 +00:00
|
|
|
if (!options.element) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-22 02:23:17 +00:00
|
|
|
if (options.leaveIcon) {
|
|
|
|
options.element.addClass('icon-spin');
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
options.element.removeClass(options.iconClass);
|
|
|
|
options.element.addClass('icon-nd-spinner');
|
|
|
|
}
|
2013-07-22 02:52:53 +00:00
|
|
|
},
|
|
|
|
|
2013-08-31 03:08:19 +00:00
|
|
|
_onSuccess: function (options, id) {
|
|
|
|
if (options.successMessage) {
|
2013-07-22 02:52:53 +00:00
|
|
|
Messenger.show({
|
2013-08-31 03:08:19 +00:00
|
|
|
id : id,
|
|
|
|
message: options.successMessage,
|
|
|
|
type : 'success'
|
2013-07-22 02:52:53 +00:00
|
|
|
});
|
|
|
|
}
|
2013-08-31 03:08:19 +00:00
|
|
|
|
|
|
|
if (options.onSuccess) {
|
|
|
|
options.onSuccess.call(options.context);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_onError: function (options, id) {
|
|
|
|
if (options.errorMessage) {
|
|
|
|
Messenger.show({
|
|
|
|
id : id,
|
|
|
|
message: options.errorMessage,
|
|
|
|
type : 'error'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.onError) {
|
|
|
|
options.onError.call(options.context);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_onComplete: function (options) {
|
|
|
|
if (options.button) {
|
|
|
|
options.button.removeClass('disable');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.leaveIcon) {
|
|
|
|
options.element.removeClass('icon-spin');
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
options.element.addClass(options.iconClass);
|
|
|
|
options.element.removeClass('icon-nd-spinner');
|
|
|
|
options.element.removeClass('icon-spin');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.always) {
|
|
|
|
options.always.call(options.context);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_showStartMessage: function (options, promise) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!promise) {
|
|
|
|
if (options.startMessage) {
|
|
|
|
Messenger.show({
|
|
|
|
message: options.startMessage
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
promise.done(function (data) {
|
|
|
|
self.trackedCommands.push({ id: data.id, options: options });
|
|
|
|
|
|
|
|
if (options.startMessage) {
|
|
|
|
Messenger.show({
|
|
|
|
id : data.id,
|
|
|
|
message: options.startMessage
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-07-22 02:23:17 +00:00
|
|
|
}
|
2013-08-31 03:08:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return new actioneer();
|
2013-07-22 02:23:17 +00:00
|
|
|
});
|