UNPKG

nakedobjects.spa

Version:

Single Page Application client for a Naked Objects application.

134 lines 7.02 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { MessageViewModel } from './message-view-model'; import * as Models from '../models'; import * as Msg from '../user-messages'; import * as Helpers from './helpers-view-models'; import each from 'lodash/each'; import filter from 'lodash/filter'; import find from 'lodash/find'; import clone from 'lodash/clone'; import every from 'lodash/every'; import forEach from 'lodash/forEach'; import keys from 'lodash/keys'; import map from 'lodash/map'; import first from 'lodash/first'; import some from 'lodash/some'; import values from 'lodash/values'; import toArray from 'lodash/toArray'; var ContributedActionParentViewModel = (function (_super) { __extends(ContributedActionParentViewModel, _super); function ContributedActionParentViewModel(context, viewModelFactory, urlManager, error, onPaneId) { var _this = _super.call(this) || this; _this.context = context; _this.viewModelFactory = viewModelFactory; _this.urlManager = urlManager; _this.error = error; _this.onPaneId = onPaneId; _this.allSelected = function () { return every(_this.items, function (item) { return item.selected; }); }; _this.selectAll = function () { var newState = !_this.allSelected(); _this.setItems(newState); }; return _this; } ContributedActionParentViewModel.prototype.isLocallyContributed = function (action) { return some(action.parameters(), function (p) { return p.isCollectionContributed(); }); }; ContributedActionParentViewModel.prototype.setActions = function (actions, routeData) { var _this = this; this.actions = map(actions, function (action) { return _this.viewModelFactory.actionViewModel(action, _this, routeData); }); this.menuItems = Helpers.createMenuItems(this.actions); forEach(this.actions, function (a) { return _this.decorate(a); }); }; ContributedActionParentViewModel.prototype.collectionContributedActionDecorator = function (actionViewModel) { var _this = this; var wrappedInvoke = actionViewModel.execute; actionViewModel.execute = function (pps, right) { var selected = filter(_this.items, function (i) { return i.selected; }); var rejectAsNeedSelection = function (action) { if (_this.isLocallyContributed(action)) { if (selected.length === 0) { var em = new Models.ErrorMap({}, 0, Msg.noItemsSelected); var rp = new Models.ErrorWrapper(Models.ErrorCategory.HttpClientError, Models.HttpStatusCode.UnprocessableEntity, em); return rp; } } return null; }; var getParms = function (action) { var parms = values(action.parameters()); var contribParm = find(parms, function (p) { return p.isCollectionContributed(); }); if (contribParm) { var parmValue = new Models.Value(map(selected, function (i) { return i.link; })); var collectionParmVm = _this.viewModelFactory.parameterViewModel(contribParm, parmValue, _this.onPaneId); var allpps = clone(pps); allpps.push(collectionParmVm); return allpps; } return pps; }; var detailsPromise = actionViewModel.invokableActionRep ? Promise.resolve(actionViewModel.invokableActionRep) : _this.context.getActionDetails(actionViewModel.actionRep); return detailsPromise. then(function (details) { var rp = rejectAsNeedSelection(details); return rp ? Promise.reject(rp) : wrappedInvoke(getParms(details), right); }). then(function (result) { // clear selected items on void actions _this.clearSelected(result); return result; }); }; }; ContributedActionParentViewModel.prototype.collectionContributedInvokeDecorator = function (actionViewModel) { var _this = this; var showDialog = function () { return _this.context.getInvokableAction(actionViewModel.actionRep). then(function (invokableAction) { actionViewModel.makeInvokable(invokableAction); var keyCount = keys(invokableAction.parameters()).length; return keyCount > 1 || keyCount === 1 && !toArray(invokableAction.parameters())[0].isCollectionContributed(); }); }; // make sure not invokable while waiting for promise to assign correct function actionViewModel.doInvoke = function () { }; var invokeWithoutDialog = function (right) { return actionViewModel.invokeWithoutDialogWithParameters(Promise.resolve([]), right).then(function (actionResult) { _this.setMessage(actionResult.shouldExpectResult() ? actionResult.warningsOrMessages() || Msg.noResultMessage : ""); // clear selected items on void actions _this.clearSelected(actionResult); }); }; showDialog(). then(function (show) { return actionViewModel.doInvoke = show ? actionViewModel.invokeWithDialog : invokeWithoutDialog; }). catch(function (reject) { return _this.error.handleError(reject); }); }; ContributedActionParentViewModel.prototype.decorate = function (actionViewModel) { this.collectionContributedActionDecorator(actionViewModel); this.collectionContributedInvokeDecorator(actionViewModel); }; ContributedActionParentViewModel.prototype.setItems = function (newValue) { each(this.items, function (item) { return item.silentSelect(newValue); }); var id = first(this.items).id; this.urlManager.setAllItemsSelected(newValue, id, this.onPaneId); }; ContributedActionParentViewModel.prototype.clearSelected = function (result) { if (result.resultType() === "void") { this.setItems(false); } }; return ContributedActionParentViewModel; }(MessageViewModel)); export { ContributedActionParentViewModel }; //# sourceMappingURL=contributed-action-parent-view-model.js.map