nakedobjects.spa
Version:
Single Page Application client for a Naked Objects application.
113 lines • 6.11 kB
JavaScript
import { InteractionMode } from '../route-data';
import * as Models from '../models';
import * as Msg from '../user-messages';
import * as Helpers from './helpers-view-models';
import forEach from 'lodash/forEach';
import map from 'lodash/map';
import zipObject from 'lodash/zipObject';
import pickBy from 'lodash/pickBy';
var ActionViewModel = (function () {
function ActionViewModel(viewModelFactory, context, urlManager, error, clickHandler, actionRep, vm, routeData) {
var _this = this;
this.viewModelFactory = viewModelFactory;
this.context = context;
this.urlManager = urlManager;
this.error = error;
this.clickHandler = clickHandler;
this.actionRep = actionRep;
this.vm = vm;
this.routeData = routeData;
this.gotoResult = true;
// form actions should never show dialogs
this.showDialog = function () { return _this.actionRep.extensions().hasParams() && (_this.routeData.interactionMode !== InteractionMode.Form); };
this.incrementPendingPotentAction = function () {
Helpers.incrementPendingPotentAction(_this.context, _this.invokableActionRep, _this.paneId);
};
this.decrementPendingPotentAction = function () {
Helpers.decrementPendingPotentAction(_this.context, _this.invokableActionRep, _this.paneId);
};
this.invokeWithDialog = function (right) {
// clear any previous dialog so we don't pick up values from it
_this.context.clearDialogCachedValues(_this.paneId);
_this.urlManager.setDialogOrMultiLineDialog(_this.actionRep, _this.paneId);
};
this.invokeWithoutDialogWithParameters = function (parameters, right) {
_this.incrementPendingPotentAction();
return parameters
.then(function (pps) {
return _this.execute(pps, right);
})
.then(function (actionResult) {
_this.decrementPendingPotentAction();
return actionResult;
})
.catch(function (reject) {
_this.decrementPendingPotentAction();
var display = function (em) { return _this.vm.setMessage(em.invalidReason() || em.warningMessage); };
_this.error.handleErrorAndDisplayMessages(reject, display);
});
};
this.invokeWithoutDialog = function (right) {
_this.invokeWithoutDialogWithParameters(_this.parameters(), right).then(function (actionResult) {
// if expect result and no warning from server generate one here
if (actionResult.shouldExpectResult() && !actionResult.warningsOrMessages()) {
_this.context.broadcastWarning(Msg.noResultMessage);
}
});
};
// open dialog on current pane always - invoke action goes to pane indicated by click
// note this can be modified by decorators
this.doInvoke = this.showDialog() ? this.invokeWithDialog : this.invokeWithoutDialog;
// note this is modified by decorators
this.execute = function (pps, right) {
var parmMap = zipObject(map(pps, function (p) { return p.id; }), map(pps, function (p) { return p.getValue(); }));
forEach(pps, function (p) { return _this.urlManager.setParameterValue(_this.actionRep.actionId(), p.parameterRep, p.getValue(), _this.paneId); });
return _this.getInvokable()
.then(function (details) { return _this.context.invokeAction(details, parmMap, _this.paneId, _this.clickHandler.pane(_this.paneId, right), _this.gotoResult); });
};
this.disabled = function () { return !!_this.actionRep.disabledReason(); };
this.tempDisabled = function () { return _this.invokableActionRep &&
_this.invokableActionRep.isPotent() &&
_this.context.isPendingPotentActionOrReload(_this.paneId); };
this.parameters = function () {
if (_this.invokableActionRep) {
var pps = _this.getParameters(_this.invokableActionRep);
return Promise.resolve(pps);
}
return _this.context.getInvokableAction(_this.actionRep)
.then(function (details) {
_this.invokableActionRep = details;
return _this.getParameters(details);
});
};
this.makeInvokable = function (details) { return _this.invokableActionRep = details; };
if (actionRep instanceof Models.ActionRepresentation || actionRep instanceof Models.InvokableActionMember) {
this.invokableActionRep = actionRep;
}
this.paneId = routeData.paneId;
this.title = actionRep.extensions().friendlyName();
this.presentationHint = actionRep.extensions().presentationHint();
this.menuPath = actionRep.extensions().menuPath() || "";
this.description = this.disabled() ? actionRep.disabledReason() : actionRep.extensions().description();
}
ActionViewModel.prototype.getInvokable = function () {
var _this = this;
if (this.invokableActionRep) {
return Promise.resolve(this.invokableActionRep);
}
return this.context.getInvokableAction(this.actionRep)
.then(function (details) {
_this.invokableActionRep = details;
return details;
});
};
ActionViewModel.prototype.getParameters = function (invokableAction) {
var _this = this;
var parameters = pickBy(invokableAction.parameters(), function (p) { return !p.isCollectionContributed(); });
var parms = this.routeData.actionParams;
return map(parameters, function (parm) { return _this.viewModelFactory.parameterViewModel(parm, parms[parm.id()], _this.paneId); });
};
return ActionViewModel;
}());
export { ActionViewModel };
//# sourceMappingURL=action-view-model.js.map