File

src/app/modules/core/directives/online-only/online-only.directive.ts

Implements

OnInit OnDestroy

Metadata

Index

Properties
Methods
Inputs
HostListeners

Constructor

constructor(el: ElementRef, connectionService: ConnectionService, renderer: Renderer2, toastService: ToasterService, resourceService: ResourceService)
Parameters :
Name Type Optional
el ElementRef No
connectionService ConnectionService No
renderer Renderer2 No
toastService ToasterService No
resourceService ResourceService No

Inputs

showWarningMessage
Type : boolean
Default value : false

HostListeners

click
Arguments : '$event'

Methods

Private disableElement
disableElement()
Returns : void
Private enableElement
enableElement()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void
onClick
onClick($event)
Decorators :
@HostListener('click', ['$event'])
Parameters :
Name Optional
$event No
Returns : void
Private showAlertMessage
showAlertMessage()
Returns : boolean

Properties

Private isConnected
Default value : true
Public renderer
Type : Renderer2
Public unsubscribe$
Default value : new Subject<void>()
import { Directive, ElementRef, HostListener, Input, OnDestroy, OnInit, Renderer2 } from '@angular/core';
import { ConnectionService, ResourceService, ToasterService } from '@sunbird/shared';
import * as _ from 'lodash-es';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

@Directive({
  selector: '[appOnlineOnly]'
})
export class OnlineOnlyDirective implements OnInit, OnDestroy {
  @Input() showWarningMessage = false;

  public unsubscribe$ = new Subject<void>();
  private isConnected = true;
  constructor(
    private el: ElementRef,
    private connectionService: ConnectionService,
    public renderer: Renderer2,
    private toastService: ToasterService,
    private resourceService: ResourceService
  ) { }

  @HostListener('click', ['$event']) onClick($event) {
    if (!this.isConnected) {
      $event.preventDefault();
      $event.stopPropagation();
      if (this.showWarningMessage) {
        this.showAlertMessage();
        this.disableElement();
      }
    }
  }

  ngOnInit() {
    this.connectionService.monitor()
      .pipe(takeUntil(this.unsubscribe$)).subscribe(isConnected => {
        this.isConnected = isConnected;
        if (!isConnected) {
          this.disableElement();
        } else {
          this.enableElement();
        }
      });
  }

  private enableElement() {
    this.renderer.removeAttribute(this.el.nativeElement, 'disabled');
    this.renderer.removeClass(this.el.nativeElement, 'sb-btn-disabled');
    this.renderer.removeClass(this.el.nativeElement, 'disabled');
    this.renderer.removeClass(this.el.nativeElement, 'pointer-events-disabled');
  }
  private disableElement() {
    this.renderer.setAttribute(this.el.nativeElement, 'disabled', 'true');
    this.renderer.addClass(this.el.nativeElement, 'sb-btn-disabled');
    this.renderer.addClass(this.el.nativeElement, 'disabled');
    this.renderer.addClass(this.el.nativeElement, 'pointer-events-disabled');
  }

  private showAlertMessage() {
    this.toastService.error(_.get(this.resourceService, 'frmelmnts.lbl.offline'));
    return false;
  }

  ngOnDestroy() {
    this.unsubscribe$.next();
    this.unsubscribe$.complete();
  }
}

results matching ""

    No results matching ""