Initial Commit ...
This commit is contained in:
@@ -0,0 +1,227 @@
|
||||
import { Component, Input } from '@angular/core';
|
||||
import {
|
||||
XReferenceDto,
|
||||
defaultReferenceDto,
|
||||
prepareReferenceDtoFields,
|
||||
} from 'x-mabsut-api-sdk';
|
||||
import {
|
||||
VBaseDtoComponent,
|
||||
VModelFieldDescriptor,
|
||||
VModelValidationResult,
|
||||
} from '../v-dto/v-dto.typings';
|
||||
import {
|
||||
toXString,
|
||||
getFieldNames,
|
||||
XExceptionIDs,
|
||||
nameofInstance,
|
||||
isNullOrUndefined,
|
||||
isNullOrEmptyString,
|
||||
} from 'x-framework-core';
|
||||
import {
|
||||
VReferencePresentType,
|
||||
isVReferencePresentType,
|
||||
VReferencePresentTypeIdentifier,
|
||||
} from './v-reference.typings';
|
||||
|
||||
@Component({
|
||||
selector: 'v-reference',
|
||||
templateUrl: './v-reference.component.html',
|
||||
styleUrls: ['./v-reference.component.scss'],
|
||||
})
|
||||
export class VReferenceComponent extends VBaseDtoComponent<XReferenceDto<any>> {
|
||||
//
|
||||
//#region Props ...
|
||||
readonly defaultModel: XReferenceDto<any> = {
|
||||
...defaultReferenceDto,
|
||||
};
|
||||
readonly FieldNames = getFieldNames(this.defaultModel);
|
||||
readonly ReferencePresentTypes = Object.assign({}, VReferencePresentType);
|
||||
|
||||
//
|
||||
@Input()
|
||||
presentType: VReferencePresentTypeIdentifier =
|
||||
this.ReferencePresentTypes.AsView;
|
||||
|
||||
//
|
||||
defaultFieldDescriptors: VModelFieldDescriptor<any>[] = [
|
||||
//
|
||||
// Index ...
|
||||
{
|
||||
hide: false,
|
||||
hideLabel: false,
|
||||
allowFieldForm: false,
|
||||
key: this.FieldNames.index,
|
||||
value: this.defaultModel.index,
|
||||
label: this.AppResourceIDs.index,
|
||||
type: this.ModelFieldTypes.Number,
|
||||
valueProvider: (value) => {
|
||||
return this.applyLocale(value);
|
||||
},
|
||||
} as VModelFieldDescriptor<number>,
|
||||
//
|
||||
// ProvidedFor ...
|
||||
{
|
||||
hide: false,
|
||||
hideLabel: false,
|
||||
allowFieldForm: false,
|
||||
key: this.FieldNames.providedFor,
|
||||
type: this.ModelFieldTypes.String,
|
||||
value: this.defaultModel.providedFor,
|
||||
label: this.AppResourceIDs.provided_for,
|
||||
} as VModelFieldDescriptor<string>,
|
||||
//
|
||||
// ReferenceTo ...
|
||||
{
|
||||
hide: false,
|
||||
hideLabel: false,
|
||||
allowFieldForm: false,
|
||||
key: this.FieldNames.referencedTo,
|
||||
type: this.ModelFieldTypes.String,
|
||||
value: this.defaultModel.referencedTo,
|
||||
label: this.AppResourceIDs.referenced_to,
|
||||
valueProvider: (value) => {
|
||||
return toXString(value);
|
||||
},
|
||||
} as VModelFieldDescriptor<any>,
|
||||
];
|
||||
//#endregion
|
||||
|
||||
//
|
||||
//#region LifeCycles ...
|
||||
onChange(changeKeys: string[]): void {
|
||||
super.onChange(changeKeys);
|
||||
|
||||
//
|
||||
let isChanged = false;
|
||||
let applyChanges = false;
|
||||
|
||||
//
|
||||
// Present Type ...
|
||||
isChanged = changeKeys.includes(nameofInstance(this, 'presentType'));
|
||||
if (isChanged) {
|
||||
//
|
||||
if (!isVReferencePresentType(this.presentType)) {
|
||||
//
|
||||
applyChanges = true;
|
||||
this.presentType = this.ReferencePresentTypes.AsView;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
if (applyChanges) {
|
||||
this.detectChanges();
|
||||
}
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//
|
||||
//#region Abstraction ...
|
||||
/**
|
||||
* Prepare Model Fields ...
|
||||
*
|
||||
* @param model
|
||||
* @returns
|
||||
*/
|
||||
prepare(model?: XReferenceDto<any>): XReferenceDto<any> {
|
||||
return prepareReferenceDtoFields(model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle Model Validation ...
|
||||
*
|
||||
* @param model
|
||||
* @returns
|
||||
*/
|
||||
validate(
|
||||
model?: Partial<XReferenceDto<any>>
|
||||
): VModelValidationResult<XReferenceDto<any>> {
|
||||
//
|
||||
let result: VModelValidationResult<XReferenceDto<any>> = undefined;
|
||||
|
||||
//
|
||||
let isValid = false;
|
||||
const validationErrors: string[] = [];
|
||||
|
||||
//
|
||||
model = {
|
||||
...this.model,
|
||||
...model,
|
||||
};
|
||||
const normalized = this.normalize(model);
|
||||
|
||||
//
|
||||
// Handle Validation ...
|
||||
isValid =
|
||||
!isNullOrUndefined(normalized) && !this.isSameAsDefault(normalized);
|
||||
if (!isValid) {
|
||||
//
|
||||
validationErrors.push(
|
||||
this.getExceptionMessage(XExceptionIDs.InvalidArgs)
|
||||
);
|
||||
} else {
|
||||
//
|
||||
// Check Other Validations ...
|
||||
|
||||
//
|
||||
// Index ...
|
||||
isValid = !isNullOrUndefined(normalized.index) && normalized.index >= 0;
|
||||
if (!isValid) {
|
||||
//
|
||||
validationErrors.push(
|
||||
`${this.resourceProvider(
|
||||
this.AppResourceIDs.index
|
||||
)}: ${this.getExceptionMessage(XExceptionIDs.InvalidArgs)}`
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// ProvidedFor ...
|
||||
isValid = !isNullOrEmptyString(normalized.providedFor);
|
||||
if (!isValid) {
|
||||
//
|
||||
validationErrors.push(
|
||||
`${this.resourceProvider(
|
||||
this.AppResourceIDs.provided_for
|
||||
)}: ${this.getExceptionMessage(XExceptionIDs.InvalidArgs)}`
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// ReferenceTo ...
|
||||
isValid = !isNullOrUndefined(normalized.referencedTo);
|
||||
if (!isValid) {
|
||||
//
|
||||
validationErrors.push(
|
||||
`${this.resourceProvider(
|
||||
this.AppResourceIDs.referenced_to
|
||||
)}: ${this.getExceptionMessage(XExceptionIDs.InvalidArgs)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Result ...
|
||||
|
||||
//
|
||||
isValid = !this.hasChild(validationErrors);
|
||||
|
||||
//
|
||||
const isValidForAdd =
|
||||
isNullOrUndefined(normalized.index) || normalized.index < 0;
|
||||
const isValidForUpdate =
|
||||
!isNullOrUndefined(normalized.index) && normalized.index > 0;
|
||||
|
||||
//
|
||||
result = {
|
||||
isValid,
|
||||
normalized,
|
||||
isValidForAdd,
|
||||
isValidForUpdate,
|
||||
validationErrors,
|
||||
};
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
//#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user