|
1 | | -'use strict'; |
2 | | - |
3 | 1 | import _ from 'lodash'; |
4 | | -import { matchString, loadClasses } from './classes'; |
| 2 | +import { Category, matchString, loadClasses } from './classes'; |
5 | 3 | const Color = require('color'); |
6 | 4 | const d3 = require('d3'); |
7 | 5 |
|
8 | 6 | // See here for examples: |
9 | 7 | // https://bl.ocks.org/pstuffa/3393ff2711a53975040077b7453781a9 |
10 | 8 |
|
11 | 9 | const scale = d3.scaleOrdinal(['#90CAF9', '#FFE082', '#EF9A9A', '#A5D6A7']); |
| 10 | +const uncatColor = '#AAA'; |
12 | 11 |
|
13 | 12 | // Needed to prewarm the color table |
14 | 13 | scale.domain([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]); |
@@ -60,28 +59,31 @@ export function getColorFromString(appname) { |
60 | 59 | return customColors[appname] || scale(Math.abs(hashcode(appname) % 20)); |
61 | 60 | } |
62 | 61 |
|
63 | | -function getColorFromCategory(c, allCats) { |
64 | | - if (c.data && c.data.color) { |
| 62 | +// TODO: Move into vuex? |
| 63 | +export function getColorFromCategory(c: Category, allCats: Category[]): string { |
| 64 | + if (c && c.data && c.data.color) { |
65 | 65 | return c.data.color; |
66 | | - } else if (c.name.slice(0, -1).length > 0) { |
| 66 | + } else if (c && c.name.slice(0, -1).length > 0) { |
67 | 67 | // If no color is set on category, traverse parents until one is found |
68 | 68 | const parent = c.name.slice(0, -1); |
69 | 69 | const parentCat = allCats.find(cc => _.isEqual(cc.name, parent)); |
70 | | - return getColorFromCategory(parentCat); |
| 70 | + return getColorFromCategory(parentCat, allCats); |
71 | 71 | } else { |
72 | 72 | // TODO: Fix reasonable fallback |
73 | | - return '#F0F'; |
| 73 | + return uncatColor; |
74 | 74 | } |
75 | 75 | } |
76 | 76 |
|
77 | | -export function getCategoryColorFromString(str) { |
| 77 | +// TODO: Move into vuex? |
| 78 | +export function getCategoryColorFromString(str: string): string { |
78 | 79 | // TODO: Don't load classes on every call |
79 | 80 | const allCats = loadClasses(); |
80 | | - const c = matchString(str); |
| 81 | + const c = matchString(str, allCats); |
81 | 82 | if (c !== null) { |
82 | 83 | return getColorFromCategory(c, allCats); |
83 | 84 | } else { |
84 | | - return getColorFromString(str); |
| 85 | + // TODO: Fix reasonable fallback |
| 86 | + return uncatColor; |
85 | 87 | } |
86 | 88 | } |
87 | 89 |
|
|
0 commit comments