all files / src/ elements-manager.ts

86.9% Statements 73/84
77.78% Branches 14/18
92.31% Functions 12/13
86.25% Lines 69/80
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161                                          12× 11× 11× 11×     11×       11× 15× 15× 15×     15×     15×         14×     11×             11× 15×     15×   15× 64× 64× 64×   64× 64×   64× 22×       15×     11×                 11×                                       11× 15× 67× 67×           11× 66×   66× 66×   66× 66× 66×     66×     11×    
import { Descriptor } from "./descriptor";
import { IDOMManager } from "./dom-manager";
 
export type Item = {
    Element: ng.IAugmentedJQuery,
    Scope: ng.IScope
}
 
export interface IElementsManager {
    UpdateCollection(newCollection: any[]): void;
    AddTop(): void;
    AddBottom(): void;
    RemoveTop(): void;
    RemoveBottom(): void;
}
 
export class ElementsManager implements IElementsManager {
    private collection: any[];
    private items: Item[];
    private displayFrom: number;
    private displayTo: number;
 
    constructor(private descriptor: Descriptor, private domManager: IDOMManager, private linker: ng.ITranscludeFunction) {
        this.items = [];
        this.displayFrom = 0;
        this.displayTo = 0;
 
        // this way the memory can only leak until the scroller lives
        this.descriptor.Scope.$on('$destroy', () => {
            this.items = [];
        });
    }
 
    public UpdateCollection = (newCollection: any[]) => {
        this.collection = newCollection;
        this.FillBottom();
        this.updateScopes();
    }
 
    private FillBottom() {
        const parentBottom = this.domManager.GetScrollBottomPosition();
 
        // don't add more if elements are already out of sight
        if (this.items.length > 0) {
            const lastItem = this.items[this.items.length - 1];
            const lastItemBottom = this.domManager.GetElementBottomPosition(lastItem.Element);
 
            if (lastItemBottom > parentBottom) {
                return;
            }
        }
 
        this.AddBottom();
    }
 
    public AddTop = () => {
        let countTillStop = this.descriptor.Settings.BufferSize;
 
        for (var i = this.displayFrom - 1; i >= 0 && countTillStop > 0; i--) {
            const newElement = this.transcludeElement(i);
            this.domManager.PrependElement(newElement.Element);
            this.items.unshift(newElement);
 
            countTillStop--;
        }
 
        this.displayFrom = i + 1;
    };
 
    public AddBottom = () => {
        const parentBottom = this.domManager.GetScrollBottomPosition();
 
        // add this many children below visible area
        let overflowCounter = this.descriptor.Settings.BufferSize;
 
        for (var i = this.displayTo; i < this.collection.length && overflowCounter > 0; i++) {
            const item = this.transcludeElement(i);
            this.domManager.AppendElement(item.Element);
            this.items.push(item);
 
            const blockEl = item.Element;
            const blockBottom = this.domManager.GetElementBottomPosition(blockEl);
 
            if (blockBottom > parentBottom) {
                overflowCounter--;
            }
        }
 
        this.displayTo = i;
    };
 
    public RemoveTop = () => {
        let hasInvisibleChildren = true;
        while (hasInvisibleChildren) {
            if (this.items.length <= this.descriptor.Settings.BufferSize) {
                break;
            }
 
            const el = this.items[this.descriptor.Settings.BufferSize].Element;
            const elementBottom = this.domManager.GetElementBottomPosition(el);
            const scrollTop = this.domManager.GetScrollTopPosition();
 
            if (elementBottom < scrollTop) {
                this.removeElement(0);
                this.displayFrom++;
            } else {
                hasInvisibleChildren = false;
            }
        }
    };
 
    public RemoveBottom = () => {
        if (this.items.length < this.descriptor.Settings.BufferSize) {
            return;
        }
 
        let hasInvisibleChildren = true;
        while (hasInvisibleChildren) {
            const el = this.items[this.items.length - this.descriptor.Settings.BufferSize].Element;
            const elementTop = this.domManager.GetElementTopPosition(el);
            const bottom = this.domManager.GetScrollBottomPosition();
 
            if (elementTop > bottom) {
                this.removeElement(this.items.length - 1);
                this.displayTo--;
            } else {
                hasInvisibleChildren = false;
            }
        }
    };
 
    private updateScopes = () => {
        for (var i = 0; i < this.items.length; i++) {
            const item = this.items[i];
            item.Scope[this.descriptor.IndexExpression] = this.collection[this.displayFrom + i];
        }
 
        // TODO: clean dom if removal took place 
    };
 
    private transcludeElement = (index: number): Item => {
        const item = {} as Item;
 
        const childScope = this.descriptor.Scope.$new();
        childScope[this.descriptor.IndexExpression] = this.collection[index];
 
        this.linker(childScope, (clone: ng.IAugmentedJQuery) => {
            item.Element = clone;
            item.Scope = childScope;
        });
 
        return item;
    }
 
    private removeElement = (index: number) => {
        const item = this.items.splice(index, 1)[0];
        this.domManager.Remove(item.Element);
        item.Scope.$destroy();
    }
}