forked from thoughtworks/build-your-own-radar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.js
172 lines (130 loc) · 4.55 KB
/
factory.js
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
const d3 = require('d3');
const Tabletop = require('tabletop');
const _ = {
map: require('lodash/map'),
uniqBy: require('lodash/uniqBy'),
capitalize: require('lodash/capitalize'),
each: require('lodash/each')
};
const InputSanitizer = require('./inputSanitizer');
const Radar = require('../models/radar');
const Quadrant = require('../models/quadrant');
const Cycle = require('../models/cycle');
const Blip = require('../models/blip');
const GraphingRadar = require('../graphing/radar');
const GoogleSheet = function (sheetId, sheetName) {
var self = {};
self.build = function () {
Tabletop.init({
key: sheetId,
callback: createRadar
});
function createRadar(sheets, tabletop) {
if (!sheetName) {
sheetName = Object.keys(sheets)[0];
}
var blips = _.map(tabletop.sheets(sheetName).all(), new InputSanitizer().sanitize);
document.title = tabletop.googleSheetName;
d3.selectAll(".loading").remove();
var cycles = _.map(_.uniqBy(blips, 'cycle'), 'cycle');
var cycleMap = {};
_.each(cycles, function (cycleName, i) {
cycleMap[cycleName] = new Cycle(cycleName, i);
});
var quadrants = {};
_.each(blips, function (blip) {
if (!quadrants[blip.quadrant]) {
quadrants[blip.quadrant] = new Quadrant(_.capitalize(blip.quadrant));
}
quadrants[blip.quadrant].add(new Blip(blip.Name, cycleMap[blip.cycle], blip.isNew.toLowerCase() === 'true', blip.topic, blip.description))
});
var radar = new Radar();
_.each(quadrants, function (quadrant) {
radar.addQuadrant(quadrant)
});
var size = (window.innerHeight - 133) < 620 ? 620 : window.innerHeight - 133;
new GraphingRadar(size, radar).init().plot();
}
};
self.init = function () {
var content = d3.select('body')
.append('div')
.attr('class', 'loading')
.append('div')
.attr('class', 'input-sheet');
plotLogo(content);
var bannerText= '<h1>Build your own radar...</h1><p>Your Technology Radar will be available in just a few seconds</p>';;
plotBanner(content, bannerText);
plotFooter(content);
return self;
};
return self;
};
var QueryParams = function (queryString) {
var decode = function (s) {
return decodeURIComponent(s.replace(/\+/g, " "));
};
var search = /([^&=]+)=?([^&]*)/g;
var queryParams = {};
var match;
while (match = search.exec(queryString))
queryParams[decode(match[1])] = decode(match[2]);
return queryParams
};
const GoogleSheetInput = function () {
var self = {};
self.build = function () {
var queryParams = QueryParams(window.location.search.substring(1));
if (queryParams.sheetId) {
return GoogleSheet(queryParams.sheetId, queryParams.sheetName).init().build();
} else {
var content = d3.select('body')
.append('div')
.attr('class', 'input-sheet');
plotLogo(content);
var bannerText = '<h1>Build your own radar</h1><p>Once you\'ve <a href ="">created your Radar</a>, you can use this service' +
' to generate an <br />interactive version of your Technology Radar. Not sure how? <a href ="">Read this first.</a></p>';
plotBanner(content, bannerText);
plotForm(content);
plotFooter(content);
}
};
return self;
};
function plotLogo(content) {
content.append('div')
.attr('class', 'input-sheet__logo')
.html('<a href=""><img src="/images/tw-logo.png" / ></a>');
}
function plotFooter(content) {
content
.append('div')
.attr('id', 'footer')
.append('p')
.classed('radar-footer', true)
.html('Powered by <a href="https://www.thoughtworks.com"> ThoughtWorks</a>. Open source, github link and credit references go here');
}
function plotBanner(content, text) {
content.append('div')
.attr('class', 'input-sheet__banner')
.html(text);
}
function plotForm(content) {
content.append('div')
.attr('class', 'input-sheet__form')
.append('p')
.html('<strong>Enter the URL of your public google sheet below...</strong>');
var form = content.select('.input-sheet__form').append('form')
.attr('method', 'get');
form.append('input')
.attr('type', 'text')
.attr('name', 'sheetId')
.attr('placeholder', 'e.g. https://docs.google.com/spreadsheets/d/1--_uLSNf/pubhtml');
form.append('button')
.attr('type', 'submit')
.append('a')
.attr('class', 'button')
.text('Build my radar');
form.append('p').html("<a href=''>Need help?</a>");
}
module.exports = GoogleSheetInput;