2017-01-18 23:46:37 +00:00
|
|
|
import { ViewArray } from './view-array';
|
2017-04-05 21:14:08 +00:00
|
|
|
import { Injectable, Inject } from 'ng-metadata/core';
|
2017-01-18 23:46:37 +00:00
|
|
|
|
|
|
|
|
2017-02-17 10:55:52 +00:00
|
|
|
@Injectable(ViewArray.name)
|
2017-01-18 23:46:37 +00:00
|
|
|
export class ViewArrayImpl implements ViewArray {
|
|
|
|
|
2017-01-21 08:14:37 +00:00
|
|
|
public entries: any[];
|
2017-01-19 08:53:38 +00:00
|
|
|
public isVisible: boolean;
|
|
|
|
public visibleEntries: any[];
|
|
|
|
public hasEntries: boolean;
|
|
|
|
public hasHiddenEntries: boolean;
|
2017-01-21 08:14:37 +00:00
|
|
|
private timerRef: any;
|
|
|
|
private currentIndex: number;
|
2017-01-23 09:57:00 +00:00
|
|
|
private additionalCount: number = 20;
|
2017-01-18 23:46:37 +00:00
|
|
|
|
2017-04-05 21:14:08 +00:00
|
|
|
constructor(@Inject('$interval') private $interval: ng.IIntervalService) {
|
2017-01-18 23:46:37 +00:00
|
|
|
this.isVisible = false;
|
|
|
|
this.visibleEntries = null;
|
|
|
|
this.hasEntries = false;
|
|
|
|
this.entries = [];
|
|
|
|
this.hasHiddenEntries = false;
|
2017-01-21 08:14:37 +00:00
|
|
|
this.timerRef = null;
|
|
|
|
this.currentIndex = 0;
|
2017-01-18 23:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2017-01-21 08:14:37 +00:00
|
|
|
this.startTimer();
|
2017-01-18 23:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public toggle(): void {
|
|
|
|
this.setVisible(!this.isVisible);
|
|
|
|
}
|
|
|
|
|
|
|
|
public setVisible(newState: boolean): void {
|
|
|
|
this.isVisible = newState;
|
|
|
|
|
|
|
|
this.visibleEntries = [];
|
2017-01-21 08:14:37 +00:00
|
|
|
this.currentIndex = 0;
|
2017-01-18 23:46:37 +00:00
|
|
|
|
|
|
|
if (newState) {
|
2017-01-21 08:14:37 +00:00
|
|
|
this.showAdditionalEntries();
|
|
|
|
this.startTimer();
|
2017-01-18 23:46:37 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-01-21 08:14:37 +00:00
|
|
|
this.stopTimer();
|
2017-01-18 23:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-23 09:57:00 +00:00
|
|
|
public create(): ViewArrayImpl {
|
2017-04-05 21:14:08 +00:00
|
|
|
return new ViewArrayImpl(this.$interval);
|
2017-01-23 09:57:00 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 08:14:37 +00:00
|
|
|
private showAdditionalEntries(): void {
|
2017-01-18 23:46:37 +00:00
|
|
|
var i: number = 0;
|
2017-01-21 08:14:37 +00:00
|
|
|
for (i = this.currentIndex; i < (this.currentIndex + this.additionalCount) && i < this.entries.length; ++i) {
|
2017-01-18 23:46:37 +00:00
|
|
|
this.visibleEntries.push(this.entries[i]);
|
|
|
|
}
|
|
|
|
|
2017-01-21 08:14:37 +00:00
|
|
|
this.currentIndex = i;
|
|
|
|
this.hasHiddenEntries = this.currentIndex < this.entries.length;
|
|
|
|
if (this.currentIndex >= this.entries.length) {
|
|
|
|
this.stopTimer();
|
2017-01-18 23:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 08:14:37 +00:00
|
|
|
private startTimer(): void {
|
|
|
|
if (this.timerRef) {
|
2017-01-18 23:46:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-05 21:14:08 +00:00
|
|
|
this.timerRef = this.$interval(() => {
|
2017-01-21 08:14:37 +00:00
|
|
|
this.showAdditionalEntries();
|
2017-01-18 23:46:37 +00:00
|
|
|
}, 10);
|
|
|
|
}
|
|
|
|
|
2017-01-21 08:14:37 +00:00
|
|
|
private stopTimer(): void {
|
|
|
|
if (this.timerRef) {
|
2017-04-05 21:14:08 +00:00
|
|
|
this.$interval.cancel(this.timerRef);
|
2017-01-21 08:14:37 +00:00
|
|
|
this.timerRef = null;
|
2017-01-18 23:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-20 06:17:42 +00:00
|
|
|
}
|