Skip to content

Commit

Permalink
Persist collapsed for array editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Oct 13, 2021
1 parent 3e56fce commit 4dc0783
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ export class ContentSectionComponent extends StatefulComponent<State> implements

this.changes.subscribe(state => {
if (this.formSection?.separator && this.schema) {
this.localStore.setBoolean(this.configKey(), state.isCollapsed);
this.localStore.setBoolean(this.expandedKey(), state.isCollapsed);
}
});
}

public ngOnChanges() {
if (this.formSection?.separator && this.schema) {
const isCollapsed = this.localStore.getBoolean(this.configKey());
const isCollapsed = this.localStore.getBoolean(this.expandedKey());

this.next({ isCollapsed });
}
Expand All @@ -84,7 +84,7 @@ export class ContentSectionComponent extends StatefulComponent<State> implements
return formState.field.fieldId;
}

private configKey(): string {
private expandedKey(): string {
return Settings.Local.FIELD_COLLAPSED(this.schema?.id, this.formSection?.separator?.fieldId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
[formContext]="formContext"
[formModel]="itemForm"
[index]="i"
[isCollapsedInitial]="isCollapsedInitial"
[isDisabled]="isDisabled | async"
[isFirst]="isFirst"
[isLast]="isLast"
Expand All @@ -35,6 +36,7 @@
[formContext]="formContext"
[formModel]="itemForm"
[index]="i"
[isCollapsedInitial]="isCollapsedInitial"
[isDisabled]="isDisabled | async"
[isFirst]="isFirst"
[isLast]="isLast"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { CdkDragDrop } from '@angular/cdk/drag-drop';
import { ChangeDetectionStrategy, Component, Input, OnChanges, QueryList, SimpleChanges, ViewChildren } from '@angular/core';
import { AppLanguageDto, ComponentsFieldPropertiesDto, disabled$, EditContentForm, fadeAnimation, FieldArrayForm, ModalModel, ObjectForm, SchemaDto, sorted, Types } from '@app/shared';
import { AppLanguageDto, ComponentsFieldPropertiesDto, disabled$, EditContentForm, fadeAnimation, FieldArrayForm, LocalStoreService, ModalModel, ObjectForm, SchemaDto, Settings, sorted, Types } from '@app/shared';
import { combineLatest, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ArrayItemComponent } from './array-item.component';
Expand Down Expand Up @@ -49,13 +49,19 @@ export class ArrayEditorComponent implements OnChanges {
public schemasList: ReadonlyArray<SchemaDto>;

public isDisabled: Observable<boolean>;
public isCollapsedInitial = false;

public isFull: Observable<boolean>;

public get hasField() {
return this.formModel.field['nested']?.length > 0;
}

constructor(
private readonly localStore: LocalStoreService,
) {
}

public ngOnChanges(changes: SimpleChanges) {
if (changes['formModel']) {
const maxItems = this.formModel.field.properties['maxItems'] || Number.MAX_VALUE;
Expand All @@ -74,6 +80,8 @@ export class ArrayEditorComponent implements OnChanges {
]).pipe(map(([disabled, items]) => {
return disabled || items.length >= maxItems;
}));

this.isCollapsedInitial = this.localStore.getBoolean(this.expandedKey());
}
}

Expand Down Expand Up @@ -113,17 +121,25 @@ export class ArrayEditorComponent implements OnChanges {
this.children.forEach(child => {
child.collapse();
});

this.localStore.setBoolean(this.expandedKey(), true);
}

public expandAll() {
this.children.forEach(child => {
child.expand();
});

this.localStore.setBoolean(this.expandedKey(), false);
}

private reset() {
this.children.forEach(child => {
child.reset();
});
}

private expandedKey(): string {
return Settings.Local.FIELD_COLLAPSED(this.form.schema?.id, this.formModel.field?.fieldId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/

import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Input, OnChanges, Output, QueryList, SimpleChanges, ViewChildren } from '@angular/core';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Input, OnChanges, OnInit, Output, QueryList, SimpleChanges, ViewChildren } from '@angular/core';
import { AppLanguageDto, ComponentForm, EditContentForm, FieldDto, FieldFormatter, FieldSection, invalid$, ObjectForm, RootFieldDto, StatefulComponent, Types, value$ } from '@app/shared';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
Expand All @@ -22,7 +22,7 @@ interface State {
templateUrl: './array-item.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ArrayItemComponent extends StatefulComponent<State> implements OnChanges {
export class ArrayItemComponent extends StatefulComponent<State> implements OnChanges, OnInit {
@Output()
public itemRemove = new EventEmitter();

Expand All @@ -44,6 +44,9 @@ export class ArrayItemComponent extends StatefulComponent<State> implements OnCh
@Input()
public canUnset?: boolean | null;

@Input()
public isCollapsedInitial = false;

@Input()
public isFirst?: boolean | null;

Expand Down Expand Up @@ -77,6 +80,10 @@ export class ArrayItemComponent extends StatefulComponent<State> implements OnCh
});
}

public ngOnInit() {
this.next({ isCollapsed: this.isCollapsedInitial });
}

public ngOnChanges(changes: SimpleChanges) {
if (changes['formModel']) {
this.isInvalid = invalid$(this.formModel.form);
Expand Down
7 changes: 5 additions & 2 deletions frontend/app/shared/state/contents.forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ export class EditContentForm extends Form<FormGroup, any> {
return this.valueChange$.value;
}

constructor(languages: ReadonlyArray<AppLanguageDto>, schema: SchemaDto, schemas: { [id: string ]: SchemaDto },
private context: any, debounce = 100,
constructor(
public readonly languages: ReadonlyArray<AppLanguageDto>,
public readonly schema: SchemaDto, schemas: { [id: string ]: SchemaDto },
public context: any,
debounce = 100,
) {
super(new FormGroup({}));

Expand Down

0 comments on commit 4dc0783

Please sign in to comment.