diff --git a/src/Squidex/app/shared/internal.ts b/src/Squidex/app/shared/internal.ts index 439b10224..69fb2ff8f 100644 --- a/src/Squidex/app/shared/internal.ts +++ b/src/Squidex/app/shared/internal.ts @@ -39,17 +39,25 @@ export * from './services/usages.service'; export * from './services/users-provider.service'; export * from './services/users.service'; +export * from './state/apps.forms'; export * from './state/apps.state'; +export * from './state/assets.forms'; export * from './state/assets.state'; export * from './state/backups.state'; +export * from './state/clients.forms'; export * from './state/clients.state'; +export * from './state/contents.forms'; export * from './state/contents.state'; +export * from './state/contributors.forms'; export * from './state/contributors.state'; +export * from './state/languages.forms'; export * from './state/languages.state'; +export * from './state/patterns.forms'; export * from './state/patterns.state'; export * from './state/plans.state'; export * from './state/rule-events.state'; export * from './state/rules.state'; +export * from './state/schemas.forms'; export * from './state/schemas.state'; export * from './utils/messages'; diff --git a/src/Squidex/app/shared/state/apps.forms.ts b/src/Squidex/app/shared/state/apps.forms.ts new file mode 100644 index 000000000..1aed69c91 --- /dev/null +++ b/src/Squidex/app/shared/state/apps.forms.ts @@ -0,0 +1,30 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. + */ + +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; + +import { Form, ValidatorsEx } from '@app/framework'; + +const FALLBACK_NAME = 'my-app'; + +export class CreateAppForm extends Form { + public appName = + this.form.controls['name'].valueChanges.map(n => n || FALLBACK_NAME) + .startWith(FALLBACK_NAME); + + constructor(formBuilder: FormBuilder) { + super(formBuilder.group({ + name: ['', + [ + Validators.required, + Validators.maxLength(40), + ValidatorsEx.pattern('[a-z0-9]+(\-[a-z0-9]+)*', 'Name can contain lower case letters (a-z), numbers and dashes (not at the end).') + ] + ] + })); + } +} \ No newline at end of file diff --git a/src/Squidex/app/shared/state/apps.state.ts b/src/Squidex/app/shared/state/apps.state.ts index d76ca0b8c..9bcd94b37 100644 --- a/src/Squidex/app/shared/state/apps.state.ts +++ b/src/Squidex/app/shared/state/apps.state.ts @@ -1,4 +1,3 @@ - /* * Squidex Headless CMS * @@ -7,7 +6,6 @@ */ import { Injectable } from '@angular/core'; -import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { Observable } from 'rxjs'; import '@app/framework/utils/rxjs-extensions'; @@ -15,10 +13,8 @@ import '@app/framework/utils/rxjs-extensions'; import { DateTime, DialogService, - Form, ImmutableArray, - State, - ValidatorsEx + State } from '@app/framework'; import { @@ -27,26 +23,6 @@ import { CreateAppDto } from './../services/apps.service'; -const FALLBACK_NAME = 'my-app'; - -export class CreateAppForm extends Form { - public appName = - this.form.controls['name'].valueChanges.map(n => n || FALLBACK_NAME) - .startWith(FALLBACK_NAME); - - constructor(formBuilder: FormBuilder) { - super(formBuilder.group({ - name: ['', - [ - Validators.required, - Validators.maxLength(40), - ValidatorsEx.pattern('[a-z0-9]+(\-[a-z0-9]+)*', 'Name can contain lower case letters (a-z), numbers and dashes (not at the end).') - ] - ] - })); - } -} - interface Snapshot { apps: ImmutableArray; diff --git a/src/Squidex/app/shared/state/assets.forms.ts b/src/Squidex/app/shared/state/assets.forms.ts new file mode 100644 index 000000000..b6a23c236 --- /dev/null +++ b/src/Squidex/app/shared/state/assets.forms.ts @@ -0,0 +1,22 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. + */ + +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; + +import { Form } from '@app/framework'; + +export class RenameAssetForm extends Form { + constructor(formBuilder: FormBuilder) { + super(formBuilder.group({ + name: ['', + [ + Validators.required + ] + ] + })); + } +} \ No newline at end of file diff --git a/src/Squidex/app/shared/state/assets.state.ts b/src/Squidex/app/shared/state/assets.state.ts index 45e936b3f..441f03092 100644 --- a/src/Squidex/app/shared/state/assets.state.ts +++ b/src/Squidex/app/shared/state/assets.state.ts @@ -6,14 +6,12 @@ */ import { Injectable } from '@angular/core'; -import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { Observable } from 'rxjs'; import '@app/framework/utils/rxjs-extensions'; import { DialogService, - Form, ImmutableArray, Pager, State @@ -22,18 +20,6 @@ import { import { AssetDto, AssetsService} from './../services/assets.service'; import { AppsState } from './apps.state'; -export class RenameAssetForm extends Form { - constructor(formBuilder: FormBuilder) { - super(formBuilder.group({ - name: ['', - [ - Validators.required - ] - ] - })); - } -} - interface Snapshot { assets: ImmutableArray; assetsPager: Pager; diff --git a/src/Squidex/app/shared/state/clients.forms.ts b/src/Squidex/app/shared/state/clients.forms.ts new file mode 100644 index 000000000..c9e073567 --- /dev/null +++ b/src/Squidex/app/shared/state/clients.forms.ts @@ -0,0 +1,38 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. + */ + +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; + +import { Form, ValidatorsEx } from '@app/framework'; + +export class RenameClientForm extends Form { + constructor(formBuilder: FormBuilder) { + super(formBuilder.group({ + name: ['', + [ + Validators.required + ] + ] + })); + } +} + +export class AttachClientForm extends Form { + public hasNoName = + this.form.controls['name'].valueChanges.startWith('').map(x => !x || x.length === 0); + + constructor(formBuilder: FormBuilder) { + super(formBuilder.group({ + name: ['', + [ + Validators.maxLength(40), + ValidatorsEx.pattern('[a-z0-9]+(\-[a-z0-9]+)*', 'Name can contain lower case letters (a-z), numbers and dashes (not at the end).') + ] + ] + })); + } +} \ No newline at end of file diff --git a/src/Squidex/app/shared/state/clients.state.ts b/src/Squidex/app/shared/state/clients.state.ts index a11da8295..d48969bcc 100644 --- a/src/Squidex/app/shared/state/clients.state.ts +++ b/src/Squidex/app/shared/state/clients.state.ts @@ -6,17 +6,14 @@ */ import { Injectable } from '@angular/core'; -import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { Observable } from 'rxjs'; import '@app/framework/utils/rxjs-extensions'; import { DialogService, - Form, ImmutableArray, State, - ValidatorsEx, Version } from '@app/framework'; @@ -29,34 +26,6 @@ import { UpdateAppClientDto } from './../services/app-clients.service'; -export class RenameClientForm extends Form { - constructor(formBuilder: FormBuilder) { - super(formBuilder.group({ - name: ['', - [ - Validators.required - ] - ] - })); - } -} - -export class AttachClientForm extends Form { - public hasNoName = - this.form.controls['name'].valueChanges.startWith('').map(x => !x || x.length === 0); - - constructor(formBuilder: FormBuilder) { - super(formBuilder.group({ - name: ['', - [ - Validators.maxLength(40), - ValidatorsEx.pattern('[a-z0-9]+(\-[a-z0-9]+)*', 'Name can contain lower case letters (a-z), numbers and dashes (not at the end).') - ] - ] - })); - } -} - interface Snapshot { clients: ImmutableArray; diff --git a/src/Squidex/app/shared/state/contents.forms.ts b/src/Squidex/app/shared/state/contents.forms.ts new file mode 100644 index 000000000..1596ff14d --- /dev/null +++ b/src/Squidex/app/shared/state/contents.forms.ts @@ -0,0 +1,196 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. + */ + + +// tslint:disable:prefer-for-of + +import { FormArray, FormControl, FormGroup } from '@angular/forms'; + +import { + ErrorDto, + Form, + ImmutableArray, + Types +} from '@app/framework'; + +import { AppLanguageDto } from './../services/app-languages.service'; +import { fieldInvariant, RootFieldDto, SchemaDetailsDto } from './../services/schemas.service'; + +export class EditContentForm extends Form { + constructor( + private readonly schema: SchemaDetailsDto, + private readonly languages: ImmutableArray + ) { + super(new FormGroup({})); + + for (const field of schema.fields) { + const fieldForm = new FormGroup({}); + const fieldDefault = field.defaultValue(); + + const createControl = (isOptional: boolean) => { + if (field.properties.fieldType === 'Array') { + return new FormArray([], field.createValidators(isOptional)); + } else { + return new FormControl(fieldDefault, field.createValidators(isOptional)); + } + }; + + if (field.isLocalizable) { + for (let language of this.languages.values) { + fieldForm.setControl(language.iso2Code, createControl(language.isOptional)); + } + } else { + fieldForm.setControl(fieldInvariant, createControl(false)); + } + + this.form.setControl(field.name, fieldForm); + } + + this.enableContentForm(); + } + + public removeArrayItem(field: RootFieldDto, language: AppLanguageDto, index: number) { + this.findArrayItemForm(field, language).removeAt(index); + } + + public insertArrayItem(field: RootFieldDto, language: AppLanguageDto) { + if (field.nested.length > 0) { + const formControl = this.findArrayItemForm(field, language); + + this.addArrayItem(field, language, formControl); + } + } + + private addArrayItem(field: RootFieldDto, language: AppLanguageDto | null, formControl: FormArray) { + const formItem = new FormGroup({}); + + let isOptional = field.isLocalizable && language !== null && language.isOptional; + + for (let nested of field.nested) { + const nestedDefault = field.defaultValue(); + + formItem.setControl(nested.name, new FormControl(nestedDefault, nested.createValidators(isOptional))); + } + + formControl.push(formItem); + } + + private findArrayItemForm(field: RootFieldDto, language: AppLanguageDto): FormArray { + const fieldForm = this.form.get(field.name)!; + + if (field.isLocalizable) { + return fieldForm.get(language.iso2Code)!; + } else { + return fieldForm.get(fieldInvariant); + } + } + + public submitCompleted(newValue?: any) { + super.submitCompleted(newValue); + + this.enableContentForm(); + } + + public submitFailed(error?: string | ErrorDto) { + super.submitFailed(error); + + this.enableContentForm(); + } + + public loadData(value: any, isArchive: boolean) { + for (let field of this.schema.fields) { + if (field.properties.fieldType === 'Array' && field.nested.length > 0) { + const fieldValue = value ? value[field.name] || {} : {}; + const fieldForm = this.form.get(field.name)!; + + const addControls = (key: string, language: AppLanguageDto | null) => { + const languageValue = fieldValue[key]; + const languageForm = new FormArray([]); + + if (Types.isArray(languageValue)) { + for (let i = 0; i < languageValue.length; i++) { + this.addArrayItem(field, language, languageForm); + } + } + + fieldForm.setControl(key, languageForm); + }; + + if (field.isLocalizable) { + for (let language of this.languages.values) { + addControls(language.iso2Code, language); + } + } else { + addControls(fieldInvariant, null); + } + } + } + + super.load(value); + + if (isArchive) { + this.form.disable(); + } else { + this.enableContentForm(); + } + } + + private enableContentForm() { + if (this.schema.fields.length === 0) { + this.form.enable(); + } else { + for (const field of this.schema.fields) { + const fieldForm = this.form.controls[field.name]; + + if (field.isDisabled) { + fieldForm.disable(); + } else { + fieldForm.enable(); + } + } + } + } +} + +export class PatchContentForm extends Form { + constructor( + private readonly schema: SchemaDetailsDto, + private readonly language: AppLanguageDto + ) { + super(new FormGroup({})); + + for (let field of this.schema.listFields) { + if (field.properties && field.properties['inlineEditable']) { + this.form.setControl(field.name, new FormControl(undefined, field.createValidators(this.language.isOptional))); + } + } + } + + public submit() { + const result = super.submit(); + + if (result) { + const request = {}; + + for (let field of this.schema.listFields) { + if (field.properties['inlineEditable']) { + const value = result[field.name]; + + if (field.isLocalizable) { + request[field.name] = { [this.language.iso2Code]: value }; + } else { + request[field.name] = { iv: value }; + } + } + } + + return request; + } + + return result; + } +} \ No newline at end of file diff --git a/src/Squidex/app/shared/state/contents.state.ts b/src/Squidex/app/shared/state/contents.state.ts index f2a3a5a68..434c4574e 100644 --- a/src/Squidex/app/shared/state/contents.state.ts +++ b/src/Squidex/app/shared/state/contents.state.ts @@ -5,10 +5,7 @@ * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. */ -// tslint:disable:prefer-for-of - import { Injectable } from '@angular/core'; -import { FormArray, FormControl, FormGroup } from '@angular/forms'; import { Observable } from 'rxjs'; import '@app/framework/utils/rxjs-extensions'; @@ -16,199 +13,20 @@ import '@app/framework/utils/rxjs-extensions'; import { DateTime, DialogService, - ErrorDto, - Form, ImmutableArray, Pager, State, - Types, Version, Versioned } from '@app/framework'; -import { AppLanguageDto } from './../services/app-languages.service'; import { AuthService } from './../services/auth.service'; -import { fieldInvariant, RootFieldDto, SchemaDetailsDto, SchemaDto } from './../services/schemas.service'; +import { SchemaDto } from './../services/schemas.service'; import { AppsState } from './apps.state'; import { SchemasState } from './schemas.state'; import { ContentDto, ContentsService, ScheduleDto } from './../services/contents.service'; -export class EditContentForm extends Form { - constructor( - private readonly schema: SchemaDetailsDto, - private readonly languages: ImmutableArray - ) { - super(new FormGroup({})); - - for (const field of schema.fields) { - const fieldForm = new FormGroup({}); - const fieldDefault = field.defaultValue(); - - const createControl = (isOptional: boolean) => { - if (field.properties.fieldType === 'Array') { - return new FormArray([], field.createValidators(isOptional)); - } else { - return new FormControl(fieldDefault, field.createValidators(isOptional)); - } - }; - - if (field.isLocalizable) { - for (let language of this.languages.values) { - fieldForm.setControl(language.iso2Code, createControl(language.isOptional)); - } - } else { - fieldForm.setControl(fieldInvariant, createControl(false)); - } - - this.form.setControl(field.name, fieldForm); - } - - this.enableContentForm(); - } - - public removeArrayItem(field: RootFieldDto, language: AppLanguageDto, index: number) { - this.findArrayItemForm(field, language).removeAt(index); - } - - public insertArrayItem(field: RootFieldDto, language: AppLanguageDto) { - if (field.nested.length > 0) { - const formControl = this.findArrayItemForm(field, language); - - this.addArrayItem(field, language, formControl); - } - } - - private addArrayItem(field: RootFieldDto, language: AppLanguageDto | null, formControl: FormArray) { - const formItem = new FormGroup({}); - - let isOptional = field.isLocalizable && language !== null && language.isOptional; - - for (let nested of field.nested) { - const nestedDefault = field.defaultValue(); - - formItem.setControl(nested.name, new FormControl(nestedDefault, nested.createValidators(isOptional))); - } - - formControl.push(formItem); - } - - private findArrayItemForm(field: RootFieldDto, language: AppLanguageDto): FormArray { - const fieldForm = this.form.get(field.name)!; - - if (field.isLocalizable) { - return fieldForm.get(language.iso2Code)!; - } else { - return fieldForm.get(fieldInvariant); - } - } - - public submitCompleted(newValue?: any) { - super.submitCompleted(newValue); - - this.enableContentForm(); - } - - public submitFailed(error?: string | ErrorDto) { - super.submitFailed(error); - - this.enableContentForm(); - } - - public loadData(value: any, isArchive: boolean) { - for (let field of this.schema.fields) { - if (field.properties.fieldType === 'Array' && field.nested.length > 0) { - const fieldValue = value ? value[field.name] || {} : {}; - const fieldForm = this.form.get(field.name)!; - - const addControls = (key: string, language: AppLanguageDto | null) => { - const languageValue = fieldValue[key]; - const languageForm = new FormArray([]); - - if (Types.isArray(languageValue)) { - for (let i = 0; i < languageValue.length; i++) { - this.addArrayItem(field, language, languageForm); - } - } - - fieldForm.setControl(key, languageForm); - }; - - if (field.isLocalizable) { - for (let language of this.languages.values) { - addControls(language.iso2Code, language); - } - } else { - addControls(fieldInvariant, null); - } - } - } - - super.load(value); - - if (isArchive) { - this.form.disable(); - } else { - this.enableContentForm(); - } - } - - private enableContentForm() { - if (this.schema.fields.length === 0) { - this.form.enable(); - } else { - for (const field of this.schema.fields) { - const fieldForm = this.form.controls[field.name]; - - if (field.isDisabled) { - fieldForm.disable(); - } else { - fieldForm.enable(); - } - } - } - } -} - -export class PatchContentForm extends Form { - constructor( - private readonly schema: SchemaDetailsDto, - private readonly language: AppLanguageDto - ) { - super(new FormGroup({})); - - for (let field of this.schema.listFields) { - if (field.properties && field.properties['inlineEditable']) { - this.form.setControl(field.name, new FormControl(undefined, field.createValidators(this.language.isOptional))); - } - } - } - - public submit() { - const result = super.submit(); - - if (result) { - const request = {}; - - for (let field of this.schema.listFields) { - if (field.properties['inlineEditable']) { - const value = result[field.name]; - - if (field.isLocalizable) { - request[field.name] = { [this.language.iso2Code]: value }; - } else { - request[field.name] = { iv: value }; - } - } - } - - return request; - } - - return result; - } -} - interface Snapshot { contents: ImmutableArray; contentsPager: Pager; diff --git a/src/Squidex/app/shared/state/contributors.forms.ts b/src/Squidex/app/shared/state/contributors.forms.ts new file mode 100644 index 000000000..c70f510b0 --- /dev/null +++ b/src/Squidex/app/shared/state/contributors.forms.ts @@ -0,0 +1,25 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. + */ + +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; + +import { Form } from '@app/framework'; + +export class AssignContributorForm extends Form { + public hasNoUser = + this.form.controls['user'].valueChanges.startWith(null).map(x => !x); + + constructor(formBuilder: FormBuilder) { + super(formBuilder.group({ + user: [null, + [ + Validators.required + ] + ] + })); + } +} \ No newline at end of file diff --git a/src/Squidex/app/shared/state/contributors.state.ts b/src/Squidex/app/shared/state/contributors.state.ts index 71986f287..e19b01a6d 100644 --- a/src/Squidex/app/shared/state/contributors.state.ts +++ b/src/Squidex/app/shared/state/contributors.state.ts @@ -6,14 +6,12 @@ */ import { Injectable } from '@angular/core'; -import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { Observable } from 'rxjs'; import '@app/framework/utils/rxjs-extensions'; import { DialogService, - Form, ImmutableArray, State, Version @@ -23,21 +21,6 @@ import { AppContributorDto, AppContributorsService } from './../services/app-con import { AuthService } from './../services/auth.service'; import { AppsState } from './apps.state'; -export class AssignContributorForm extends Form { - public hasNoUser = - this.form.controls['user'].valueChanges.startWith(null).map(x => !x); - - constructor(formBuilder: FormBuilder) { - super(formBuilder.group({ - user: [null, - [ - Validators.required - ] - ] - })); - } -} - interface SnapshotContributor { contributor: AppContributorDto; diff --git a/src/Squidex/app/shared/state/languages.forms.ts b/src/Squidex/app/shared/state/languages.forms.ts new file mode 100644 index 000000000..bc2e57568 --- /dev/null +++ b/src/Squidex/app/shared/state/languages.forms.ts @@ -0,0 +1,45 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. + */ + +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; + +import { Form } from '@app/framework'; + +export class EditLanguageForm extends Form { + constructor(formBuilder: FormBuilder) { + super(formBuilder.group({ + isMaster: false, + isOptional: false + })); + + this.form.controls['isMaster'].valueChanges + .subscribe(value => { + if (value) { + this.form.controls['isOptional'].setValue(false); + } + }); + + this.form.controls['isOptional'].valueChanges + .subscribe(value => { + if (value) { + this.form.controls['isMaster'].setValue(false); + } + }); + } +} + +export class AddLanguageForm extends Form { + constructor(formBuilder: FormBuilder) { + super(formBuilder.group({ + language: [null, + [ + Validators.required + ] + ] + })); + } +} \ No newline at end of file diff --git a/src/Squidex/app/shared/state/languages.state.ts b/src/Squidex/app/shared/state/languages.state.ts index 64c53da5d..2cbc86c6f 100644 --- a/src/Squidex/app/shared/state/languages.state.ts +++ b/src/Squidex/app/shared/state/languages.state.ts @@ -6,14 +6,12 @@ */ import { Injectable } from '@angular/core'; -import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { Observable } from 'rxjs'; import '@app/framework/utils/rxjs-extensions'; import { DialogService, - Form, ImmutableArray, State, Version @@ -23,42 +21,6 @@ import { AddAppLanguageDto, AppLanguageDto, AppLanguagesService, UpdateAppLangua import { LanguageDto, LanguagesService } from './../services/languages.service'; import { AppsState } from './apps.state'; - -export class EditLanguageForm extends Form { - constructor(formBuilder: FormBuilder) { - super(formBuilder.group({ - isMaster: false, - isOptional: false - })); - - this.form.controls['isMaster'].valueChanges - .subscribe(value => { - if (value) { - this.form.controls['isOptional'].setValue(false); - } - }); - - this.form.controls['isOptional'].valueChanges - .subscribe(value => { - if (value) { - this.form.controls['isMaster'].setValue(false); - } - }); - } -} - -export class AddLanguageForm extends Form { - constructor(formBuilder: FormBuilder) { - super(formBuilder.group({ - language: [null, - [ - Validators.required - ] - ] - })); - } -} - interface SnapshotLanguage { language: AppLanguageDto; diff --git a/src/Squidex/app/shared/state/patterns.forms.ts b/src/Squidex/app/shared/state/patterns.forms.ts new file mode 100644 index 000000000..4267d5da2 --- /dev/null +++ b/src/Squidex/app/shared/state/patterns.forms.ts @@ -0,0 +1,34 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. + */ + +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; + +import { Form, ValidatorsEx } from '@app/framework'; + +export class EditPatternForm extends Form { + constructor(formBuilder: FormBuilder) { + super(formBuilder.group({ + name: ['', + [ + Validators.required, + Validators.maxLength(100), + ValidatorsEx.pattern('[A-z0-9]+[A-z0-9\- ]*[A-z0-9]', 'Name can only contain letters, numbers, dashes and spaces.') + ] + ], + pattern: ['', + [ + Validators.required + ] + ], + message: ['', + [ + Validators.maxLength(1000) + ] + ] + })); + } +} \ No newline at end of file diff --git a/src/Squidex/app/shared/state/patterns.state.ts b/src/Squidex/app/shared/state/patterns.state.ts index 1a1153764..ab86c0b05 100644 --- a/src/Squidex/app/shared/state/patterns.state.ts +++ b/src/Squidex/app/shared/state/patterns.state.ts @@ -6,17 +6,14 @@ */ import { Injectable } from '@angular/core'; -import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { Observable } from 'rxjs'; import '@app/framework/utils/rxjs-extensions'; import { DialogService, - Form, ImmutableArray, State, - ValidatorsEx, Version } from '@app/framework'; @@ -28,30 +25,6 @@ import { EditAppPatternDto } from './../services/app-patterns.service'; -export class EditPatternForm extends Form { - constructor(formBuilder: FormBuilder) { - super(formBuilder.group({ - name: ['', - [ - Validators.required, - Validators.maxLength(100), - ValidatorsEx.pattern('[A-z0-9]+[A-z0-9\- ]*[A-z0-9]', 'Name can only contain letters, numbers, dashes and spaces.') - ] - ], - pattern: ['', - [ - Validators.required - ] - ], - message: ['', - [ - Validators.maxLength(1000) - ] - ] - })); - } -} - interface Snapshot { patterns: ImmutableArray; diff --git a/src/Squidex/app/shared/state/schemas.forms.ts b/src/Squidex/app/shared/state/schemas.forms.ts new file mode 100644 index 000000000..18dc57507 --- /dev/null +++ b/src/Squidex/app/shared/state/schemas.forms.ts @@ -0,0 +1,128 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. + */ + +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; + +import { Form, ValidatorsEx } from '@app/framework'; + +import { createProperties } from './../services/schemas.service'; + +const FALLBACK_NAME = 'my-schema'; + +export class CreateCategoryForm extends Form { + constructor(formBuilder: FormBuilder) { + super(formBuilder.group({ + name: [''] + })); + } +} + +export class CreateSchemaForm extends Form { + public schemaName = + this.form.controls['name'].valueChanges.map(n => n || FALLBACK_NAME) + .startWith(FALLBACK_NAME); + + constructor(formBuilder: FormBuilder) { + super(formBuilder.group({ + name: ['', + [ + Validators.required, + Validators.maxLength(40), + ValidatorsEx.pattern('[a-z0-9]+(\-[a-z0-9]+)*', 'Name can contain lower case letters (a-z), numbers and dashes only (not at the end).') + ] + ], + import: {} + })); + } +} + +export class EditScriptsForm extends Form { + constructor(formBuilder: FormBuilder) { + super(formBuilder.group({ + scriptQuery: '', + scriptCreate: '', + scriptUpdate: '', + scriptDelete: '', + scriptChange: '' + })); + } +} + +export class EditFieldForm extends Form { + constructor(formBuilder: FormBuilder) { + super(formBuilder.group({ + label: ['', + [ + Validators.maxLength(100) + ] + ], + hints: ['', + [ + Validators.maxLength(1000) + ] + ], + placeholder: ['', + [ + Validators.maxLength(1000) + ] + ], + editorUrl: null, + isRequired: false, + isListField: false + })); + } +} + +export class EditSchemaForm extends Form { + constructor(formBuilder: FormBuilder) { + super(formBuilder.group({ + label: ['', + [ + Validators.maxLength(100) + ] + ], + hints: ['', + [ + Validators.maxLength(1000) + ] + ] + })); + } +} + +export class AddFieldForm extends Form { + constructor(formBuilder: FormBuilder) { + super(formBuilder.group({ + type: ['String', + [ + Validators.required + ] + ], + name: ['', + [ + Validators.required, + Validators.maxLength(40), + ValidatorsEx.pattern('[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*', 'Name must be a valid javascript name in camel case.') + ] + ], + isLocalizable: false + })); + } + + public submit() { + const value = super.submit(); + + if (value) { + const properties = createProperties(value.type); + const partitioning = value.isLocalizable ? 'language' : 'invariant'; + + return { name: value.name, partitioning, properties }; + } + + return null; + } +} \ No newline at end of file diff --git a/src/Squidex/app/shared/state/schemas.state.ts b/src/Squidex/app/shared/state/schemas.state.ts index f8f2bc59b..e4b8fe870 100644 --- a/src/Squidex/app/shared/state/schemas.state.ts +++ b/src/Squidex/app/shared/state/schemas.state.ts @@ -6,7 +6,6 @@ */ import { Injectable } from '@angular/core'; -import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { Observable } from 'rxjs'; import '@app/framework/utils/rxjs-extensions'; @@ -14,11 +13,9 @@ import '@app/framework/utils/rxjs-extensions'; import { DateTime, DialogService, - Form, ImmutableArray, State, Types, - ValidatorsEx, Version } from '@app/framework'; @@ -28,7 +25,6 @@ import { AppsState } from './apps.state'; import { AddFieldDto, AnyFieldDto, - createProperties, CreateSchemaDto, FieldDto, FieldPropertiesDto, @@ -44,122 +40,6 @@ import { UpdateSchemaScriptsDto } from './../services/schemas.service'; -const FALLBACK_NAME = 'my-schema'; - -export class CreateCategoryForm extends Form { - constructor(formBuilder: FormBuilder) { - super(formBuilder.group({ - name: [''] - })); - } -} - -export class CreateSchemaForm extends Form { - public schemaName = - this.form.controls['name'].valueChanges.map(n => n || FALLBACK_NAME) - .startWith(FALLBACK_NAME); - - constructor(formBuilder: FormBuilder) { - super(formBuilder.group({ - name: ['', - [ - Validators.required, - Validators.maxLength(40), - ValidatorsEx.pattern('[a-z0-9]+(\-[a-z0-9]+)*', 'Name can contain lower case letters (a-z), numbers and dashes only (not at the end).') - ] - ], - import: {} - })); - } -} - -export class EditScriptsForm extends Form { - constructor(formBuilder: FormBuilder) { - super(formBuilder.group({ - scriptQuery: '', - scriptCreate: '', - scriptUpdate: '', - scriptDelete: '', - scriptChange: '' - })); - } -} - -export class EditFieldForm extends Form { - constructor(formBuilder: FormBuilder) { - super(formBuilder.group({ - label: ['', - [ - Validators.maxLength(100) - ] - ], - hints: ['', - [ - Validators.maxLength(1000) - ] - ], - placeholder: ['', - [ - Validators.maxLength(1000) - ] - ], - editorUrl: null, - isRequired: false, - isListField: false - })); - } -} - -export class EditSchemaForm extends Form { - constructor(formBuilder: FormBuilder) { - super(formBuilder.group({ - label: ['', - [ - Validators.maxLength(100) - ] - ], - hints: ['', - [ - Validators.maxLength(1000) - ] - ] - })); - } -} - -export class AddFieldForm extends Form { - constructor(formBuilder: FormBuilder) { - super(formBuilder.group({ - type: ['String', - [ - Validators.required - ] - ], - name: ['', - [ - Validators.required, - Validators.maxLength(40), - ValidatorsEx.pattern('[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*', 'Name must be a valid javascript name in camel case.') - ] - ], - isLocalizable: false - })); - } - - public submit() { - const value = super.submit(); - - if (value) { - const properties = createProperties(value.type); - const partitioning = value.isLocalizable ? 'language' : 'invariant'; - - return { name: value.name, partitioning, properties }; - } - - return null; - } -} - interface Snapshot { categories: { [name: string]: boolean };