UNPKG

nakedobjects.spa

Version:

Single Page Application client for a Naked Objects application.

149 lines 7.36 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 { FieldViewModel } from './field-view-model'; import { ChoiceViewModel } from './choice-view-model'; import * as Msg from '../user-messages'; import * as Models from '../models'; import * as Helpers from './helpers-view-models'; import find from 'lodash/find'; import concat from 'lodash/concat'; var PropertyViewModel = (function (_super) { __extends(PropertyViewModel, _super); function PropertyViewModel(propertyRep, color, error, viewModelfactory, context, maskService, urlManager, clickHandler, configService, id, previousValue, onPaneId, parentValues) { var _this = _super.call(this, propertyRep, color, error, context, configService, onPaneId, propertyRep.isScalar(), id, propertyRep.isCollectionContributed(), propertyRep.entryType()) || this; _this.propertyRep = propertyRep; _this.viewModelfactory = viewModelfactory; _this.maskService = maskService; _this.urlManager = urlManager; _this.clickHandler = clickHandler; _this.previousValue = previousValue; _this.draggableTitle = function () { return _this.formattedValue; }; _this.canDropOn = function (targetType) { return _this.context.isSubTypeOf(_this.returnType, targetType); }; _this.doClick = function (right) { return _this.urlManager.setProperty(_this.reference, _this.clickHandler.pane(_this.onPaneId, right)); }; _this.isDirty = function () { return !!_this.previousValue || _this.getValue().toValueString() !== _this.originalValue.toValueString(); }; _this.draggableType = propertyRep.extensions().returnType(); _this.isEditable = !propertyRep.disabledReason(); _this.attachment = _this.viewModelfactory.attachmentViewModel(propertyRep, onPaneId); var fieldEntryType = _this.entryType; if (fieldEntryType === Models.EntryType.AutoComplete) { _this.setupPropertyAutocomplete(parentValues); } if (fieldEntryType === Models.EntryType.ConditionalChoices) { _this.setupPropertyConditionalChoices(); } if (propertyRep.isScalar()) { _this.setupScalarPropertyValue(); } else { // is reference _this.setupReferencePropertyValue(); } _this.refresh(previousValue); if (!previousValue) { _this.originalValue = _this.getValue(); } _this.hasValue = previousValue && !previousValue.isNull; _this.description = _this.getRequiredIndicator() + _this.description; return _this; } PropertyViewModel.prototype.getDigest = function (propertyRep) { var parent = propertyRep.parent; if (parent instanceof Models.DomainObjectRepresentation) { if (parent.isTransient()) { return Models.withNull(parent.etagDigest); } } return null; }; PropertyViewModel.prototype.setupPropertyAutocomplete = function (parentValues) { var propertyRep = this.propertyRep; this.setupAutocomplete(propertyRep, parentValues, this.getDigest(propertyRep)); }; PropertyViewModel.prototype.setupPropertyConditionalChoices = function () { var propertyRep = this.propertyRep; this.setupConditionalChoices(propertyRep, this.getDigest(propertyRep)); }; PropertyViewModel.prototype.callIfChanged = function (newValue, doRefresh) { var propertyRep = this.propertyRep; var value = newValue || propertyRep.value(); if (this.currentValue == null || value.toValueString() !== this.currentValue.toValueString()) { doRefresh(value); this.currentValue = value; } }; PropertyViewModel.prototype.setupChoice = function (newValue) { var propertyRep = this.propertyRep; if (this.entryType === Models.EntryType.Choices) { var choices = propertyRep.choices(); this.setupChoices(choices); if (this.optional) { var emptyChoice = new ChoiceViewModel(new Models.Value(""), this.id); this.choices = concat([emptyChoice], this.choices); } var currentChoice_1 = new ChoiceViewModel(newValue, this.id); this.selectedChoice = find(this.choices, function (c) { return c.valuesEqual(currentChoice_1); }) || null; } else if (!propertyRep.isScalar()) { this.selectedChoice = new ChoiceViewModel(newValue, this.id); } }; PropertyViewModel.prototype.setupReference = function (value, rep) { if (value.isNull()) { this.reference = ""; this.value = this.description; this.formattedValue = ""; this.refType = "null"; } else { this.reference = value.link().href(); this.value = value.toString(); this.formattedValue = value.toString(); this.refType = rep.extensions().notNavigable() ? "notNavigable" : "navigable"; } if (this.entryType === Models.EntryType.FreeForm) { this.description = this.description || Msg.dropPrompt; } }; PropertyViewModel.prototype.setupReferencePropertyValue = function () { var _this = this; var propertyRep = this.propertyRep; this.refresh = function (newValue) { return _this.callIfChanged(newValue, function (value) { _this.setupChoice(value); _this.setupReference(value, propertyRep); }); }; }; PropertyViewModel.prototype.setupScalarPropertyValue = function () { var _this = this; var propertyRep = this.propertyRep; var remoteMask = propertyRep.extensions().mask(); var localFilter = this.maskService.toLocalFilter(remoteMask, propertyRep.extensions().format()); this.localFilter = localFilter; this.refresh = function (newValue) { return _this.callIfChanged(newValue, function (value) { _this.setupChoice(value); Helpers.setScalarValueInView(_this, _this.propertyRep, value); if (propertyRep.entryType() === Models.EntryType.Choices) { if (_this.selectedChoice) { _this.value = _this.selectedChoice.name; _this.formattedValue = _this.selectedChoice.name; } } else if (_this.password) { _this.formattedValue = Msg.obscuredText; } else { _this.formattedValue = localFilter.filter(_this.value); } }); }; }; return PropertyViewModel; }(FieldViewModel)); export { PropertyViewModel }; //# sourceMappingURL=property-view-model.js.map