nakedobjects.spa
Version:
Single Page Application client for a Naked Objects application.
284 lines • 13.3 kB
JavaScript
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 { ChoiceViewModel } from './choice-view-model';
import * as Models from '../models';
import * as Msg from '../user-messages';
import * as Helpers from './helpers-view-models';
import filter from 'lodash/filter';
import find from 'lodash/find';
import every from 'lodash/every';
import map from 'lodash/map';
import fromPairs from 'lodash/fromPairs';
import some from 'lodash/some';
import partial from 'lodash/partial';
import concat from 'lodash/concat';
var FieldViewModel = (function (_super) {
__extends(FieldViewModel, _super);
function FieldViewModel(rep, colorService, error, context, configService, onPaneId, isScalar, id, isCollectionContributed, entryType) {
var _this = _super.call(this) || this;
_this.rep = rep;
_this.colorService = colorService;
_this.error = error;
_this.context = context;
_this.configService = configService;
_this.onPaneId = onPaneId;
_this.isScalar = isScalar;
_this.id = id;
_this.isCollectionContributed = isCollectionContributed;
_this.entryType = entryType;
_this.clientValid = true;
_this.reference = "";
_this.currentRawValue = null;
_this.choiceOptions = [];
_this.hasValue = false;
_this.drop = function (newValue) { return Helpers.drop(_this.context, _this.error, _this, newValue); };
_this.validate = function (modelValue, viewValue, mandatoryOnly) { return Helpers.validate(_this.rep, _this, modelValue, viewValue, mandatoryOnly); };
_this.validator = function (c) {
var viewValue = c.value;
var isvalid = _this.isValid(viewValue);
return isvalid ? null : { invalid: "invalid entry" };
};
_this.setNewValue = function (newValue) {
_this.selectedChoice = newValue.selectedChoice;
_this.value = newValue.value;
_this.reference = newValue.reference;
};
_this.clear = function () {
_this.selectedChoice = null;
_this.value = null;
_this.reference = "";
};
_this.setValueFromControl = function (newValue) {
if (newValue instanceof Array) {
_this.selectedMultiChoices = newValue;
}
else if (newValue instanceof ChoiceViewModel) {
_this.selectedChoice = newValue;
}
else {
_this.value = newValue;
}
};
_this.getValueForControl = function () { return _this.selectedMultiChoices || _this.selectedChoice || _this.value; };
_this.getValue = function () {
if (_this.entryType === Models.EntryType.File) {
return new Models.Value(_this.file);
}
if (_this.entryType !== Models.EntryType.FreeForm || _this.isCollectionContributed) {
if (_this.entryType === Models.EntryType.MultipleChoices || _this.entryType === Models.EntryType.MultipleConditionalChoices || _this.isCollectionContributed) {
var selections = _this.selectedMultiChoices || [];
if (_this.type === "scalar") {
var selValues = map(selections, function (cvm) { return cvm.getValue().scalar(); });
return new Models.Value(selValues);
}
var selRefs = map(selections, function (cvm) { return ({ href: cvm.getValue().getHref(), title: cvm.name }); }); // reference
return new Models.Value(selRefs);
}
var choiceValue = _this.selectedChoice ? _this.selectedChoice.getValue() : null;
if (_this.type === "scalar") {
return new Models.Value(choiceValue && choiceValue.scalar() != null ? choiceValue.scalar() : "");
}
// reference
return new Models.Value(choiceValue && choiceValue.isReference() && _this.selectedChoice ? { href: choiceValue.getHref(), title: _this.selectedChoice.name } : null);
}
if (_this.type === "scalar") {
if (_this.value == null) {
return new Models.Value("");
}
if (_this.value instanceof Date) {
if (_this.format === "time") {
// time format
return new Models.Value(Models.toTimeString(_this.value));
}
if (_this.format === "date") {
// truncate time;
return new Models.Value(Models.toDateString(_this.value));
}
// date-time
return new Models.Value(_this.value.toISOString());
}
return new Models.Value(_this.value);
}
// reference
return new Models.Value(_this.reference ? { href: _this.reference, title: _this.value.toString() } : null);
};
var ext = rep.extensions();
_this.optional = ext.optional();
_this.description = ext.description();
_this.presentationHint = ext.presentationHint();
_this.mask = ext.mask();
_this.title = ext.friendlyName();
_this.returnType = ext.returnType();
_this.format = Models.withNull(ext.format());
_this.multipleLines = ext.multipleLines() || 1;
_this.password = ext.dataType() === "password";
_this.type = isScalar ? "scalar" : "ref";
_this.argId = "" + id.toLowerCase();
_this.paneArgId = "" + _this.argId + onPaneId;
return _this;
}
Object.defineProperty(FieldViewModel.prototype, "choices", {
get: function () {
return this.choiceOptions;
},
set: function (options) {
this.choiceOptions = options;
if (this.entryType === Models.EntryType.MultipleConditionalChoices) {
var currentSelectedOptions_1 = this.selectedMultiChoices;
this.selectedMultiChoices = filter(this.choiceOptions, function (c) { return some(currentSelectedOptions_1, function (choiceToSet) { return c.valuesEqual(choiceToSet); }); });
}
else if (this.entryType === Models.EntryType.ConditionalChoices) {
var currentSelectedOption_1 = this.selectedChoice;
this.selectedChoice = find(this.choiceOptions, function (c) { return c.valuesEqual(currentSelectedOption_1); });
}
if (!this.optional && !this.hasValue && this.entryType !== Models.EntryType.AutoComplete) {
// mandatory and not selected so add a mandatory indicator choice
var indicatorChoice = new ChoiceViewModel(new Models.Value(""), this.id, "*");
this.choiceOptions = concat([indicatorChoice], this.choices);
this.selectedChoice = indicatorChoice;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(FieldViewModel.prototype, "selectedChoice", {
get: function () {
return this.currentChoice;
},
set: function (newChoice) {
// type guard because angular pushes string value here until directive finds
// choice
if (newChoice instanceof ChoiceViewModel || newChoice == null) {
this.currentChoice = newChoice;
this.update();
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(FieldViewModel.prototype, "value", {
get: function () {
return this.currentRawValue;
},
set: function (newValue) {
this.currentRawValue = newValue;
this.update();
},
enumerable: true,
configurable: true
});
Object.defineProperty(FieldViewModel.prototype, "selectedMultiChoices", {
get: function () {
return this.currentMultipleChoices;
},
set: function (choices) {
this.currentMultipleChoices = choices;
this.update();
},
enumerable: true,
configurable: true
});
FieldViewModel.prototype.isValid = function (viewValue) {
var _this = this;
var val;
if (viewValue instanceof ChoiceViewModel) {
val = viewValue.getValue().toValueString();
}
else if (viewValue instanceof Array) {
if (viewValue.length) {
return every(viewValue, function (v) { return _this.isValid(v); });
}
val = "";
}
else {
val = viewValue;
}
if (this.entryType === Models.EntryType.AutoComplete && !(viewValue instanceof ChoiceViewModel)) {
if (val) {
this.setMessage(Msg.pendingAutoComplete);
this.clientValid = false;
return false;
}
else if (!this.optional) {
this.resetMessage();
this.clientValid = false;
return false;
}
}
// only fully validate freeform scalar
var fullValidate = this.entryType === Models.EntryType.FreeForm && this.type === "scalar";
return this.validate(viewValue, val, !fullValidate);
};
;
FieldViewModel.prototype.update = function () {
this.setColor();
};
;
FieldViewModel.prototype.setupChoices = function (choices) {
var _this = this;
this.choices = map(choices, function (v, n) { return new ChoiceViewModel(v, _this.id, n); });
};
FieldViewModel.prototype.setupAutocomplete = function (rep, parentValues, digest) {
var _this = this;
this.prompt = function (searchTerm) {
var createcvm = partial(Helpers.createChoiceViewModels, _this.id, searchTerm);
return _this.context.autoComplete(rep, _this.id, parentValues, searchTerm, digest).then(createcvm);
};
var promptLink = rep.promptLink(); // always
this.minLength = promptLink.extensions().minLength(); // always
this.description = this.description || Msg.autoCompletePrompt;
};
FieldViewModel.prototype.setupConditionalChoices = function (rep, digest) {
var _this = this;
this.conditionalChoices = function (args) {
var createcvm = partial(Helpers.createChoiceViewModels, _this.id, null);
return _this.context.conditionalChoices(rep, _this.id, function () { return ({}); }, args, digest).then(createcvm);
};
var promptLink = rep.promptLink();
this.promptArguments = fromPairs(map(promptLink.arguments(), function (v, key) { return [key, new Models.Value(v.value)]; }));
};
FieldViewModel.prototype.getRequiredIndicator = function () {
return this.optional || typeof this.value === "boolean" ? "" : "* ";
};
FieldViewModel.prototype.setColor = function () {
var _this = this;
if (this.entryType === Models.EntryType.AutoComplete && this.selectedChoice && this.type === "ref") {
var href = this.selectedChoice.getValue().getHref();
if (href) {
this.colorService.toColorNumberFromHref(href)
.then(function (c) {
// only if we still have a choice may have been cleared by a later call
if (_this.selectedChoice) {
_this.color = "" + _this.configService.config.linkColor + c;
}
})
.catch(function (reject) { return _this.error.handleError(reject); });
return;
}
}
else if (this.entryType !== Models.EntryType.AutoComplete && this.value) {
this.colorService.toColorNumberFromType(this.returnType)
.then(function (c) {
// only if we still have a value may have been cleared by a later call
if (_this.value) {
_this.color = "" + _this.configService.config.linkColor + c;
}
})
.catch(function (reject) { return _this.error.handleError(reject); });
return;
}
this.color = "";
};
return FieldViewModel;
}(MessageViewModel));
export { FieldViewModel };
//# sourceMappingURL=field-view-model.js.map