all files / common/models/colors/ colors.js

75% Statements 159/212
63.46% Branches 66/104
78.57% Functions 22/28
75% Lines 159/212
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307                          40× 40× 400× 286× 114×   40×   181× 145× 36× 31×   31×   31×   44× 38× 29×   29×   29× 155× 155× 155× 16× 139×   12×   40× 22× 18×     65× 65×   65× 65× 65× 65×     82×     15× 15×     13× 13×   15×             50×       50× 50× 45× 45× 45× 450× 328× 122× 122×       122×     45× 45×   50×   11×             47×     47× 40× 47× 16× 47× 47×       72×                                                                                     13× 13×   13× 76× 34× 42×                                                                                            
"use strict";
var immutable_class_1 = require('immutable-class');
var plywood_1 = require('plywood');
var general_1 = require('../../../common/utils/general/general');
var NULL_COLOR = '#666666';
//const OTHERS_COLOR = '#AAAAAA';
var NORMAL_COLORS = [
    '#2D95CA',
    '#EFB925',
    '#DA4E99',
    '#4CC873',
    '#745CBD',
    '#EA7136',
    '#E68EE0',
    '#218C35',
    '#B0B510',
    '#904064'
];
function valuesToJS(values) {
    var valuesJS = {};
    for (var i = 0; i < NORMAL_COLORS.length; i++) {
        if (!general_1.hasOwnProperty(values, i))
            continue;
        valuesJS[i] = plywood_1.valueToJS(values[i]);
    }
    return valuesJS;
}
function valueEquals(v1, v2) {
    if (v1 === v2)
        return true;
    if (!v1 !== !v2)
        return false;
    Iif (v1.toISOString && v2.toISOString)
        return v1.valueOf() === v2.valueOf();
    Iif (immutable_class_1.isImmutableClass(v1))
        return v1.equals(v2);
    return false;
}
function valuesEqual(values1, values2) {
    if (!Boolean(values1) === Boolean(values2))
        return false;
    if (values1 === values2)
        return true;
    Iif (!values1 !== !values2)
        return false;
    Iif (typeof values1 !== typeof values2)
        return false;
    for (var i = 0; i < NORMAL_COLORS.length; i++) {
        var v1 = values1[i];
        var v2 = values2[i];
        if (general_1.hasOwnProperty(values1, i) !== general_1.hasOwnProperty(values2, i))
            return false;
        if (!valueEquals(v1, v2))
            return false;
    }
    return true;
}
function cloneValues(values) {
    var newValues = {};
    for (var i = 0; i < NORMAL_COLORS.length; i++) {
        if (!general_1.hasOwnProperty(values, i))
            continue;
        newValues[i] = values[i];
    }
    return newValues;
}
var check;
var Colors = (function () {
    function Colors(parameters) {
        this.dimension = parameters.dimension;
        Iif (!this.dimension)
            throw new Error('must have a dimension');
        this.values = parameters.values;
        this.hasNull = parameters.hasNull;
        this.limit = parameters.limit;
        Iif (!this.values && !this.limit)
            throw new Error('must have values or limit');
    }
    Colors.isColors = function (candidate) {
        return immutable_class_1.isInstanceOf(candidate, Colors);
    };
    Colors.fromLimit = function (dimension, limit) {
        return new Colors({ dimension: dimension, limit: limit });
    };
    Colors.fromValues = function (dimension, values) {
        var valueLookup = {};
        var hasNull = false;
        var n = Math.min(values.length, NORMAL_COLORS.length + 1);
        var i = 0;
        var j = 0;
        while (i < n) {
            var v = values[i];
            if (v === null) {
                hasNull = true;
            }
            else {
                valueLookup[j] = v;
                j++;
            }
            i++;
        }
        return new Colors({
            dimension: dimension,
            hasNull: hasNull,
            values: valueLookup
        });
    };
    Colors.fromJS = function (parameters) {
        var value = {
            dimension: parameters.dimension,
            limit: parameters.limit
        };
        var valuesJS = parameters.values;
        if (valuesJS) {
            var hasNull = Boolean(parameters.hasNull);
            var values = {};
            for (var i = 0; i < NORMAL_COLORS.length; i++) {
                if (!general_1.hasOwnProperty(valuesJS, i))
                    continue;
                var vJS = valuesJS[i];
                Iif (vJS === null) {
                    hasNull = true; // Back compat (there might be a null in values)
                }
                else {
                    values[i] = plywood_1.valueFromJS(vJS);
                }
            }
            value.values = values;
            value.hasNull = hasNull;
        }
        return new Colors(value);
    };
    Colors.prototype.valueOf = function () {
        return {
            dimension: this.dimension,
            values: this.values,
            hasNull: this.hasNull,
            limit: this.limit
        };
    };
    Colors.prototype.toJS = function () {
        var js = {
            dimension: this.dimension
        };
        if (this.values)
            js.values = valuesToJS(this.values);
        if (this.hasNull)
            js.hasNull = true;
        if (this.limit)
            js.limit = this.limit;
        return js;
    };
    Colors.prototype.toJSON = function () {
        return this.toJS();
    };
    Colors.prototype.toString = function () {
        return "[Colors: " + this.dimension + "]";
    };
    Colors.prototype.equals = function (other) {
        return Colors.isColors(other) &&
            valuesEqual(this.values, other.values) &&
            this.hasNull === other.hasNull &&
            this.limit === other.limit;
    };
    Colors.prototype.numColors = function () {
        var _a = this, values = _a.values, limit = _a.limit;
        if (values) {
            return Object.keys(values).length + Number(this.hasNull);
        }
        return limit;
    };
    Colors.prototype.toArray = function () {
        var _a = this, values = _a.values, hasNull = _a.hasNull;
        if (!values)
            return null;
        var vs = [];
        if (hasNull)
            vs.push(null);
        for (var i = 0; i < NORMAL_COLORS.length; i++) {
            if (!general_1.hasOwnProperty(values, i))
                continue;
            vs.push(values[i]);
        }
        return vs;
    };
    Colors.prototype.toSet = function () {
        if (!this.values)
            return null;
        return plywood_1.Set.fromJS(this.toArray());
    };
    Colors.prototype.toHavingFilter = function (segmentName) {
        var _a = this, dimension = _a.dimension, values = _a.values;
        if (!segmentName)
            segmentName = dimension;
        if (!values)
            return null;
        return new plywood_1.FilterAction({
            expression: plywood_1.$(segmentName).in(this.toSet())
        });
    };
    Colors.prototype.toLimitAction = function () {
        return new plywood_1.LimitAction({
            limit: this.numColors()
        });
    };
    Colors.prototype.toggle = function (v) {
        return this.has(v) ? this.remove(v) : this.add(v);
    };
    Colors.prototype.valueIndex = function (v) {
        var values = this.values;
        Iif (!values)
            return -1;
        for (var i = 0; i < NORMAL_COLORS.length; i++) {
            if (!general_1.hasOwnProperty(values, i))
                continue;
            if (valueEquals(values[i], v))
                return i;
        }
        return -1;
    };
    Colors.prototype.nextIndex = function () {
        var values = this.values;
        Iif (!values)
            return 0;
        for (var i = 0; i < NORMAL_COLORS.length; i++) {
            if (general_1.hasOwnProperty(values, i))
                continue;
            return i;
        }
        return -1;
    };
    Colors.prototype.has = function (v) {
        if (v == null)
            return this.hasNull;
        return this.valueIndex(v) !== -1;
    };
    Colors.prototype.add = function (v) {
        Iif (this.has(v))
            return this;
        var value = this.valueOf();
        Iif (v === null) {
            value.hasNull = true;
        }
        else {
            var idx = this.nextIndex();
            Iif (idx === -1)
                return this;
            value.values = value.values ? cloneValues(value.values) : {};
            value.values[idx] = v;
            delete value.limit;
        }
        return new Colors(value);
    };
    Colors.prototype.remove = function (v) {
        Iif (!this.has(v))
            return this;
        var value = this.valueOf();
        Iif (v == null) {
            value.hasNull = false;
        }
        else {
            var idx = this.valueIndex(v);
            Iif (idx === -1)
                return this;
            value.values = cloneValues(value.values);
            delete value.values[idx];
            delete value.limit;
        }
        return new Colors(value);
    };
    Colors.prototype.getColors = function (valuesToColor) {
        var _this = this;
        var _a = this, values = _a.values, limit = _a.limit, hasNull = _a.hasNull;
        Eif (values) {
            return valuesToColor.map(function (value) {
                if (value === null && hasNull)
                    return NULL_COLOR;
                var colorIdx = _this.valueIndex(value);
                return colorIdx === -1 ? null : NORMAL_COLORS[colorIdx];
            });
        }
        else {
            var colors = [];
            var colorIdx = 0;
            for (var i = 0; i < valuesToColor.length; i++) {
                if (i < limit) {
                    var v = valuesToColor[i];
                    if (v === null) {
                        colors.push(NULL_COLOR);
                    }
                    else {
                        colors.push(NORMAL_COLORS[colorIdx]);
                        colorIdx++;
                    }
                }
                else {
                    colors.push(null);
                }
            }
            return colors;
        }
    };
    return Colors;
}());
exports.Colors = Colors;
check = Colors;