-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
495 lines (423 loc) · 14.2 KB
/
Copy pathapp.js
File metadata and controls
495 lines (423 loc) · 14.2 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
(function() {
/**
* Obtains parameters from the hash of the URL.
* @return {Object}
*/
function getHashParams() {
var hashParams = {};
var e, r = /([^&;=]+)=?([^&;]*)/g,
q = window.location.hash.substring(1);
while (e = r.exec(q)) {
hashParams[e[1]] = decodeURIComponent(e[2]);
}
return hashParams;
}
function generateRandomString(length) {
var text = '';
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};
var SpotifyAuth = function(initialState) {
/**
* The client ID of this Spotify application.
*
* @type {String}
*/
this.clientId = '5fdeabe6e1eb4532ab32205e9b72b38f';
/**
* Response type. Either `code` or `token`.
*
* @type {String}
*/
this.responseType = 'token';
/**
* The URI to redirect to after the user grants/denies permission.
*
* @type {String}
*/
this.redirectUri = 'http://localhost:3000/';
this.STATE_KEY = 'spotify-artist-search-app-state';
this.accessToken = '';
this.state = this.getState() || initialState;
};
SpotifyAuth.prototype.getState = function() {
return this.state || localStorage.getItem(this.STATE_KEY);
};
SpotifyAuth.prototype.setState = function(newState) {
localStorage.setItem(this.STATE_KEY, newState);
this.state = newState;
};
SpotifyAuth.prototype.authorize = function() {
var params = getHashParams();
var access_token = params.access_token;
var state = params.state;
var storedState = this.getState();
if (access_token && (state === storedState)) {
this.accessToken = access_token;
return;
}
this.setState(state || storedState);
var params = {
client_id: this.clientId,
response_type: this.responseType,
redirect_uri: this.redirectUri,
state: this.state
};
var url = 'https://accounts.spotify.com/authorize?' + $.param(params);
window.location = url;
};
var SpotifyApp = function(options) {
this.accessToken = options.accessToken;
/**
* The current query string.
*
* @type {String}
*/
this.query = '';
/**
* The current offset value, used for pagination.
*
* @type {Number}
*/
this.offset = 0;
/**
* A number representing the total for the artist search results.
*
* @type {Number}
*/
this.total = 0;
/**
* The number of items to return from calling the Spotify `/search` API.
*
* @type {Number}
*/
this.limit = 20;
/**
* Flag used to control whether or not to display popular artists when we
* start the app. Useful for rate-limiting restrictions.
*
* @type {Boolean}
*/
this.showPopularArtistsOnStartup = true;
};
/**
* Binds the pagination scroll event on `document`. When the scroll bar is
* near the bottom of the page, `paginate` is fired.
*/
SpotifyApp.prototype.bindScrollHandler = function() {
var _self = this;
$(document).scroll(function() {
var scrollPos = $(window).scrollTop();
var nearBottom = $(document).height() - $(window).height() - 100;
if (scrollPos >= nearBottom) {
_self.unbindScrollHandler();
_self.offset += _self.limit;
_self.paginate({
q: _self.query,
offset: _self.offset,
});
}
});
};
/**
* Removes the pagination scroll event on `document`.
*/
SpotifyApp.prototype.unbindScrollHandler = function() {
$(document).off('scroll');
};
/**
* Renders the message bar with the given `el`.
*
* @param {String} el The compiled string template.
*/
SpotifyApp.prototype.renderMessageBar = function(el) {
$('.message-bar').html(el);
};
/**
* Renders the results list with the given `el`.
*
* @param {String} el The compiled string template.
*/
SpotifyApp.prototype.renderResults = function(el) {
$('.results').html(el);
};
/**
* Appends the results list with the given `el`.
*
* @param {String} el The compiled string template.
*/
SpotifyApp.prototype.appendResults = function(el) {
$('.results').append(el);
};
/**
* Displays a shorthand/truncated version of a large number, rounded to 2
* decimal places.
*
* @param {Number} num An integer to be truncated.
* @return {String} The formatted number, e.g. `1.08M`, `2.25k`, `250`.
*/
SpotifyApp.prototype.truncateNumber = function (num) {
var split = num.toLocaleString('en-US').split(',');
// If the number is small, just return the number.
if (!split[1]) {
return split[0];
}
// Otherwise, concat the comma-separated numbers, round and truncate
// using `map`.
var map = {
1: 'k',
2: 'M',
3: 'B',
};
var formatted = split[0] + '.';
split.shift();
_.each(split, function (val) {
formatted += val;
});
formatted = Number(Math.round(parseFloat(formatted) + 'e2') + 'e-2').toString() + map[split.length];
return formatted;
};
/**
* Performs a `GET` request on the Spotify artist search endpoint.
*
* @param {Object} [params] Query parameters to pass while issuing the
* request.
* @param {Function} success Callback to be executed on success.
* @param {Function} error Callback to be executed on error.
* @return {jQuery.Deferred} The jQuery Deferred object.
*/
SpotifyApp.prototype.searchArtist = function(params, success, error) {
params = _.defaults(params, { type: 'artist', limit: this.limit });
return $.ajax({
url: 'https://api.spotify.com/v1/search',
headers: {
'Authorization': 'Bearer ' + this.accessToken
},
data: params,
success: success
}).fail(error);
};
/**
* Callback to be executed on error when requesting the search endpoint.
*
* @param {jQuery.Deferred} xhr The error response object.
* @param {String} type The type of error (e.g. 'error').
* @param {String} msg The error message (e.g. 'Not Found').
*/
SpotifyApp.prototype.searchArtistErrorCallback = function(xhr, type, msg) {
var template = Handlebars.templates['error.hbs'];
var el = template({ xhr: xhr, msg: msg });
this.renderMessageBar(el);
};
/**
* Builds the template for the list items to be rendered in the list.
*
* @param {Object} data The data returned from the Spotify API.
* @return {String} The template of list items.
*/
SpotifyApp.prototype.buildListTemplate = function(data) {
var _self = this;
var content = '';
_.each(data.artists.items, function(artist) {
var templateData = {
image: !_.isEmpty(artist.images) && artist.images[0].url,
name: artist.name,
popularity: artist.popularity,
popularityIcon: 'fa-ellipsis-h',
followersFormatted: _self.truncateNumber(artist.followers.total),
link: artist.external_urls.spotify,
};
var totalFollowers = artist.followers.total.toLocaleString('en-US');
templateData.followersLabel = artist.followers.total === 1 ? totalFollowers + ' follower' : totalFollowers + ' followers';
if (artist.popularity >= 60) {
templateData.popularityIcon = 'fa-chevron-up';
} else if (artist.popularity < 40) {
templateData.popularityIcon = 'fa-chevron-down';
}
template = Handlebars.templates['result.hbs'];
el = template(templateData);
if (templateData.image) {
// Unfortunately, some images are REALLY small, and
// `backdrop-filter` has very limited support, so we need to
// dynamically add the `background-image` and use
// `background-size: cover` to achieve the blurred overlay.
var $el = $(el);
$el.find('.image-container, .blurred')
.css('background-image', 'url("' + templateData.image + '")');
el = $el.get(0).outerHTML;
}
content += el;
});
return content;
};
/**
* Handler for triggering a new search. Does not issue a request if the
* previous search term matches the current, or if the search bar is empty.
*/
SpotifyApp.prototype.searchHandler = function() {
var searchTerm = $('[data-searchbar]').val().trim();
if (_.isEmpty(searchTerm) || this.query === searchTerm) {
return;
}
this.query = searchTerm;
var loadingTpl = Handlebars.templates['loading.hbs']();
this.renderMessageBar(loadingTpl);
this.searchArtist(
{ q: this.query },
_.bind(function(data) {
var template;
var el;
// Unbind the pagination handler prior to appending results.
this.unbindScrollHandler();
// Starting a new search should reset the offset.
this.offset = data.artists.offset;
this.total = data.artists.total;
if (_.isEmpty(data.artists.items)) {
template = Handlebars.templates['no-results.hbs'];
el = template(this.query);
this.renderMessageBar(el);
this.renderResults('');
return;
}
// Scroll to the top of the page, since we have a fixed navbar.
if ($(window).scrollTop() > 0) {
window.scrollTo(0, 0);
}
var content = this.buildListTemplate(data);
this.renderResultsBar(data);
this.renderResults(content);
if (this.total > this.limit) {
// Re-bind the pagination handler if we can paginate.
this.bindScrollHandler();
}
}, this),
_.bind(this.searchArtistErrorCallback, this)
);
};
/**
* Renders the message bar with an appropriate results template.
*
* @param {Object} data The data returned from the Spotify API.
* @param {Object} [templateContext] Data to pass to the template being
* rendered.
*/
SpotifyApp.prototype.renderResultsBar = function(data, templateContext) {
if (this.total === 1) {
// Don't render anything in the message bar for only 1 result, since
// it looks silly.
this.renderMessageBar('');
return;
}
// For the case where we are increasing the `count` while paginating.
templateContext = _.extend({
count: data.artists.items.length,
total: this.total,
term: this.query,
}, templateContext);
// Template chosen depends if the `total` is less than the `limit`, or
// if we've already paginated to the end.
var allResults = this.total <= this.limit || templateContext.count === this.total;
var countTemplate = allResults ? 'result-count-total.hbs' : 'result-count.hbs';
var messageTpl = Handlebars.templates[countTemplate](templateContext);
this.renderMessageBar(messageTpl);
};
/**
* Issues a request to the Spotify `/search` endpoint, passing in the
* `offset` to perform pagination.
*
* @param {Object} [params] Query parameters to pass while issuing the
* request.
*/
SpotifyApp.prototype.paginate = function(params) {
params = _.defaults(params, { offset: this.offset });
this.searchArtist(params,
_.bind(function(data) {
var content = this.buildListTemplate(data);
var remainder = this.total - this.offset;
var paginationCount;
if (remainder < data.artists.limit) {
// We're at the last page.
paginationCount = this.total;
this.unbindScrollHandler();
} else {
// We have more pages.
paginationCount = this.offset + data.artists.limit;
this.bindScrollHandler();
}
this.renderResultsBar(data, { count: paginationCount });
this.appendResults(content);
}, this),
_.bind(this.searchArtistErrorCallback, this)
);
};
/**
* This method handles what to display when the app starts. In a nutshell,
* it takes every letter of the English alphabet and calls the `/search`
* endpoint against that letter (scary, I know). Using jQuery Deferreds, it
* waits until each `GET` request is completed, and filters the results to
* unique items, and artists that have a popularity score 70 or higher.
*
* Unfortunately, Spotify doesn't have an endpoint to fetch popular artists,
* artists with lots of followers, or even some random artists - hence this
* method.
*/
SpotifyApp.prototype.start = function() {
if (!this.showPopularArtistsOnStartup) {
return;
}
var _self = this;
var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
var ids = {};
var deferreds = [];
var loadingTpl = Handlebars.templates['loading.hbs']();
this.renderMessageBar(loadingTpl);
_.each(alphabet, function (letter) {
deferreds.push(_self.searchArtist({ q: letter, limit: 50 }));
});
$.when.apply($, deferreds)
.done(function() {
var args = Array.prototype.slice.call(arguments);
// First argument in each of the args has the data.
var responseList = _.map(args, _.first);
var results = [];
// Filter out artists with popularity < 70.
_.each(responseList, function(data) {
_.each(data.artists.items, function(item) {
if (item.popularity >= 70 && !ids[item.id]) {
ids[item.id] = item;
results = results.concat([item]);
}
});
});
var hasResults = !_.isEmpty(results);
var templateToLoad = hasResults ? 'popular-artists.hbs' : 'no-results.hbs';
var template = Handlebars.templates[templateToLoad]();
_self.renderMessageBar(template);
if (!hasResults) {
return;
}
var content = _self.buildListTemplate({ artists: { items: results } });
_self.renderResults(content);
}).fail(_.bind(_self.searchArtistErrorCallback, _self));
};
$(document).ready(function() {
var auth = new SpotifyAuth(generateRandomString(16));
auth.authorize();
var app = new SpotifyApp({
accessToken: auth.accessToken,
});
// Set up event listeners.
$('[data-searchbutton]').click(app.searchHandler);
$('[data-searchbar]').keypress(function (e) {
// e.keyCode is deprecated, but Safari still doesn't support e.key.
var isEnter = e.keyCode === 13;
if (isEnter) {
app.searchHandler();
}
});
app.start();
});
})();