-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgroups.js
71 lines (66 loc) · 1.78 KB
/
groups.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
var database = require('./database.js');
function sort(member) {
var sorted = {},
key, keys = [];
for (key in member) {
keys.push(key);
}
keys.sort();
for (var i = 0; i < keys.length; i++) {
sorted[keys[i]] = member[keys[i]];
}
return sorted;
}
var groups;
var conferences;
var rawdata;
function getTree(callback) {
if(groups){
console.log('using cached data');
callback(null, groups, conferences, rawdata);
return;
}
database.queryData(function(err, data) {
if(err) {
callback(err)
return;
}
groups = {};
conferences = {};
conferences['Conference'] = {}; //dummy continent
rawdata = data;
for (var i in data) {
var continent = data[i].continent;
if(!continent) continue;
var country = data[i].country;
var state = data[i].state;
var link = '<a href="'+ data[i].link+ '">' + data[i].link + '</a>';
var item = data[i].town + ': ' + link;
if(!state) state = 'no-state';
if(continent != 'Conference') {
if(!groups[continent])
groups[continent] = {};
if(!groups[continent][country])
groups[continent][country] = [];
if(!groups[continent][country][state])
groups[continent][country][state] = [];
groups[continent][country][state].push(item);
groups = sort(groups)
groups[continent] = sort(groups[continent]);
groups[continent][country] = sort(groups[continent][country]);
} else {
if(!conferences[continent][country])
conferences[continent][country] = [];
if(!conferences[continent][country][state])
conferences[continent][country][state] = [];
conferences[continent][country][state].push(item);
}
}
callback(null, groups, conferences, rawdata);
})
}
function flushCache() {
groups = null;
}
module.exports.flushCache = flushCache;
module.exports.getTree = getTree;