move window.__config to an Angular constant; refactor AngularViewArray -> ViewArray + ViewArrayFactory

This commit is contained in:
alecmerdler 2017-01-21 00:14:37 -08:00
parent 615e233671
commit eea2a18c3f
16 changed files with 316 additions and 231 deletions

View file

@ -0,0 +1,89 @@
import { ViewArray } from './view-array';
export class ViewArrayImpl implements ViewArray {
public entries: any[];
public isVisible: boolean;
public visibleEntries: any[];
public hasEntries: boolean;
public hasHiddenEntries: boolean;
private timerRef: any;
private currentIndex: number;
constructor(private interval: any, private additionalCount: number) {
this.isVisible = false;
this.visibleEntries = null;
this.hasEntries = false;
this.entries = [];
this.hasHiddenEntries = false;
this.timerRef = null;
this.currentIndex = 0;
}
public length(): number {
return this.entries.length;
}
public get(index: number): any {
return this.entries[index];
}
public push(elem: any): void {
this.entries.push(elem);
this.hasEntries = true;
if (this.isVisible) {
this.startTimer();
}
}
public toggle(): void {
this.setVisible(!this.isVisible);
}
public setVisible(newState: boolean): void {
this.isVisible = newState;
this.visibleEntries = [];
this.currentIndex = 0;
if (newState) {
this.showAdditionalEntries();
this.startTimer();
}
else {
this.stopTimer();
}
}
private showAdditionalEntries(): void {
var i: number = 0;
for (i = this.currentIndex; i < (this.currentIndex + this.additionalCount) && i < this.entries.length; ++i) {
this.visibleEntries.push(this.entries[i]);
}
this.currentIndex = i;
this.hasHiddenEntries = this.currentIndex < this.entries.length;
if (this.currentIndex >= this.entries.length) {
this.stopTimer();
}
}
private startTimer(): void {
if (this.timerRef) {
return;
}
this.timerRef = this.interval(() => {
this.showAdditionalEntries();
}, 10);
}
private stopTimer(): void {
if (this.timerRef) {
this.interval.cancel(this.timerRef);
this.timerRef = null;
}
}
}