-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.js
executable file
·147 lines (127 loc) · 3.72 KB
/
play.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
/*globals R, Main, Modernizr, rdioUtils */
(function() {
window.Main = {
// ----------
init: function() {
var self = this;
this.$input = $(".search input");
this.$results = $(".results");
var rawTemplate = $.trim($("#album-template").text());
this.albumTemplate = _.template(rawTemplate);
if (Modernizr.touch) {
self.$results
.click(function() {
$(".album").removeClass("hover");
});
} else {
_.defer(function() {
self.$input.focus();
});
}
$(".search")
.submit(function(event) {
event.preventDefault();
var query = self.$input.val();
if (query) {
R.ready(function() { // just in case the API isn't ready yet
self.search(query);
});
}
});
if (!rdioUtils.startupChecks()) {
return;
}
R.ready(function() {
var $play = $(".header .play")
.click(function() {
R.player.togglePause();
});
$(".header .next")
.click(function() {
R.player.next();
});
$(".header .prev")
.click(function() {
R.player.previous();
});
R.player.on("change:playingTrack", function(track) {
$(".header .icon").attr("src", track.get("icon"));
$(".header .track").text("Track: " + track.get("name"));
$(".header .album-title").text("Album: " + track.get("album"));
$(".header .artist").text("Artist: " + track.get("artist"));
});
R.player.on("change:playState", function(state) {
if (state === R.player.PLAYSTATE_PLAYING || state === R.player.PLAYSTATE_BUFFERING) {
$play.text("pause");
} else {
$play.text("play");
}
});
R.request({
method: "getTopCharts",
content: {
type: "Album"
},
success: function(response) {
self.showResults(response.result);
},
error: function(response) {
$(".error").text(response.message);
}
});
});
},
// ----------
search: function(query) {
var self = this;
R.request({
method: "search",
content: {
query: query,
types: "Album"
},
success: function(response) {
self.$input.val("");
self.showResults(response.result.results);
},
error: function(response) {
$(".error").text(response.message);
}
});
},
// ----------
showResults: function(albums) {
var self = this;
this.$results.empty();
_.each(albums, function(album) {
album.appUrl = album.shortUrl.replace("http", "rdio");
var $el = $(self.albumTemplate(album))
.appendTo(self.$results);
var $cover = $el.find(".icon");
if (Modernizr.touch) {
$cover.click(function(event) {
event.stopPropagation();
if (!$el.hasClass("hover")) {
$(".album").not($el).removeClass("hover");
$el.addClass("hover");
}
});
} else {
$cover.hover(function() {
$el.addClass("hover");
}, function() {
$el.removeClass("hover");
});
}
$el.find(".play")
.click(function() {
R.player.play({source: album.key});
});
});
}
};
// ----------
$(document).ready(function() {
Main.init();
});
})();