Merge pull request #10 from andyw8/add-license
[tech-radar.git] / src / graphing / ref-table.js
1 tr.graphing.RefTable = function (radar) {
2 var self = {};
3 var injectionElement;
4
5 function blipsByCycle () {
6 // set up empty blip arrays for each cycle
7 var cycles = {};
8 radar.cycles()
9 .map(function (cycle) {
10 return {
11 order: cycle.order(),
12 name: cycle.name()
13 };
14 })
15 .sort(function (a, b) {
16 if (a.order === b.order) {
17 return 0;
18 } else if (a.order < b.order) {
19 return -1;
20 } else {
21 return 1;
22 }
23 })
24 .forEach(function (cycle) {
25 cycles[cycle.name] = [];
26 });
27
28 // group blips by cycle
29 var blips = [];
30 var quadrants = radar.quadrants();
31 Object.keys(quadrants).forEach(function (quadrant) {
32 blips = blips.concat(quadrants[quadrant].blips());
33 });
34
35 blips.forEach(function (blip) {
36 cycles[blip.cycle().name()].push(blip);
37 });
38
39 return cycles;
40 }
41
42 self.init = function (selector) {
43 injectionElement = document.querySelector(selector || 'body');
44 return self;
45 };
46
47 self.render = function () {
48 var blips = blipsByCycle();
49
50 var html = '<table class="radar-ref-table">';
51
52 Object.keys(blips).forEach(function (cycle) {
53 html += '<tr class="radar-ref-status-group"><td colspan="3">' + cycle + '</td></tr>';
54
55 blips[cycle].forEach(function (blip) {
56 html += '<tr>' +
57 '<td>' + blip.number() + '</td>' +
58 '<td>' + blip.name() + '</td>' +
59 '<td>' + blip.description() + '</td>' +
60 '</tr>';
61 });
62 });
63
64 html += '</table>';
65
66 injectionElement.innerHTML = html;
67 };
68
69 return self;
70 };