File

src/lib/service/clipboard/clipboard.service.ts

Description

Collection of all Clipboard Services

Index

Properties
Methods

Methods

clear
clear()

Clears the Clipboard by removing all entries from the Clipboard

Returns : void
get
get()

Getter for the Clipboard

Returns : Clipboard

Clipboard

set
set(elements: DmsObject[], action: ClipboardAction, noCache?: boolean)

Setter for the Clipboard

Parameters :
Name Type Optional Description
elements DmsObject[] no

The elements that should be set to the clipboard

action ClipboardAction no

The type of action (COPY, CUT)

noCache boolean yes

Whether or not to bypass cache

Returns : void

Properties

Public clipboard$
clipboard$: Observable<Clipboard>
Type : Observable<Clipboard>
Default value : this.clipboardSource.asObservable()

clipboard.service

import {Injectable} from '@angular/core';
import {ClipboardAction} from './clipboard-action.enum';
import {Clipboard} from './clipboard.interface';
import {DmsObject} from '../../model/dms-object.model';
import {ReplaySubject, Observable} from 'rxjs';
import {EnaioEvent} from '../events/events';
import {EventService} from '../events/event.service';


/**
 * Collection of all Clipboard Services
 */
@Injectable({
  providedIn: 'root'
})
export class ClipboardService {

  private cacheKey = 'eo.clipboard';
  private clipboard: Clipboard = {
    action: null,
    elements: []
  };
  private clipboardSource = new ReplaySubject<Clipboard>(1);
  public clipboard$: Observable<Clipboard> = this.clipboardSource.asObservable();

  /**
   * @ignore
   */
  constructor(private eventService: EventService) {

    eventService.on(EnaioEvent.LOGOUT).subscribe(res => {
      this.clear();
    });

    this.loadCachedClipboard();
    window.addEventListener('storage', evt => {
      if (evt.key === this.cacheKey) {
        this.loadCachedClipboard();
      }
    });
  }

  /**
   * Setter for the Clipboard
   *
   * @param elements The elements that should be set to the clipboard
   * @param action The type of action (COPY, CUT)
   * @param noCache Whether or not to bypass cache
   */
  set(elements: DmsObject[], action: ClipboardAction, noCache?: boolean) {
    this.clipboard = {
      action: action,
      elements: elements
    };
    if (!noCache) {
      this.updateCache();
    }
    this.clipboardSource.next(this.clipboard);
  }

  /**
   * Getter for the Clipboard
   *
   * @returns Clipboard
   */
  get(): Clipboard {
    return this.clipboard;
  }

  /**
   * Clears the Clipboard by removing all entries from the Clipboard
   */
  clear() {
    this.clipboard = {
      action: null,
      elements: []
    };
    this.updateCache();
    this.clipboardSource.next(this.clipboard);
  }

  /**
   * Update LocalStorage with Clipboard
   */
  private updateCache() {
    window.localStorage.setItem(this.cacheKey, JSON.stringify(this.clipboard));
  }

  /**
   * Load Cached Clipbord from LocalStorage
   */
  private loadCachedClipboard() {
    const json = window.localStorage.getItem(this.cacheKey);
    try {
      let clipboard = JSON.parse(json);
      if (clipboard) {
        this.set(clipboard.elements, clipboard.action, true);
      } else {
        this.clear();
      }
    } catch (err) {

    }
  }
}

results matching ""

    No results matching ""