UNPKG

nakedobjects.spa

Version:

Single Page Application client for a Naked Objects application.

196 lines 12.2 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 { InteractionMode } from '../route-data'; import { ChoiceViewModel } from './choice-view-model'; import * as Models from '../models'; import * as Helpers from './helpers-view-models'; import * as Msg from '../user-messages'; import filter from 'lodash/filter'; import every from 'lodash/every'; import forEach from 'lodash/forEach'; import map from 'lodash/map'; import zipObject from 'lodash/zipObject'; import fromPairs from 'lodash/fromPairs'; import values from 'lodash/values'; import mapValues from 'lodash/mapValues'; var DomainObjectViewModel = (function (_super) { __extends(DomainObjectViewModel, _super); function DomainObjectViewModel(colorService, contextService, viewModelFactory, urlManager, error, configService, obj, routeData, forceReload) { var _this = _super.call(this) || this; _this.colorService = colorService; _this.contextService = contextService; _this.viewModelFactory = viewModelFactory; _this.urlManager = urlManager; _this.error = error; _this.configService = configService; _this.routeData = routeData; _this.draggableTitle = function () { return _this.title; }; _this.editProperties = function () { return filter(_this.properties, function (p) { return p.isEditable; }); }; _this.isFormOrTransient = function () { return _this.domainObject.extensions().interactionMode() === "form" || _this.domainObject.extensions().interactionMode() === "transient"; }; _this.cancelHandler = function () { return _this.isFormOrTransient() ? function () { return _this.urlManager.popUrlState(_this.onPaneId); } : function () { return _this.urlManager.setInteractionMode(InteractionMode.View, _this.onPaneId); }; }; _this.saveHandler = function () { return _this.domainObject.isTransient() ? _this.contextService.saveObject : _this.contextService.updateObject; }; _this.validateHandler = function () { return _this.domainObject.isTransient() ? _this.contextService.validateSaveObject : _this.contextService.validateUpdateObject; }; // leave this a lambda as it's passed as a function and we must keep the 'this'. _this.propertyMap = function () { var pps = filter(_this.properties, function (property) { return property.isEditable; }); return zipObject(map(pps, function (p) { return p.id; }), map(pps, function (p) { return p.getValue(); })); }; _this.clientValid = function () { return every(_this.properties, function (p) { return p.clientValid; }); }; _this.tooltip = function () { return Helpers.tooltip(_this, _this.properties); }; _this.actionsTooltip = function () { return Helpers.actionsTooltip(_this, !!_this.routeData.actionsOpen); }; _this.toggleActionMenu = function () { _this.urlManager.toggleObjectMenu(_this.onPaneId); }; _this.setProperties = function () { return forEach(_this.editProperties(), function (p) { return _this.contextService.cachePropertyValue(_this.domainObject, p.propertyRep, p.getValue(), _this.onPaneId); }); }; _this.doEditCancel = function () { _this.contextService.clearObjectCachedValues(_this.onPaneId); _this.cancelHandler()(); }; _this.clearCachedFiles = function () { return forEach(_this.properties, function (p) { return p.attachment ? p.attachment.clearCachedFile() : null; }); }; _this.doSave = function (viewObject, onSuccess) { _this.clearCachedFiles(); var propMap = _this.propertyMap(); return _this.saveHandler()(_this.domainObject, propMap, _this.onPaneId, viewObject) .then(function (obj) { onSuccess(); _this.reset(obj, _this.urlManager.getRouteData().pane(_this.onPaneId), true); }) .catch(function (reject) { return _this.handleWrappedError(reject); }); }; _this.currentPaneData = function () { return _this.urlManager.getRouteData().pane(_this.onPaneId); }; _this.doSaveValidate = function () { var propMap = _this.propertyMap(); return _this.validateHandler()(_this.domainObject, propMap) .then(function () { _this.resetMessage(); return true; }) .catch(function (reject) { _this.handleWrappedError(reject); return Promise.reject(false); }); }; _this.doEdit = function () { _this.clearCachedFiles(); _this.contextService.clearObjectCachedValues(_this.onPaneId); _this.contextService.getObjectForEdit(_this.onPaneId, _this.domainObject) .then(function (updatedObject) { _this.reset(updatedObject, _this.currentPaneData(), true); _this.urlManager.pushUrlState(_this.onPaneId); _this.urlManager.setInteractionMode(InteractionMode.Edit, _this.onPaneId); }) .catch(function (reject) { return _this.handleWrappedError(reject); }); }; _this.doReload = function () { _this.clearCachedFiles(); _this.contextService.reloadObject(_this.onPaneId, _this.domainObject) .then(function (updatedObject) { return _this.reset(updatedObject, _this.currentPaneData(), true); }) .catch(function (reject) { return _this.handleWrappedError(reject); }); }; _this.hideEdit = function () { return _this.isFormOrTransient() || every(_this.properties, function (p) { return !p.isEditable; }); }; _this.noActions = function () { return !_this.actions || _this.actions.length === 0; }; _this.canDropOn = function (targetType) { return _this.contextService.isSubTypeOf(_this.domainType, targetType); }; _this.showActions = function () { return !!_this.currentPaneData().actionsOpen; }; _this.keySeparator = configService.config.keySeparator; _this.reset(obj, routeData, forceReload); return _this; } DomainObjectViewModel.prototype.handleWrappedError = function (reject) { var _this = this; var display = function (em) { return Helpers.handleErrorResponse(em, _this, _this.properties); }; this.error.handleErrorAndDisplayMessages(reject, display); }; ; DomainObjectViewModel.prototype.wrapAction = function (a) { var _this = this; var wrappedInvoke = a.execute; a.execute = function (pps, right) { _this.setProperties(); var pairs = map(_this.editProperties(), function (p) { return [p.id, p.getValue()]; }); var prps = fromPairs(pairs); var parmValueMap = mapValues(a.invokableActionRep.parameters(), function (p) { return ({ parm: p, value: prps[p.id()] }); }); var allpps = map(parmValueMap, function (o) { return _this.viewModelFactory.parameterViewModel(o.parm, o.value, _this.onPaneId); }); return wrappedInvoke(allpps, right) .catch(function (reject) { _this.handleWrappedError(reject); return Promise.reject(reject); }); }; }; DomainObjectViewModel.prototype.reset = function (obj, routeData, resetting) { var _this = this; this.domainObject = obj; this.onPaneId = routeData.paneId; this.routeData = routeData; var iMode = this.domainObject.extensions().interactionMode(); this.isInEdit = routeData.interactionMode !== InteractionMode.View || iMode === "form" || iMode === "transient"; this.props = routeData.interactionMode !== InteractionMode.View ? this.contextService.getObjectCachedValues(this.domainObject.id(), routeData.paneId) : {}; var actions = values(this.domainObject.actionMembers()); this.actions = map(actions, function (action) { return _this.viewModelFactory.actionViewModel(action, _this, _this.routeData); }); this.menuItems = Helpers.createMenuItems(this.actions); this.properties = map(this.domainObject.propertyMembers(), function (property, id) { return _this.viewModelFactory.propertyViewModel(property, id, _this.props[id], _this.onPaneId, _this.propertyMap); }); this.collections = map(this.domainObject.collectionMembers(), function (collection) { return _this.viewModelFactory.collectionViewModel(collection, _this.routeData, resetting); }); this.unsaved = routeData.interactionMode === InteractionMode.Transient; this.title = this.unsaved ? "Unsaved " + this.domainObject.extensions().friendlyName() : this.domainObject.title(); this.title = this.title + Helpers.dirtyMarker(this.contextService, this.configService, obj.getOid()); this.friendlyName = this.domainObject.extensions().friendlyName(); this.presentationHint = this.domainObject.extensions().presentationHint(); this.domainType = this.domainObject.domainType(); this.instanceId = this.domainObject.instanceId(); this.draggableType = this.domainObject.domainType(); var selfAsValue = function () { var link = _this.domainObject.selfLink(); if (link) { // not transient - can't drag transients so no need to set up IDraggable members on transients link.setTitle(_this.title); return new Models.Value(link); } return null; }; var sav = selfAsValue(); this.value = sav ? sav.toString() : ""; this.reference = sav ? sav.toValueString() : ""; this.selectedChoice = sav ? new ChoiceViewModel(sav, "") : null; this.colorService.toColorNumberFromType(this.domainObject.domainType()) .then(function (c) { return _this.color = "" + _this.configService.config.objectColor + c; }) .catch(function (reject) { return _this.error.handleError(reject); }); this.resetMessage(); if (routeData.interactionMode === InteractionMode.Form) { forEach(this.actions, function (a) { return _this.wrapAction(a); }); } }; DomainObjectViewModel.prototype.concurrency = function () { var _this = this; this.routeData = this.urlManager.getRouteData().pane(this.onPaneId); this.contextService.getObject(this.onPaneId, this.domainObject.getOid(), this.routeData.interactionMode) .then(function (obj) { // cleared cached values so all values are from reloaded representation _this.contextService.clearObjectCachedValues(_this.onPaneId); return _this.contextService.reloadObject(_this.onPaneId, obj); }) .then(function (reloadedObj) { if (_this.routeData.dialogId) { _this.urlManager.closeDialogReplaceHistory(_this.routeData.dialogId, _this.onPaneId); } _this.reset(reloadedObj, _this.routeData, true); var em = new Models.ErrorMap({}, 0, Msg.concurrencyMessage); Helpers.handleErrorResponse(em, _this, _this.properties); }) .catch(function (reject) { return _this.error.handleError(reject); }); }; ; return DomainObjectViewModel; }(MessageViewModel)); export { DomainObjectViewModel }; //# sourceMappingURL=domain-object-view-model.js.map