File

packages/ui/src/lib/utility/keycommands/keycommands.service.ts

Index

Properties
Methods

Constructor

constructor(router: Router, notificationsService: NotificationsService)
Parameters :
Name Type Optional
router Router No
notificationsService NotificationsService No

Methods

activate
activate(keyconfig: KeyAction, e?)
Parameters :
Name Type Optional
keyconfig KeyAction No
e Yes
Returns : void
copyCellToClipboard
copyCellToClipboard(label: string)
Parameters :
Name Type Optional Default value
label string No 'Cell'
Returns : (item: any, property: any) => void
copyToClipBoard
copyToClipBoard(value, label)
Parameters :
Name Optional
value No
label No
Returns : void
mute
mute(e?)
Parameters :
Name Optional
e Yes
Returns : void
navigate
navigate(path: string[], options: object)
Parameters :
Name Type Optional Default value
path string[] No
options object No {}
Returns : void
openUrl
openUrl(url)
Parameters :
Name Optional
url No
Returns : void
register
register(keyconfig: KeyAction)
Parameters :
Name Type Optional
keyconfig KeyAction No
Returns : this
unmute
unmute(e?)
Parameters :
Name Optional
e Yes
Returns : void

Properties

input
keys
Type : literal type
Default value : {}
meta
Default value : false
muted
Default value : false
Public notificationsService
Type : NotificationsService
Public router
Type : Router
shift
Default value : false
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { NotificationsService } from '../../notifications/notifications.service';

export interface KeyAction {
  key: string;
  description: string;
  action: (e?) => void;
  canActivate?: (e?) => boolean;
}

@Injectable()
export class KeycommandsService {
  keys: { [key: string]: KeyAction } = {};
  muted = false;
  input;
  meta = false;
  shift = false;

  constructor(public router: Router, public notificationsService: NotificationsService) {
    window.addEventListener('keydown', (e) => {
      if (e.altKey && this.keys[e.key]) {
        e.preventDefault();
        e.stopImmediatePropagation();
        e.stopPropagation();
      }
    });
    window.addEventListener('keyup', (e) => {
      if ((!this.muted || e.altKey) && this.keys[e.key]) {
        this.activate(this.keys[e.key], e);
      }
    });
    window.addEventListener('mousedown', (e) => {
      this.meta = e.metaKey;
    });
    // prevents shortcuts to be fired when inputs are focused
    document.addEventListener(
      'focus',
      (e) => {
        this.mute(e);
      },
      true,
    );

    document.addEventListener(
      'blur',
      (e) => {
        this.unmute(e);
      },
      true,
    );
  }

  mute(e?) {
    this.input = e.target;
    this.muted = true;
  }

  unmute(e?) {
    delete this.input;
    this.muted = false;
  }

  register(keyconfig: KeyAction) {
    this.keys[keyconfig.key] = keyconfig;
    return this;
  }

  activate(keyconfig: KeyAction, e?) {
    if (!keyconfig.canActivate(e)) {
      return;
    }
    keyconfig.action(e);
  }

  copyToClipBoard(value, label) {
    const el = document.createElement('textarea');
    el.value = value;
    document.body.appendChild(el);
    el.select();
    document.execCommand('copy');
    document.body.removeChild(el);

    this.notificationsService.emit({
      title: `${label} has been copied to the clipboard`,
      message: value,
      type: 'info',
    });
  }

  copyCellToClipboard(label = 'Cell') {
    return (item, property) => {
      this.copyToClipBoard(JSON.stringify(item.resolve(property) || ''), label);
    };
  }

  openUrl(url) {
    window.open(url, '_blank');
  }

  navigate(path: string[], options = {}) {
    if (this.meta) {
      this.openUrl(this.router.serializeUrl(this.router.createUrlTree(path, options)));
    } else {
      this.router.navigate(path, options);
    }
  }
}

result-matching ""

    No results matching ""