import { ViewArray } from './view-array'; export class ViewArrayImpl implements ViewArray { isVisible: boolean; visibleEntries: any[]; hasEntries: boolean; entries: any[]; hasHiddenEntries: boolean; timerRef_: any; 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; } var that = this; this.timerRef_ = this.interval(function() { that.showAdditionalEntries_(); }, 10); } private stopTimer_(): void { if (this.timerRef_) { this.interval.cancel(this.timerRef_); this.timerRef_ = null; } } }