@crnk/angular-ngrx
Version:
Angular helper library for ngrx-json-api and crnk:
137 lines • 6.53 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 { Directive, EventEmitter, forwardRef, Inject, Input, OnChanges, OnDestroy, Optional, Output, Self } from '@angular/core';
import { AbstractFormGroupDirective, AsyncValidatorFn, ControlContainer, ControlValueAccessor, NG_ASYNC_VALIDATORS, NG_VALIDATORS, NG_VALUE_ACCESSOR, NgControl, NgForm, NgModelGroup, Validator, ValidatorFn } from '@angular/forms';
import { controlPath, selectValueAccessor, TemplateDrivenErrors } from './crnk.expression.form.utils';
import { ExpressionAccessor, Path } from '../crnk.expression';
import { CrnkControl } from './crnk.expression.form.model.base';
var formExpressionBinding = {
provide: NgControl,
useExisting: forwardRef(function () { return FormExpressionDirective; })
};
/**
* Allows to bind expressions of type {link Expression} to expectForm controls. The directive is very similar to
* NgModel, but allows to bind an expression instead of a generic object. The advantage is that an expression
* can hold both
*/
var FormExpressionDirective = /** @class */ (function (_super) {
__extends(FormExpressionDirective, _super);
/**
* NOTE that in contract to NgModel we do not use @Host for parent. This would enforce that the
* components are direct children of the forms component. TODO investigate whether there is
* any better solutions (maybe something similar to FormGroup). It does not seem like it is possible
* to expose an existing ControlContainer again with @Component.providers as it would lead to a circular
* dependency (there is no provider for children only, but itself as well).
*/
function FormExpressionDirective(parent, validators, asyncValidators, valueAccessors) {
var _this = _super.call(this) || this;
_this.arbFormExpressionChange = new EventEmitter();
_this._parent = parent;
_this._rawValidators = validators || [];
_this._rawAsyncValidators = asyncValidators || [];
_this.valueAccessor = selectValueAccessor(_this, valueAccessors);
return _this;
}
Object.defineProperty(FormExpressionDirective.prototype, "pathModel", {
get: function () {
return this._pathModel;
},
set: function (pathModel) {
this._pathModel = pathModel;
this._control['_pathModel'] = pathModel;
var expressionAccessor = this.valueAccessor;
if (!expressionAccessor) {
throw new Error('no value accessor found');
}
if (expressionAccessor.setExpression) {
expressionAccessor.setExpression(pathModel);
}
},
enumerable: true,
configurable: true
});
FormExpressionDirective.prototype.ngOnDestroy = function () {
if (this.formDirective) {
this.formDirective.removeControl(this);
}
};
Object.defineProperty(FormExpressionDirective.prototype, "path", {
get: function () {
if (!this._pathModel) {
return [];
}
if (this._parent) {
return controlPath(this._pathModel.toFormName(), this._parent);
}
else {
return [this._pathModel.toFormName()];
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(FormExpressionDirective.prototype, "formDirective", {
get: function () {
return this._parent ? this._parent.formDirective : null;
},
enumerable: true,
configurable: true
});
FormExpressionDirective.prototype.viewToModelUpdate = function (newValue) {
this.viewModel = newValue;
this.arbFormExpressionChange.emit(newValue);
};
FormExpressionDirective.prototype._setUpControl = function () {
if (!this._pathModel) {
throw new Error('Attribute crnkFormExpression is required');
}
this.name = this._pathModel.toFormName();
if (this.formDirective) {
this.formDirective.addControl(this);
this._registered = true;
}
};
FormExpressionDirective.prototype._checkForErrors = function () {
if (this._parent) {
this._checkParentType();
}
};
FormExpressionDirective.prototype._checkParentType = function () {
if (!(this._parent instanceof NgModelGroup) &&
this._parent instanceof AbstractFormGroupDirective) {
TemplateDrivenErrors.formGroupNameException();
}
else if (!(this._parent instanceof NgModelGroup) && !(this._parent instanceof NgForm)) {
TemplateDrivenErrors.modelParentException();
}
};
FormExpressionDirective.decorators = [
{ type: Directive, args: [{
selector: '[crnkFormExpression]:not([formControlName]):not([formControl])',
providers: [formExpressionBinding],
exportAs: 'crnkFormExpression'
},] },
];
/** @nocollapse */
FormExpressionDirective.ctorParameters = function () { return [
{ type: ControlContainer, decorators: [{ type: Optional },] },
{ type: Array, decorators: [{ type: Optional }, { type: Self }, { type: Inject, args: [NG_VALIDATORS,] },] },
{ type: Array, decorators: [{ type: Optional }, { type: Self }, { type: Inject, args: [NG_ASYNC_VALIDATORS,] },] },
{ type: Array, decorators: [{ type: Optional }, { type: Self }, { type: Inject, args: [NG_VALUE_ACCESSOR,] },] },
]; };
FormExpressionDirective.propDecorators = {
"pathModel": [{ type: Input, args: ['crnkFormExpression',] },],
"arbFormExpressionChange": [{ type: Output, args: ['arbFormExpressionChange',] },],
};
return FormExpressionDirective;
}(CrnkControl));
export { FormExpressionDirective };
//# sourceMappingURL=crnk.expression.form.model.form.js.map