]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/share/accesskey/accesskey.directive.ts
LP#1775466 Angular(6) base application
[Evergreen.git] / Open-ILS / src / eg2 / src / app / share / accesskey / accesskey.directive.ts
1 /**
2  * Assign access keys to <a> tags.
3  *
4  * Access key action is peformed via .click(). hrefs, routerLinks,
5  * and (click) actions are all supported.
6  *
7  *   <a
8  *     routerLink="/staff/splash"
9  *     egAccessKey
10  *     keySpec="alt+h" i18n-keySpec
11  *     keyDesc="My Description" 18n-keyDesc
12  *   >
13  */
14 import {Directive, ElementRef, Input, OnInit} from '@angular/core';
15 import {AccessKeyService} from '@eg/share/accesskey/accesskey.service';
16
17 @Directive({
18   selector: '[egAccessKey]'
19 })
20 export class AccessKeyDirective implements OnInit {
21
22     // Space-separated list of key combinations
23     // E.g. "ctrl+h", "alt+h ctrl+y"
24     @Input() keySpec: string;
25
26     // Description to display in the accesskey info dialog
27     @Input() keyDesc: string;
28
29     // Context info to display in the accesskey info dialog
30     // E.g. "navbar"
31     @Input() keyCtx: string;
32
33     constructor(
34         private elm: ElementRef,
35         private keyService: AccessKeyService
36     ) { }
37
38     ngOnInit() {
39
40         if (!this.keySpec) {
41             console.warn('AccessKey no keySpec provided');
42             return;
43         }
44
45         this.keySpec.split(/ /).forEach(keySpec => {
46             this.keyService.assign({
47                 key: keySpec,
48                 desc: this.keyDesc,
49                 ctx: this.keyCtx,
50                 action: () => this.elm.nativeElement.click()
51             });
52         });
53     }
54 }
55
56