/
main.js
155 lines (125 loc) · 4.18 KB
/
main.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
'use strict';
var container = document.getElementById('container');
var globe = new DAT.Globe(container);
var dates = ['', '2000-04-29,' + new Date().toISOString().split('T')[0], 'upcoming', 'all'];
var past = 1; // 0-1
var upcoming = 2; // 0-2
var artistId = '';
var timer;
// inputs
$('#searchBtn').click(function () {
search();
});
$('#artistName').keyup(function (e) {
if (e.keyCode === 13) {
search();
} else {
clearTimeout(timer);
timer = setTimeout(search, 500);
}
});
$('#artistName').keydown(function () {
clearTimeout(timer);
});
$('a[href="#raw"]').click(function () {
var name = $('#artistName').val();
fetchData(null, name, name);
});
$('#tray').click(function () {
$('#title').toggle(200);
$('#description').toggle(200);
});
$('#past').click(function () {
past = this.checked ? 1 : 0;
fetchData();
});
$('#upcoming').click(function () {
upcoming = this.checked ? 2 : 0;
fetchData();
});
function resetGlobe() {
globe.reset();
$('#artist').empty();
$('#top10').empty().hide();
$('#past').attr('disabled', true);
$('#upcoming').attr('disabled', true);
}
function enableCbs() {
$('#past').removeAttr('disabled');
$('#upcoming').removeAttr('disabled');
}
function fetchData(mbid, name, rawName) {
resetGlobe();
if (mbid) artistId = mbid;
var date = dates[past+upcoming];
if ((!artistId && !rawName) || !date) {
enableCbs();
return;
}
$('#artists').fadeOut(200, function () {
var url = '//api.bandsintown.com/artists/' + (rawName ? rawName : 'mbid_' + artistId) + '/events.json?api_version=2.0&app_id=MUSIC_GLOBE&date=' + date;
$.get(url, {}, function (data) {
if (!data || (data.errors && data.errors[0])) {
$('#artists').fadeOut();
$('#infos').fadeOut();
$('#error').html('Bandsintown: ' + data.errors[0]);
$('#error').fadeIn();
return;
}
resetGlobe();
enableCbs();
var cities = {};
// first pass
data.forEach(function (d) {
var city = d.formatted_location;
cities[city] = cities.hasOwnProperty(city) ? cities[city] + 1 : 1;
});
var top = Object.keys(cities).map(function(c) { return {city: c, n: cities[c]}; }).sort(function (a, b) {
return b.n - a.n;
});
if (top.length === 0 || top[0].n === 0) return;
var scale = 1 / top[0].n;
var topHtml = top.slice(0, 100).reduce(function (prev, curr) {
return prev + '<li class="list-group-item"> <span class="badge alert-success">' + curr.n + '</span> ' + curr.city + ' </li>';
}, '');
$('#artist').html(name);
$('#top10').append(topHtml).show();
var globeDataLL = data.reduce(function (prev, curr) {
var venue = curr.venue;
return prev.concat([parseFloat(venue.latitude), parseFloat(venue.longitude), cities[curr.formatted_location] * scale]);
}, []);
globe.addData(globeDataLL, {format: 'magnitude'});
globe.createPoints();
$('#infos').fadeIn();
$('#artists').hide();
}, 'jsonp');
});
}
function fetchArtists(query) {
resetGlobe();
query = query.trim();
if (query.length === 0) return;
$('#infos').fadeOut();
$('#artists').fadeOut();
var url = '//musicbrainz.org/ws/2/artist/?query=artist:' + query + '*&fmt=json&limit=15';
$.get(url, {}, function (data) {
var suggest = data.artists.slice(0, 10).reduce(function (prev, curr) {
return prev + '<a href="javascript:fetchData(\'' + curr.id + '\', \'' + curr.name + '\');" class="list-group-item">' + curr.name + '</a>';
}, '');
$('#infos').show();
$('#artists').html(suggest).fadeIn();
});
}
function search() {
$('#error').hide();
fetchArtists($('#artistName').val());
globe.reset();
}
$(document).ready(function () {
if(!Detector.webgl){
Detector.addGetWebGLMessage();
} else {
resetGlobe();
globe.animate();
}
});