-
Notifications
You must be signed in to change notification settings - Fork 643
Expand file tree
/
Copy pathConfigFactory.js
More file actions
360 lines (315 loc) · 11.7 KB
/
ConfigFactory.js
File metadata and controls
360 lines (315 loc) · 11.7 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import { TimelineConfig } from "../core/TimelineConfig"
import { trim, isEmptyObject, mergeData, trace } from "../core/Util";
import { parseDate } from "../date/TLDate"
import TLError from "../core/TLError"
import { ajax } from "../net/Net"
import { parseTime, validDateConfig } from "../date/DateUtil"
import { fetchCSV } from '../core/CSV';
function clean_integer(s) {
if (s) {
return s.replace(/[\s,]+/g, ''); // doesn't handle '.' as comma separator, but how to distinguish that from decimal separator?
}
}
export function parseGoogleSpreadsheetURL(url) {
let parts = {
key: null,
worksheet: 0 // not really sure how to use this to get the feed for that sheet, so this is not ready except for first sheet right now
}
// key as url parameter (old-fashioned)
var key_pat = /\bkey=([-_A-Za-z0-9]+)&?/i;
var url_pat = /docs.google.com\/spreadsheets(.*?)\/d\//; // fixing issue of URLs with u/0/d
if (url.match(key_pat)) {
parts.key = url.match(key_pat)[1];
// can we get a worksheet from this form?
} else if (url.match(url_pat)) {
var pos = url.search(url_pat) + url.match(url_pat)[0].length;
var tail = url.substr(pos);
parts.key = tail.split('/')[0]
if (url.match(/\?gid=(\d+)/)) {
parts.worksheet = url.match(/\?gid=(\d+)/)[1];
}
} else if (url.match(/^\b[-_A-Za-z0-9]+$/)) {
parts.key = url;
}
if (parts.key) {
return parts;
} else {
return null;
}
}
function interpretBackground(bkgd) {
if (typeof(bkgd) != 'string') return ''
if (bkgd.match(/^(https?:)?\/\/?/)) { // support http, https, protocol relative, site relative
return { 'url': bkgd }
} else { // for now we'll trust it's a color
return { 'color': bkgd }
}
}
function extractEventFromCSVObject(orig_row) {
let row = {}
Object.keys(orig_row).forEach(k => {
row[k] = trim(orig_row[k]) // get rid of white-space and reduce all-blank cells to empty strings
})
var d = {
media: {
caption: row['Media Caption'] || '',
credit: row['Media Credit'] || '',
url: row['Media'] || '',
thumbnail: row['Media Thumbnail'] || ''
},
text: {
headline: row['Headline'] || '',
text: row['Text'] || ''
},
display_date: row['Display Date'] || '', // only in v3 but no problem
group: row['Group'] || row['Tag'] || '', // small diff between v1 and v3 sheets
background: interpretBackground(row['Background']), // only in v3 but no problem
id: row['n°ID'] || '',
type: row['Type'] || '',
categories: row['Categorie'] || '',
markerColor: row['MarkerColor'] || '',
GroupOrder: row['GroupOrder'] || '',
GroupColor: row['GroupColor'] || '',
parentOf: row['parentOf'] || '',
childOf: row['childOf'] || '',
typeOfLink: row['TypeOfLink'] || '',
ZoomOnClick: row['ZoomOnClick'] || '',
}
if(d.parentOf != ''){
var parents = d.parentOf.split(/[,;]/);
d.parentOf = parents;
}
if(d.childOf != ''){
var children = d.childOf.split(/[,;]/);
d.childOf = children;
}
if (Object.keys(row).includes('Start Date') || Object.keys(row).includes('End Date')) {
// V1 date handling
if (row['Start Date']) {
d.start_date = parseDate(row['Start Date'])
}
if (row['End Date']) {
d.end_date = parseDate(row['End Date'])
}
} else {
// V3 date handling
// every date must have at least a year to be valid.
if (row['Year']) {
d.start_date = {
year: clean_integer(row['Year']),
month: clean_integer(row['Month']) || '',
day: clean_integer(row['Day']) || ''
}
}
if (row['End Year']) {
d.end_date = {
year: clean_integer(row['End Year']) || '',
month: clean_integer(row['End Month']) || '',
day: clean_integer(row['End Day']) || ''
}
}
if (row['Time']) {
if (d.start_date) {
mergeData(d.start_date, parseTime(row['Time']));
} else {
throw new TLError("invalid_start_time_without_date")
}
}
if (row['End Time']) {
if (d.end_date) {
mergeData(d.end_date, parseTime(row['End Time']));
} else {
throw new TLError("invalid_end_time_without_date")
}
}
if (d.start_date && !validDateConfig(d.start_date)) {
throw new TLError("invalid_date_err")
}
if (d.end_date && !validDateConfig(d.end_date)) {
throw new TLError("invalid_date_err")
}
}
return d
}
/**
* Given a Google Sheets URL (or mere document ID), read the data and return
* a Timeline JSON file suitable for instantiating a timeline.
*
* @param {string} url
*/
export async function readGoogleAsCSV(url, sheets_proxy) {
let rows = []
url = makeGoogleCSVURL(url)
let error = null;
await fetchCSV({
url: `${url}`,
}).then(d => {
rows = d;
}).catch(error_json => {
if (error_json.proxy_err_code == 'response_not_csv') {
throw new TLError('Timeline could not read the data for your timeline. Make sure you have published it to the web.')
}
throw new TLError(error_json.message)
})
let timeline_config = { 'events': [], 'errors': [], 'warnings': [], 'eras': [] }
rows.forEach((row, i) => {
try {
if (!isEmptyObject(row)) {
let event = extractEventFromCSVObject(row)
handleRow(event, timeline_config)
}
} catch (e) {
if (e.constructor == TLError) {
timeline_config.errors.push(e);
} else {
if (e.message) {
e = e.message;
}
let label = row['Headline'] || i
timeline_config.errors.push(e + `[${label}]`);
}
}
});
return timeline_config
}
/**
* Given a Google Sheets URL or a bare spreadsheet key, return a URL expected
* to retrieve a CSV file, assuming the Sheets doc has been "published to the web".
* No checking for the actual availability is done.
* @param {string} url_or_key
*/
export function makeGoogleCSVURL(url_or_key) {
url_or_key = url_or_key.trim()
if (url_or_key.match(/^[a-zA-Z0-9-_]+$/)) {
// key pattern from https://developers.google.com/sheets/api/guides/concepts#spreadsheet_id
return `https://docs.google.com/spreadsheets/d/${url_or_key}/pub?output=csv`
}
if (url_or_key.startsWith('https://docs.google.com/spreadsheets/')) {
if (url_or_key.match(/\/pub\?output=csv$/)) return url_or_key
let parsed = new URL(url_or_key)
let params = new URLSearchParams(parsed.search)
params.set('output', 'csv')
if (params.get('gid')) {
params.set('single', 'true')
}
parsed.search = `?${params.toString()}`
let base_path = parsed.pathname.substr(0, parsed.pathname.lastIndexOf('/'))
parsed.pathname = `${base_path}/pub`
return parsed.toString()
}
throw new TLError('invalid_url_err', url_or_key);
}
var buildGoogleFeedURL = function(key, api_version) {
if (api_version == 'v4') {
return "https://sheets.googleapis.com/v4/spreadsheets/" + key + "/values/A1:R1000?key=AIzaSyCInR0kjJJ2Co6aQAXjLBQ14CEHam3K0xg";
} else {
return "https://spreadsheets.google.com/feeds/list/" + key + "/1/public/values?alt=json";
}
}
async function jsonFromGoogleURL(google_url, options) {
if (!options['sheets_proxy']) {
throw new TLError("Proxy option must be set to read data from Google")
}
var timeline_json = await readGoogleAsCSV(google_url, options['sheets_proxy']);
if (timeline_json) {
return timeline_json;
}
}
/**
* Using the given URL, fetch or create a JS Object suitable for configuring a timeline. Use
* that to create a TimelineConfig, and invoke the callback with that object as its argument.
* If the second argument is an object instead of a callback function, it must have a
* 'callback' property which will be invoked with the config.
* Even in error cases, a minimal TimelineConfig object will be created and passed to the callback
* so that error messages can be displayed in the host page.
*
* @param {String} url the URL or Google Spreadsheet key which can be used to get configuration information
* @param {function|object} callback_or_options either a callback function or an object with a 'callback' property and other configuration properties
*/
export async function makeConfig(url, callback_or_options) {
let callback = null,
options = {};
if (typeof(callback_or_options) == 'function') {
callback = callback_or_options
} else if (typeof(callback_or_options) == 'object') {
options = callback_or_options
callback = callback_or_options['callback']
if (typeof(options['callback']) == 'function') callback = options['callback']
}
if (!callback) {
throw new TLError("Second argument to makeConfig must be either a function or an object which includes a 'callback' property with a 'function' type value")
}
var tc,
json,
key = parseGoogleSpreadsheetURL(url);
if (key) {
try {
json = await jsonFromGoogleURL(url, options);
} catch (e) {
// even with an error, we make
// a TimelineConfig because it's
// the most straightforward way to display messages
// in the DOM
tc = new TimelineConfig();
if (e.name == 'NetworkError') {
tc.logError(new TLError("network_err"));
} else if (e.name == 'TLError') {
tc.logError(e);
} else {
tc.logError(new TLError("unknown_read_err", e.name));
}
callback(tc);
return; // don't process further if there were errors
}
tc = new TimelineConfig(json);
if (json.errors) {
for (var i = 0; i < json.errors.length; i++) {
tc.logError(json.errors[i]);
};
}
callback(tc);
} else {
ajax({
url: url,
dataType: 'json',
success: function(data) {
try {
tc = new TimelineConfig(data);
} catch (e) {
tc = new TimelineConfig();
tc.logError(e);
}
callback(tc);
},
error: function(xhr, errorType, error) {
tc = new TimelineConfig();
if (errorType == 'parsererror') {
var error = new TLError("invalid_url_err");
} else {
var error = new TLError("unknown_read_err", errorType)
}
tc.logError(error);
callback(tc);
}
});
}
}
function handleRow(event, timeline_config) {
var row_type = 'event';
if (typeof(event.type) != 'undefined') {
row_type = event.type;
delete event.type;
}
if (row_type == 'title') {
if (!timeline_config.title) {
timeline_config.title = event;
} else {
timeline_config.warnings.push("Multiple title slides detected.");
timeline_config.events.push(event);
}
} else if (row_type == 'era') {
timeline_config.eras.push(event);
} else {
timeline_config.events.push(event);
}
}