-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmtv.mobl
130 lines (116 loc) · 3.24 KB
/
mtv.mobl
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
application mtv
import mobl
external sync function findArtist(query : String) : Collection<Artist>
external sync function getVideos(a : Artist) : Collection<Video>
external sync function getRelatedArtists(a : Artist) : Collection<Video>
entity Artist {
url : String
name : String
favorite : Bool
}
external type Video {
name : String
picture : String
url : String
}
function lookupLocalArtist(a : Artist) : Artist {
var found = Artist.all().filter("url", "=", a.url).one();
return found == null ? a : found;
}
function toggleFavorite(a : Artist) {
add(a);
a.favorite = a.favorite ? false : true;
}
template search() {
var artistQuery = "Michael"
searchbox(artistQuery)
group {
list(a : Artist in findArtist(artistQuery)) {
artistItem(a)
}
}
}
template favorites() {
group {
list(a in Artist.all().filter("favorite", "=", true)) {
artistItem(a)
}
}
}
screen root() {
tabset([("Search", "lib/toolbar/icon_magnify_glass.png", search), ("Favorites", "lib/toolbar/icon_favorities.png", favorites)])
}
template artistItem(a : Artist) {
itemArrow(onclick={showArtist(a); }) {
var localArtist : Artist = lookupLocalArtist(a)
label(localArtist.name)
cond(localArtist.favorite == true) {
image("lib/toolbar/icon_favorities_remove.png", onclick={ toggleFavorite(localArtist); })
}
cond(localArtist.favorite == false) {
image("lib/toolbar/icon_favorities_add.png", onclick={ toggleFavorite(localArtist); })
}
}
}
screen showArtist(a : Artist) {
backButton("Back", onclick={ screen return; })
basicView(a.name) {
<h2>"Related"</h2>
group {
list(relatedArtist : Artist in getRelatedArtists(a)) {
artistItem(relatedArtist)
}
}
<h2>"Videos"</h2>
group {
list(v : Video in getVideos(a)) {
item(onclick={ openUrl(v.url); }) { <img width="70" height="53" src=v.picture valign="middle" /> " " label(v.name) }
}
}
}
}
<script>
mtv.findArtist = function(query) {
return mobl.remoteCollection('http://api.mtvnservices.com/1/artist/search/?term=' + escape(query) + '&max-results=10', 'xml', function(data) {
var doc = $(data);
var results = [];
doc.find("entry").each(function(_, el) {
el = $(el);
results.push(new mtv.Artist({
url: el.find("id").text(),
name: el.find("title").text()
}));
});
return results;
});
};
mtv.getVideos = function(artist) {
return mobl.remoteCollection(artist.url + 'videos/', 'xml', function(data) {
var doc = $(data);
var results = [];
doc.find("entry").each(function(_, el) {
el = $(el);
results.push({
name: el.find("title").eq(0).text(),
picture: el.find("thumbnail").attr("url"),
url: el.find("player").attr("url")
});
});
return results;
});
};
mtv.getRelatedArtists = function(artist) {
return mobl.remoteCollection(artist.url + 'related/?max-results=5', 'xml', function(data) {
var doc = $(data);
var results = [];
doc.find("entry").each(function(_, el) {
el = $(el);
results.push(new mtv.Artist({
url: el.find("id").text(),
name: el.find("title").text()
}));
});
return results.slice(0, 5);
});
};
</script>