From 2b84a6db56835290b561e2f2a5a6d6789d547405 Mon Sep 17 00:00:00 2001 From: wittlock <1336118+wittlock@users.noreply.github.com> Date: Mon, 2 Mar 2020 13:21:22 +0100 Subject: [PATCH] Various lint-fixes and code cleanup. --- src/lib/hotkey.model.ts | 29 ++++---- src/lib/hotkey.module.ts | 1 + .../hotkeys-cheatsheet.component.ts | 2 +- src/lib/hotkeys.directive.ts | 18 ++--- src/lib/hotkeys.service.ts | 73 ++++++++++--------- 5 files changed, 65 insertions(+), 58 deletions(-) diff --git a/src/lib/hotkey.model.ts b/src/lib/hotkey.model.ts index 7d38caf..2f35374 100644 --- a/src/lib/hotkey.model.ts +++ b/src/lib/hotkey.model.ts @@ -3,20 +3,21 @@ export interface ExtendedKeyboardEvent extends KeyboardEvent { } export class Hotkey { - _formatted: string[]; + private formattedHotkey: string[]; static symbolize(combo: string): string { - let map: any = { + const map: any = { command: '\u2318', // ⌘ shift: '\u21E7', // ⇧ left: '\u2190', // ← right: '\u2192', // → up: '\u2191', // ↑ down: '\u2193', // ↓ + // tslint:disable-next-line:object-literal-key-quotes 'return': '\u23CE', // ⏎ backspace: '\u232B' // ⌫ }; - let comboSplit: string[] = combo.split('+'); + const comboSplit: string[] = combo.split('+'); for (let i = 0; i < comboSplit.length; i++) { // try to resolve command / ctrl based on OS: @@ -37,30 +38,30 @@ export class Hotkey { /** * Creates a new Hotkey for Mousetrap binding * - * @param {string} combo mousetrap key binding - * @param {string} description description for the help menu - * @param {Function} callback method to call when key is pressed - * @param {string} action the type of event to listen for (for mousetrap) - * @param {array} allowIn an array of tag names to allow this combo in ('INPUT', 'SELECT', and/or 'TEXTAREA') - * @param {boolean} persistent if true, the binding is preserved upon route changes + * @param combo mousetrap key binding + * @param description description for the help menu + * @param callback method to call when key is pressed + * @param action the type of event to listen for (for mousetrap) + * @param allowIn an array of tag names to allow this combo in ('INPUT', 'SELECT', and/or 'TEXTAREA') + * @param persistent if true, the binding is preserved upon route changes */ constructor(public combo: string | string[], public callback: (event: KeyboardEvent, combo: string) => ExtendedKeyboardEvent | boolean, public allowIn?: string[], public description?: string | Function, public action?: string, public persistent?: boolean) { - this.combo = (Array.isArray(combo) ? combo : [combo]); + this.combo = (Array.isArray(combo) ? combo : [combo as string]); this.allowIn = allowIn || []; this.description = description || ''; } get formatted(): string[] { - if (!this._formatted) { + if (!this.formattedHotkey) { - let sequence: string[] = this.combo as Array; + const sequence: string[] = this.combo as Array; for (let i = 0; i < sequence.length; i++) { sequence[i] = Hotkey.symbolize(sequence[i]); } - this._formatted = sequence; + this.formattedHotkey = sequence; } - return this._formatted; + return this.formattedHotkey; } } diff --git a/src/lib/hotkey.module.ts b/src/lib/hotkey.module.ts index c604f08..8b00585 100644 --- a/src/lib/hotkey.module.ts +++ b/src/lib/hotkey.module.ts @@ -11,6 +11,7 @@ import { HotkeysService } from './hotkeys.service'; exports: [HotkeysDirective, HotkeysCheatsheetComponent] }) export class HotkeyModule { + // noinspection JSUnusedGlobalSymbols static forRoot(options: IHotkeyOptions = {}): ModuleWithProviders { return { ngModule : HotkeyModule, diff --git a/src/lib/hotkeys-cheatsheet/hotkeys-cheatsheet.component.ts b/src/lib/hotkeys-cheatsheet/hotkeys-cheatsheet.component.ts index 092663b..33b57cd 100644 --- a/src/lib/hotkeys-cheatsheet/hotkeys-cheatsheet.component.ts +++ b/src/lib/hotkeys-cheatsheet/hotkeys-cheatsheet.component.ts @@ -10,7 +10,7 @@ import { Subscription } from 'rxjs'; }) export class HotkeysCheatsheetComponent implements OnInit, OnDestroy { helpVisible = false; - @Input() title: string = 'Keyboard Shortcuts:'; + @Input() title = 'Keyboard Shortcuts:'; subscription: Subscription; hotkeys: Hotkey[]; diff --git a/src/lib/hotkeys.directive.ts b/src/lib/hotkeys.directive.ts index e6eaeb7..c9b292d 100644 --- a/src/lib/hotkeys.directive.ts +++ b/src/lib/hotkeys.directive.ts @@ -14,18 +14,18 @@ export class HotkeysDirective implements OnInit, OnDestroy { private hotkeysList: Hotkey[] = []; private oldHotkeys: Hotkey[] = []; - constructor(private _hotkeysService: HotkeysService, private _elementRef: ElementRef) { - this.mousetrap = new Mousetrap(this._elementRef.nativeElement); // Bind hotkeys to the current element (and any children) + constructor(private hotkeysService: HotkeysService, private elementRef: ElementRef) { + this.mousetrap = new Mousetrap(this.elementRef.nativeElement); // Bind hotkeys to the current element (and any children) } ngOnInit() { - for (let hotkey of this.hotkeys) { - let combo = Object.keys(hotkey)[0]; - let hotkeyObj: Hotkey = new Hotkey(combo, hotkey[combo]); - let oldHotkey: Hotkey = this._hotkeysService.get(combo); + for (const hotkey of this.hotkeys) { + const combo = Object.keys(hotkey)[0]; + const hotkeyObj: Hotkey = new Hotkey(combo, hotkey[combo]); + const oldHotkey: Hotkey = this.hotkeysService.get(combo) as Hotkey; if (oldHotkey !== null) { // We let the user overwrite callbacks temporarily if you specify it in HTML this.oldHotkeys.push(oldHotkey); - this._hotkeysService.remove(oldHotkey); + this.hotkeysService.remove(oldHotkey); } this.hotkeysList.push(hotkeyObj); this.mousetrap.bind(hotkeyObj.combo, hotkeyObj.callback); @@ -33,10 +33,10 @@ export class HotkeysDirective implements OnInit, OnDestroy { } ngOnDestroy() { - for (let hotkey of this.hotkeysList) { + for (const hotkey of this.hotkeysList) { this.mousetrap.unbind(hotkey.combo); } - this._hotkeysService.add(this.oldHotkeys); + this.hotkeysService.add(this.oldHotkeys); } } diff --git a/src/lib/hotkeys.service.ts b/src/lib/hotkeys.service.ts index bb2c188..7c14edb 100644 --- a/src/lib/hotkeys.service.ts +++ b/src/lib/hotkeys.service.ts @@ -13,9 +13,10 @@ export class HotkeysService { mousetrap: MousetrapInstance; cheatSheetToggle: Subject = new Subject(); - private _preventIn = ['INPUT', 'SELECT', 'TEXTAREA']; + private preventIn = ['INPUT', 'SELECT', 'TEXTAREA']; constructor(@Inject(HotkeyOptions) private options: IHotkeyOptions) { + // noinspection JSUnusedGlobalSymbols,JSUnusedLocalSymbols Mousetrap.prototype.stopCallback = (event: KeyboardEvent, element: HTMLElement, combo: string, callback: Function) => { // if the element has the class "mousetrap" then no need to stop if ((' ' + element.className + ' ').indexOf(' mousetrap ') > -1) { @@ -23,11 +24,11 @@ export class HotkeysService { } return (element.contentEditable && element.contentEditable === 'true'); }; - this.mousetrap = new (Mousetrap)(); + this.mousetrap = new (Mousetrap as any)(); if (!this.options.disableCheatSheet) { this.add(new Hotkey( this.options.cheatSheetHotkey || '?', - function (event: KeyboardEvent) { + function(_: KeyboardEvent) { this.cheatSheetToggle.next(); }.bind(this), [], @@ -38,7 +39,7 @@ export class HotkeysService { if (this.options.cheatSheetCloseEsc) { this.add(new Hotkey( 'esc', - function (event: KeyboardEvent) { + function(_: KeyboardEvent) { this.cheatSheetToggle.next(false); }.bind(this), ['HOTKEYS-CHEATSHEET'], @@ -50,57 +51,58 @@ export class HotkeysService { add(hotkey: Hotkey | Hotkey[], specificEvent?: string): Hotkey | Hotkey[] { if (Array.isArray(hotkey)) { - let temp: Hotkey[] = []; - for (let key of hotkey) { - temp.push(this.add(key, specificEvent)); + const temp: Hotkey[] = []; + for (const key of hotkey) { + temp.push(this.add(key, specificEvent) as Hotkey); } return temp; } this.remove(hotkey); - this.hotkeys.push(hotkey); - this.mousetrap.bind((hotkey).combo, (event: KeyboardEvent, combo: string) => { + this.hotkeys.push(hotkey as Hotkey); + this.mousetrap.bind((hotkey as Hotkey).combo, (event: KeyboardEvent, combo: string) => { let shouldExecute = true; // if the callback is executed directly `hotkey.get('w').callback()` // there will be no event, so just execute the callback. if (event) { - let target: HTMLElement = (event.target || event.srcElement); // srcElement is IE only - let nodeName: string = target.nodeName.toUpperCase(); + const target: HTMLElement = (event.target || event.srcElement) as HTMLElement; // srcElement is IE only + const nodeName: string = target.nodeName.toUpperCase(); // check if the input has a mousetrap class, and skip checking preventIn if so if ((' ' + target.className + ' ').indexOf(' mousetrap ') > -1) { shouldExecute = true; - } else if (this._preventIn.indexOf(nodeName) > -1 && (hotkey).allowIn.map(allow => allow.toUpperCase()).indexOf(nodeName) === -1) { + } else if (this.preventIn.indexOf(nodeName) > -1 && + (hotkey as Hotkey).allowIn.map(allow => allow.toUpperCase()).indexOf(nodeName) === -1) { // don't execute callback if the event was fired from inside an element listed in preventIn but not in allowIn shouldExecute = false; } } if (shouldExecute) { - return (hotkey).callback.apply(this, [event, combo]); + return (hotkey as Hotkey).callback.apply(this, [event, combo]); } }, specificEvent); return hotkey; } remove(hotkey?: Hotkey | Hotkey[]): Hotkey | Hotkey[] { - let temp: Hotkey[] = []; + const temp: Hotkey[] = []; if (!hotkey) { - for (let key of this.hotkeys) { - temp.push(this.remove(key)); + for (const key of this.hotkeys) { + temp.push(this.remove(key) as Hotkey); } return temp; } if (Array.isArray(hotkey)) { - for (let key of hotkey) { - temp.push(this.remove(key)); + for (const key of hotkey) { + temp.push(this.remove(key) as Hotkey); } return temp; } - let index = this.findHotkey(hotkey); + const index = this.findHotkey(hotkey as Hotkey); if (index > -1) { this.hotkeys.splice(index, 1); - this.mousetrap.unbind((hotkey).combo); + this.mousetrap.unbind((hotkey as Hotkey).combo); return hotkey; } return null; @@ -111,48 +113,50 @@ export class HotkeysService { return this.hotkeys; } if (Array.isArray(combo)) { - let temp: Hotkey[] = []; - for (let key of combo) { - temp.push(this.get(key)); + const temp: Hotkey[] = []; + for (const key of combo) { + temp.push(this.get(key) as Hotkey); } return temp; } - for (let i = 0; i < this.hotkeys.length; i++) { - if (this.hotkeys[i].combo.indexOf(combo) > -1) { - return this.hotkeys[i]; + for (const hotkey of this.hotkeys) { + if (hotkey.combo.indexOf(combo as string) > -1) { + return hotkey; } } return null; } + // noinspection JSUnusedGlobalSymbols pause(hotkey?: Hotkey | Hotkey[]): Hotkey | Hotkey[] { if (!hotkey) { return this.pause(this.hotkeys); } if (Array.isArray(hotkey)) { - let temp: Hotkey[] = []; - for (let key of hotkey) { - temp.push(this.pause(key)); + const temp: Hotkey[] = []; + for (const key of hotkey) { + temp.push(this.pause(key) as Hotkey); } return temp; } this.remove(hotkey); - this.pausedHotkeys.push(hotkey); + this.pausedHotkeys.push(hotkey as Hotkey); return hotkey; } + // noinspection JSUnusedGlobalSymbols unpause(hotkey?: Hotkey | Hotkey[]): Hotkey | Hotkey[] { if (!hotkey) { return this.unpause(this.pausedHotkeys); } if (Array.isArray(hotkey)) { - let temp: Hotkey[] = []; - for (let key of hotkey) { - temp.push(this.unpause(key)); + const temp: Hotkey[] = []; + for (const key of hotkey) { + temp.push(this.unpause(key) as Hotkey); } return temp; } - let index: number = this.pausedHotkeys.indexOf(hotkey); + const index: number = this.pausedHotkeys.indexOf(hotkey as Hotkey); if (index > -1) { this.add(hotkey); return this.pausedHotkeys.splice(index, 1); @@ -160,6 +164,7 @@ export class HotkeysService { return null; } + // noinspection JSUnusedGlobalSymbols reset() { this.mousetrap.reset(); }