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. 18
      src/Squidex/app/features/content/pages/content/content-page.component.ts
  3. 33
      src/Squidex/app/shared/services/schemas.service.spec.ts
  4. 37
      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>
<div *ngSwitchCase="'Dropdown'"> <div *ngSwitchCase="'Dropdown'">
<select class="form-control" [formControlName]="partition"> <select class="form-control" [formControlName]="partition">
<option></option> <option [ngValue]="null"></option>
<option *ngFor="let value of field.properties.allowedValues" [ngValue]="value">{{value}}</option> <option *ngFor="let value of field.properties.allowedValues" [ngValue]="value">{{value}}</option>
</select> </select>
</div> </div>
@ -60,6 +60,7 @@
</div> </div>
<div *ngSwitchCase="'Dropdown'"> <div *ngSwitchCase="'Dropdown'">
<select class="form-control" [formControlName]="partition"> <select class="form-control" [formControlName]="partition">
<option [ngValue]="null"></option>
<option *ngFor="let value of field.properties.allowedValues" [ngValue]="value">{{value}}</option> <option *ngFor="let value of field.properties.allowedValues" [ngValue]="value">{{value}}</option>
</select> </select>
</div> </div>

18
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']); fieldForm.controls['iv'].setValue(fieldValue['iv'] === undefined ? null : fieldValue['iv']);
} }
} }
if (this.content.status === 'Archived') { if (this.content.status === 'Archived') {
this.contentForm.disable(); 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, AddFieldDto,
AnalyticsService, AnalyticsService,
ApiUrlConfig, ApiUrlConfig,
AssetsFieldPropertiesDto,
CreateSchemaDto, CreateSchemaDto,
createProperties, createProperties,
DateTime, DateTime,
DateTimeFieldPropertiesDto,
FieldDto, FieldDto,
LocalCacheService, LocalCacheService,
SchemaDetailsDto, SchemaDetailsDto,
SchemaDto, SchemaDto,
SchemaPropertiesDto, SchemaPropertiesDto,
SchemasService, SchemasService,
StringFieldPropertiesDto,
UpdateFieldDto, UpdateFieldDto,
UpdateSchemaDto, UpdateSchemaDto,
UpdateSchemaScriptsDto, UpdateSchemaScriptsDto,
@ -682,4 +685,34 @@ describe('SchemasService', () => {
req.flush({}); 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);
});
}); });

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

@ -317,6 +317,10 @@ export class FieldDto {
public createValidators(isOptional: boolean): ValidatorFn[] { public createValidators(isOptional: boolean): ValidatorFn[] {
return this.properties.createValidators(isOptional); return this.properties.createValidators(isOptional);
} }
public defaultValue(): any {
return this.properties.getDefaultValue();
}
} }
export abstract class FieldPropertiesDto { export abstract class FieldPropertiesDto {
@ -333,6 +337,10 @@ export abstract class FieldPropertiesDto {
public abstract formatValue(value: any): string; public abstract formatValue(value: any): string;
public abstract createValidators(isOptional: boolean): ValidatorFn[]; public abstract createValidators(isOptional: boolean): ValidatorFn[];
public getDefaultValue(): any {
return null;
}
} }
export class StringFieldPropertiesDto extends FieldPropertiesDto { export class StringFieldPropertiesDto extends FieldPropertiesDto {
@ -387,6 +395,10 @@ export class StringFieldPropertiesDto extends FieldPropertiesDto {
return validators; return validators;
} }
public getDefaultValue(): any {
return this.defaultValue;
}
} }
export class NumberFieldPropertiesDto extends FieldPropertiesDto { export class NumberFieldPropertiesDto extends FieldPropertiesDto {
@ -426,11 +438,19 @@ export class NumberFieldPropertiesDto extends FieldPropertiesDto {
} }
if (this.allowedValues && this.allowedValues.length > 0) { if (this.allowedValues && this.allowedValues.length > 0) {
validators.push(ValidatorsEx.validValues(this.allowedValues)); if (this.isRequired && !isOptional) {
validators.push(ValidatorsEx.validValues(this.allowedValues));
} else {
validators.push(ValidatorsEx.validValues(this.allowedValues.concat([null])));
}
} }
return validators; return validators;
} }
public getDefaultValue(): any {
return this.defaultValue;
}
} }
export class DateTimeFieldPropertiesDto extends FieldPropertiesDto { export class DateTimeFieldPropertiesDto extends FieldPropertiesDto {
@ -473,6 +493,17 @@ export class DateTimeFieldPropertiesDto extends FieldPropertiesDto {
return validators; 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 { export class BooleanFieldPropertiesDto extends FieldPropertiesDto {
@ -502,6 +533,10 @@ export class BooleanFieldPropertiesDto extends FieldPropertiesDto {
return validators; return validators;
} }
public getDefaultValue(): any {
return this.defaultValue;
}
} }
export class GeolocationFieldPropertiesDto extends FieldPropertiesDto { export class GeolocationFieldPropertiesDto extends FieldPropertiesDto {

Loading…
Cancel
Save