Browse Source

All forms updated.

pull/271/head
Sebastian Stehle 8 years ago
parent
commit
6e8bd9551b
  1. 98
      src/Squidex/app/features/schemas/pages/schema/field-wizard.component.html
  2. 123
      src/Squidex/app/features/schemas/pages/schema/field-wizard.component.ts
  3. 8
      src/Squidex/app/features/schemas/pages/schema/field.component.html
  4. 37
      src/Squidex/app/features/schemas/pages/schema/field.component.ts
  5. 22
      src/Squidex/app/features/schemas/pages/schema/forms/field-form-common.component.ts
  6. 8
      src/Squidex/app/features/schemas/pages/schema/schema-edit-form.component.html
  7. 47
      src/Squidex/app/features/schemas/pages/schema/schema-edit-form.component.ts
  8. 5
      src/Squidex/app/features/schemas/pages/schema/schema-scripts-form.component.ts
  9. 70
      src/Squidex/app/features/schemas/state/schemas.state.ts
  10. 10
      src/Squidex/app/framework/state.ts

98
src/Squidex/app/features/schemas/pages/schema/field-wizard.component.html

@ -1,22 +1,13 @@
<div class="modal-dialog modal-lg">
<div class="modal-content" *ngIf="!editForm">
<form [formGroup]="addFieldForm" (ngSubmit)="addField(false, false)">
<div class="modal-header">
<h4 class="modal-title">
<form [formGroup]="addFieldForm.form" (ngSubmit)="addField(false)">
<sqx-modal-dialog (close)="complete()">
<ng-container title>
Create Field
</h4>
</ng-container>
<button type="button" class="close" data-dismiss="modal" aria-label="Close" (click)="complete()">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<h3 class="wizard-title">Create Field</h3>
<div *ngIf="addFieldError">
<div class="form-alert form-alert-error" [innerHTML]="addFieldError"></div>
</div>
<ng-container content>
<ng-container *ngIf="addFieldForm.error | async; let error">
<div class="form-alert form-alert-error" [innerHTML]="error"></div>
</ng-container>
<div class="form-group">
<div class="row">
@ -26,7 +17,7 @@
<div class="row no-gutters">
<div class="col col-auto">
<div class="type-icon" [class.active]="addFieldForm.controls.type.value === fieldType.type">
<div class="type-icon" [class.active]="addFieldForm.form.controls.type.value === fieldType.type">
<i class="icon-type-{{fieldType.type}}"></i>
</div>
</div>
@ -42,7 +33,7 @@
</div>
<div class="form-group">
<sqx-control-errors for="name" submitOnly="true" [submitted]="addFieldFormSubmitted"></sqx-control-errors>
<sqx-control-errors for="name" submitOnly="true" [submitted]="addFieldForm.submitted | async"></sqx-control-errors>
<input type="text" class="form-control" formControlName="name" maxlength="40" #nameInput placeholder="Enter field name" sqxFocusOnInit />
</div>
@ -59,70 +50,15 @@
You can the field as localizable. It means that is dependent on the language, for example a city name.
</small>
</div>
</div>
<div class="modal-footer">
<div class="clearfix">
<button type="reset" class="float-left btn btn-secondary" (click)="complete()">Cancel</button>
<div class="float-right">
<button class="btn btn-success" (click)="addField(false, false)">Create and close</button>
<button class="btn btn-success" (click)="addField(true, false)">Create and new field</button>
<button class="btn btn-success" (click)="addField(false, true)">Create and configure</button>
</div>
</div>
</div>
</form>
</div>
<div class="modal-content" *ngIf="editForm">
<form [formGroup]="editForm">
<div class="modal-header">
<h4 class="modal-title">
Configure Field
</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close" (click)="complete()">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-tabs">
<ul class="nav nav-tabs2">
<li class="nav-item">
<a class="nav-link" [class.active]="editTab === 1" (click)="selectTab(1)">Common</a>
</li>
<li class="nav-item">
<a class="nav-link" [class.active]="editTab === 2" (click)="selectTab(2)">Validation</a>
</li>
<li class="nav-item">
<a class="nav-link" [class.active]="editTab === 3" (click)="selectTab(3)">Editing</a>
</li>
</ul>
</div>
</ng-container>
<div class="modal-body">
<div [class.hidden]="editTab !== 1">
<sqx-field-form-common [editForm]="editForm" [editFormSubmitted]="editFormSubmitted" [showName]="false" [field]="editField"></sqx-field-form-common>
</div>
<div [class.hidden]="editTab !== 2">
<sqx-field-form-validation [editForm]="editForm" [field]="editField"></sqx-field-form-validation>
</div>
<div [class.hidden]="editTab !== 3">
<sqx-field-form-ui [editForm]="editForm" [field]="editField"></sqx-field-form-ui>
</div>
</div>
<div class="modal-footer">
<div class="clearfix">
<ng-container footer>
<button type="reset" class="float-left btn btn-secondary" (click)="complete()">Cancel</button>
<div class="float-right">
<button class="btn btn-success" (click)="configureField(false)">Configure and close</button>
<button class="btn btn-success" (click)="configureField(true)">Configure and new field</button>
</div>
</div>
</div>
</form>
<button class="btn btn-success" (click)="addField(false)">Create and close</button>
<button class="btn btn-success" (click)="addField(true)">Create and new field</button>
</div>
</div>
</ng-container>
</sqx-modal-dialog>
</form>

123
src/Squidex/app/features/schemas/pages/schema/field-wizard.component.ts

@ -6,56 +6,18 @@
*/
import { Component, ElementRef, EventEmitter, Input, Output, ViewChild } from '@angular/core';
import { FormBuilder, Validators, FormGroup } from '@angular/forms';
import { FormBuilder } from '@angular/forms';
import {
AddFieldDto,
createProperties,
fadeAnimation,
FieldDto,
fieldTypes,
SchemaDetailsDto,
UpdateFieldDto,
ValidatorsEx
} from '@app/shared';
import { fieldTypes, SchemaDetailsDto } from '@app/shared';
import { SchemasState } from './../../state/schemas.state';
import { SchemasState, AddFieldForm } from './../../state/schemas.state';
@Component({
selector: 'sqx-field-wizard',
styleUrls: ['./field-wizard.component.scss'],
templateUrl: './field-wizard.component.html',
animations: [
fadeAnimation
]
templateUrl: './field-wizard.component.html'
})
export class FieldWizardComponent {
public fieldTypes = fieldTypes;
public editFormSubmitted = false;
public editTab = 0;
public editForm: FormGroup | null;
public editField: FieldDto | null;
public addFieldError = '';
public addFieldFormSubmitted = false;
public addFieldForm =
this.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
});
@ViewChild('nameInput')
public nameInput: ElementRef;
@ -65,88 +27,37 @@ export class FieldWizardComponent {
@Output()
public completed = new EventEmitter();
constructor(
private readonly formBuilder: FormBuilder,
public fieldTypes = fieldTypes;
public addFieldForm: AddFieldForm;
constructor(formBuilder: FormBuilder,
private readonly schemasState: SchemasState
) {
this.addFieldForm = new AddFieldForm(formBuilder);
}
public complete() {
this.completed.emit();
}
public selectTab(tab: number) {
this.editTab = tab;
}
public addField(next: boolean, configure: boolean) {
this.addFieldFormSubmitted = true;
if (this.addFieldForm.valid) {
this.addFieldForm.disable();
const properties = createProperties(this.addFieldForm.controls['type'].value);
public addField(next: boolean) {
const value = this.addFieldForm.submit();
const partitioning =
this.addFieldForm.controls['isLocalizable'].value ?
'language' :
'invariant';
const requestDto = new AddFieldDto(this.addFieldForm.controls['name'].value, partitioning, properties);
this.schemasState.addField(this.schema, requestDto)
if (value) {
this.schemasState.addField(this.schema, value)
.subscribe(dto => {
this.resetFieldForm();
if (configure) {
this.editField = dto;
this.editTab = 1;
this.editForm = new FormGroup({});
} else if (next) {
this.nameInput.nativeElement.focus();
} else {
this.complete();
}
}, error => {
this.resetFieldForm(error.displayMessage);
});
}
}
public configureField(next: boolean) {
this.editFormSubmitted = true;
if (this.editForm!.valid) {
const properties = createProperties(this.editField!.properties['fieldType'], this.editForm!.value);
this.schemasState.updateField(this.schema, this.editField!, new UpdateFieldDto(properties))
.subscribe(() => {
this.resetEditForm();
this.addFieldForm.submitCompleted({ type: fieldTypes[0].type });
if (next) {
this.editField = null;
this.editForm = null;
this.editTab = 1;
this.nameInput.nativeElement.focus();
} else {
this.complete();
}
}, error => {
this.resetEditForm();
this.addFieldForm.submitFailed(error);
});
}
}
private resetEditForm() {
this.editFormSubmitted = false;
this.editForm!.enable();
this.editForm!.reset(this.editField!.properties);
}
private resetFieldForm(error = '') {
this.addFieldFormSubmitted = false;
this.addFieldError = error;
this.addFieldForm.enable();
this.addFieldForm.reset({ type: 'String' }, { emitEvent: false });
}
}

8
src/Squidex/app/features/schemas/pages/schema/field.component.html

@ -59,7 +59,7 @@
</div>
<div class="table-items-row-details" *ngIf="isEditing">
<form [formGroup]="editForm" (ngSubmit)="save()" [attr.disabled]="field.isLocked">
<form [formGroup]="editForm.form" (ngSubmit)="save()">
<div class="table-items-row-details-tabs clearfix">
<ul class="nav nav-tabs2">
<li class="nav-item">
@ -80,15 +80,15 @@
</div>
<div class="table-items-row-details-tab" [class.hidden]="selectedTab !== 0">
<sqx-field-form-common [editForm]="editForm" [editFormSubmitted]="editFormSubmitted" [field]="field"></sqx-field-form-common>
<sqx-field-form-common [editForm]="editForm.form" [editFormSubmitted]="editForm.submitted | async" [field]="field"></sqx-field-form-common>
</div>
<div class="table-items-row-details-tab" [class.hidden]="selectedTab !== 1">
<sqx-field-form-validation [editForm]="editForm" [field]="field"></sqx-field-form-validation>
<sqx-field-form-validation [editForm]="editForm.form" [field]="field"></sqx-field-form-validation>
</div>
<div class="table-items-row-details-tab" [class.hidden]="selectedTab !== 2">
<sqx-field-form-ui [editForm]="editForm" [field]="field"></sqx-field-form-ui>
<sqx-field-form-ui [editForm]="editForm.form" [field]="field"></sqx-field-form-ui>
</div>
</form>
</div>

37
src/Squidex/app/features/schemas/pages/schema/field.component.ts

@ -6,7 +6,7 @@
*/
import { Component, Input, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormBuilder } from '@angular/forms';
import {
createProperties,
@ -17,7 +17,7 @@ import {
UpdateFieldDto
} from '@app/shared';
import { SchemasState } from './../../state/schemas.state';
import { SchemasState, EditFieldForm } from './../../state/schemas.state';
@Component({
selector: 'sqx-field',
@ -39,16 +39,21 @@ export class FieldComponent implements OnInit {
public isEditing = false;
public selectedTab = 0;
public editFormSubmitted = false;
public editForm = new FormGroup({});
public editForm: EditFieldForm;
constructor(
private readonly formBuilder: FormBuilder,
private readonly schemasState: SchemasState
) {
}
public ngOnInit() {
this.resetEditForm();
this.editForm = new EditFieldForm(this.formBuilder);
this.editForm.load(this.field.properties);
if (this.field.isLocked) {
this.editForm.form.disable();
}
}
public toggleEditing() {
@ -60,7 +65,7 @@ export class FieldComponent implements OnInit {
}
public cancel() {
this.resetEditForm();
this.editForm.load(this.field);
}
public deleteField() {
@ -88,26 +93,18 @@ export class FieldComponent implements OnInit {
}
public save() {
this.editFormSubmitted = true;
const value = this.editForm.submit();
if (this.editForm.valid) {
const properties = createProperties(this.field.properties['fieldType'], this.editForm.value);
if (value) {
const properties = createProperties(this.field.properties['fieldType'], value);
this.schemasState.updateField(this.schema, this.field, new UpdateFieldDto(properties))
.subscribe(() => {
this.resetEditForm();
this.editForm.submitCompleted();
}, error => {
this.editForm.submitFailed(error);
});
}
}
private resetEditForm() {
this.isEditing = false;
this.editFormSubmitted = false;
this.editForm.reset(this.field.properties);
if (this.field.isLocked) {
this.editForm.disable();
}
}
}

22
src/Squidex/app/features/schemas/pages/schema/forms/field-form-common.component.ts

@ -5,8 +5,8 @@
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/
import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FieldDto } from '@app/shared';
@ -16,7 +16,7 @@ import { FieldDto } from '@app/shared';
templateUrl: 'field-form-common.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class FieldFormCommonComponent implements OnInit {
export class FieldFormCommonComponent {
@Input()
public editForm: FormGroup;
@ -28,20 +28,4 @@ export class FieldFormCommonComponent implements OnInit {
@Input()
public field: FieldDto;
public ngOnInit() {
this.editForm.setControl('label',
new FormControl(this.field.properties.label,
Validators.maxLength(100)));
this.editForm.setControl('hints',
new FormControl(this.field.properties.label,
Validators.maxLength(100)));
this.editForm.setControl('isRequired',
new FormControl(this.field.properties.isRequired));
this.editForm.setControl('isListField',
new FormControl(this.field.properties.isListField));
}
}

8
src/Squidex/app/features/schemas/pages/schema/schema-edit-form.component.html

@ -1,4 +1,4 @@
<form [formGroup]="editForm" (ngSubmit)="saveSchema()">
<form [formGroup]="editForm.form" (ngSubmit)="saveSchema()">
<sqx-modal-dialog (close)="complete()">
<ng-container title>
Edit Schema
@ -14,7 +14,7 @@
<div class="form-group">
<label for="schemaLabel">Label</label>
<sqx-control-errors for="label" [submitted]="editFormSubmitted"></sqx-control-errors>
<sqx-control-errors for="label" [submitted]="editForm.submitted | async"></sqx-control-errors>
<input type="text" class="form-control" id="schemaLabel" formControlName="label" />
</div>
@ -22,14 +22,14 @@
<div class="form-group">
<label for="schemaHints">Hints</label>
<sqx-control-errors for="hints" [submitted]="editFormSubmitted"></sqx-control-errors>
<sqx-control-errors for="hints" [submitted]="editForm.submitted | async"></sqx-control-errors>
<textarea type="text" class="form-control" id="schemaHints" formControlName="hints" rows="4"></textarea>
</div>
</ng-container>
<ng-container footer>
<button type="reset" class="float-left btn btn-secondary" (click)="complete()" [disabled]="editFormSubmitted">Cancel</button>
<button type="reset" class="float-left btn btn-secondary" (click)="complete()">Cancel</button>
<button type="submit" class="float-right btn btn-primary">Save</button>
</ng-container>
</sqx-modal-dialog>

47
src/Squidex/app/features/schemas/pages/schema/schema-edit-form.component.ts

@ -6,11 +6,10 @@
*/
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { FormBuilder } from '@angular/forms';
import { SchemaDetailsDto, UpdateSchemaDto } from '@app/shared';
import { SchemasState } from './../../state/schemas.state';
import { SchemaDetailsDto } from '@app/shared';
import { SchemasState, EditSchemaForm } from './../../state/schemas.state';
@Component({
selector: 'sqx-schema-edit-form',
@ -24,29 +23,16 @@ export class SchemaEditFormComponent implements OnInit {
@Input()
public schema: SchemaDetailsDto;
public editFormSubmitted = false;
public editForm =
this.formBuilder.group({
label: ['',
[
Validators.maxLength(100)
]
],
hints: ['',
[
Validators.maxLength(1000)
]
]
});
public editForm: EditSchemaForm;
constructor(
private readonly schemasState: SchemasState,
private readonly formBuilder: FormBuilder
constructor(formBuilder: FormBuilder,
private readonly schemasState: SchemasState
) {
this.editForm = new EditSchemaForm(formBuilder);
}
public ngOnInit() {
this.editForm.patchValue(this.schema.properties);
this.editForm.load(this.schema.properties);
}
public complete() {
@ -54,24 +40,15 @@ export class SchemaEditFormComponent implements OnInit {
}
public saveSchema() {
this.editFormSubmitted = true;
if (this.editForm.valid) {
this.editForm.disable();
const value = this.editForm.submit();
const requestDto = <UpdateSchemaDto>this.editForm.value;
this.schemasState.update(this.schema, requestDto)
if (value) {
this.schemasState.update(this.schema, value)
.subscribe(dto => {
this.complete();
}, error => {
this.enableEditForm();
this.editForm.submitFailed(error);
});
}
}
private enableEditForm() {
this.editForm.enable();
this.editFormSubmitted = false;
}
}

5
src/Squidex/app/features/schemas/pages/schema/schema-scripts-form.component.ts

@ -9,8 +9,7 @@ import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { SchemaDetailsDto } from '@app/shared';
import { EditScriptsForm, SchemasState } from './../../state/schemas.state';
import { SchemasState, EditScriptsForm } from './../../state/schemas.state';
@Component({
selector: 'sqx-schema-scripts-form',
@ -54,7 +53,7 @@ export class SchemaScriptsFormComponent implements OnInit {
.subscribe(dto => {
this.complete();
}, error => {
this.editForm.submitFailed();
this.editForm.submitFailed(error);
});
}
}

70
src/Squidex/app/features/schemas/state/schemas.state.ts

@ -15,6 +15,7 @@ import {
AddFieldDto,
AppsState,
AuthService,
createProperties,
CreateSchemaDto,
DateTime,
DialogService,
@ -63,6 +64,75 @@ export class EditScriptsForm extends Form<FormGroup> {
}
}
export class EditFieldForm extends Form<FormGroup> {
constructor(formBuilder: FormBuilder) {
super(formBuilder.group({
label: ['',
[
Validators.maxLength(100)
]
],
hints: ['',
[
Validators.maxLength(1000)
]
],
isRequired: false,
isListField: false
}));
}
}
export class EditSchemaForm extends Form<FormGroup> {
constructor(formBuilder: FormBuilder) {
super(formBuilder.group({
label: ['',
[
Validators.maxLength(100)
]
],
hints: ['',
[
Validators.maxLength(1000)
]
]
}));
}
}
export class AddFieldForm extends Form<FormGroup> {
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;
}
}
@Injectable()
export class SchemasState {
public schemasItems = new BehaviorSubject<ImmutableArray<SchemaDto>>(ImmutableArray.empty());

10
src/Squidex/app/framework/state.ts

@ -40,18 +40,24 @@ export class Form<T extends AbstractControl> {
this.state.next({ submitted: true });
if (this.form.valid) {
const value = this.form.value;
this.form.disable();
return this.form.value;
return value;
} else {
return null;
}
}
public submitCompleted() {
public submitCompleted(newValue?: any) {
this.state.next({ submitted: false, error: null });
this.form.enable();
if (newValue) {
this.form.reset(newValue);
}
}
public submitFailed(error?: string | ErrorDto) {

Loading…
Cancel
Save