All files Engine.js

92.77% Statements 77/83
67.86% Branches 19/28
100% Functions 28/28
92.68% Lines 76/82
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 180 181 182 183 184 185 186 187 1881x 1x 1x 1x         3x 3x       2x 2x       2x 2x               1x 1x               1x     1x 1x       1x     1x 1x       1x       1x     1x 1x       1x       1x     1x 1x       1x       1x     1x 1x       1x 1x 1x 1x   1x       1x 1x     1x   1x 2x     1x 1x 1x   1x         1x 1x 1x 1x 1x 1x   1x             1x 1x             1x       2x 2x 2x 2x 2x           1x     1x       1x 1x 1x 1x 1x   1x       1x   1x 1x 5x   1x 1x     1x 1x   1x          
export const COLUMNS_MAX_COUNT = 5;
export const COLUMN_MAX_WIDTH = 200;
export const COLUMN_MAX_HEIGHT = 200;
export const GUTTER_IN_PERCENT = 0.5;
 
class Engine {
 
    static getMinHeight(items) {
        return Math.min.apply(null,
            (items.map(item => item.height)));
    }
 
    static getRowWidth(items) {
        return items.map(item => item.width)
            .reduce((a, b) => a + b, 0);
    }
 
    static resizeByHeight(item, newHeight) {
        const aspectRatio = item.width / item.height;
        return {
            ...item,
            width: aspectRatio * newHeight,
            height: newHeight,
        };
    }
 
    static resizeByWidth(item, newWidth) {
        const aspectRatio = item.width / item.height;
        return {
            ...item,
            width: newWidth,
            height: newWidth / aspectRatio,
        };
    }
 
    setImages(images) {
        Iif (!images) {
            images = [];
        }
        this.images = images;
        return this;
    }
 
    setGutterInPercent(gutterInPercent) {
        Iif (!gutterInPercent || gutterInPercent < 0) {
            gutterInPercent = 0;
        }
        this.gutterInPercent = gutterInPercent;
        return this;
    }
 
    getGutterInPercent() {
        return this.gutterInPercent;
    }
 
    setMaxColumnsCount(maxColumnsCount) {
        Iif (!maxColumnsCount || maxColumnsCount < 0) {
            maxColumnsCount = COLUMNS_MAX_COUNT;
        }
        this.maxColumnsCount = maxColumnsCount;
        return this;
    }
 
    getMaxColumnsCount() {
        return this.maxColumnsCount;
    }
 
    setColumnMaxWidth(columnMaxWidth) {
        Iif (!columnMaxWidth || columnMaxWidth < 0) {
            columnMaxWidth = COLUMN_MAX_WIDTH;
        }
        this.columnMaxWidth = columnMaxWidth;
        return this;
    }
 
    getColumnsMaxWidth() {
        return this.columnMaxWidth;
    }
 
    setColumnMaxHeight(columnMaxHeight) {
        Iif (!columnMaxHeight || columnMaxHeight < 0) {
            columnMaxHeight = COLUMN_MAX_HEIGHT;
        }
        this.columnMaxHeight = columnMaxHeight;
        return this;
    }
 
    normalizeByHeight(items) {
        const minHeight = Engine.getMinHeight(items);
        let result = [];
        items.forEach(el => {
            result.push(Engine.resizeByHeight(el, minHeight));
        });
        return result;
    }
 
    normalizeByWidth() {
        const calculateHeight = (item) => {
            const itemAfterResize = Engine.resizeByWidth(
                item, calculateWidth(item)
            );
            return itemAfterResize.height;
        };
        const calculateWidth = (item) => {
            return item.width > this.columnMaxWidth ? this.columnMaxWidth : item.width;
        };
 
        let result = [];
        this.images.forEach(el => {
            result.push({...el, height: calculateHeight(el), width: calculateWidth(el)});
        });
        return result;
 
    }
 
    buildRow(items) {
        let row = [];
        let columnsCount = 0;
        while (items.length > 0 && columnsCount < this.maxColumnsCount) {
            const column = items.shift();
            row.push(column);
            columnsCount++;
        }
        return {
            row,
            isIncomplete: columnsCount < this.maxColumnsCount,
        };
    }
 
    getNormalizedItems(items) {
        items = items.map(item => {
            return {
                ...item,
                height: item.height,
                width: item.width,
                src: item.src,
            };
        });
        return this.normalizeByHeight(items);
    }
 
    calculateHeight(item, row, isLastRow) {
        const rowWidth = Engine.getRowWidth(row);
        const ratio = this.maxColumnsCount * this.columnMaxWidth / rowWidth;
        const newHeight = Engine.getMinHeight(row) * ratio * (100 - (row.length - 1) * this.gutterInPercent) / 100;
        Eif (isLastRow) {
            return newHeight > this.columnMaxHeight ? this.columnMaxHeight : newHeight;
        }
        return newHeight;
    }
 
    calculateWidth(item, row, isLastRow) {
        const itemAfterResize = Engine.resizeByHeight(
            item, this.calculateHeight(item, row, isLastRow)
        );
        return itemAfterResize.width;
    }
 
    buildRows() {
        let rows = [];
        const items = this.getNormalizedItems(this.images);
        while (items.length > 0) {
            const row = this.buildRow(items);
            rows.push(row);
        }
        return rows;
    }
 
    buildColumns() {
        const columns = [];
        let order;
        const items = this.normalizeByWidth();
        for (let i = 0; i < this.maxColumnsCount; i++) {
            columns.push({images: [], order: i});
        }
        for (let i = 0; i < items.length; i++) {
            order = (i + 1) % this.maxColumnsCount === 0
                ? this.maxColumnsCount
                : (i + 1) % this.maxColumnsCount;
            columns[order - 1].images.push(items[i]);
            items[i].order = order;
        }
        return columns;
    }
}
 
export default Engine;