File

src/lib/service/upload-registry/upload-registry.service.ts

Description

Service for registration of upload targets. Any app component can register itself to retrieve files dragged to the client. This way also plugins are able to fetch and process uploaded files.

Index

Properties
Methods

Methods

isOverlayActive
isOverlayActive()
Returns : boolean
isSubFolderAllowed
isSubFolderAllowed(subFolder: any, locationTypes: ObjectType[])

Checks if it is allowed to drop Files in SubFolders

Parameters :
Name Type Optional
subFolder any no
locationTypes ObjectType[] no
Returns : boolean

boolean

register
register(uploadTarget: UploadTarget)

Registers a new upload target. If there is an existing target with the same ID it will be updated.

Parameters :
Name Type Optional Description
uploadTarget UploadTarget no

The target to be registered

Returns : void
setOverlayActive
setOverlayActive(oa: boolean)
Parameters :
Name Type Optional
oa boolean no
Returns : void
unregister
unregister(uploadTargetId: string)

Removes an upload target from the list of registered targets.

Parameters :
Name Type Optional Description
uploadTargetId string no

ID of the target to be removed

Returns : void

Properties

Public uploadTargets$
uploadTargets$: Observable<UploadTarget[]>
Type : Observable<UploadTarget[]>
Default value : this.targetsSource.asObservable()

Stream of currently available upload targets

import {Injectable} from '@angular/core';
import {ReplaySubject, Observable} from 'rxjs';
import {UploadTarget} from '../../model/upload-target.model';
import {PrepareService} from '../prepare/prepare.service';
import {ObjectType} from '../../model/object-type.model';

/**
 * Service for registration of upload targets. Any app component can register itself
 * to retrieve files dragged to the client. This way also plugins are able to fetch and
 * process uploaded files.
 */

@Injectable({
  providedIn: 'root'
})
export class UploadRegistryService {

  private _targets: UploadTarget[] = [];
  private targetsSource = new ReplaySubject<UploadTarget[]>(1);
  private overlayActive: boolean;
  /**
   * Stream of currently available upload targets
   */
  public uploadTargets$: Observable<UploadTarget[]> = this.targetsSource.asObservable();

  /**
   * @ignore
   */
  constructor(private prepareService: PrepareService) {
  }

  setOverlayActive(oa: boolean) {
    this.overlayActive = oa;
  }
  isOverlayActive() {
    return this.overlayActive;
  }

  /**
   * Registers a new upload target.
   * If there is an existing target with the same ID it will be updated.
   *
   * @param uploadTarget The target to be registered
   */
  register(uploadTarget: UploadTarget) {

    if (uploadTarget.referenceObject) {
      uploadTarget.description = `${(uploadTarget.type === 'object' && uploadTarget.referenceObject?.type?.label) ? `${uploadTarget.referenceObject.type.label}: ` : ''}${uploadTarget.referenceObject.title}`;
       if (uploadTarget.subFolder) {
        this.prepareService.getPrepareChildTypes(uploadTarget.referenceObject.id).subscribe((res) => { 
          if (this.isSubFolderAllowed(uploadTarget.subFolder.data, res.types)) {
            uploadTarget.description += '; ' + uploadTarget.subFolder.name;
          }
        });
      }
    }

    const target = this._targets.find(t => t.id === uploadTarget.id);
    if (target) {
      Object.assign(target, uploadTarget);
    } else {
      this._targets.push(uploadTarget);
    }
    this.targetsSource.next(this._targets);
  }

  /**
   * Checks if it is allowed to drop Files in SubFolders
   *
   * @param subFolder
   * @param ObjectType[] locationTypes
   * @returns boolean
   */
  isSubFolderAllowed(subFolder: any, locationTypes: ObjectType[]): boolean {
    return !!locationTypes.find(lt => lt.name === subFolder.type);
  }

  /**
   * Removes an upload target from the list of registered targets.
   *
   * @param uploadTargetId ID of the target to be removed
   */
  unregister(uploadTargetId: string) {
    this._targets = this._targets.filter((t) => t.id !== uploadTargetId);
    this.targetsSource.next(this._targets);
  }
}

results matching ""

    No results matching ""