From d188c022de7df6d2e3a4d5a726a37f769739e317 Mon Sep 17 00:00:00 2001 From: Derek Begnoche Date: Thu, 30 Nov 2017 17:05:40 -0600 Subject: [PATCH] Add default value method to FieldDto --- .../pages/content/content-page.component.ts | 2 +- .../app/shared/services/schemas.service.ts | 381 ++++++++++-------- 2 files changed, 216 insertions(+), 167 deletions(-) diff --git a/src/Squidex/app/features/content/pages/content/content-page.component.ts b/src/Squidex/app/features/content/pages/content/content-page.component.ts index 2b4cf62a0..0cde3d13c 100644 --- a/src/Squidex/app/features/content/pages/content/content-page.component.ts +++ b/src/Squidex/app/features/content/pages/content/content-page.component.ts @@ -262,7 +262,7 @@ export class ContentPageComponent implements CanComponentDeactivate, OnDestroy, } else { for (const field of this.schema.fields) { if (field.properties.hasOwnProperty('defaultValue')) { - const defaultValue = (field.properties as any).defaultValue; + const defaultValue = field.defaultValue(); if (defaultValue) { const fieldForm = this.contentForm.get(field.name); if (field.partitioning === 'language') { diff --git a/src/Squidex/app/shared/services/schemas.service.ts b/src/Squidex/app/shared/services/schemas.service.ts index 13cc9597a..e013ccb64 100644 --- a/src/Squidex/app/shared/services/schemas.service.ts +++ b/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,8 @@ export abstract class FieldPropertiesDto { public abstract formatValue(value: any): string; public abstract createValidators(isOptional: boolean): ValidatorFn[]; + + public abstract getDefaultValue(): any; } export class StringFieldPropertiesDto extends FieldPropertiesDto { @@ -387,6 +393,10 @@ export class StringFieldPropertiesDto extends FieldPropertiesDto { return validators; } + + public getDefaultValue(): any { + return this.defaultValue; + } } export class NumberFieldPropertiesDto extends FieldPropertiesDto { @@ -435,6 +445,10 @@ export class NumberFieldPropertiesDto extends FieldPropertiesDto { return validators; } + + public getDefaultValue(): any { + return this.defaultValue; + } } export class DateTimeFieldPropertiesDto extends FieldPropertiesDto { @@ -477,6 +491,17 @@ export class DateTimeFieldPropertiesDto extends FieldPropertiesDto { return validators; } + + public getDefaultValue(): any { + if (this.calculatedDefaultValue != null) { + let now = new Date(); + if (this.calculatedDefaultValue === 'Today') { + now = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()); + } + return now.toUTCString(); + } + return this.defaultValue; + } } export class BooleanFieldPropertiesDto extends FieldPropertiesDto { @@ -506,6 +531,10 @@ export class BooleanFieldPropertiesDto extends FieldPropertiesDto { return validators; } + + public getDefaultValue(): any { + return this.defaultValue; + } } export class GeolocationFieldPropertiesDto extends FieldPropertiesDto { @@ -534,6 +563,10 @@ export class GeolocationFieldPropertiesDto extends FieldPropertiesDto { return validators; } + + public getDefaultValue(): any { + return null; + } } export class ReferencesFieldPropertiesDto extends FieldPropertiesDto { @@ -576,6 +609,10 @@ export class ReferencesFieldPropertiesDto extends FieldPropertiesDto { return validators; } + + public getDefaultValue(): any { + return null; + } } export class AssetsFieldPropertiesDto extends FieldPropertiesDto { @@ -617,6 +654,10 @@ export class AssetsFieldPropertiesDto extends FieldPropertiesDto { return validators; } + + public getDefaultValue(): any { + return null; + } } export class TagsFieldPropertiesDto extends FieldPropertiesDto { @@ -658,6 +699,10 @@ export class TagsFieldPropertiesDto extends FieldPropertiesDto { return validators; } + + public getDefaultValue(): any { + return null; + } } export class JsonFieldPropertiesDto extends FieldPropertiesDto { @@ -685,6 +730,10 @@ export class JsonFieldPropertiesDto extends FieldPropertiesDto { return validators; } + + public getDefaultValue(): any { + return null; + } } export class SchemaPropertiesDto { @@ -753,272 +802,272 @@ export class SchemasService { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas`); return HTTP.getVersioned(this.http, url) - .map(response => { - const body = response.payload.body; + .map(response => { + const body = response.payload.body; - const items: any[] = body; + const items: any[] = body; - return items.map(item => { - const properties = new SchemaPropertiesDto(item.properties.label, item.properties.hints); + return items.map(item => { + const properties = new SchemaPropertiesDto(item.properties.label, item.properties.hints); - return new SchemaDto( - item.id, - item.name, properties, - item.isPublished, - item.createdBy, - item.lastModifiedBy, - DateTime.parseISO_UTC(item.created), - DateTime.parseISO_UTC(item.lastModified), - new Version(item.version.toString())); - }); - }) - .pretifyError('Failed to load schemas. Please reload.'); + return new SchemaDto( + item.id, + item.name, properties, + item.isPublished, + item.createdBy, + item.lastModifiedBy, + DateTime.parseISO_UTC(item.created), + DateTime.parseISO_UTC(item.lastModified), + new Version(item.version.toString())); + }); + }) + .pretifyError('Failed to load schemas. Please reload.'); } public getSchema(appName: string, id: string): Observable { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${id}`); return HTTP.getVersioned(this.http, url) - .map(response => { - const body = response.payload.body; - - const fields = body.fields.map((item: any) => { - const propertiesDto = - createProperties( - item.properties.fieldType, - item.properties); - - return new FieldDto( - item.fieldId, - item.name, - item.isLocked, - item.isHidden, - item.isDisabled, - item.partitioning, - propertiesDto); - }); - - const properties = new SchemaPropertiesDto(body.properties.label, body.properties.hints); - - return new SchemaDetailsDto( - body.id, - body.name, properties, - body.isPublished, - body.createdBy, - body.lastModifiedBy, - DateTime.parseISO_UTC(body.created), - DateTime.parseISO_UTC(body.lastModified), - response.version, - fields, - body.scriptQuery, - body.scriptCreate, - body.scriptUpdate, - body.scriptDelete, - body.scriptChange); - }) - .catch(error => { - if (error instanceof HttpErrorResponse && error.status === 404) { - const cached = this.localCache.get(`schema.${appName}.${id}`); - - if (cached) { - return Observable.of(cached); - } + .map(response => { + const body = response.payload.body; + + const fields = body.fields.map((item: any) => { + const propertiesDto = + createProperties( + item.properties.fieldType, + item.properties); + + return new FieldDto( + item.fieldId, + item.name, + item.isLocked, + item.isHidden, + item.isDisabled, + item.partitioning, + propertiesDto); + }); + + const properties = new SchemaPropertiesDto(body.properties.label, body.properties.hints); + + return new SchemaDetailsDto( + body.id, + body.name, properties, + body.isPublished, + body.createdBy, + body.lastModifiedBy, + DateTime.parseISO_UTC(body.created), + DateTime.parseISO_UTC(body.lastModified), + response.version, + fields, + body.scriptQuery, + body.scriptCreate, + body.scriptUpdate, + body.scriptDelete, + body.scriptChange); + }) + .catch(error => { + if (error instanceof HttpErrorResponse && error.status === 404) { + const cached = this.localCache.get(`schema.${appName}.${id}`); + + if (cached) { + return Observable.of(cached); } + } - return Observable.throw(error); - }) - .pretifyError('Failed to load schema. Please reload.'); + return Observable.throw(error); + }) + .pretifyError('Failed to load schema. Please reload.'); } public postSchema(appName: string, dto: CreateSchemaDto, user: string, now: DateTime): Observable { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas`); return HTTP.postVersioned(this.http, url, dto) - .map(response => { - const body = response.payload.body; - - now = now || DateTime.now(); - - return new SchemaDetailsDto( - body.id, - dto.name, - dto.properties || new SchemaPropertiesDto(), - false, - user, - user, - now, - now, - response.version, - dto.fields || [], - body.scriptQuery, - body.scriptCreate, - body.scriptUpdate, - body.scriptDelete, - body.scriptChange); - }) - .do(schema => { - this.analytics.trackEvent('Schema', 'Created', appName); - - this.localCache.set(`schema.${appName}.${schema.id}`, schema, 5000); - this.localCache.set(`schema.${appName}.${schema.name}`, schema, 5000); - }) - .pretifyError('Failed to create schema. Please reload.'); + .map(response => { + const body = response.payload.body; + + now = now || DateTime.now(); + + return new SchemaDetailsDto( + body.id, + dto.name, + dto.properties || new SchemaPropertiesDto(), + false, + user, + user, + now, + now, + response.version, + dto.fields || [], + body.scriptQuery, + body.scriptCreate, + body.scriptUpdate, + body.scriptDelete, + body.scriptChange); + }) + .do(schema => { + this.analytics.trackEvent('Schema', 'Created', appName); + + this.localCache.set(`schema.${appName}.${schema.id}`, schema, 5000); + this.localCache.set(`schema.${appName}.${schema.name}`, schema, 5000); + }) + .pretifyError('Failed to create schema. Please reload.'); } public postField(appName: string, schemaName: string, dto: AddFieldDto, version: Version): Observable> { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/fields`); return HTTP.postVersioned(this.http, url, dto, version) - .map(response => { - const body = response.payload.body; - - const field = new FieldDto( - body.id, - dto.name, - false, - false, - false, - dto.partitioning, - dto.properties); - - return new Versioned(response.version, field); - }) - .do(() => { - this.analytics.trackEvent('Schema', 'FieldCreated', appName); - }) - .pretifyError('Failed to add field. Please reload.'); + .map(response => { + const body = response.payload.body; + + const field = new FieldDto( + body.id, + dto.name, + false, + false, + false, + dto.partitioning, + dto.properties); + + return new Versioned(response.version, field); + }) + .do(() => { + this.analytics.trackEvent('Schema', 'FieldCreated', appName); + }) + .pretifyError('Failed to add field. Please reload.'); } public deleteSchema(appName: string, schemaName: string, version: Version): Observable> { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}`); return HTTP.deleteVersioned(this.http, url, version) - .do(() => { - this.localCache.remove(`schema.${appName}.${schemaName}`); - }) - .do(() => { - this.analytics.trackEvent('Schema', 'Deleted', appName); - }) - .pretifyError('Failed to delete schema. Please reload.'); + .do(() => { + this.localCache.remove(`schema.${appName}.${schemaName}`); + }) + .do(() => { + this.analytics.trackEvent('Schema', 'Deleted', appName); + }) + .pretifyError('Failed to delete schema. Please reload.'); } public putSchemaScripts(appName: string, schemaName: string, dto: UpdateSchemaScriptsDto, version: Version): Observable> { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/scripts`); return HTTP.putVersioned(this.http, url, dto, version) - .do(() => { - this.analytics.trackEvent('Schema', 'ScriptsConfigured', appName); - }) - .pretifyError('Failed to update schema scripts. Please reload.'); + .do(() => { + this.analytics.trackEvent('Schema', 'ScriptsConfigured', appName); + }) + .pretifyError('Failed to update schema scripts. Please reload.'); } public putSchema(appName: string, schemaName: string, dto: UpdateSchemaDto, version: Version): Observable> { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}`); return HTTP.putVersioned(this.http, url, dto, version) - .do(() => { - this.analytics.trackEvent('Schema', 'Updated', appName); - }) - .pretifyError('Failed to update schema. Please reload.'); + .do(() => { + this.analytics.trackEvent('Schema', 'Updated', appName); + }) + .pretifyError('Failed to update schema. Please reload.'); } public putFieldOrdering(appName: string, schemaName: string, dto: number[], version: Version): Observable> { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/fields/ordering`); return HTTP.putVersioned(this.http, url, { fieldIds: dto }, version) - .do(() => { - this.analytics.trackEvent('Schema', 'FieldsReordered', appName); - }) - .pretifyError('Failed to reorder fields. Please reload.'); + .do(() => { + this.analytics.trackEvent('Schema', 'FieldsReordered', appName); + }) + .pretifyError('Failed to reorder fields. Please reload.'); } public publishSchema(appName: string, schemaName: string, version: Version): Observable> { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/publish`); return HTTP.putVersioned(this.http, url, {}, version) - .do(() => { - this.analytics.trackEvent('Schema', 'Published', appName); - }) - .pretifyError('Failed to publish schema. Please reload.'); + .do(() => { + this.analytics.trackEvent('Schema', 'Published', appName); + }) + .pretifyError('Failed to publish schema. Please reload.'); } public unpublishSchema(appName: string, schemaName: string, version: Version): Observable> { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/unpublish`); return HTTP.putVersioned(this.http, url, {}, version) - .do(() => { - this.analytics.trackEvent('Schema', 'Unpublished', appName); - }) - .pretifyError('Failed to unpublish schema. Please reload.'); + .do(() => { + this.analytics.trackEvent('Schema', 'Unpublished', appName); + }) + .pretifyError('Failed to unpublish schema. Please reload.'); } public putField(appName: string, schemaName: string, fieldId: number, dto: UpdateFieldDto, version: Version): Observable> { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/fields/${fieldId}`); return HTTP.putVersioned(this.http, url, dto, version) - .do(() => { - this.analytics.trackEvent('Schema', 'FieldUpdated', appName); - }) - .pretifyError('Failed to update field. Please reload.'); + .do(() => { + this.analytics.trackEvent('Schema', 'FieldUpdated', appName); + }) + .pretifyError('Failed to update field. Please reload.'); } public enableField(appName: string, schemaName: string, fieldId: number, version: Version): Observable> { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/fields/${fieldId}/enable`); return HTTP.putVersioned(this.http, url, {}, version) - .do(() => { - this.analytics.trackEvent('Schema', 'FieldEnabled', appName); - }) - .pretifyError('Failed to enable field. Please reload.'); + .do(() => { + this.analytics.trackEvent('Schema', 'FieldEnabled', appName); + }) + .pretifyError('Failed to enable field. Please reload.'); } public disableField(appName: string, schemaName: string, fieldId: number, version: Version): Observable> { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/fields/${fieldId}/disable`); return HTTP.putVersioned(this.http, url, {}, version) - .do(() => { - this.analytics.trackEvent('Schema', 'FieldDisabled', appName); - }) - .pretifyError('Failed to disable field. Please reload.'); + .do(() => { + this.analytics.trackEvent('Schema', 'FieldDisabled', appName); + }) + .pretifyError('Failed to disable field. Please reload.'); } public lockField(appName: string, schemaName: string, fieldId: number, version: Version): Observable> { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/fields/${fieldId}/lock`); return HTTP.putVersioned(this.http, url, {}, version) - .do(() => { - this.analytics.trackEvent('Schema', 'FieldLocked', appName); - }) - .pretifyError('Failed to lock field. Please reload.'); + .do(() => { + this.analytics.trackEvent('Schema', 'FieldLocked', appName); + }) + .pretifyError('Failed to lock field. Please reload.'); } public showField(appName: string, schemaName: string, fieldId: number, version: Version): Observable> { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/fields/${fieldId}/show`); return HTTP.putVersioned(this.http, url, {}, version) - .do(() => { - this.analytics.trackEvent('Schema', 'FieldShown', appName); - }) - .pretifyError('Failed to show field. Please reload.'); + .do(() => { + this.analytics.trackEvent('Schema', 'FieldShown', appName); + }) + .pretifyError('Failed to show field. Please reload.'); } public hideField(appName: string, schemaName: string, fieldId: number, version: Version): Observable> { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/fields/${fieldId}/hide`); return HTTP.putVersioned(this.http, url, {}, version) - .do(() => { - this.analytics.trackEvent('Schema', 'FieldHidden', appName); - }) - .pretifyError('Failed to hide field. Please reload.'); + .do(() => { + this.analytics.trackEvent('Schema', 'FieldHidden', appName); + }) + .pretifyError('Failed to hide field. Please reload.'); } public deleteField(appName: string, schemaName: string, fieldId: number, version: Version): Observable> { const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/fields/${fieldId}`); return HTTP.deleteVersioned(this.http, url, version) - .do(() => { - this.analytics.trackEvent('Schema', 'FieldDeleted', appName); - }) - .pretifyError('Failed to delete field. Please reload.'); + .do(() => { + this.analytics.trackEvent('Schema', 'FieldDeleted', appName); + }) + .pretifyError('Failed to delete field. Please reload.'); } } \ No newline at end of file