all files / src/ elements-manager.ts

84.69% Statements 83/98
68.18% Branches 15/22
93.33% Functions 14/15
84.04% Lines 79/94
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179                                          11× 10× 10× 10×     10×       10× 14× 14× 14× 14× 14×     10×             10× 14×     14×   14× 14× 61× 61× 61×   61× 61×   61× 22×       14×     10×                 10×                                       14×     14×         13×     10× 14× 64× 64×       10× 14× 14×             10× 14× 14×         10× 63×   63× 63×   63× 63× 63×     63×     10×    
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.fixDisplayWindow();
        this.fillBottom();
        this.updateScopes();
        this.cleanUpBottom();
    }
 
    public AddTop = () => {
        let countTillStop = this.descriptor.Settings.BufferSize;
 
        let i = 0;
        for (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;
 
        let i = 0;
        for (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 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();
    }
 
    private updateScopes = () => {
        for (let i = 0; i < this.items.length; i++) {
            const item = this.items[i];
            item.Scope[this.descriptor.IndexExpression] = this.collection[this.displayFrom + i];
        }
    }
 
    private fixDisplayWindow = () => {
        const endDiff = this.displayTo - this.collection.length;
        Iif (endDiff > 0) {
            this.displayTo -= endDiff;
            const fixedFrom = this.displayFrom - endDiff;
            this.displayFrom = fixedFrom > 0 ? fixedFrom : 0;
        }
    }
 
    private cleanUpBottom = () => {
        let i = this.collection.length;
        while (i < this.items.length) {
            this.removeElement(i);
        }
    }
 
    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();
    }
}