89 lines
No EOL
1.9 KiB
TypeScript
89 lines
No EOL
1.9 KiB
TypeScript
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;
|
|
}
|
|
}
|
|
} |