|
|
|
@ -1,15 +1,30 @@
|
|
|
|
|
'use strict';
|
|
|
|
|
define(['Commands/CommandController', 'Shared/Messenger'],
|
|
|
|
|
function(CommandController, Messenger) {
|
|
|
|
|
return {
|
|
|
|
|
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);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
ExecuteCommand: function (options) {
|
|
|
|
|
options.iconClass = this._getIconClass(options.element);
|
|
|
|
|
|
|
|
|
|
this._showStartMessage(options);
|
|
|
|
|
if (options.button) {
|
|
|
|
|
options.button.addClass('disable');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._setSpinnerOnElement(options);
|
|
|
|
|
|
|
|
|
|
var promise = CommandController.Execute(options.command, options.properties);
|
|
|
|
|
this._handlePromise(promise, options);
|
|
|
|
|
this._showStartMessage(options, promise);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
SaveModel: function (options) {
|
|
|
|
@ -24,25 +39,91 @@ define(['Commands/CommandController', 'Shared/Messenger'],
|
|
|
|
|
|
|
|
|
|
_handlePromise: function (promise, options) {
|
|
|
|
|
promise.done(function () {
|
|
|
|
|
if (options.successMessage) {
|
|
|
|
|
Messenger.show({
|
|
|
|
|
message: options.successMessage
|
|
|
|
|
self._onSuccess(options);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
promise.fail(function (ajaxOptions) {
|
|
|
|
|
if (ajaxOptions.readyState === 0 || ajaxOptions.status === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.onSuccess) {
|
|
|
|
|
options.onSuccess.call(options.context);
|
|
|
|
|
self._onError(options);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
promise.always(function () {
|
|
|
|
|
self._onComplete(options);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_handleCommands: function () {
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
_.each(this.trackedCommands, function (trackedCommand){
|
|
|
|
|
if (trackedCommand.completed === true) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var options = trackedCommand.options;
|
|
|
|
|
var command = CommandCollection.find({ 'id': trackedCommand.id });
|
|
|
|
|
|
|
|
|
|
if (!command) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (command.get('state') === 'completed') {
|
|
|
|
|
trackedCommand.completed = true;
|
|
|
|
|
|
|
|
|
|
self._onSuccess(options, command.get('id'));
|
|
|
|
|
self._onComplete(options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (command.get('state') === 'failed') {
|
|
|
|
|
trackedCommand.completed = true;
|
|
|
|
|
|
|
|
|
|
self._onError(options, command.get('id'));
|
|
|
|
|
self._onComplete(options);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
promise.fail(function (ajaxOptions) {
|
|
|
|
|
if (ajaxOptions.readyState === 0 || ajaxOptions.status === 0) {
|
|
|
|
|
_getIconClass: function(element) {
|
|
|
|
|
return element.attr('class').match(/(?:^|\s)icon\-.+?(?:$|\s)/)[0];
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_setSpinnerOnElement: function (options) {
|
|
|
|
|
if (!options.element) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.failMessage) {
|
|
|
|
|
if (options.leaveIcon) {
|
|
|
|
|
options.element.addClass('icon-spin');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
options.element.removeClass(options.iconClass);
|
|
|
|
|
options.element.addClass('icon-nd-spinner');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_onSuccess: function (options, id) {
|
|
|
|
|
if (options.successMessage) {
|
|
|
|
|
Messenger.show({
|
|
|
|
|
id : id,
|
|
|
|
|
message: options.successMessage,
|
|
|
|
|
type : 'success'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.onSuccess) {
|
|
|
|
|
options.onSuccess.call(options.context);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_onError: function (options, id) {
|
|
|
|
|
if (options.errorMessage) {
|
|
|
|
|
Messenger.show({
|
|
|
|
|
message: options.failMessage,
|
|
|
|
|
id : id,
|
|
|
|
|
message: options.errorMessage,
|
|
|
|
|
type : 'error'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
@ -50,9 +131,12 @@ define(['Commands/CommandController', 'Shared/Messenger'],
|
|
|
|
|
if (options.onError) {
|
|
|
|
|
options.onError.call(options.context);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
promise.always(function () {
|
|
|
|
|
_onComplete: function (options) {
|
|
|
|
|
if (options.button) {
|
|
|
|
|
options.button.removeClass('disable');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.leaveIcon) {
|
|
|
|
|
options.element.removeClass('icon-spin');
|
|
|
|
@ -61,35 +145,39 @@ define(['Commands/CommandController', 'Shared/Messenger'],
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_getIconClass: function(element) {
|
|
|
|
|
return element.attr('class').match(/(?:^|\s)icon\-.+?(?:$|\s)/)[0];
|
|
|
|
|
},
|
|
|
|
|
_showStartMessage: function (options, promise) {
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
_setSpinnerOnElement: function (options) {
|
|
|
|
|
if (options.leaveIcon) {
|
|
|
|
|
options.element.addClass('icon-spin');
|
|
|
|
|
if (!promise) {
|
|
|
|
|
if (options.startMessage) {
|
|
|
|
|
Messenger.show({
|
|
|
|
|
message: options.startMessage
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
options.element.removeClass(options.iconClass);
|
|
|
|
|
options.element.addClass('icon-nd-spinner');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_showStartMessage: function (options) {
|
|
|
|
|
promise.done(function (data) {
|
|
|
|
|
self.trackedCommands.push({ id: data.id, options: options });
|
|
|
|
|
|
|
|
|
|
if (options.startMessage) {
|
|
|
|
|
Messenger.show({
|
|
|
|
|
id : data.id,
|
|
|
|
|
message: options.startMessage
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return new actioneer();
|
|
|
|
|
});
|
|
|
|
|