Browse Source

Fix cloning.

pull/337/head
Sebastian Stehle 7 years ago
parent
commit
05567ee503
  1. 4
      src/Squidex/app/features/content/shared/array-editor.component.html
  2. 4
      src/Squidex/app/features/content/shared/array-editor.component.ts
  3. 19
      src/Squidex/app/shared/state/contents.forms.ts

4
src/Squidex/app/features/content/shared/array-editor.component.html

@ -11,12 +11,12 @@
[language]="language"
[languages]="languages"
(toggle)="hide($event)"
(cloning)="addItem(itemForm.value)"
(cloning)="addItem(itemForm)"
(removing)="removeItem(i)">
</sqx-array-item>
</div>
</div>
<button class="btn btn-success" (click)="addItem(); $event.preventDefault()">
<button class="btn btn-success" (click)="addItem(undefined); $event.preventDefault()">
Add Item
</button>

4
src/Squidex/app/features/content/shared/array-editor.component.ts

@ -6,7 +6,7 @@
*/
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { AbstractControl, FormArray } from '@angular/forms';
import { AbstractControl, FormArray, FormGroup } from '@angular/forms';
import {
AppLanguageDto,
@ -47,7 +47,7 @@ export class ArrayEditorComponent {
this.form.removeArrayItem(this.field, this.language, index);
}
public addItem(value: {}) {
public addItem(value?: FormGroup) {
this.form.insertArrayItem(this.field, this.language, value);
}

19
src/Squidex/app/shared/state/contents.forms.ts

@ -374,24 +374,33 @@ export class EditContentForm extends Form<FormGroup> {
this.findArrayItemForm(field, language).removeAt(index);
}
public insertArrayItem(field: RootFieldDto, language: AppLanguageDto, value?: {}) {
public insertArrayItem(field: RootFieldDto, language: AppLanguageDto, source?: FormGroup) {
if (field.nested.length > 0) {
const formControl = this.findArrayItemForm(field, language);
this.addArrayItem(field, language, formControl, value);
this.addArrayItem(field, language, formControl, source);
}
}
private addArrayItem(field: RootFieldDto, language: AppLanguageDto | null, partitionForm: FormArray, value?: {}) {
private addArrayItem(field: RootFieldDto, language: AppLanguageDto | null, partitionForm: FormArray, source?: FormGroup) {
const itemForm = new FormGroup({});
let isOptional = field.isLocalizable && !!language && language.isOptional;
for (let nested of field.nested) {
const nestedValidators = FieldValidatorsFactory.createValidators(nested, isOptional);
const nestedDefault = value ? value[nested.name] : FieldDefaultValue.get(nested);
itemForm.setControl(nested.name, new FormControl(nestedDefault, nestedValidators));
let value = FieldDefaultValue.get(nested);
if (source) {
const sourceField = source.get(nested.name);
if (sourceField) {
value = sourceField.value;
}
}
itemForm.setControl(nested.name, new FormControl(value, nestedValidators));
}
partitionForm.push(itemForm);

Loading…
Cancel
Save