mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
3.4 KiB
118 lines
3.4 KiB
/*
|
|
* Squidex Headless CMS
|
|
*
|
|
* @license
|
|
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
|
|
*/
|
|
|
|
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, forwardRef, OnInit } from '@angular/core';
|
|
import { FormControl, NG_VALUE_ACCESSOR } from '@angular/forms';
|
|
import { StatefulControlComponent, StockPhotoDto, StockPhotoService, thumbnail, Types, value$ } from '@app/shared';
|
|
import { of } from 'rxjs';
|
|
import { debounceTime, map, switchMap, tap } from 'rxjs/operators';
|
|
|
|
export const SQX_STOCK_PHOTO_EDITOR_CONTROL_VALUE_ACCESSOR: any = {
|
|
provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => StockPhotoEditorComponent), multi: true
|
|
};
|
|
|
|
const NO_EMIT = { emitEvent: false };
|
|
|
|
interface State {
|
|
// True when loading assets.
|
|
isLoading?: boolean;
|
|
|
|
// True, when width less than 600 pixels.
|
|
isCompact?: boolean;
|
|
}
|
|
|
|
@Component({
|
|
selector: 'sqx-stock-photo-editor',
|
|
styleUrls: ['./stock-photo-editor.component.scss'],
|
|
templateUrl: './stock-photo-editor.component.html',
|
|
providers: [
|
|
SQX_STOCK_PHOTO_EDITOR_CONTROL_VALUE_ACCESSOR
|
|
],
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
|
})
|
|
export class StockPhotoEditorComponent extends StatefulControlComponent<State, string> implements OnInit {
|
|
public valueControl = new FormControl('');
|
|
|
|
public stockPhotoThumbnail = value$(this.valueControl).pipe(map(v => thumbnail(v, 400) || v));
|
|
public stockPhotoSearch = new FormControl('');
|
|
|
|
public stockPhotos =
|
|
value$(this.stockPhotoSearch).pipe(
|
|
debounceTime(500),
|
|
tap(query => {
|
|
if (query && query.length > 0) {
|
|
this.next({ isLoading: true });
|
|
}
|
|
}),
|
|
switchMap(query => {
|
|
if (query && query.length > 0) {
|
|
return this.stockPhotoService.getImages(query);
|
|
} else {
|
|
return of([]);
|
|
}
|
|
}),
|
|
tap(() => {
|
|
this.next({ isLoading: false });
|
|
}));
|
|
|
|
constructor(changeDetector: ChangeDetectorRef,
|
|
private readonly stockPhotoService: StockPhotoService
|
|
) {
|
|
super(changeDetector, {});
|
|
}
|
|
|
|
public ngOnInit() {
|
|
this.own(
|
|
this.valueControl.valueChanges
|
|
.subscribe(value => {
|
|
this.callChange(value);
|
|
this.callTouched();
|
|
}));
|
|
}
|
|
|
|
public writeValue(obj: string) {
|
|
if (Types.isString(obj)) {
|
|
this.valueControl.setValue(obj);
|
|
} else {
|
|
this.valueControl.setValue('');
|
|
}
|
|
}
|
|
|
|
public setDisabledState(isDisabled: boolean): void {
|
|
super.setDisabledState(isDisabled);
|
|
|
|
if (isDisabled) {
|
|
this.stockPhotoSearch.disable(NO_EMIT);
|
|
} else {
|
|
this.stockPhotoSearch.enable(NO_EMIT);
|
|
}
|
|
}
|
|
|
|
public setCompact(isCompact: boolean) {
|
|
this.next({ isCompact });
|
|
}
|
|
|
|
public selectPhoto(photo: StockPhotoDto) {
|
|
if (!this.snapshot.isDisabled) {
|
|
this.valueControl.setValue(photo.url);
|
|
}
|
|
}
|
|
|
|
public reset() {
|
|
if (!this.snapshot.isDisabled) {
|
|
this.valueControl.setValue('');
|
|
}
|
|
}
|
|
|
|
public isSelected(photo: StockPhotoDto) {
|
|
return photo.url === this.valueControl.value;
|
|
}
|
|
|
|
public trackByPhoto(_index: number, photo: StockPhotoDto) {
|
|
return photo.thumbUrl;
|
|
}
|
|
}
|