| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- import { APP_INITIALIZER, ChangeDetectorRef, Component, NgModule, OnInit } from '@angular/core';
- import { FormControl } from '@angular/forms';
- import {
- AssetPickerDialogComponent,
- CustomFieldComponentService,
- CustomFieldConfig,
- CustomFieldControl,
- DataService,
- ModalService,
- NavBuilderService,
- SharedModule,
- } from '@vendure/admin-ui/src';
- import { unique } from '@vendure/common/lib/unique';
- import gql from 'graphql-tag';
- import { Observable, of } from 'rxjs';
- import { startWith, switchMap } from 'rxjs/operators';
- @Component({
- template: `
- <input
- type="range"
- [min]="customFieldConfig.intMin"
- [max]="customFieldConfig.intMax"
- [formControl]="formControl"
- />
- {{ formControl.value }}
- `,
- })
- export class SliderControl implements CustomFieldControl {
- customFieldConfig: CustomFieldConfig;
- formControl: FormControl;
- }
- @Component({
- template: `
- <div class="featured-asset">
- <img
- *ngIf="currentAsset$ | async as asset; else placeholder"
- [src]="asset!.preview + '?preset=thumb'"
- />
- <ng-template #placeholder>
- <div class="placeholder">
- <clr-icon shape="image" size="128"></clr-icon>
- <div>{{ 'catalog.no-featured-asset' | translate }}</div>
- </div>
- </ng-template>
- </div>
- <button class="btn" (click)="selectAssets()">
- <clr-icon shape="attachment"></clr-icon>
- {{ 'catalog.add-asset' | translate }}
- </button>
- `,
- })
- export class AssetPickerControl implements CustomFieldControl, OnInit {
- customFieldConfig: CustomFieldConfig;
- formControl: FormControl;
- currentAsset$: Observable<any | null>;
- constructor(
- private changeDetectorRef: ChangeDetectorRef,
- private modalService: ModalService,
- private dataService: DataService,
- ) {}
- ngOnInit(): void {
- this.currentAsset$ = this.formControl.valueChanges.pipe(
- startWith(this.formControl.value),
- switchMap(assetId => {
- if (!assetId) {
- return of(null);
- }
- return this.dataService
- .query(
- gql`
- query($id: ID!) {
- asset(id: $id) {
- id
- name
- preview
- width
- height
- }
- }
- `,
- { id: assetId },
- )
- .mapStream((data: any) => data.asset);
- }),
- );
- }
- selectAssets() {
- this.modalService
- .fromComponent(AssetPickerDialogComponent, {
- size: 'xl',
- })
- .subscribe((result: any) => {
- if (result && result.length) {
- this.formControl.setValue(result[0].id);
- this.formControl.markAsDirty();
- // this.changeDetectorRef.markForCheck();
- }
- });
- }
- }
- @NgModule({
- imports: [SharedModule],
- declarations: [SliderControl, AssetPickerControl],
- entryComponents: [SliderControl, AssetPickerControl],
- providers: [
- {
- provide: APP_INITIALIZER,
- multi: true,
- useFactory: addNavItems,
- deps: [NavBuilderService],
- },
- {
- provide: APP_INITIALIZER,
- multi: true,
- useFactory: registerCustomFieldComponents,
- deps: [CustomFieldComponentService],
- },
- ],
- })
- export class TestSharedModule {}
- export function registerCustomFieldComponents(customFieldComponentService: CustomFieldComponentService) {
- return () => {
- customFieldComponentService.registerCustomFieldComponent('Product', 'length', SliderControl);
- customFieldComponentService.registerCustomFieldComponent('ProductVariant', 'length', SliderControl);
- customFieldComponentService.registerCustomFieldComponent(
- 'Product',
- 'offerImageId',
- AssetPickerControl,
- );
- };
- }
- export function addNavItems(navBuilder: NavBuilderService) {
- return () => {
- navBuilder.addNavMenuSection(
- {
- id: 'test-plugin',
- label: 'Test Plugin',
- items: [
- {
- id: 'stats',
- label: 'Test',
- routerLink: ['/extensions/test'],
- icon: 'line-chart',
- },
- ],
- },
- 'settings',
- );
- };
- }
|