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