projects/netgrif-components-core/src/lib/data-fields/time-instance-abstract-field/models/abstract-time-instance-field.ts
Protected
constructor(stringId: string, title: string, value: Moment, behavior: Behavior, placeholder?: string, description?: string, layout?: Layout, validations?: any, component?: Component, parentTaskId?: string)
|
Public max |
Type : Moment
|
Public min |
Type : Moment
|
Protected _blockSubscription |
Type : Subscription
|
Inherited from
DataField
|
Defined in
DataField:71
|
Protected _formControlValueSubscription |
Type : Subscription
|
Inherited from
DataField
|
Defined in
DataField:73
|
Protected _initializedSubscription |
Type : Subscription
|
Inherited from
DataField
|
Defined in
DataField:103
|
Stores the last subscription to the _initialized$ Stream, to prevent multiple block events from executing at the same time |
Protected _localLayout |
Type : Layout
|
Inherited from
DataField
|
Defined in
DataField:115
|
Stores a copy of the fields layout, that can be modified by the layouting algorithm as needed without affecting the base configuration. |
Protected _myValueSubscription |
Type : Subscription
|
Inherited from
DataField
|
Defined in
DataField:74
|
Protected _touchSubscription |
Type : Subscription
|
Inherited from
DataField
|
Defined in
DataField:72
|
Protected _updateSubscription |
Type : Subscription
|
Inherited from
DataField
|
Defined in
DataField:70
|
Protected _validators |
Type : Array<ValidatorFn>
|
Inherited from
DataField
|
Defined in
DataField:97
|
Validators resolved from field validations |
Protected layoutSubject |
Type : BehaviorSubject<Layout>
|
Inherited from
DataField
|
Defined in
DataField:120
|
Listens for layout changes |
Public Optional validations |
Type : Array<Validation>
|
Inherited from
DataField
|
Defined in
DataField:152
|
Static isEqual | ||||||||||||
isEqual(a: Moment, b: Moment, granularity?: moment.unitOfTime.StartOf)
|
||||||||||||
Parameters :
Returns :
boolean
|
Static parseDate | ||||||||
parseDate(date: string)
|
||||||||
Parse date from string: date is string 'past' - return string 'past' date is string 'future' - return string 'future' date is string 'today'/'now' - return moment instance of current date/time date is string build by combination of 'today'/'now'+/-ISO8601 (eg. today-P1DT1H) - return parsed moment instance of date date is string of specific date - return moment instance of this specific date
Parameters :
Returns :
any
|
Protected resolveValidations |
resolveValidations()
|
Inherited from
DataField
|
Defined in
DataField:64
|
Returns :
Array<ValidatorFn>
|
Protected validBetween | |||||||||
validBetween(first: Moment, second: Moment)
|
|||||||||
Parameters :
Returns :
ValidatorFn
|
Protected validFromPast | ||||||
validFromPast(range: Moment)
|
||||||
Parameters :
Returns :
ValidatorFn
|
Protected validToFuture | ||||||
validToFuture(range: Moment)
|
||||||
Parameters :
Returns :
ValidatorFn
|
Protected validWeekend | ||||||
validWeekend(fc: FormControl)
|
||||||
Parameters :
Returns :
{ validWeekend: boolean; }
|
Protected validWorkday | ||||||
validWorkday(fc: FormControl)
|
||||||
Parameters :
Returns :
{ validWorkday: boolean; }
|
Protected _determineFormControlValidity | ||||||||
_determineFormControlValidity(formControl: FormControl)
|
||||||||
Inherited from
DataField
|
||||||||
Defined in
DataField:490
|
||||||||
Computes whether the FormControl si valid.
Parameters :
Returns :
boolean
|
Public applyChange | ||||||||
applyChange(change: Change)
|
||||||||
Inherited from
DataField
|
||||||||
Defined in
DataField:555
|
||||||||
Updates the state of this data field model object. Also see ChangedFields.
Parameters :
Returns :
void
|
Protected calculateValidity | |||||||||
calculateValidity(forValidRequired: boolean, formControl: FormControl)
|
|||||||||
Inherited from
DataField
|
|||||||||
Defined in
DataField:598
|
|||||||||
Parameters :
Returns :
boolean
|
Public clearValidators |
clearValidators()
|
Inherited from
DataField
|
Defined in
DataField:530
|
Returns :
void
|
Public componentChange$ |
componentChange$()
|
Inherited from
DataField
|
Defined in
DataField:307
|
Returns :
Observable<Component>
|
Public destroy |
destroy()
|
Inherited from
DataField
|
Defined in
DataField:393
|
Returns :
void
|
Public disconnectFormControl |
disconnectFormControl()
|
Inherited from
DataField
|
Defined in
DataField:433
|
Returns :
void
|
Public focus |
focus()
|
Inherited from
DataField
|
Defined in
DataField:357
|
Returns :
void
|
Public getComponentType |
getComponentType()
|
Inherited from
DataField
|
Defined in
DataField:387
|
This function resolve type of component for HTML
Returns :
string
type of component in string |
Public Abstract getTypedComponentType |
getTypedComponentType()
|
Inherited from
DataField
|
Defined in
DataField:391
|
Returns :
string
|
Public getUpdateOnStrategy |
getUpdateOnStrategy()
|
Inherited from
DataField
|
Defined in
DataField:379
|
Returns :
UpdateOnStrategy
|
Public isInvalid | ||||||
isInvalid(formControl: FormControl)
|
||||||
Inherited from
DataField
|
||||||
Defined in
DataField:615
|
||||||
Parameters :
Returns :
boolean
|
Public registerFormControl | ||||||
registerFormControl(formControl: FormControl)
|
||||||
Inherited from
DataField
|
||||||
Defined in
DataField:403
|
||||||
Parameters :
Returns :
void
|
Public replaceValidations | ||||||
replaceValidations(validations: Array<Validation>)
|
||||||
Inherited from
DataField
|
||||||
Defined in
DataField:525
|
||||||
Parameters :
Returns :
void
|
Public resetLocalLayout |
resetLocalLayout()
|
Inherited from
DataField
|
Defined in
DataField:622
|
Copies the layout settings into the local layout.
Returns :
void
|
Public resolveAppearance | ||||||
resolveAppearance(config: ConfigurationService)
|
||||||
Inherited from
DataField
|
||||||
Defined in
DataField:571
|
||||||
Parameters :
Returns :
void
|
Protected resolveFormControlValidators |
resolveFormControlValidators()
|
Inherited from
DataField
|
Defined in
DataField:506
|
Creates Validator objects based on field See Behavior for information about data field behavior. See ValidatorFn and Validators for information about Validators. Alternatively see Form Validation guide from Angular.
Returns :
Array<ValidatorFn>
|
Public resolvePrevValue | ||||||
resolvePrevValue(value: T)
|
||||||
Inherited from
DataField
|
||||||
Defined in
DataField:591
|
||||||
Parameters :
Returns :
void
|
Public revertToPreviousValue |
revertToPreviousValue()
|
Inherited from
DataField
|
Defined in
DataField:311
|
Returns :
void
|
Protected subscribeToInnerSubjects | ||||||
subscribeToInnerSubjects(formControl: FormControl)
|
||||||
Inherited from
DataField
|
||||||
Defined in
DataField:458
|
||||||
Parameters :
Returns :
void
|
Public update |
update()
|
Inherited from
DataField
|
Defined in
DataField:341
|
Returns :
void
|
Protected updateFormControlState | ||||||
updateFormControlState(formControl: FormControl)
|
||||||
Inherited from
DataField
|
||||||
Defined in
DataField:452
|
||||||
Parameters :
Returns :
void
|
Public valueChanges |
valueChanges()
|
Inherited from
DataField
|
Defined in
DataField:345
|
Returns :
Observable<T>
|
Protected valueEquality | ||||||||||||
valueEquality(a: T, b: T)
|
||||||||||||
Inherited from
DataField
|
||||||||||||
Defined in
DataField:545
|
||||||||||||
Determines if two values of the data field are equal.
Parameters :
Returns :
boolean
|
Public valueWithoutChange | ||||||
valueWithoutChange(value: T)
|
||||||
Inherited from
DataField
|
||||||
Defined in
DataField:235
|
||||||
Parameters :
Returns :
void
|
import {Behavior} from '../../models/behavior';
import {FormControl, ValidatorFn, Validators} from '@angular/forms';
import moment, {Moment} from 'moment';
import {Layout} from '../../models/layout';
import {Component} from '../../models/component';
import {DataField} from '../../models/abstract-data-field';
export enum AbstractTimeInstanceFieldValidation {
BETWEEN = 'between',
WORKDAY = 'workday',
WEEKEND = 'weekend',
REQUIRED = 'required',
VALID_BETWEEN = 'validBetween',
VALID_WORKDAY = 'validWorkday',
VALID_WEEKEND = 'validWeekend'
}
export abstract class AbstractTimeInstanceField extends DataField<Moment> {
public min: Moment;
public max: Moment;
protected constructor(stringId: string, title: string, value: Moment, behavior: Behavior, placeholder?: string,
description?: string, layout?: Layout, validations?: any, component?: Component, parentTaskId?: string) {
super(stringId, title, value, behavior, placeholder, description, layout, validations, component, parentTaskId);
}
public static isEqual(a: Moment, b: Moment, granularity?: moment.unitOfTime.StartOf): boolean {
return (!a && !b) || (!!a && !!b && a.isSame(b, granularity));
}
/**
* Parse date from string:
* date is string 'past' - return string 'past'
* date is string 'future' - return string 'future'
* date is string 'today'/'now' - return moment instance of current date/time
* date is string build by combination of 'today'/'now'+/-ISO8601 (eg. today-P1DT1H) - return parsed moment instance of date
* date is string of specific date - return moment instance of this specific date
* @param date - string that should be parsed
*/
public static parseDate(date: string) {
if (date.includes('past')) {
return 'past';
} else if (date.includes('future')) {
return 'future';
} else if (date.includes('today') || date.includes('now')) {
if (date.includes('+') || date.includes('-')) {
const difference = date.includes('+') ? date.split('+')[1] : date.split('-')[1];
if (date.includes('today')) {
return date.includes('+') ? moment().startOf('day').add(moment.duration(difference))
: moment().startOf('day').subtract(moment.duration(difference));
}
if (date.includes('now')) {
return date.includes('+') ? moment().add(moment.duration(difference)) : moment().subtract(moment.duration(difference));
}
}
return date.includes('today') ? moment().startOf('day') : moment();
} else {
const newDate = moment(date);
return newDate.isValid ? newDate : null;
}
}
protected resolveValidations(): Array<ValidatorFn> {
const result = [];
this.validations.forEach(item => {
if (item.validationRule.includes(AbstractTimeInstanceFieldValidation.BETWEEN)) {
const tmp = item.validationRule.split(' ');
const ranges = tmp[1].split(',');
const start = AbstractTimeInstanceField.parseDate(ranges[0]);
const end = AbstractTimeInstanceField.parseDate(ranges[1]);
if (start && end) {
if (start === 'past' && moment(end).isValid()) {
result.push(this.validFromPast(moment(end)));
this.max = moment(end);
} else if (end === 'future' && moment(start).isValid()) {
result.push(this.validToFuture(moment(start)));
this.min = moment(start);
} else if (moment(start).isValid() && moment(end).isValid()) {
result.push(this.validBetween(moment(start), moment(end)));
this.min = moment(start);
this.max = moment(end);
}
}
} else if (item.validationRule.includes(AbstractTimeInstanceFieldValidation.WORKDAY)) {
result.push(this.validWorkday);
} else if (item.validationRule.includes(AbstractTimeInstanceFieldValidation.WEEKEND)) {
result.push(this.validWeekend);
}
});
return result;
}
protected validFromPast(range: Moment): ValidatorFn {
return (fc: FormControl): { [key: string]: any } | null => fc.value > range ? {validBetween: true} : null;
}
protected validToFuture(range: Moment): ValidatorFn {
return (fc: FormControl): { [key: string]: any } | null => fc.value < range ? {validBetween: true} : null;
}
protected validBetween(first: Moment, second: Moment): ValidatorFn {
return (fc: FormControl): { [key: string]: any } | null => fc.value < first || fc.value > second ? {validBetween: true} : null;
}
protected validWorkday(fc: FormControl) {
const dayOfWeek = !!fc.value ? fc.value.isoWeekday() : null;
return dayOfWeek === 6 || dayOfWeek === 7 ? {validWorkday: true} : null;
}
protected validWeekend(fc: FormControl) {
const dayOfWeek = !!fc.value ? fc.value.isoWeekday() : null;
return dayOfWeek >= 1 && dayOfWeek <= 5 ? {validWeekend: true} : null;
}
}