File

projects/angular-cesium/src/lib/angular-cesium/services/context-menu/context-menu.service.ts

Description

The Service manages a singleton context menu over the map. It should be initialized with MapEventsManager. The Service allows opening and closing of the context menu and passing data to the context menu inner component.

notice, data will be injected to your custom menu component into the data field in the component. Usage :

 *  ngOnInit() {
 *   this.clickEvent$ = this.eventsManager.register({ event: CesiumEvent.RIGHT_CLICK, pick: PickOptions.PICK_ONE });
 *   this.clickEvent$.subscribe(result => {
 *    if (result.entities) {
 *      const pickedMarker = result.entities[0];
 *      this.contextMenuService.open(MapContextmenuComponent, pickedMarker.position, {
 *        data: {
 *          myData: data,
 *          onDelete: () => this.delete(pickedMarker.id)
 *        }
 *      });
 *    }
 *   });
 *  }
 *
 *
 *  private delete(id) {
 *    this.mapMenu.close();
 *    this.detailedSiteService.removeMarker(id);
 *  }
 *

Index

Properties
Methods
Accessors

Methods

close
close()
Returns : void
init
init(mapEventsManager: MapEventsManagerService)
Parameters :
Name Type Optional
mapEventsManager MapEventsManagerService No
Returns : void
open
open(contentComponent: any, position: Cartesian3, options: ContextMenuOptions)
Type parameters :
  • D
Parameters :
Name Type Optional Default value
contentComponent any No
position Cartesian3 No
options ContextMenuOptions<D> No {}
Returns : void

Properties

Private _content
_content: BasicContextMenu
Type : BasicContextMenu
Private _contextMenuChangeNotifier
_contextMenuChangeNotifier:
Default value : new EventEmitter()
Private _defaultContextMenuOptions
_defaultContextMenuOptions: ContextMenuOptions
Type : ContextMenuOptions
Default value : { closeOnLeftCLick: true, closeOnLeftClickPriority: 10, }
Private _onClose
_onClose:
Default value : new EventEmitter()
Private _onOpen
_onOpen:
Default value : new EventEmitter()
Private _options
_options: ContextMenuOptions
Type : ContextMenuOptions
Private _position
_position: Cartesian3
Type : Cartesian3
Private _showContextMenu
_showContextMenu:
Default value : false
Private leftClickRegistration
leftClickRegistration: DisposableObservable<any>
Type : DisposableObservable<any>
Private leftClickSubscription
leftClickSubscription: Subscription
Type : Subscription
Private mapEventsManager
mapEventsManager: MapEventsManagerService
Type : MapEventsManagerService

Accessors

contextMenuChangeNotifier
getcontextMenuChangeNotifier()
showContextMenu
getshowContextMenu()
options
getoptions()
position
getposition()
content
getcontent()
onOpen
getonOpen()
onClose
getonClose()
import { EventEmitter, Injectable } from '@angular/core';
import { Cartesian3 } from '../../models/cartesian3';
import { ContextMenuOptions } from '../../models/context-menu-options';
import { MapEventsManagerService } from '../map-events-mananger/map-events-manager';
import { CesiumEvent } from '../map-events-mananger/consts/cesium-event.enum';
import { PickOptions } from '../map-events-mananger/consts/pickOptions.enum';
import { DisposableObservable } from '../map-events-mananger/disposable-observable';
import { BasicContextMenu } from '../../models/basic-context-menu';
import { Subscription } from 'rxjs';


/**
 * The Service manages a singleton context menu over the map. It should be initialized with MapEventsManager.
 * The Service allows opening and closing of the context menu and passing data to the context menu inner component.
 *
 * notice, `data` will be injected to your custom menu component into the `data` field in the component.
 * __Usage :__
 * ```
 *  ngOnInit() {
 *   this.clickEvent$ = this.eventsManager.register({ event: CesiumEvent.RIGHT_CLICK, pick: PickOptions.PICK_ONE });
 *   this.clickEvent$.subscribe(result => {
 *    if (result.entities) {
 *      const pickedMarker = result.entities[0];
 *      this.contextMenuService.open(MapContextmenuComponent, pickedMarker.position, {
 *        data: {
 *          myData: data,
 *          onDelete: () => this.delete(pickedMarker.id)
 *        }
 *      });
 *    }
 *   });
 *  }
 *
 *
 *  private delete(id) {
 *    this.mapMenu.close();
 *    this.detailedSiteService.removeMarker(id);
 *  }
 * ```
 */
@Injectable()
export class ContextMenuService {
  private _showContextMenu = false;
  private _options: ContextMenuOptions;
  private _position: Cartesian3;
  private _content: BasicContextMenu;
  private mapEventsManager: MapEventsManagerService;
  private leftClickRegistration: DisposableObservable<any>;
  private leftClickSubscription: Subscription;
  private _contextMenuChangeNotifier = new EventEmitter();
  private _onOpen = new EventEmitter();
  private _onClose = new EventEmitter();
  private _defaultContextMenuOptions: ContextMenuOptions = {
    closeOnLeftCLick: true,
    closeOnLeftClickPriority: 10,
  };

  get contextMenuChangeNotifier(): EventEmitter<any> {
    return this._contextMenuChangeNotifier;
  }

  get showContextMenu(): boolean {
    return this._showContextMenu;
  }

  get options(): ContextMenuOptions {
    return this._options;
  }

  get position(): Cartesian3 {
    return this._position;
  }

  get content(): BasicContextMenu {
    return this._content;
  }

  get onOpen(): EventEmitter<any> {
    return this._onOpen;
  }

  get onClose(): EventEmitter<any> {
    return this._onClose;
  }


  init(mapEventsManager: MapEventsManagerService) {
    this.mapEventsManager = mapEventsManager;
  }

  open<D>(contentComponent: any, position: Cartesian3, options: ContextMenuOptions<D> = {}) {
    this.close();
    this._content = contentComponent;
    this._position = position;
    this._options = Object.assign({}, this._defaultContextMenuOptions, options);
    this._showContextMenu = true;
    if (this.mapEventsManager && this._options.closeOnLeftCLick) {
      this.leftClickRegistration = this.mapEventsManager.register({
        event: CesiumEvent.LEFT_CLICK,
        pick: PickOptions.NO_PICK,
        priority: this._options.closeOnLeftClickPriority,
      });
      this.leftClickSubscription = this.leftClickRegistration.subscribe(() => {
        this.leftClickSubscription.unsubscribe();
        this.close();
      });
    }

    this._contextMenuChangeNotifier.emit();
    this._onOpen.emit();
  }

  close() {
    this._content = undefined;
    this._position = undefined;
    this._options = undefined;
    this._showContextMenu = false;
    if (this.leftClickRegistration) {
      this.leftClickRegistration.dispose();
      this.leftClickRegistration = undefined;
    }
    if (this.leftClickSubscription) {
      this.leftClickSubscription.unsubscribe();
      this.leftClickSubscription = undefined;
    }

    this._contextMenuChangeNotifier.emit();
    this._onClose.emit();
  }
}

result-matching ""

    No results matching ""