Browse Source

Merge pull request #198 from dsbegnoche/DisplayDefaultValues

Display Default Values in UI
pull/204/head
Sebastian Stehle 8 years ago
committed by GitHub
parent
commit
8b3395cc45
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      src/Squidex/app/features/content/pages/content/content-field.component.html
  2. 16
      src/Squidex/app/features/content/pages/content/content-page.component.ts
  3. 33
      src/Squidex/app/shared/services/schemas.service.spec.ts
  4. 35
      src/Squidex/app/shared/services/schemas.service.ts

3
src/Squidex/app/features/content/pages/content/content-field.component.html

@ -31,7 +31,7 @@
</div>
<div *ngSwitchCase="'Dropdown'">
<select class="form-control" [formControlName]="partition">
<option></option>
<option [ngValue]="null"></option>
<option *ngFor="let value of field.properties.allowedValues" [ngValue]="value">{{value}}</option>
</select>
</div>
@ -60,6 +60,7 @@
</div>
<div *ngSwitchCase="'Dropdown'">
<select class="form-control" [formControlName]="partition">
<option [ngValue]="null"></option>
<option *ngFor="let value of field.properties.allowedValues" [ngValue]="value">{{value}}</option>
</select>
</div>

16
src/Squidex/app/features/content/pages/content/content-page.component.ts

@ -270,11 +270,23 @@ export class ContentPageComponent implements CanComponentDeactivate, OnDestroy,
fieldForm.controls['iv'].setValue(fieldValue['iv'] === undefined ? null : fieldValue['iv']);
}
}
if (this.content.status === 'Archived') {
this.contentForm.disable();
}
} else {
for (const field of this.schema.fields) {
const defaultValue = field.defaultValue();
if (defaultValue) {
const fieldForm = <FormGroup>this.contentForm.get(field.name);
if (field.partitioning === 'language') {
for (let language of this.languages) {
fieldForm.controls[language.iso2Code].setValue(defaultValue);
}
} else {
fieldForm.controls['iv'].setValue(defaultValue);
}
}
}
}
}
}

33
src/Squidex/app/shared/services/schemas.service.spec.ts

@ -12,15 +12,18 @@ import {
AddFieldDto,
AnalyticsService,
ApiUrlConfig,
AssetsFieldPropertiesDto,
CreateSchemaDto,
createProperties,
DateTime,
DateTimeFieldPropertiesDto,
FieldDto,
LocalCacheService,
SchemaDetailsDto,
SchemaDto,
SchemaPropertiesDto,
SchemasService,
StringFieldPropertiesDto,
UpdateFieldDto,
UpdateSchemaDto,
UpdateSchemaScriptsDto,
@ -683,3 +686,33 @@ describe('SchemasService', () => {
req.flush({});
}));
});
describe('FieldPropertiesDto', () => {
it('should return default value for StringFieldProperties', () => {
const dto = new StringFieldPropertiesDto(null, null, null, false, false, 'Input', 'Default Value');
expect(dto.getDefaultValue()).toEqual('Default Value');
});
it('should return default for DateFieldProperties', () => {
const now = DateTime.now();
const dto = new DateTimeFieldPropertiesDto(null, null, null, false, false, 'Input', now.toISOString());
expect(dto.getDefaultValue()).toEqual(now.toISOString());
});
it('should return calculated date when Today for DateFieldProperties', () => {
const now = DateTime.now().date;
const dto = new DateTimeFieldPropertiesDto(null, null, null, false, false, 'Input', null, null, null, 'Today');
expect(dto.getDefaultValue()).toEqual(now.toUTCStringFormat('ddd, DD MMM YYYY hh:mm:ss Z'));
});
it('should return calculated date when Now for DateFieldProperties', () => {
const now = DateTime.now();
const dto = new DateTimeFieldPropertiesDto(null, null, null, false, false, 'Input', null, null, null, 'Now');
expect(dto.getDefaultValue()).toEqual(now.toUTCStringFormat('ddd, DD MMM YYYY hh:mm:ss Z'));
});
it('should return null for default properties', () => {
const dto = new AssetsFieldPropertiesDto(null, null, null, false, false);
expect(dto.getDefaultValue()).toEqual(null);
});
});

35
src/Squidex/app/shared/services/schemas.service.ts

@ -317,6 +317,10 @@ export class FieldDto {
public createValidators(isOptional: boolean): ValidatorFn[] {
return this.properties.createValidators(isOptional);
}
public defaultValue(): any {
return this.properties.getDefaultValue();
}
}
export abstract class FieldPropertiesDto {
@ -333,6 +337,10 @@ export abstract class FieldPropertiesDto {
public abstract formatValue(value: any): string;
public abstract createValidators(isOptional: boolean): ValidatorFn[];
public getDefaultValue(): any {
return null;
}
}
export class StringFieldPropertiesDto extends FieldPropertiesDto {
@ -387,6 +395,10 @@ export class StringFieldPropertiesDto extends FieldPropertiesDto {
return validators;
}
public getDefaultValue(): any {
return this.defaultValue;
}
}
export class NumberFieldPropertiesDto extends FieldPropertiesDto {
@ -426,11 +438,19 @@ export class NumberFieldPropertiesDto extends FieldPropertiesDto {
}
if (this.allowedValues && this.allowedValues.length > 0) {
if (this.isRequired && !isOptional) {
validators.push(ValidatorsEx.validValues(this.allowedValues));
} else {
validators.push(ValidatorsEx.validValues(this.allowedValues.concat([null])));
}
}
return validators;
}
public getDefaultValue(): any {
return this.defaultValue;
}
}
export class DateTimeFieldPropertiesDto extends FieldPropertiesDto {
@ -473,6 +493,17 @@ export class DateTimeFieldPropertiesDto extends FieldPropertiesDto {
return validators;
}
public getDefaultValue(): any {
if (this.calculatedDefaultValue != null) {
let now = DateTime.now();
if (this.calculatedDefaultValue === 'Today') {
now = now.date;
}
return now.toUTCStringFormat('ddd, DD MMM YYYY hh:mm:ss Z');
}
return this.defaultValue;
}
}
export class BooleanFieldPropertiesDto extends FieldPropertiesDto {
@ -502,6 +533,10 @@ export class BooleanFieldPropertiesDto extends FieldPropertiesDto {
return validators;
}
public getDefaultValue(): any {
return this.defaultValue;
}
}
export class GeolocationFieldPropertiesDto extends FieldPropertiesDto {

Loading…
Cancel
Save