move window.__config to an Angular constant; refactor AngularViewArray -> ViewArray + ViewArrayFactory

This commit is contained in:
alecmerdler 2017-01-21 00:14:37 -08:00
parent 615e233671
commit eea2a18c3f
16 changed files with 316 additions and 231 deletions

View file

@ -1,152 +0,0 @@
import { angularViewArrayFactory } from './angular-view-array';
import { ViewArrayImpl } from './view-array.impl';
describe("Service: AngularViewArray", () => {
var angularViewArray: any;
var $interval: ng.IIntervalService;
beforeEach(inject(($injector: ng.auto.IInjectorService) => {
$interval = $injector.get('$interval');
angularViewArray = angularViewArrayFactory($interval);
}));
describe("create", () => {
it("sanity", () => {
expect(angularViewArrayFactory).toBeDefined();
});
it("returns a ViewArray object", () => {
var viewArray: ViewArrayImpl = angularViewArray.create();
expect(viewArray).toBeDefined();
});
describe("returned ViewArray object", () => {
var viewArray: ViewArrayImpl;
beforeEach(() => {
viewArray = angularViewArray.create();
});
describe("constructor", () => {
// TODO
});
describe("length", () => {
it("returns the number of entries", () => {
viewArray.entries = [{}, {}, {}];
expect(viewArray.length()).toEqual(viewArray.entries.length);
});
});
describe("get", () => {
it("returns the entry at a given index", () => {
var index: number = 8;
viewArray.entries = new Array(10);
viewArray.entries[index] = 3;
expect(viewArray.get(index)).toEqual(viewArray.entries[index]);
});
});
describe("push", () => {
it("adds given element to the end of entries", () => {
var element: number = 3;
var originalLength: number = viewArray.length();
viewArray.push(element);
expect(viewArray.entries.length).toEqual(originalLength + 1);
expect(viewArray.get(originalLength)).toEqual(element);
});
it("sets 'hasEntries' to true", () => {
viewArray.push(2);
expect(viewArray.hasEntries).toBe(true);
});
it("starts timer if 'isVisible' is true", () => {
spyOn(viewArray, "startTimer_").and.returnValue(null);
viewArray.isVisible = true;
viewArray.push(2);
expect(viewArray.startTimer_).toHaveBeenCalled();
});
it("does not start timer if 'isVisible' is false", () => {
spyOn(viewArray, "startTimer_").and.returnValue(null);
viewArray.isVisible = false;
viewArray.push(2);
expect(viewArray.startTimer_).not.toHaveBeenCalled();
});
});
describe("toggle", () => {
it("sets 'isVisible' to false if currently true", () => {
viewArray.isVisible = true;
viewArray.toggle();
expect(viewArray.isVisible).toBe(false);
});
it("sets 'isVisible' to true if currently false", () => {
viewArray.isVisible = false;
viewArray.toggle();
expect(viewArray.isVisible).toBe(true);
});
});
describe("setVisible", () => {
it("sets 'isVisible' to false if given false", () => {
viewArray.setVisible(false);
expect(viewArray.isVisible).toBe(false);
});
it("sets 'visibleEntries' to empty array if given false", () => {
viewArray.setVisible(false);
expect(viewArray.visibleEntries.length).toEqual(0);
});
it("shows additional entries if given true", () => {
spyOn(viewArray, "showAdditionalEntries_").and.returnValue(null);
viewArray.setVisible(true);
expect(viewArray.showAdditionalEntries_).toHaveBeenCalled();
});
it("does not show additional entries if given false", () => {
spyOn(viewArray, "showAdditionalEntries_").and.returnValue(null);
viewArray.setVisible(false);
expect(viewArray.showAdditionalEntries_).not.toHaveBeenCalled();
});
it("starts timer if given true", () => {
spyOn(viewArray, "startTimer_").and.returnValue(null);
viewArray.setVisible(true);
expect(viewArray.startTimer_).toHaveBeenCalled();
});
it("stops timer if given false", () => {
spyOn(viewArray, "stopTimer_").and.returnValue(null);
viewArray.setVisible(true);
expect(viewArray.stopTimer_).toHaveBeenCalled();
});
});
});
});
});

View file

@ -1,24 +0,0 @@
import { ViewArray } from './view-array';
import { ViewArrayImpl } from './view-array.impl';
/**
* Specialized wrapper around array which provides a toggle() method for viewing the contents of the
* array in a manner that is asynchronously filled in over a short time period. This prevents long
* pauses in the UI for ngRepeat's when the array is significant in size.
*/
angularViewArrayFactory.$inject = [
'$interval'
];
export function angularViewArrayFactory($interval): any {
const ADDITIONAL_ENTRIES: number = 20;
return {
create: function(): ViewArray {
return new ViewArrayImpl($interval, ADDITIONAL_ENTRIES);
}
};
}

View file

@ -1,12 +0,0 @@
export abstract class ViewArray {
public abstract length(): number;
public abstract get(index: number): any;
public abstract push(elem: any): void;
public abstract toggle(): void;
public abstract setVisible(newState: boolean): void;
}

View file

@ -1,7 +1,7 @@
/**
* Service which provides helper methods for constructing and managing tabular data.
*/
angular.module('quay').factory('TableService', ['AngularViewArray', function(AngularViewArray) {
angular.module('quay').factory('TableService', ['ViewArrayFactory', function(ViewArrayFactory) {
var tableService = {};
tableService.tablePredicateClass = function(name, predicate, reverse) {
@ -31,7 +31,7 @@ angular.module('quay').factory('TableService', ['AngularViewArray', function(Ang
};
tableService.buildOrderedItems = function(items, options, filterFields, numericFields, opt_extrafilter) {
var orderedItems = AngularViewArray.create();
var orderedItems = ViewArrayFactory.create();
items.forEach(function(item) {
var filter = options.filter;

View file

@ -0,0 +1,27 @@
import { ViewArrayFactory } from './view-array.factory';
import { ViewArray } from './view-array';
describe("Factory: ViewArrayFactory", () => {
var viewArrayFactory: ViewArrayFactory;
var $intervalMock: any;
beforeEach(() => {
$intervalMock = jasmine.createSpy('$intervalSpy');
$intervalMock.cancel = jasmine.createSpy('cancelSpy');
viewArrayFactory = new ViewArrayFactory($intervalMock)
});
describe("constructor", () => {
});
describe("create", () => {
it("returns a ViewArray object", () => {
var viewArray: ViewArray = viewArrayFactory.create();
expect(viewArray).toBeDefined();
});
});
});

View file

@ -0,0 +1,29 @@
import { ViewArray } from './view-array';
import { ViewArrayImpl } from './view-array.impl';
/**
* Factory for creating ViewArray instances.
*/
export class ViewArrayFactory {
private ADDITIONAL_ENTRIES: number = 20;
/**
* @param $interval Angular $interval service.
* @return viewArrayFactory A factory for creating ViewArray objects.
*/
static $inject = ['$interval'];
constructor(private $interval: ng.IIntervalService) {
}
/**
* Create a new ViewArray object.
* @return viewArray New ViewArrayImpl instance.
*/
public create(): ViewArray {
return new ViewArrayImpl(this.$interval, this.ADDITIONAL_ENTRIES);
}
}

View file

@ -0,0 +1,140 @@
import { ViewArrayImpl } from './view-array.impl';
describe("ViewArrayImplImpl", () => {
var viewArrayImpl: ViewArrayImpl;
var $intervalMock: any;
var additionalCount: number;
beforeEach(inject(($injector: ng.auto.IInjectorService) => {
$intervalMock = jasmine.createSpy('$intervalSpy');
$intervalMock.and.returnValue({});
$intervalMock.cancel = jasmine.createSpy('cancelSpy');
additionalCount = 20;
viewArrayImpl = new ViewArrayImpl($intervalMock, additionalCount);
}));
describe("constructor", () => {
it("initializes values", () => {
expect(viewArrayImpl.isVisible).toBe(false);
expect(viewArrayImpl.visibleEntries).toBe(null);
expect(viewArrayImpl.entries.length).toEqual(0);
expect(viewArrayImpl.hasEntries).toBe(false);
expect(viewArrayImpl.hasHiddenEntries).toBe(false);
});
});
describe("length", () => {
it("returns the number of entries", () => {
viewArrayImpl.entries = [{}, {}, {}];
expect(viewArrayImpl.length()).toEqual(viewArrayImpl.entries.length);
});
});
describe("get", () => {
it("returns the entry at a given index", () => {
var index: number = 8;
viewArrayImpl.entries = new Array(10);
viewArrayImpl.entries[index] = 3;
expect(viewArrayImpl.get(index)).toEqual(viewArrayImpl.entries[index]);
});
});
describe("push", () => {
it("adds given element to the end of entries", () => {
var element: number = 3;
var originalLength: number = viewArrayImpl.length();
viewArrayImpl.push(element);
expect(viewArrayImpl.entries.length).toEqual(originalLength + 1);
expect(viewArrayImpl.get(originalLength)).toEqual(element);
});
it("sets 'hasEntries' to true", () => {
viewArrayImpl.push(2);
expect(viewArrayImpl.hasEntries).toBe(true);
});
it("starts timer if 'isVisible' is true", () => {
viewArrayImpl.isVisible = true;
viewArrayImpl.push(2);
expect($intervalMock).toHaveBeenCalled();
});
it("does not start timer if 'isVisible' is false", () => {
viewArrayImpl.isVisible = false;
viewArrayImpl.push(2);
expect($intervalMock).not.toHaveBeenCalled();
});
});
describe("toggle", () => {
it("sets 'isVisible' to false if currently true", () => {
viewArrayImpl.isVisible = true;
viewArrayImpl.toggle();
expect(viewArrayImpl.isVisible).toBe(false);
});
it("sets 'isVisible' to true if currently false", () => {
viewArrayImpl.isVisible = false;
viewArrayImpl.toggle();
expect(viewArrayImpl.isVisible).toBe(true);
});
});
describe("setVisible", () => {
it("sets 'isVisible' to false if given false", () => {
viewArrayImpl.setVisible(false);
expect(viewArrayImpl.isVisible).toBe(false);
});
it("sets 'visibleEntries' to empty array if given false", () => {
viewArrayImpl.setVisible(false);
expect(viewArrayImpl.visibleEntries.length).toEqual(0);
});
it("shows additional entries if given true", () => {
viewArrayImpl.setVisible(true);
});
it("does not show additional entries if given false", () => {
viewArrayImpl.setVisible(false);
});
it("starts timer if given true", () => {
viewArrayImpl.setVisible(true);
expect($intervalMock).toHaveBeenCalled();
});
it("does not stop timer if given false and timer is not active", () => {
viewArrayImpl.setVisible(false);
expect($intervalMock.cancel).not.toHaveBeenCalled();
});
it("stops timer if given false and timer is active", () => {
viewArrayImpl.isVisible = true;
viewArrayImpl.push(2);
viewArrayImpl.setVisible(false);
expect($intervalMock.cancel).toHaveBeenCalled();
});
});
});

View file

@ -3,13 +3,13 @@ import { ViewArray } from './view-array';
export class ViewArrayImpl implements ViewArray {
public entries: any[];
public isVisible: boolean;
public visibleEntries: any[];
public hasEntries: boolean;
public entries: any[];
public hasHiddenEntries: boolean;
public timerRef_: any;
public currentIndex_: number;
private timerRef: any;
private currentIndex: number;
constructor(private interval: any, private additionalCount: number) {
this.isVisible = false;
@ -17,8 +17,8 @@ export class ViewArrayImpl implements ViewArray {
this.hasEntries = false;
this.entries = [];
this.hasHiddenEntries = false;
this.timerRef_ = null;
this.currentIndex_ = 0;
this.timerRef = null;
this.currentIndex = 0;
}
public length(): number {
@ -34,7 +34,7 @@ export class ViewArrayImpl implements ViewArray {
this.hasEntries = true;
if (this.isVisible) {
this.startTimer_();
this.startTimer();
}
}
@ -46,45 +46,44 @@ export class ViewArrayImpl implements ViewArray {
this.isVisible = newState;
this.visibleEntries = [];
this.currentIndex_ = 0;
this.currentIndex = 0;
if (newState) {
this.showAdditionalEntries_();
this.startTimer_();
this.showAdditionalEntries();
this.startTimer();
}
else {
this.stopTimer_();
this.stopTimer();
}
}
public showAdditionalEntries_(): void {
private showAdditionalEntries(): void {
var i: number = 0;
for (i = this.currentIndex_; i < (this.currentIndex_ + this.additionalCount) && i < this.entries.length; ++i) {
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_();
this.currentIndex = i;
this.hasHiddenEntries = this.currentIndex < this.entries.length;
if (this.currentIndex >= this.entries.length) {
this.stopTimer();
}
}
public startTimer_(): void {
if (this.timerRef_) {
private startTimer(): void {
if (this.timerRef) {
return;
}
var that = this;
this.timerRef_ = this.interval(function() {
that.showAdditionalEntries_();
this.timerRef = this.interval(() => {
this.showAdditionalEntries();
}, 10);
}
public stopTimer_(): void {
if (this.timerRef_) {
this.interval.cancel(this.timerRef_);
this.timerRef_ = null;
private stopTimer(): void {
if (this.timerRef) {
this.interval.cancel(this.timerRef);
this.timerRef = null;
}
}
}

View file

@ -0,0 +1,62 @@
/**
* Specialized wrapper around array which provides a toggle() method for viewing the contents of the
* array in a manner that is asynchronously filled in over a short time period. This prevents long
* pauses in the UI for ngRepeat's when the array is significant in size.
*/
export abstract class ViewArray {
/**
* The stored entries.
*/
public abstract entries: any;
/**
* If the entries are displayed.
*/
public abstract isVisible: boolean;
/**
* The displayed entries.
*/
public abstract visibleEntries: any[];
/**
* If there are stored entries.
*/
public abstract hasEntries: boolean;
/**
* If there are entries not visible.
*/
public abstract hasHiddenEntries: boolean;
/**
* Get the number of entries stored.
* @return number The number of entries.
*/
public abstract length(): number;
/**
* Get a specific entry.
* @param index The index of the entry.
* @return element The element at the given index.
*/
public abstract get(index: number): any;
/**
* Add a new element.
* @param elem The new element.
*/
public abstract push(elem: any): void;
/**
* Toggle whether the elements are visible.
*/
public abstract toggle(): void;
/**
* Set whether the elements are visible.
* @param newState True/False if the contents are visible.
*/
public abstract setVisible(newState: boolean): void;
}