This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/static/js/services/angular-view-array/view-array.impl.ts
2017-01-18 22:10:43 -08:00

90 lines
No EOL
1.9 KiB
TypeScript

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_();
}
}
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;
}
}
}