nakedobjects.spa
Version:
Single Page Application client for a Naked Objects application.
1,239 lines • 88.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 * as Constants from './constants';
import * as Msg from './user-messages';
import each from 'lodash/each';
import find from 'lodash/find';
import assign from 'lodash/assign';
import clone from 'lodash/clone';
import keys from 'lodash/keys';
import map from 'lodash/map';
import concat from 'lodash/concat';
import mapValues from 'lodash/mapValues';
import pickBy from 'lodash/pickBy';
import reduce from 'lodash/reduce';
import some from 'lodash/some';
import fromPairs from 'lodash/fromPairs';
import forOwn from 'lodash/forOwn';
import last from 'lodash/last';
import merge from 'lodash/merge';
import forEach from 'lodash/forEach';
// do not couple this back to angular by imports
// log directly to avoid coupling back to angular
function error(message) {
console.error(message);
throw new Error(message);
}
// coerce undefined to null
export function withNull(v) {
return v === undefined ? null : v;
}
export function withUndefined(v) {
return v === null ? undefined : v;
}
function validateExists(obj, name) {
if (obj) {
return obj;
}
return error("validateExists - Expected " + name + " does not exist");
}
function getMember(members, id, owner) {
var member = members[id];
if (member) {
return member;
}
return error("getMember - no member " + id + " on " + owner);
}
export function checkNotNull(v) {
if (v != null) {
return v;
}
return error("checkNotNull - Unexpected null");
}
export function toDateString(dt) {
var year = dt.getFullYear().toString();
var month = (dt.getMonth() + 1).toString();
var day = dt.getDate().toString();
month = month.length === 1 ? "0" + month : month;
day = day.length === 1 ? "0" + day : day;
return year + "-" + month + "-" + day;
}
export function toDateTimeString(dt) {
return toDateString(dt) + " " + toTimeString(dt);
}
export function toTimeString(dt) {
var hours = dt.getHours().toString();
var minutes = dt.getMinutes().toString();
var seconds = dt.getSeconds().toString();
hours = hours.length === 1 ? "0" + hours : hours;
minutes = minutes.length === 1 ? "0" + minutes : minutes;
seconds = seconds.length === 1 ? "0" + seconds : seconds;
return hours + ":" + minutes + ":" + seconds;
}
export function getTime(rawTime) {
if (!rawTime || rawTime.length === 0) {
return null;
}
var hours = parseInt(rawTime.substring(0, 2));
var mins = parseInt(rawTime.substring(3, 5));
var secs = parseInt(rawTime.substring(6, 8));
return new Date(1970, 0, 1, hours, mins, secs);
}
export function isDate(rep) {
var returnType = rep.extensions().returnType();
var format = rep.extensions().format();
return (returnType === "string" && format === "date");
}
export function isDateTime(rep) {
var returnType = rep.extensions().returnType();
var format = rep.extensions().format();
return (returnType === "string" && format === "date-time");
}
export function isDateOrDateTime(rep) {
return isDate(rep) || isDateTime(rep);
}
export function isTime(rep) {
var returnType = rep.extensions().returnType();
var format = rep.extensions().format();
return returnType === "string" && format === "time";
}
export function toTime(value) {
var rawValue = value ? value.toString() : "";
var dateValue = getTime(rawValue);
return dateValue ? dateValue : null;
}
export function toUtcDate(value) {
var rawValue = value ? value.toString() : "";
var dateValue = getUtcDate(rawValue);
return dateValue ? dateValue : null;
}
export function getUtcDate(rawDate) {
if (!rawDate || rawDate.length === 0) {
return null;
}
var year = parseInt(rawDate.substring(0, 4));
var month = parseInt(rawDate.substring(5, 7)) - 1;
var day = parseInt(rawDate.substring(8, 10));
if (rawDate.length === 10) {
return new Date(Date.UTC(year, month, day, 0, 0, 0));
}
if (rawDate.length >= 20) {
var hours = parseInt(rawDate.substring(11, 13));
var mins = parseInt(rawDate.substring(14, 16));
var secs = parseInt(rawDate.substring(17, 19));
return new Date(Date.UTC(year, month, day, hours, mins, secs));
}
return null;
}
export function compress(toCompress, shortCutMarker, urlShortCuts) {
if (toCompress) {
forEach(urlShortCuts, function (sc, i) { return toCompress = toCompress.replace(sc, "" + shortCutMarker + i); });
}
return toCompress;
}
export function decompress(toDecompress, shortCutMarker, urlShortCuts) {
if (toDecompress) {
forEach(urlShortCuts, function (sc, i) { return toDecompress = toDecompress.replace("" + shortCutMarker + i, sc); });
}
return toDecompress;
}
export function getClassName(obj) {
var funcNameRegex = /function (.{1,})\(/;
var results = (funcNameRegex).exec(obj.constructor.toString());
return (results && results.length > 1) ? results[1] : "";
}
export function typeFromUrl(url) {
var typeRegex = /(objects|services)\/([\w|\.]+)/;
var results = (typeRegex).exec(url);
return (results && results.length > 2) ? results[2] : "";
}
export function idFromUrl(href) {
var urlRegex = /(objects|services)\/(.*?)\/([^\/]*)/;
var results = (urlRegex).exec(href);
return (results && results.length > 3) ? results[3] : "";
}
export function propertyIdFromUrl(href) {
var urlRegex = /(objects)\/(.*)\/(.*)\/(properties)\/(.*)/;
var results = (urlRegex).exec(href);
return (results && results.length > 5) ? results[5] : "";
}
export function friendlyTypeName(fullName) {
var shortName = last(fullName.split("."));
var result = shortName.replace(/([A-Z])/g, " $1").trim();
return result.charAt(0).toUpperCase() + result.slice(1);
}
export function friendlyNameForParam(action, parmId) {
var param = find(action.parameters(), function (p) { return p.id() === parmId; });
return param ? param.extensions().friendlyName() : "";
}
export function friendlyNameForProperty(obj, propId) {
var prop = obj.propertyMember(propId);
return prop ? prop.extensions().friendlyName() : propId;
}
export function typePlusTitle(obj) {
var type = obj.extensions().friendlyName();
var title = obj.title();
return type + ": " + title;
}
// helper functions
function isAutoComplete(args) {
return args && args.hasOwnProperty(Constants.roSearchTerm);
}
function isScalarType(typeName) {
return typeName === "string" || typeName === "number" || typeName === "boolean" || typeName === "integer";
}
function isListType(typeName) {
return typeName === "list";
}
function emptyResource() {
return { links: [], extensions: {} };
}
function isILink(object) {
return object && object instanceof Object && "href" in object;
}
function isIObjectOfType(object) {
return object && object instanceof Object && "members" in object;
}
function isIValue(object) {
return object && object instanceof Object && "value" in object;
}
export function isResourceRepresentation(object) {
return object && object instanceof Object && "links" in object && "extensions" in object;
}
export function isErrorRepresentation(object) {
return isResourceRepresentation(object) && "message" in object;
}
export function isIDomainObjectRepresentation(object) {
return isResourceRepresentation(object) && "domainType" in object && "instanceId" in object && "members" in object;
}
function getId(prop) {
if (prop instanceof PropertyRepresentation) {
return prop.instanceId();
}
else {
return prop.id();
}
}
function wrapLinks(links) {
return map(links, function (l) { return new Link(l); });
}
function getLinkByRel(links, rel) {
return find(links, function (i) { return i.rel().uniqueValue === rel.uniqueValue; });
}
function linkByRel(links, rel) {
return getLinkByRel(links, new Rel(rel));
}
function linkByNamespacedRel(links, rel) {
return getLinkByRel(links, new Rel("urn:org.restfulobjects:rels/" + rel));
}
export var ErrorCategory;
(function (ErrorCategory) {
ErrorCategory[ErrorCategory["HttpClientError"] = 0] = "HttpClientError";
ErrorCategory[ErrorCategory["HttpServerError"] = 1] = "HttpServerError";
ErrorCategory[ErrorCategory["ClientError"] = 2] = "ClientError";
})(ErrorCategory || (ErrorCategory = {}));
export var HttpStatusCode;
(function (HttpStatusCode) {
HttpStatusCode[HttpStatusCode["NoContent"] = 204] = "NoContent";
HttpStatusCode[HttpStatusCode["BadRequest"] = 400] = "BadRequest";
HttpStatusCode[HttpStatusCode["Unauthorized"] = 401] = "Unauthorized";
HttpStatusCode[HttpStatusCode["Forbidden"] = 403] = "Forbidden";
HttpStatusCode[HttpStatusCode["NotFound"] = 404] = "NotFound";
HttpStatusCode[HttpStatusCode["MethodNotAllowed"] = 405] = "MethodNotAllowed";
HttpStatusCode[HttpStatusCode["NotAcceptable"] = 406] = "NotAcceptable";
HttpStatusCode[HttpStatusCode["PreconditionFailed"] = 412] = "PreconditionFailed";
HttpStatusCode[HttpStatusCode["UnprocessableEntity"] = 422] = "UnprocessableEntity";
HttpStatusCode[HttpStatusCode["PreconditionRequired"] = 428] = "PreconditionRequired";
HttpStatusCode[HttpStatusCode["InternalServerError"] = 500] = "InternalServerError";
})(HttpStatusCode || (HttpStatusCode = {}));
export var ClientErrorCode;
(function (ClientErrorCode) {
ClientErrorCode[ClientErrorCode["ExpiredTransient"] = 0] = "ExpiredTransient";
ClientErrorCode[ClientErrorCode["WrongType"] = 1] = "WrongType";
ClientErrorCode[ClientErrorCode["NotImplemented"] = 2] = "NotImplemented";
ClientErrorCode[ClientErrorCode["SoftwareError"] = 3] = "SoftwareError";
ClientErrorCode[ClientErrorCode["ConnectionProblem"] = 0] = "ConnectionProblem";
})(ClientErrorCode || (ClientErrorCode = {}));
var ErrorWrapper = (function () {
function ErrorWrapper(category, code, err, originalUrl) {
this.category = category;
this.originalUrl = originalUrl;
this.handled = false;
if (category === ErrorCategory.ClientError) {
this.clientErrorCode = code;
this.errorCode = ClientErrorCode[this.clientErrorCode];
var description = Msg.errorUnknown;
switch (this.clientErrorCode) {
case ClientErrorCode.ExpiredTransient:
description = Msg.errorExpiredTransient;
break;
case ClientErrorCode.WrongType:
description = Msg.errorWrongType;
break;
case ClientErrorCode.NotImplemented:
description = Msg.errorNotImplemented;
break;
case ClientErrorCode.SoftwareError:
description = Msg.errorSoftware;
break;
case ClientErrorCode.ConnectionProblem:
description = Msg.errorConnection;
break;
}
this.description = description;
this.title = Msg.errorClient;
}
if (category === ErrorCategory.HttpClientError || category === ErrorCategory.HttpServerError) {
this.httpErrorCode = code;
this.errorCode = HttpStatusCode[this.httpErrorCode] + "(" + this.httpErrorCode + ")";
this.description = category === ErrorCategory.HttpServerError
? "A software error has occurred on the server"
: "An HTTP error code has been received from the server\n" +
"You can look up the meaning of this code in the Restful Objects specification.";
this.title = "Error message received from server";
}
if (err instanceof ErrorMap) {
var em = err;
this.message = em.invalidReason() || em.warningMessage;
this.error = em;
this.stackTrace = [];
}
else if (err instanceof ErrorRepresentation) {
var er = err;
this.message = er.message();
this.error = er;
this.stackTrace = err.stackTrace();
}
else {
this.message = err;
this.error = null;
this.stackTrace = [];
}
}
return ErrorWrapper;
}());
export { ErrorWrapper };
// abstract classes
function toOid(id, keySeparator) {
return reduce(id, function (a, v) { return "" + a + keySeparator + v; });
}
var ObjectIdWrapper = (function () {
function ObjectIdWrapper(keySeparator) {
this.keySeparator = keySeparator;
if (keySeparator == null) {
error("ObjectIdWrapper must have a keySeparator");
}
}
ObjectIdWrapper.prototype.getKey = function () {
return this.domainType + this.keySeparator + this.instanceId;
};
ObjectIdWrapper.safeSplit = function (id, keySeparator) {
return id ? id.split(keySeparator) : [];
};
ObjectIdWrapper.fromObject = function (object) {
var oid = new ObjectIdWrapper(object.keySeparator);
oid.domainType = object.domainType() || "";
oid.instanceId = object.instanceId() || "";
oid.splitInstanceId = this.safeSplit(oid.instanceId, object.keySeparator);
oid.isService = !oid.instanceId;
return oid;
};
ObjectIdWrapper.fromLink = function (link, keySeparator) {
var href = link.href();
return this.fromHref(href, keySeparator);
};
ObjectIdWrapper.fromHref = function (href, keySeparator) {
var oid = new ObjectIdWrapper(keySeparator);
oid.domainType = typeFromUrl(href);
oid.instanceId = idFromUrl(href);
oid.splitInstanceId = this.safeSplit(oid.instanceId, keySeparator);
oid.isService = !oid.instanceId;
return oid;
};
ObjectIdWrapper.fromObjectId = function (objectId, keySeparator) {
var oid = new ObjectIdWrapper(keySeparator);
var _a = objectId.split(keySeparator), dt = _a[0], id = _a.slice(1);
oid.domainType = dt;
oid.splitInstanceId = id;
oid.instanceId = toOid(id, keySeparator);
oid.isService = !oid.instanceId;
return oid;
};
ObjectIdWrapper.fromRaw = function (dt, id, keySeparator) {
var oid = new ObjectIdWrapper(keySeparator);
oid.domainType = dt;
oid.instanceId = id;
oid.splitInstanceId = this.safeSplit(oid.instanceId, keySeparator);
oid.isService = !oid.instanceId;
return oid;
};
ObjectIdWrapper.fromSplitRaw = function (dt, id, keySeparator) {
var oid = new ObjectIdWrapper(keySeparator);
oid.domainType = dt;
oid.splitInstanceId = id;
oid.instanceId = toOid(id, keySeparator);
oid.isService = !oid.instanceId;
return oid;
};
ObjectIdWrapper.prototype.isSame = function (other) {
return other && other.domainType === this.domainType && other.instanceId === this.instanceId;
};
return ObjectIdWrapper;
}());
export { ObjectIdWrapper };
var HateosModel = (function () {
function HateosModel(model) {
this.model = model;
this.hateoasUrl = "";
this.method = "GET";
}
HateosModel.prototype.populate = function (model) {
this.model = model;
};
HateosModel.prototype.getBody = function () {
if (this.method === "POST" || this.method === "PUT") {
var m = clone(this.model);
var up = clone(this.urlParms);
return merge(m, up);
}
return {};
};
HateosModel.prototype.getUrl = function () {
var url = this.hateoasUrl;
var attrAsJson = clone(this.model);
if (this.method === "GET" || this.method === "DELETE") {
if (keys(attrAsJson).length > 0) {
// there are model parms so encode everything into json
var urlParmsAsJson = clone(this.urlParms);
var asJson = merge(attrAsJson, urlParmsAsJson);
if (keys(asJson).length > 0) {
var map_1 = JSON.stringify(asJson);
var parmString = encodeURI(map_1);
return url + "?" + parmString;
}
return url;
}
if (keys(this.urlParms).length > 0) {
// there are only url reserved parms so they can just be appended to url
var urlParmString = reduce(this.urlParms, function (result, n, key) { return (result === "" ? "" : result + "&") + key + "=" + n; }, "");
return url + "?" + urlParmString;
}
}
return url;
};
HateosModel.prototype.setUrlParameter = function (name, value) {
this.urlParms = this.urlParms || {};
this.urlParms[name] = value;
};
return HateosModel;
}());
export { HateosModel };
var ArgumentMap = (function (_super) {
__extends(ArgumentMap, _super);
function ArgumentMap(map, id) {
var _this = _super.call(this, map) || this;
_this.map = map;
_this.id = id;
return _this;
}
ArgumentMap.prototype.populate = function (wrapped) {
_super.prototype.populate.call(this, wrapped);
};
return ArgumentMap;
}(HateosModel));
export { ArgumentMap };
var NestedRepresentation = (function () {
function NestedRepresentation(model) {
var _this = this;
this.model = model;
this.resource = function () { return _this.model; };
}
NestedRepresentation.prototype.links = function () {
this.lazyLinks = this.lazyLinks || wrapLinks(this.resource().links);
return this.lazyLinks;
};
NestedRepresentation.prototype.update = function (newResource) {
this.model = newResource;
this.lazyLinks = null;
};
NestedRepresentation.prototype.extensions = function () {
this.lazyExtensions = this.lazyExtensions || new Extensions(this.model.extensions);
return this.lazyExtensions;
};
return NestedRepresentation;
}());
export { NestedRepresentation };
// classes
var RelParm = (function () {
function RelParm(asString) {
this.asString = asString;
this.decomposeParm();
}
RelParm.prototype.decomposeParm = function () {
var regex = /(\w+)\W+(\w+)\W+/;
var result = regex.exec(this.asString) || [];
this.name = result[1], this.value = result[2];
};
return RelParm;
}());
export { RelParm };
var Rel = (function () {
function Rel(asString) {
this.asString = asString;
this.ns = "";
this.parms = [];
this.decomposeRel();
}
Rel.prototype.decomposeRel = function () {
var postFix;
if (this.asString.substring(0, 3) === "urn") {
// namespaced
this.ns = this.asString.substring(0, this.asString.indexOf("/") + 1);
postFix = this.asString.substring(this.asString.indexOf("/") + 1);
}
else {
postFix = this.asString;
}
var splitPostFix = postFix.split(";");
this.uniqueValue = splitPostFix[0];
if (splitPostFix.length > 1) {
this.parms = map(splitPostFix.slice(1), function (s) { return new RelParm(s); });
}
};
return Rel;
}());
export { Rel };
var MediaType = (function () {
function MediaType(asString) {
this.asString = asString;
this.decomposeMediaType();
}
MediaType.prototype.decomposeMediaType = function () {
var parms = this.asString.split(";");
if (parms.length > 0) {
this.applicationType = parms[0];
}
for (var i = 1; i < parms.length; i++) {
if (parms[i].trim().substring(0, 7) === "profile") {
this.profile = parms[i].trim();
var profileValue = (this.profile.split("=")[1].replace(/\"/g, "")).trim();
this.representationType = (profileValue.split("/")[1]).trim();
}
if (parms[i].trim().substring(0, 16) === Constants.roDomainType) {
this.xRoDomainType = (parms[i]).trim();
this.domainType = (this.xRoDomainType.split("=")[1].replace(/\"/g, "")).trim();
}
}
};
return MediaType;
}());
export { MediaType };
var Value = (function () {
function Value(raw) {
// can only be Link, number, boolean, string or null
if (raw instanceof Array) {
this.wrapped = map(raw, function (i) { return new Value(i); });
}
else if (raw instanceof Link) {
this.wrapped = raw;
}
else if (isILink(raw)) {
this.wrapped = new Link(raw);
}
else if (raw instanceof Value) {
this.wrapped = raw.wrapped;
}
else {
this.wrapped = raw;
}
}
Value.prototype.isBlob = function () {
return this.wrapped instanceof Blob;
};
Value.prototype.isScalar = function () {
return !this.isReference() && !this.isList();
};
Value.prototype.isReference = function () {
return this.wrapped instanceof Link;
};
Value.prototype.isFileReference = function () {
var href = this.getHref();
return href ? href.indexOf("data") === 0 : false;
};
Value.prototype.isList = function () {
return this.wrapped instanceof Array;
};
Value.prototype.isNull = function () {
return this.wrapped == null;
};
Value.prototype.blob = function () {
return this.isBlob() ? this.wrapped : null;
};
Value.prototype.link = function () {
return this.isReference() ? this.wrapped : null;
};
Value.prototype.getHref = function () {
var link = this.link();
return link ? link.href() : null;
};
Value.prototype.scalar = function () {
return this.isScalar() ? this.wrapped : null;
};
Value.prototype.list = function () {
return this.isList() ? this.wrapped : null;
};
Value.prototype.toString = function () {
if (this.isReference()) {
return this.link().title() || ""; // know true
}
if (this.isList()) {
var list = this.list(); // know true
var ss = map(list, function (v) { return v.toString(); });
return ss.length === 0 ? "" : reduce(ss, function (m, s) { return m + "-" + s; }, "");
}
return (this.wrapped == null) ? "" : this.wrapped.toString();
};
Value.prototype.compress = function (shortCutMarker, urlShortCuts) {
if (this.isReference()) {
var link = this.link().compress(shortCutMarker, urlShortCuts); // know true
return new Value(link);
}
if (this.isList()) {
var list = map(this.list(), function (i) { return i.compress(shortCutMarker, urlShortCuts); });
return new Value(list);
}
;
if (this.scalar() && this.wrapped instanceof String) {
var scalar = compress(this.wrapped, shortCutMarker, urlShortCuts);
return new Value(scalar);
}
return this;
};
Value.prototype.decompress = function (shortCutMarker, urlShortCuts) {
if (this.isReference()) {
var link = this.link().decompress(shortCutMarker, urlShortCuts); // know true
return new Value(link);
}
if (this.isList()) {
var list = map(this.list(), function (i) { return i.decompress(shortCutMarker, urlShortCuts); });
return new Value(list);
}
;
if (this.scalar() && this.wrapped instanceof String) {
var scalar = decompress(this.wrapped, shortCutMarker, urlShortCuts);
return new Value(scalar);
}
return this;
};
Value.fromJsonString = function (jsonString, shortCutMarker, urlShortCuts) {
var value = new Value(JSON.parse(jsonString));
return value.decompress(shortCutMarker, urlShortCuts);
};
Value.prototype.toValueString = function () {
if (this.isReference()) {
return this.link().href(); // know true
}
return (this.wrapped == null) ? "" : this.wrapped.toString();
};
Value.prototype.toJsonString = function (shortCutMarker, urlShortCuts) {
var cloneThis = this.compress(shortCutMarker, urlShortCuts);
var value = cloneThis.wrapped;
var raw = (value instanceof Link) ? value.wrapped : value;
return JSON.stringify(raw);
};
Value.prototype.setValue = function (target) {
if (this.isFileReference()) {
target.value = this.link().wrapped; // know true
}
else if (this.isReference()) {
target.value = { "href": this.link().href() }; // know true
}
else if (this.isList()) {
var list = this.list(); // know true
target.value = map(list, function (v) { return v.isReference() ? { "href": v.link().href() } : v.scalar(); });
}
else if (this.isBlob()) {
target.value = this.blob();
}
else {
target.value = this.scalar();
}
};
Value.prototype.set = function (target, name) {
var t = target[name] = { value: null };
this.setValue(t);
};
return Value;
}());
export { Value };
var ErrorValue = (function () {
function ErrorValue(value, invalidReason) {
this.value = value;
this.invalidReason = invalidReason;
}
return ErrorValue;
}());
export { ErrorValue };
var Result = (function () {
function Result(wrapped, resultType) {
this.wrapped = wrapped;
this.resultType = resultType;
}
Result.prototype.object = function () {
if (!this.isNull() && this.resultType === "object") {
return new DomainObjectRepresentation(this.wrapped);
}
return null;
};
Result.prototype.list = function () {
if (!this.isNull() && this.resultType === "list") {
return new ListRepresentation(this.wrapped);
}
return null;
};
Result.prototype.scalar = function () {
if (!this.isNull() && this.resultType === "scalar") {
return new ScalarValueRepresentation(this.wrapped);
}
return null;
};
Result.prototype.isNull = function () {
return this.wrapped == null;
};
Result.prototype.isVoid = function () {
return (this.resultType === "void");
};
return Result;
}());
export { Result };
var ErrorMap = (function () {
function ErrorMap(map, statusCode, warningMessage) {
var _this = this;
this.map = map;
this.statusCode = statusCode;
this.warningMessage = warningMessage;
this.wrapped = function () {
var temp = _this.map;
if (isIObjectOfType(temp)) {
return temp.members;
}
else {
return temp;
}
};
}
ErrorMap.prototype.valuesMap = function () {
var values = pickBy(this.wrapped(), function (i) { return isIValue(i); });
return mapValues(values, function (v) { return new ErrorValue(new Value(v.value), withNull(v.invalidReason)); });
};
ErrorMap.prototype.invalidReason = function () {
var temp = this.map;
if (isIObjectOfType(temp)) {
return temp[Constants.roInvalidReason];
}
return this.wrapped()[Constants.roInvalidReason];
};
ErrorMap.prototype.containsError = function () {
return !!this.invalidReason() || !!this.warningMessage || some(this.valuesMap(), function (ev) { return !!ev.invalidReason; });
};
return ErrorMap;
}());
export { ErrorMap };
var UpdateMap = (function (_super) {
__extends(UpdateMap, _super);
function UpdateMap(domainObject, map) {
var _this = _super.call(this, map, checkNotNull(domainObject.instanceId())) || this;
_this.domainObject = domainObject;
var link = domainObject.updateLink();
if (link) {
link.copyToHateoasModel(_this);
}
else {
error("UpdateMap - attempting to create update map for object without update link");
}
each(_this.properties(), function (value, key) {
_this.setProperty(key, value);
});
return _this;
}
UpdateMap.prototype.properties = function () {
return mapValues(this.map, function (v) { return new Value(v.value); });
};
UpdateMap.prototype.setProperty = function (name, value) {
value.set(this.map, name);
};
UpdateMap.prototype.setValidateOnly = function () {
this.map[Constants.roValidateOnly] = true;
};
return UpdateMap;
}(ArgumentMap));
export { UpdateMap };
var AddToRemoveFromMap = (function (_super) {
__extends(AddToRemoveFromMap, _super);
function AddToRemoveFromMap(collectionResource, map, add) {
var _this = _super.call(this, map, collectionResource.collectionId()) || this;
_this.collectionResource = collectionResource;
var link = add ? collectionResource.addToLink() : collectionResource.removeFromLink();
if (link) {
link.copyToHateoasModel(_this);
}
else {
var type = add ? "add" : "remove";
error("AddToRemoveFromMap attempting to create " + type + " map for object without " + type + " link");
}
return _this;
}
return AddToRemoveFromMap;
}(ArgumentMap));
export { AddToRemoveFromMap };
var ModifyMap = (function (_super) {
__extends(ModifyMap, _super);
function ModifyMap(propertyResource, map) {
var _this = _super.call(this, map, getId(propertyResource)) || this;
_this.propertyResource = propertyResource;
var link = propertyResource.modifyLink();
if (link) {
link.copyToHateoasModel(_this);
}
else {
error("ModifyMap attempting to create modify map for object without modify link");
}
propertyResource.value().set(_this.map, _this.id);
return _this;
}
return ModifyMap;
}(ArgumentMap));
export { ModifyMap };
var ClearMap = (function (_super) {
__extends(ClearMap, _super);
function ClearMap(propertyResource) {
var _this = _super.call(this, {}, getId(propertyResource)) || this;
var link = propertyResource.clearLink();
if (link) {
link.copyToHateoasModel(_this);
}
else {
error("ClearMap attempting to create clear map for object without clear link");
}
return _this;
}
return ClearMap;
}(ArgumentMap));
export { ClearMap };
// REPRESENTATIONS
var ResourceRepresentation = (function (_super) {
__extends(ResourceRepresentation, _super);
function ResourceRepresentation() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.resource = function () { return _this.model; };
return _this;
}
ResourceRepresentation.prototype.populate = function (wrapped) {
_super.prototype.populate.call(this, wrapped);
};
ResourceRepresentation.prototype.links = function () {
this.lazyLinks = this.lazyLinks || wrapLinks(this.resource().links);
return this.lazyLinks;
};
ResourceRepresentation.prototype.extensions = function () {
this.lazyExtensions = this.lazyExtensions || new Extensions(this.resource().extensions);
return this.lazyExtensions;
};
return ResourceRepresentation;
}(HateosModel));
export { ResourceRepresentation };
var Extensions = (function () {
function Extensions(wrapped) {
var _this = this;
this.wrapped = wrapped;
//Standard RO:
this.friendlyName = function () { return _this.wrapped.friendlyName || ""; };
this.description = function () { return _this.wrapped.description || ""; };
this.returnType = function () { return _this.wrapped.returnType || null; };
this.optional = function () { return _this.wrapped.optional || false; };
this.hasParams = function () { return _this.wrapped.hasParams || false; };
this.elementType = function () { return _this.wrapped.elementType || null; };
this.domainType = function () { return _this.wrapped.domainType || null; };
this.pluralName = function () { return _this.wrapped.pluralName || ""; };
this.format = function () { return _this.wrapped.format; };
this.memberOrder = function () { return _this.wrapped.memberOrder; };
this.isService = function () { return _this.wrapped.isService || false; };
this.minLength = function () { return _this.wrapped.minLength; };
this.maxLength = function () { return _this.wrapped.maxLength; };
this.pattern = function () { return _this.wrapped.pattern; };
//Nof custom:
this.choices = function () { return _this.wrapped["x-ro-nof-choices"]; };
this.menuPath = function () { return _this.wrapped["x-ro-nof-menuPath"]; };
this.mask = function () { return _this.wrapped["x-ro-nof-mask"]; };
this.tableViewTitle = function () { return _this.wrapped["x-ro-nof-tableViewTitle"]; };
this.tableViewColumns = function () { return _this.wrapped["x-ro-nof-tableViewColumns"]; };
this.multipleLines = function () { return _this.wrapped["x-ro-nof-multipleLines"]; };
this.warnings = function () { return _this.wrapped["x-ro-nof-warnings"]; };
this.messages = function () { return _this.wrapped["x-ro-nof-messages"]; };
this.interactionMode = function () { return _this.wrapped["x-ro-nof-interactionMode"]; };
this.dataType = function () { return _this.wrapped["x-ro-nof-dataType"]; };
this.range = function () { return _this.wrapped["x-ro-nof-range"]; };
this.notNavigable = function () { return _this.wrapped["x-ro-nof-notNavigable"]; };
this.renderEagerly = function () { return _this.wrapped["x-ro-nof-renderEagerly"]; };
this.presentationHint = function () { return _this.wrapped["x-ro-nof-presentationHint"]; };
}
return Extensions;
}());
export { Extensions };
// matches a action invoke resource 19.0 representation
var InvokeMap = (function (_super) {
__extends(InvokeMap, _super);
function InvokeMap(link) {
var _this = _super.call(this, link.arguments(), "") || this;
_this.link = link;
link.copyToHateoasModel(_this);
return _this;
}
InvokeMap.prototype.setParameter = function (name, value) {
value.set(this.map, name);
};
return InvokeMap;
}(ArgumentMap));
export { InvokeMap };
var ActionResultRepresentation = (function (_super) {
__extends(ActionResultRepresentation, _super);
function ActionResultRepresentation() {
var _this = _super.call(this) || this;
_this.wrapped = function () { return _this.resource(); };
return _this;
}
// links
ActionResultRepresentation.prototype.selfLink = function () {
return linkByRel(this.links(), "self") || null;
};
// link representations
ActionResultRepresentation.prototype.getSelf = function () {
var self = this.selfLink();
return self ? self.getTargetAs() : null;
};
// properties
ActionResultRepresentation.prototype.resultType = function () {
return this.wrapped().resultType;
};
ActionResultRepresentation.prototype.result = function () {
return new Result(withNull(this.wrapped().result), this.resultType());
};
ActionResultRepresentation.prototype.warningsOrMessages = function () {
var has = function (arr) { return arr && arr.length > 0; };
var wOrM = has(this.extensions().warnings()) ? this.extensions().warnings() : this.extensions().messages();
if (has(wOrM)) {
return reduce(wOrM, function (s, t) { return s + " " + t; }, "");
}
return undefined;
};
ActionResultRepresentation.prototype.shouldExpectResult = function () {
return this.result().isNull() && this.resultType() !== "void";
};
return ActionResultRepresentation;
}(ResourceRepresentation));
export { ActionResultRepresentation };
// matches 18.2.1
var Parameter = (function (_super) {
__extends(Parameter, _super);
// fix parent type
function Parameter(wrapped, parent, paramId) {
var _this = _super.call(this, wrapped) || this;
_this.parent = parent;
_this.paramId = paramId;
_this.wrapped = function () { return _this.resource(); };
return _this;
}
Parameter.prototype.id = function () {
return this.paramId;
};
// properties
Parameter.prototype.choices = function () {
var customExtensions = this.extensions();
// use custom choices extension by preference
if (customExtensions.choices()) {
return mapValues(customExtensions.choices(), function (v) { return new Value(v); });
}
var choices = this.wrapped().choices;
if (choices) {
var values = map(choices, function (item) { return new Value(item); });
return fromPairs(map(values, function (v) { return [v.toString(), v]; }));
}
return null;
};
Parameter.prototype.promptLink = function () {
return linkByNamespacedRel(this.links(), "prompt") || null;
};
Parameter.prototype.getPromptMap = function () {
var promptLink = this.promptLink();
if (promptLink) {
var pr = promptLink.getTargetAs();
return new PromptMap(promptLink, pr.instanceId());
}
return null;
};
Parameter.prototype.default = function () {
var dflt = this.wrapped().default == null ? (isScalarType(this.extensions().returnType()) ? "" : null) : this.wrapped().default;
return new Value(withNull(dflt));
};
// helper
Parameter.prototype.isScalar = function () {
return isScalarType(this.extensions().returnType()) ||
(isListType(this.extensions().returnType()) && isScalarType(this.extensions().elementType()));
};
Parameter.prototype.isList = function () {
return isListType(this.extensions().returnType());
};
Parameter.prototype.hasPrompt = function () {
return !!this.promptLink();
};
Parameter.prototype.isCollectionContributed = function () {
var myparent = this.parent;
var isOnList = (myparent instanceof ActionMember || myparent instanceof ActionRepresentation) &&
(myparent.parent instanceof ListRepresentation || myparent.parent instanceof CollectionRepresentation || myparent.parent instanceof CollectionMember);
var isList = this.isList();
return isList && isOnList;
};
Parameter.prototype.hasChoices = function () { return some(this.choices() || {}); };
Parameter.prototype.entryType = function () {
var promptLink = this.promptLink();
if (promptLink) {
// ConditionalChoices, ConditionalMultipleChoices, AutoComplete
if (isAutoComplete(promptLink.arguments())) {
// autocomplete
return EntryType.AutoComplete;
}
if (isListType(this.extensions().returnType())) {
return EntryType.MultipleConditionalChoices;
}
return EntryType.ConditionalChoices;
}
if (this.choices()) {
if (isListType(this.extensions().returnType())) {
return EntryType.MultipleChoices;
}
return EntryType.Choices;
}
if (this.extensions().format() === "blob") {
return EntryType.File;
}
return EntryType.FreeForm;
};
return Parameter;
}(NestedRepresentation));
export { Parameter };
var ActionRepresentation = (function (_super) {
__extends(ActionRepresentation, _super);
function ActionRepresentation() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.wrapped = function () { return _this.resource(); };
return _this;
}
// links
ActionRepresentation.prototype.selfLink = function () {
return linkByRel(this.links(), "self"); // known to exist
};
ActionRepresentation.prototype.upLink = function () {
return linkByRel(this.links(), "up"); // known to exist
};
ActionRepresentation.prototype.invokeLink = function () {
return linkByNamespacedRel(this.links(), "invoke") || null; // may not exist if disabled
};
// linked representations
ActionRepresentation.prototype.getSelf = function () {
return this.selfLink().getTargetAs();
};
ActionRepresentation.prototype.getUp = function () {
return this.upLink().getTargetAs();
};
ActionRepresentation.prototype.getInvokeMap = function () {
var link = this.invokeLink();
return link ? new InvokeMap(link) : null;
};
// properties
ActionRepresentation.prototype.actionId = function () {
return this.wrapped().id;
};
ActionRepresentation.prototype.initParameterMap = function () {
var _this = this;
if (!this.parameterMap) {
var parameters = this.wrapped().parameters;
this.parameterMap = mapValues(parameters, function (p, id) { return new Parameter(p, _this, id); });
}
};
ActionRepresentation.prototype.parameters = function () {
this.initParameterMap();
return this.parameterMap;
};
ActionRepresentation.prototype.disabledReason = function () {
return this.wrapped().disabledReason || "";
};
ActionRepresentation.prototype.isQueryOnly = function () {
var invokeLink = this.invokeLink();
return !!invokeLink && invokeLink.method() === "GET";
};
ActionRepresentation.prototype.isNotQueryOnly = function () {
var invokeLink = this.invokeLink();
return !!invokeLink && invokeLink.method() !== "GET";
};
ActionRepresentation.prototype.isPotent = function () {
var invokeLink = this.invokeLink();
return !!invokeLink && invokeLink.method() === "POST";
};
return ActionRepresentation;
}(ResourceRepresentation));
export { ActionRepresentation };
// new in 1.1 15.0 in spec
var PromptMap = (function (_super) {
__extends(PromptMap, _super);
function PromptMap(link, promptId) {
var _this = _super.call(this, link.arguments(), promptId) || this;
_this.link = link;
_this.promptId = promptId;
link.copyToHateoasModel(_this);
return _this;
}
PromptMap.prototype.promptMap = function () {
return this.map;
};
PromptMap.prototype.setSearchTerm = function (term) {
this.setArgument(Constants.roSearchTerm, new Value(term));
};
PromptMap.prototype.setArgument = function (name, val) {
val.set(this.map, name);
};
PromptMap.prototype.setArguments = function (args) {
var _this = this;
each(args, function (arg, key) { return _this.setArgument(key, arg); });
};
PromptMap.prototype.setMember = function (name, value) {
value.set(this.promptMap()["x-ro-nof-members"], name);
};
PromptMap.prototype.setMembers = function (objectValues) {
var _this = this;
if (this.map["x-ro-nof-members"]) {
forEach(objectValues(), function (v, k) { return _this.setMember(k, v); });
}
};
return PromptMap;
}(ArgumentMap));
export { PromptMap };
var PromptRepresentation = (function (_super) {
__extends(PromptRepresentation, _super);
function PromptRepresentation() {
var _this = _super.call(this, emptyResource()) || this;
_this.wrapped = function () { return _this.resource(); };
return _this;
}
// links
PromptRepresentation.prototype.selfLink = function () {
return linkByRel(this.links(), "self"); //known to exist
};
PromptRepresentation.prototype.upLink = function () {
return linkByRel(this.links(), "up"); //known to exist
};
// linked representations
PromptRepresentation.prototype.getSelf = function () {
return this.selfLink().getTargetAs();
};
PromptRepresentation.prototype.getUp = function () {
return this.upLink().getTargetAs();
};
// properties
PromptRepresentation.prototype.instanceId = function () {
return this.wrapped().id;
};
PromptRepresentation.prototype.choices = function (addEmpty) {
var ch = this.wrapped().choices;
if (ch) {
var values = map(ch, function (item) { return new Value(item); });
if (addEmpty) {
var emptyValue = new Value("");
values = concat([emptyValue], values);
}
return fromPairs(map(values, function (v) { return [v.toString(), v]; }));
}
return null;
};
return PromptRepresentation;
}(ResourceRepresentation));
export { PromptRepresentation };
// matches a collection representation 17.0
var CollectionRepresentation = (function (_super) {
__extends(CollectionRepresentation, _super);
function CollectionRepresentation() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.wrapped = function () { return _this.resource(); };
_this.hasTableData = function () {
var valueLinks = _this.value();
return valueLinks && some(valueLinks, function (i) { return i.members(); });
};
return _this;
}
// links
CollectionRepresentation.prototype.selfLink = function () {
return linkByRel(this.links(), "self"); // known to exist
};
CollectionRepresentation.prototype.upLink = function () {
return linkByRel(this.links(), "up"); // known to exist
};
CollectionRepresentation.prototype.addToLink = function () {
return linkByNamespacedRel(this.links(), "add-to") || null;
};
CollectionRepresentation.prototype.removeFromLink = function () {
return linkByNamespacedRel(this.links(), "remove-from") || null;
};
// linked representations
CollectionRepresentation.prototype.getSelf = function () {
return this.selfLink().getTargetAs();
};
CollectionRepresentation.prototype.getUp = function () {
return this.upLink().getTargetAs();
};
CollectionRepresentation.prototype.setFromMap = function (map) {
//this.set(map.attributes);
assign(this.resource(), map.map);
};
CollectionRepresentation.prototype.addToMap = function () {
var link = this.addToLink();
return link ? link.arguments() : null;
};
CollectionRepresentation.prototype.getAddToMap = function () {
var map = this.addToMap();
return map ? new AddToRemoveFromMap(this, map, true) : null;
};
CollectionRepresentation.prototype.removeFromMap = function () {
var link = this.addToLink();
return link ? link.arguments() : null;
};
CollectionRepresentation.prototype.getRemoveFromMap = function () {
var map = this.removeFromMap();
return map ? new AddToRemoveFromMap(this, map, false) : null;
};
// properties
CollectionRepresentation.prototype.collectionId = function () {
return this.wrapped().id;
};
CollectionRepresentation.prototype.size = function () {
return this.value().length;
};
CollectionRepresentation.prototype.value = function () {
this.lazyValue = this.lazyValue || wrapLinks(this.wrapped().value);
return this.lazyValue;
};
CollectionRepresentation.prototype.disabledReason = function () {
return this.wrapped().disabledReason || "";
};
CollectionRepresentation.prototype.actionMembers = function () {
var _this = this;
this.actionMemberMap = this.actionMemberMap || mapValues(this.wrapped().members, function (m, id) { return Member.wrapMember(m, _this, id); });
return this.actionMemberMap;
};
CollectionRepresentation.prototype.actionMember = function (id) {