annotate src/app/visualisations/notes/notes.component.ts @ 383:1241ca979fd9

Refactor based on pattern which emerged when implementing multiple components. Still some very obvious dupe regarding the ElementRef stuff, I don't think ViewChild decorated props are inherited.. but I haven't actually verified that.
author Lucas Thompson <dev@lucas.im>
date Wed, 31 May 2017 19:14:46 +0100
parents b81ed55fdee3
children 7119d62121f0
rev   line source
dev@380 1 /**
dev@380 2 * Created by lucast on 31/05/2017.
dev@380 3 */
dev@380 4 import {WavesComponent} from '../waves-base.component';
dev@380 5 import {
dev@380 6 AfterViewInit,
dev@380 7 ChangeDetectionStrategy,
dev@380 8 Component,
dev@380 9 ElementRef,
dev@380 10 Input,
dev@380 11 ViewChild
dev@380 12 } from '@angular/core';
dev@380 13 import {Note} from '../FeatureUtilities';
dev@380 14 import Waves from 'waves-ui-piper';
dev@380 15
dev@380 16 @Component({
dev@380 17 selector: 'ugly-notes',
dev@380 18 templateUrl: '../waves-template.html',
dev@380 19 styleUrls: ['../waves-template.css'],
dev@380 20 changeDetection: ChangeDetectionStrategy.OnPush
dev@380 21 })
dev@383 22 export class NotesComponent extends WavesComponent<Note[]> {
dev@380 23 @ViewChild('track') trackDiv: ElementRef;
dev@380 24
dev@380 25 @Input() set notes(notes: Note[]) {
dev@383 26 this.feature = notes;
dev@380 27 }
dev@380 28
dev@383 29 protected get containerHeight(): number {
dev@383 30 return this.trackDiv.nativeElement.getBoundingClientRect().height;
dev@380 31 }
dev@380 32
dev@383 33 protected get trackContainer(): ElementRef {
dev@383 34 return this.trackDiv;
dev@380 35 }
dev@380 36
dev@383 37 protected get featureLayers(): Layer[] {
dev@383 38 return [
dev@380 39 new Waves.helpers.PianoRollLayer(
dev@383 40 this.feature,
dev@380 41 {
dev@380 42 height: this.height,
dev@380 43 color: this.colour,
dev@383 44 yDomain: findVerticalRange(this.feature)
dev@380 45 }
dev@383 46 )
dev@383 47 ];
dev@380 48 }
dev@380 49 }
dev@380 50
dev@380 51 // TODO there might be scope to create a generic utility function like this
dev@380 52 function findVerticalRange(notes: Note[]): [number, number] {
dev@380 53 let [min, max] = notes.reduce((acc, note) => {
dev@380 54 const [min, max] = acc;
dev@380 55 return [Math.min (min, note.pitch), Math.max (max, note.pitch)];
dev@380 56 }, [Infinity, -Infinity]);
dev@380 57 if (min === Infinity || min < 0 || max < 0) {
dev@380 58 min = 0;
dev@380 59 max = 127;
dev@380 60 }
dev@380 61 // round min and max to octave boundaries (starting at C as in MIDI)
dev@380 62 return [
dev@380 63 12 * Math.floor(min / 12),
dev@380 64 12 * Math.ceil(max / 12)
dev@380 65 ];
dev@380 66 }