UNPKG

@crnk/angular-ngrx

Version:

Angular helper library for ngrx-json-api and crnk:

606 lines 20.7 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 __()); }; })(); /** * Base class that allows the construction of expressions. * The type parameter is a reference to the type the expression is bound to. * * The most important subinterfaces are: * * <ul> * <li>{@link Path}: property member access</li> * </ul> */ import { toQueryPath } from '../binding'; export var OPERATION_EQ = 'EQ'; export var OPERATION_NEQ = 'NEQ'; export var OPERATION_LIKE = 'LIKE'; export var OPERATION_GT = 'GT'; export var OPERATION_LT = 'LT'; export var OPERATION_GE = 'GE'; export var OPERATION_LE = 'LE'; /** * Base implementation for a {@link Expression} providing a EQ and NEQ operation. */ var /** * Base implementation for a {@link Expression} providing a EQ and NEQ operation. */ SimpleExpression = /** @class */ (function () { function SimpleExpression() { } SimpleExpression.prototype.eq = function (right) { return new BooleanOperation(OPERATION_EQ, this, ExpressionFactory.toLiteral(right)); }; SimpleExpression.prototype.neq = function (right) { return new BooleanOperation(OPERATION_NEQ, this, ExpressionFactory.toLiteral(right)); }; SimpleExpression.prototype.isConstant = function () { return false; }; SimpleExpression.prototype.isPath = function () { return false; }; return SimpleExpression; }()); /** * Base implementation for a {@link Expression} providing a EQ and NEQ operation. */ export { SimpleExpression }; /** * Base implementation for a {@link Expression} providing equals and compare operations. */ var /** * Base implementation for a {@link Expression} providing equals and compare operations. */ ComparableExpression = /** @class */ (function (_super) { __extends(ComparableExpression, _super); function ComparableExpression() { return _super !== null && _super.apply(this, arguments) || this; } ComparableExpression.prototype.lt = function (right) { return new BooleanOperation(OPERATION_LT.toString(), this, ExpressionFactory.toLiteral(right)); }; ComparableExpression.prototype.le = function (right) { return new BooleanOperation(OPERATION_LE.toString(), this, ExpressionFactory.toLiteral(right)); }; ComparableExpression.prototype.gt = function (right) { return new BooleanOperation(OPERATION_GT.toString(), this, ExpressionFactory.toLiteral(right)); }; ComparableExpression.prototype.ge = function (right) { return new BooleanOperation(OPERATION_GE.toString(), this, ExpressionFactory.toLiteral(right)); }; return ComparableExpression; }(SimpleExpression)); /** * Base implementation for a {@link Expression} providing equals and compare operations. */ export { ComparableExpression }; /** * Base implementation for {@link Expression} bound to a string. */ var /** * Base implementation for {@link Expression} bound to a string. */ StringExpression = /** @class */ (function (_super) { __extends(StringExpression, _super); function StringExpression() { return _super !== null && _super.apply(this, arguments) || this; } StringExpression.prototype.like = function (right) { return new BooleanOperation(OPERATION_LIKE.toString(), this, new StringConstant(right)); }; return StringExpression; }(ComparableExpression)); /** * Base implementation for {@link Expression} bound to a string. */ export { StringExpression }; /** * Base implementation for {@link Expression} bound to a number. */ var /** * Base implementation for {@link Expression} bound to a number. */ NumberExpression = /** @class */ (function (_super) { __extends(NumberExpression, _super); function NumberExpression() { return _super !== null && _super.apply(this, arguments) || this; } return NumberExpression; }(ComparableExpression)); /** * Base implementation for {@link Expression} bound to a number. */ export { NumberExpression }; /** * Base implementation for {@link Expression} bound to a boolean. */ var /** * Base implementation for {@link Expression} bound to a boolean. */ BooleanExpression = /** @class */ (function (_super) { __extends(BooleanExpression, _super); function BooleanExpression() { return _super !== null && _super.apply(this, arguments) || this; } return BooleanExpression; }(ComparableExpression)); /** * Base implementation for {@link Expression} bound to a boolean. */ export { BooleanExpression }; /** * Represents a boolean operation comparing two expressions with the provided operation. */ var /** * Represents a boolean operation comparing two expressions with the provided operation. */ BooleanOperation = /** @class */ (function (_super) { __extends(BooleanOperation, _super); function BooleanOperation(operation, left, right) { var _this = _super.call(this) || this; _this.operation = operation; _this.expressions = [left, right]; return _this; } return BooleanOperation; }(BooleanExpression)); /** * Represents a boolean operation comparing two expressions with the provided operation. */ export { BooleanOperation }; /** * Helper methods to create expressions. */ var /** * Helper methods to create expressions. */ ExpressionFactory = /** @class */ (function () { function ExpressionFactory() { } ExpressionFactory.toBeanPath = function (object, attributeNames) { var path = new BeanBinding(object); for (var _i = 0, attributeNames_1 = attributeNames; _i < attributeNames_1.length; _i++) { var attributeName = attributeNames_1[_i]; path = new BeanPath(path, attributeName); } return path; }; ExpressionFactory.toLiteral = function (value) { if (typeof value === 'string') { return new StringConstant(value); } else if (typeof value === 'number') { return new NumberConstant(value); } else if (typeof value === 'boolean') { return new BooleanConstant(value); } else { throw Error('unknown type for value ' + value); } }; ExpressionFactory.concat = function (parent, value1) { var value0 = parent != null ? parent.toString() : null; if (value0) { if (parent instanceof MapPath || parent instanceof ArrayPath) { return value0 + '[' + value1 + ']'; } else { return value0 + '.' + value1; } } if (value1) { return value1; } return null; }; return ExpressionFactory; }()); /** * Helper methods to create expressions. */ export { ExpressionFactory }; var BooleanConstant = /** @class */ (function (_super) { __extends(BooleanConstant, _super); function BooleanConstant(value) { var _this = _super.call(this) || this; _this.value = value; return _this; } BooleanConstant.prototype.isConstant = function () { return true; }; BooleanConstant.prototype.toString = function () { return this.value.toString(); }; return BooleanConstant; }(BooleanExpression)); export { BooleanConstant }; function toSourcePointerInternal(path, resource) { if (resource !== null) { return '/data/' + path.replace(new RegExp('\\.', 'g'), '/'); } else { throw new Error('resource not available for ' + path.toString()); } } function toFormNameInternal(path, resource) { if (resource !== null) { return '//' + resource.type + '//' + resource.id + '//' + path.toString(); } else { throw new Error('resource not available for ' + path.toString()); } } var NumberConstant = /** @class */ (function (_super) { __extends(NumberConstant, _super); function NumberConstant(literal) { var _this = _super.call(this) || this; _this.value = literal; return _this; } NumberConstant.prototype.isConstant = function () { return true; }; NumberConstant.prototype.toString = function () { return this.value.toString(); }; return NumberConstant; }(NumberExpression)); export { NumberConstant }; var StringConstant = /** @class */ (function (_super) { __extends(StringConstant, _super); function StringConstant(value) { var _this = _super.call(this) || this; _this.value = value; return _this; } StringConstant.prototype.isConstant = function () { return true; }; StringConstant.prototype.toString = function () { return this.value; }; return StringConstant; }(StringExpression)); export { StringConstant }; /** * Represents a path accessing a object property. */ var /** * Represents a path accessing a object property. */ BeanPath = /** @class */ (function (_super) { __extends(BeanPath, _super); function BeanPath(parentPath, property) { var _this = _super.call(this) || this; _this.parentPath = parentPath; _this.property = property; return _this; } BeanPath.prototype.add = function (path) { return path; }; BeanPath.prototype.createString = function (property) { return this.add(new StringPath(this, property)); }; BeanPath.prototype.createBoolean = function (property) { return this.add(new BooleanPath(this, property)); }; BeanPath.prototype.createNumber = function (property) { return this.add(new NumberPath(this, property)); }; BeanPath.prototype.toString = function () { if (this.parentPath) { if (this.parentPath !== null) { return ExpressionFactory.concat(this.parentPath, this.property); } } return this.property; }; BeanPath.prototype.isPath = function () { return true; }; BeanPath.prototype.getValue = function () { return doGetValue(this.parentPath, this.property); }; BeanPath.prototype.setValue = function (newValue) { return doSetValue(this.parentPath, this.property, newValue); }; BeanPath.prototype.getResource = function () { return this.parentPath.getResource(); }; BeanPath.prototype.toFormName = function () { return toFormNameInternal(this, this.parentPath.getResource()); }; BeanPath.prototype.getSourcePointer = function () { return toSourcePointerInternal(this.toString(), this.parentPath.getResource()); }; BeanPath.prototype.toQueryPath = function () { return toQueryPath(this.toString()); }; return BeanPath; }(SimpleExpression)); /** * Represents a path accessing a object property. */ export { BeanPath }; /** * Represents a path accessing a string property. */ var /** * Represents a path accessing a string property. */ StringPath = /** @class */ (function (_super) { __extends(StringPath, _super); function StringPath(parent, property) { var _this = _super.call(this) || this; _this.parent = parent; _this.property = property; return _this; } StringPath.prototype.toString = function () { return ExpressionFactory.concat(this.parent, this.property); }; StringPath.prototype.isPath = function () { return true; }; StringPath.prototype.getValue = function () { return doGetValue(this.parent, this.property); }; StringPath.prototype.setValue = function (newValue) { return doSetValue(this.parent, this.property, newValue); }; StringPath.prototype.toFormName = function () { return toFormNameInternal(this, this.parent.getResource()); }; StringPath.prototype.getResource = function () { return this.parent.getResource(); }; StringPath.prototype.getSourcePointer = function () { return toSourcePointerInternal(this.toString(), this.parent.getResource()); }; StringPath.prototype.toQueryPath = function () { return toQueryPath(this.toString()); }; return StringPath; }(StringExpression)); /** * Represents a path accessing a string property. */ export { StringPath }; var ArrayPath = /** @class */ (function (_super) { __extends(ArrayPath, _super); function ArrayPath(parent, property, _referenceType) { var _this = _super.call(this) || this; _this.parent = parent; _this.property = property; _this._referenceType = _referenceType; return _this; } ArrayPath.prototype.getElement = function (index) { return new this._referenceType(this, index); }; ArrayPath.prototype.toString = function () { return ExpressionFactory.concat(this.parent, this.property); }; ArrayPath.prototype.isPath = function () { return true; }; ArrayPath.prototype.isConstant = function () { return false; }; ArrayPath.prototype.getValue = function () { return doGetValue(this.parent, this.property); }; ArrayPath.prototype.setValue = function (newValue) { return doSetValue(this.parent, this.property, newValue); }; ArrayPath.prototype.toFormName = function () { return toFormNameInternal(this, this.parent.getResource()); }; ArrayPath.prototype.getResource = function () { return this.parent.getResource(); }; ArrayPath.prototype.getSourcePointer = function () { return toSourcePointerInternal(this.toString(), this.parent.getResource()); }; ArrayPath.prototype.toQueryPath = function () { return toQueryPath(this.toString()); }; return ArrayPath; }(SimpleExpression)); export { ArrayPath }; var MapPath = /** @class */ (function (_super) { __extends(MapPath, _super); function MapPath(parent, property, _referenceType) { var _this = _super.call(this) || this; _this.parent = parent; _this.property = property; _this._referenceType = _referenceType; return _this; } MapPath.prototype.getElement = function (key) { return new this._referenceType(this, key); }; MapPath.prototype.toString = function () { return ExpressionFactory.concat(this.parent, this.property); }; MapPath.prototype.isPath = function () { return true; }; MapPath.prototype.isConstant = function () { return false; }; MapPath.prototype.getValue = function () { return doGetValue(this.parent, this.property); }; MapPath.prototype.setValue = function (newValue) { return doSetValue(this.parent, this.property, newValue); }; MapPath.prototype.toFormName = function () { return toFormNameInternal(this, this.parent.getResource()); }; MapPath.prototype.getResource = function () { return this.parent.getResource(); }; MapPath.prototype.getSourcePointer = function () { return toSourcePointerInternal(this.toString(), this.parent.getResource()); }; MapPath.prototype.toQueryPath = function () { return toQueryPath(this.toString()); }; return MapPath; }(SimpleExpression)); export { MapPath }; function doGetValue(parent, property) { var parentValue = parent.getValue(); // consider to eliminate this case, root between binding and path if (!property) { return parentValue; } if (parentValue != null) { return parentValue[property]; } else { return null; } } function doSetValue(parent, property, newValue) { var parentValue = parent.getValue(); if (parentValue === null) { throw new Error(); // TODO setup map in parent } if (!property) { return new Error(); } parentValue[property] = newValue; } /** * Represents a path accessing a number property. */ var /** * Represents a path accessing a number property. */ NumberPath = /** @class */ (function (_super) { __extends(NumberPath, _super); function NumberPath(parent, property) { var _this = _super.call(this) || this; _this.parent = parent; _this.property = property; return _this; } NumberPath.prototype.toString = function () { return ExpressionFactory.concat(this.parent, this.property); }; NumberPath.prototype.isPath = function () { return true; }; NumberPath.prototype.getValue = function () { return doGetValue(this.parent, this.property); }; NumberPath.prototype.setValue = function (newValue) { return doSetValue(this.parent, this.property, newValue); }; NumberPath.prototype.toFormName = function () { return toFormNameInternal(this, this.parent.getResource()); }; NumberPath.prototype.getResource = function () { return this.parent.getResource(); }; NumberPath.prototype.getSourcePointer = function () { return toSourcePointerInternal(this.toString(), this.parent.getResource()); }; NumberPath.prototype.toQueryPath = function () { return toQueryPath(this.toString()); }; return NumberPath; }(NumberExpression)); /** * Represents a path accessing a number property. */ export { NumberPath }; /** * Represents a path accessing a boolean property. */ var /** * Represents a path accessing a boolean property. */ BooleanPath = /** @class */ (function (_super) { __extends(BooleanPath, _super); function BooleanPath(parent, property) { var _this = _super.call(this) || this; _this.parent = parent; _this.property = property; return _this; } BooleanPath.prototype.toString = function () { return ExpressionFactory.concat(this.parent, this.property); }; BooleanPath.prototype.isPath = function () { return true; }; BooleanPath.prototype.getValue = function () { return doGetValue(this.parent, this.property); }; BooleanPath.prototype.setValue = function (newValue) { return doSetValue(this.parent, this.property, newValue); }; BooleanPath.prototype.toFormName = function () { return toFormNameInternal(this, this.parent.getResource()); }; BooleanPath.prototype.getResource = function () { return this.parent.getResource(); }; BooleanPath.prototype.getSourcePointer = function () { return toSourcePointerInternal(this.toString(), this.parent.getResource()); }; BooleanPath.prototype.toQueryPath = function () { return toQueryPath(this.toString()); }; return BooleanPath; }(BooleanExpression)); /** * Represents a path accessing a boolean property. */ export { BooleanPath }; /** * Represents a direct refresh to object. Sits at the root of an expression chain. */ var /** * Represents a direct refresh to object. Sits at the root of an expression chain. */ BeanBinding = /** @class */ (function (_super) { __extends(BeanBinding, _super); function BeanBinding(bean) { var _this = _super.call(this, null, null) || this; _this.bean = bean; return _this; } BeanBinding.prototype.toString = function () { return null; }; BeanBinding.prototype.getValue = function () { return this.bean; }; BeanBinding.prototype.setValue = function (value) { this.bean = value; }; BeanBinding.prototype.getResource = function () { return this.bean; }; return BeanBinding; }(BeanPath)); /** * Represents a direct refresh to object. Sits at the root of an expression chain. */ export { BeanBinding }; //# sourceMappingURL=crnk.expression.js.map