my-test123
Version:
A planner front-end for Fabric8.
104 lines • 4.38 kB
JavaScript
import { Injectable } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { Logger } from 'ngx-base';
/**
* This service provides a pre-processing of the type information attached to the WITs.
* It is used by the dynamic form components to retrieve the type information and to
* create the FormGroup.
*/
var WorkItemTypeControlService = /** @class */ (function () {
function WorkItemTypeControlService(log) {
this.log = log;
// this properties/keys are considered fixed and will not be
// displayed in the dynamic form area. They will be skipped
// when creating the FormGroup.
this.FIXED_PROPERTIES = [
'system.area',
'system.assignees',
'system.codebase',
'system.created_at',
'system.creator',
'system.description',
'system.iteration',
'system.order',
'system.remote_item_id',
'system.state',
'system.title',
'system.updated_at',
'system.labels'
];
}
/**
* Validator for numbers only.
*/
WorkItemTypeControlService.prototype.numberValidator = function () {
return Validators.pattern(/^\d+$/);
};
/**
* Validator for enums.
*/
WorkItemTypeControlService.prototype.enumValidator = function (validValues) {
return function (control) {
var value = control.value;
// if the value is empty, it is valid (add a required validator in toFormGroup() if required).
if (!value)
return null;
var isInvalid = (validValues.indexOf(value) == -1);
return isInvalid ? { 'forbiddenName': { value: value } } : null;
};
};
// create the FormGroup with the FormControl instances
WorkItemTypeControlService.prototype.toFormGroup = function (workItem) {
var group = {};
// if there is no type information attached, just return an empty group
if (!workItem.relationalData || !workItem.relationships.baseType.data) {
this.log.warn('The work item ' + workItem.id + ' contains no normalized type information, no controls for form group can be created!');
return group;
}
var fields = workItem.relationships.baseType.data.attributes.fields;
for (var key in fields) {
if (this.FIXED_PROPERTIES.indexOf(key) != -1) {
this.log.log('Skipping form control for ' + key);
}
else {
this.log.log('Generating form control for ' + key);
// create validators array
var validators = [];
if (fields[key].required)
validators.push(Validators.required);
if (fields[key].type.kind === 'integer' || fields[key].type.kind === 'float')
validators.push(this.numberValidator());
else if (fields[key].type.kind === 'enum')
validators.push(this.enumValidator(fields[key].type.values));
// finally create FormControl, put it into the group under the key
group[key] = new FormControl(workItem.attributes[key] || '', validators);
}
}
return new FormGroup(group);
};
// create an array of the type description objects to enable Angular to iterate over it using ngFor
WorkItemTypeControlService.prototype.toAttributeArray = function (fields) {
var resultArray = [];
for (var key in fields) {
if (this.FIXED_PROPERTIES.indexOf(key) != -1) {
this.log.log('Skipping creating array entry form control for ' + key);
}
else {
var thisElement = JSON.parse(JSON.stringify(fields[key]));
thisElement.key = key;
resultArray.push(thisElement);
}
}
return resultArray;
};
WorkItemTypeControlService.decorators = [
{ type: Injectable },
];
/** @nocollapse */
WorkItemTypeControlService.ctorParameters = function () { return [
{ type: Logger, },
]; };
return WorkItemTypeControlService;
}());
export { WorkItemTypeControlService };
//# sourceMappingURL=work-item-type-control.service.js.map