Browse Source

Dropdown improved.

pull/422/head
Sebastian 7 years ago
parent
commit
fe7e55393d
  1. 37
      src/Squidex/app/shared/components/references-dropdown.component.ts

37
src/Squidex/app/shared/components/references-dropdown.component.ts

@ -7,8 +7,6 @@
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, forwardRef, Input, OnInit } from '@angular/core'; import { ChangeDetectionStrategy, ChangeDetectorRef, Component, forwardRef, Input, OnInit } from '@angular/core';
import { FormControl, NG_VALUE_ACCESSOR } from '@angular/forms'; import { FormControl, NG_VALUE_ACCESSOR } from '@angular/forms';
import { throwError } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { import {
AppLanguageDto, AppLanguageDto,
@ -16,8 +14,6 @@ import {
ContentDto, ContentDto,
ContentsService, ContentsService,
getContentValue, getContentValue,
SchemaDetailsDto,
SchemasService,
StatefulControlComponent, StatefulControlComponent,
Types, Types,
UIOptions UIOptions
@ -28,9 +24,7 @@ export const SQX_REFERENCES_DROPDOWN_CONTROL_VALUE_ACCESSOR: any = {
}; };
interface State { interface State {
schema?: SchemaDetailsDto | null; contents: ContentDto[];
contentItems: ContentDto[];
contentNames: ContentName[]; contentNames: ContentName[];
selectedItem?: ContentName; selectedItem?: ContentName;
@ -69,19 +63,17 @@ export class ReferencesDropdownComponent extends StatefulControlComponent<State,
public set language(value: AppLanguageDto) { public set language(value: AppLanguageDto) {
this.languageField = value; this.languageField = value;
this.next(s => ({ ...s, contentNames: this.createContentNames(s.schema, s.contentItems) })); this.next(s => ({ ...s, contentNames: this.createContentNames(s.contents) }));
} }
public selectionControl = new FormControl(''); public selectionControl = new FormControl('');
constructor(changeDetector: ChangeDetectorRef, uiOptions: UIOptions, constructor(changeDetector: ChangeDetectorRef, uiOptions: UIOptions,
private readonly appsState: AppsState, private readonly appsState: AppsState,
private readonly contentsService: ContentsService, private readonly contentsService: ContentsService
private readonly schemasService: SchemasService
) { ) {
super(changeDetector, { super(changeDetector, {
schema: null, contents: [],
contentItems: [],
contentNames: [] contentNames: []
}); });
@ -116,19 +108,12 @@ export class ReferencesDropdownComponent extends StatefulControlComponent<State,
return; return;
} }
this.schemasService.getSchema(this.appsState.appName, this.schemaId).pipe( this.contentsService.getContents(this.appsState.appName, this.schemaId, this.itemCount, 0)
switchMap(schema => { .subscribe(contents => {
if (schema) {
return this.contentsService.getContents(this.appsState.appName, this.schemaId, this.itemCount, 0);
} else {
return throwError('Invalid schema');
}
}, (schema, contents) => ({ schema, contents })))
.subscribe(({ schema, contents }) => {
const contentItems = contents.items; const contentItems = contents.items;
const contentNames = this.createContentNames(schema, contentItems); const contentNames = this.createContentNames(contentItems);
this.next(s => ({ ...s, schema, contentItems, contentNames })); this.next(s => ({ ...s, contents: contentItems, contentNames }));
this.selectContent(); this.selectContent();
}, () => { }, () => {
@ -160,14 +145,14 @@ export class ReferencesDropdownComponent extends StatefulControlComponent<State,
this.selectionControl.setValue(undefined, NO_EMIT); this.selectionControl.setValue(undefined, NO_EMIT);
} }
private createContentNames(schema: SchemaDetailsDto | undefined | null, contents: ContentDto[]): ContentName[] { private createContentNames(contents: ContentDto[]): ContentName[] {
if (contents.length === 0 || !schema) { if (contents.length === 0) {
return []; return [];
} }
const names = contents.map(content => { const names = contents.map(content => {
const name = const name =
schema.referenceFields content.referenceFields
.map(f => getContentValue(content, this.languageField, f, false)) .map(f => getContentValue(content, this.languageField, f, false))
.map(v => v.formatted) .map(v => v.formatted)
.filter(v => !!v) .filter(v => !!v)

Loading…
Cancel
Save