import { Component, ElementRef, EventEmitter, HostListener, Inject, Input, OnChanges, OnInit, Output } from '@angular/core'; import { ContextOption } from './types/ics-context-option'; import { isNullOrUndefined } from 'util'; import { ICS_CONTEXT_OPTIONS } from './types/ics-context-options'; import { ContextMenu } from './types/interfaces'; import { CREATE_ACTION, DOWNLOAD, EMAIL, NOTIFICATION, SHARE } from './factory/options'; import { SharingService } from './sharing/sharing.service'; /** * This component is responsible to show the list * of ContextOption and handling onClick events. * * Consumes the context menu options in nearest module imported with * forRoot method of IcsContextMenuModule. * * * ContextMenu is the instance of parent component. Used to * invoke parent component functions with respect to fnName in * ContextOption. * * @author Harsha * */ @Component({ selector: 'ics-context-menu', templateUrl: './ics-context-menu.component.html', styleUrls: ['./ics-context-menu.component.scss'] }) export class IcsContextMenuComponent implements OnInit { public options: Array; public shareId; public isOpen = false; public positions: any[] = [ { originX: 'start', originY: 'bottom', overlayX: 'end', overlayY: 'top' } ]; processing: boolean = false; @Input('contextOptions') set contextOptions(options: Array) { this.options = options; } @Input() appendContextOptions: Array; @Input() elementId; /** * Open and close the context option based on area of click event. * */ @HostListener('document:click', ['$event']) onClickListener(event) { if (this.elementRef.nativeElement.contains(event.target)) { this.open(); } else { this.close(); } } constructor( @Inject(ICS_CONTEXT_OPTIONS) private globalDefaultOptions: ContextOption[], private contextMenu: ContextMenu, private elementRef: ElementRef, private sharingService: SharingService ) { this.sharingService.processingEmitter.subscribe(status=>{ this.processing = status; }); } ngOnInit() { this.dirtyCheck(); this.appendOptions(); this.shareId = this.elementId; } onOptionClick(option: ContextOption) { if (option.equals(NOTIFICATION)) { this.contextMenu.onContextCreateNotification(); } else if (option.equals(EMAIL)) { this.contextMenu.onContextSendEmail(); } else if (option.equals(CREATE_ACTION)) { this.contextMenu.onContextCreateAction(); } else if (option.equals(DOWNLOAD)) { this.sharingService.downloadElementImage(this.shareId); } else if (option.equals(SHARE)) { this.sharingService.openModalShare(this.shareId); } } private dirtyCheck() { if (isNullOrUndefined(this.options)) { this.options = this.globalDefaultOptions; if (isNullOrUndefined(this.options)) { this.options = []; } } } private appendOptions() { if (this.appendContextOptions) { this.options = [...this.options, ...this.appendContextOptions]; } } public open(): void { if (this.isOpen) { // if already open close it this.close(); } else { this.isOpen = true; } } public close(): void { this.isOpen = false; } }