Skip to content
Snippets Groups Projects
Commit 786cb835 authored by jojohoch's avatar jojohoch
Browse files

[player] Refactor MathKeyboardComponent

- Rename NumbersKeyboardComponent to MathKeyboardComponent
- Decrease the number of input properties
- Split source code into private methods
parent 1730347a
No related branches found
No related tags found
No related merge requests found
...@@ -21,7 +21,7 @@ import { IntersectionDetectionDirective } from './directives/intersection-detect ...@@ -21,7 +21,7 @@ import { IntersectionDetectionDirective } from './directives/intersection-detect
import { KeyboardComponent } from './components/keyboard/keyboard.component'; import { KeyboardComponent } from './components/keyboard/keyboard.component';
import { KeyComponent } from './components/key/key.component'; import { KeyComponent } from './components/key/key.component';
import { FrenchKeyboardComponent } from './components/french-keyboard/french-keyboard.component'; import { FrenchKeyboardComponent } from './components/french-keyboard/french-keyboard.component';
import { NumbersKeyboardComponent } from './components/numbers-keyboard/numbers-keyboard.component'; import { MathKeyboardComponent } from './components/math-keyboard/math-keyboard.component';
@NgModule({ @NgModule({
declarations: [ declarations: [
...@@ -39,7 +39,7 @@ import { NumbersKeyboardComponent } from './components/numbers-keyboard/numbers- ...@@ -39,7 +39,7 @@ import { NumbersKeyboardComponent } from './components/numbers-keyboard/numbers-
KeyboardComponent, KeyboardComponent,
KeyComponent, KeyComponent,
FrenchKeyboardComponent, FrenchKeyboardComponent,
NumbersKeyboardComponent MathKeyboardComponent
], ],
imports: [ imports: [
BrowserModule, BrowserModule,
......
...@@ -6,18 +6,21 @@ ...@@ -6,18 +6,21 @@
<div class="keyboard-inner-container" <div class="keyboard-inner-container"
(mousedown)="onMouseDown($event, true)"> (mousedown)="onMouseDown($event, true)">
<app-french-keyboard *ngIf="preset === 'french'"></app-french-keyboard> <app-french-keyboard *ngIf="preset === 'french'"></app-french-keyboard>
<app-numbers-keyboard *ngIf="preset === 'numbers'" <app-math-keyboard
[inputComponent]="inputComponent" *ngIf="preset === 'numbers'"
[useComparisonOperators]="false" [inputComponent]="inputComponent"
[showNumbersWithOperators]="false"></app-numbers-keyboard> [preset]="preset">
<app-numbers-keyboard *ngIf="preset === 'numbersAndOperators'" </app-math-keyboard>
[inputComponent]="inputComponent" <app-math-keyboard
[useComparisonOperators]="false" *ngIf="preset === 'numbersAndOperators'"
[showNumbersWithOperators]="true"></app-numbers-keyboard> [inputComponent]="inputComponent"
<app-numbers-keyboard *ngIf="preset === 'comparisonOperators'" [preset]="preset">
[inputComponent]="inputComponent" </app-math-keyboard>
[useComparisonOperators]="true" <app-math-keyboard
[showNumbersWithOperators]="false"></app-numbers-keyboard> *ngIf="preset === 'comparisonOperators'"
[inputComponent]="inputComponent"
[preset]="preset">
</app-math-keyboard>
</div> </div>
</div> </div>
<ng-container *ngTemplateOutlet="useComparisonOperators ? comparisonOperatorsTemplate : numbersTemplate"></ng-container> <ng-container *ngTemplateOutlet="preset === 'comparisonOperators' ?
comparisonOperatorsTemplate :
numbersTemplate">
</ng-container>
<ng-template #comparisonOperatorsTemplate> <ng-template #comparisonOperatorsTemplate>
<div *ngFor="let row of comparisonOperators"> <div *ngFor="let row of comparisonOperators">
...@@ -31,7 +34,7 @@ ...@@ -31,7 +34,7 @@
</div> </div>
</ng-template> </ng-template>
<div *ngIf="showNumbersWithOperators" class="grid-layout"> <div *ngIf="preset === 'numbersAndOperators'" class="grid-layout">
<ng-container *ngFor="let row of operators; let rowIndex = index "> <ng-container *ngFor="let row of operators; let rowIndex = index ">
<ng-container *ngFor="let key of row; let columnIndex = index"> <ng-container *ngFor="let key of row; let columnIndex = index">
<app-key *ngIf="key !== '='" <app-key *ngIf="key !== '='"
......
...@@ -2,13 +2,12 @@ import { AfterViewInit, Component, Input } from '@angular/core'; ...@@ -2,13 +2,12 @@ import { AfterViewInit, Component, Input } from '@angular/core';
import { KeyboardService } from '../../services/keyboard.service'; import { KeyboardService } from '../../services/keyboard.service';
@Component({ @Component({
selector: 'app-numbers-keyboard', selector: 'app-math-keyboard',
templateUrl: './numbers-keyboard.component.html', templateUrl: './math-keyboard.component.html',
styleUrls: ['./numbers-keyboard.component.css'] styleUrls: ['./math-keyboard.component.css']
}) })
export class NumbersKeyboardComponent implements AfterViewInit { export class MathKeyboardComponent implements AfterViewInit {
@Input() useComparisonOperators!: boolean; @Input() preset!: 'french' | 'numbers' | 'numbersAndOperators' | 'comparisonOperators' | 'none';
@Input() showNumbersWithOperators!: boolean;
@Input() inputComponent!: HTMLTextAreaElement | HTMLInputElement; @Input() inputComponent!: HTMLTextAreaElement | HTMLInputElement;
allowedKeys!: string[]; allowedKeys!: string[];
...@@ -33,25 +32,19 @@ export class NumbersKeyboardComponent implements AfterViewInit { ...@@ -33,25 +32,19 @@ export class NumbersKeyboardComponent implements AfterViewInit {
} }
ngAfterViewInit(): void { ngAfterViewInit(): void {
if (this.useComparisonOperators) { if (this.preset === 'comparisonOperators') {
this.allowedKeys = this.comparisonOperators this.allowedKeys = this.getAllowedKeys(this.comparisonOperators);
.reduce((accumulator: string[], currentValue: string[]): string[] => accumulator.concat(currentValue));
} else { } else {
this.allowedKeys = this.showNumbersWithOperators ? this.allowedKeys = this.preset === 'numbersAndOperators' ?
this.operators this.getAllowedKeys(this.operators).concat(this.getAllowedKeys(this.numbers)) :
.reduce((accumulator: string[], currentValue: string[]): string[] => accumulator.concat(currentValue)).concat( this.getAllowedKeys(this.numbers);
this.numbers }
.reduce((accumulator: string[], currentValue: string[]): string[] => accumulator.concat(currentValue)) if (this.inputComponent) {
) : this.inputComponent.addEventListener('keydown', this.restrict.bind(this));
this.numbers
.reduce((accumulator: string[], currentValue: string[]): string[] => accumulator.concat(currentValue));
if (this.inputComponent) {
this.inputComponent.addEventListener('keydown', this.restrict.bind(this));
}
} }
} }
restrict(event: Event): void { private restrict(event: Event): void {
const keyboardEvent: KeyboardEvent = event as KeyboardEvent; const keyboardEvent: KeyboardEvent = event as KeyboardEvent;
if (!keyboardEvent || if (!keyboardEvent ||
(!this.allowedKeys.includes(keyboardEvent.key) && keyboardEvent.key !== 'Backspace')) { (!this.allowedKeys.includes(keyboardEvent.key) && keyboardEvent.key !== 'Backspace')) {
...@@ -59,4 +52,7 @@ export class NumbersKeyboardComponent implements AfterViewInit { ...@@ -59,4 +52,7 @@ export class NumbersKeyboardComponent implements AfterViewInit {
event.stopPropagation(); event.stopPropagation();
} }
} }
private getAllowedKeys = (keys: string[][]): string[] => keys
.reduce((accumulator: string[], currentValue: string[]): string[] => accumulator.concat(currentValue));
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment