forked from forcedotcom/mobile-ui-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicBrowser.html
More file actions
305 lines (264 loc) · 13.4 KB
/
MusicBrowser.html
File metadata and controls
305 lines (264 loc) · 13.4 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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width">
<!-- Importing Google Polymer framework -->
<script src="dependencies/platform/platform.js"></script>
<!-- Container --/>
<script src="cordova-2.3.0.js"></script>
<script src="dependencies/mobilesdk-shared/cordova.force.js"></script>
<!-- End Container -->
<!-- Local Testing -->
<script src="dependencies/mobilesdk-shared/MockCordova.js"></script>
<script src="dependencies/mobilesdk-shared/cordova.force.js"></script>
<script src="dependencies/mobilesdk-shared/MockSmartStore.js"></script>
<!-- End Local Testing -->
<!-- Place your HTML imports here -->
<link rel="import" href="elements/mobile-ui-elements.html">
<link rel="import" href="dependencies/polymer-ui-elements/polymer-ui-toggle-button/polymer-ui-toggle-button.html">
</head>
<body>
<polymer-element name="search-screen">
<template>
<header class="bar-title">
<ul class="segmented-controller">
<li class="active" id="tabAlbums"><a on-click="{{searchAlbums}}">Albums</a></li>
<li id="tabArtists"><a on-click="{{searchArtists}}">Artists</a></li>
<li id="tabTracks"><a on-click="{{searchTracks}}">Tracks</a></li>
</ul>
</header>
<div class="bar-standard bar-header-secondary">
<input type="search" class="search-key" value={{criteria}} on-keyup={{runSearch}}/>
</div>
<div class="content">
<ul id="listbody" class="list">
<template repeat="{{collection.models}}">
<li><a href="#detail/{{attributes.href}}">{{attributes.name}}</a></li>
</template>
</ul>
</div>
</template>
<script type="text/javascript">
Polymer('search-screen', {
itemType: 'album',
activeTab: '#tabAlbums',
criteria: '',
observe: {
itemType: 'setupCollection',
criteria: 'setupCollection',
'collection.config': 'runSearch'
},
switchTab: function(selectedTab) {
if (this.activeTab === selectedTab) {
return false;
}
$(this.activeTab).removeClass('active');
this.activeTab = selectedTab;
$(this.activeTab).addClass('active');
return true;
},
searchAlbums: function() {
if (this.switchTab('#tabAlbums')) {
this.itemType = 'album';
}
},
searchArtists: function() {
if (this.switchTab('#tabArtists')) {
this.itemType = 'artist';
}
},
searchTracks: function() {
if (this.switchTab('#tabTracks')) {
this.itemType = 'track';
}
},
setupCollection: function() {
this.collection = new app.models.MusicItemsCollection();
this.collection.cache = app.caches[this.itemType];
if (SFDC.isOnline()) {
this.collection.config = {itemType: this.itemType, type: 'search', query:this.criteria};
}
else {
this.collection.config = {itemType: this.itemType, type: 'cache', cacheQuery: {queryType:"like", indexPath:"name", likeKey: this.criteria + '%', order:"ascending", pageSize:100}};
}
},
runSearch: function() {
this.collection.fetch();
}
});
</script>
</polymer-element>
<polymer-element name="detail-screen" attributes="itemId itemType">
<template>
<header class="bar-title">
<a class="button-prev" on-click="{{goBack}}">Back</a>
<h1 style="font-size:14px" class="title">{{model.attributes.name}}</h1>
</header>
<div class="content">
<div>
<img height=320 width=320 src="{{model.attributes.thumbnail_url}}"></img>
</div>
<div flex>
<template if="{{itemType == 'album'}}">
<ul class="list">
<li class="list-divider">Artist</li>
<li><a href="#detail/{{model.attributes.artistId}}">{{model.attributes.artist}}</a></li>
<li class="list-divider">Tracks</li>
<template repeat="{{model.attributes.tracks}}">
<li><a href="#detail/{{href}}">{{name}}</a></li>
</template>
</ul>
</template>
<template if="{{itemType == 'artist'}}">
<ul class="list">
<li class="list-divider">Albums</li>
<template repeat="{{model.attributes.albums}}">
<li><a href="#detail/{{album.href}}">{{album.name}}</a></li>
</template>
</ul>
</template>
<template if="{{itemType == 'track'}}">
<ul class="list">
<li class="list-divider">Album</li>
<li><a href="#detail/{{model.attributes.album.href}}">{{model.attributes.album.name}}</a></li>
<li class="list-divider">Artists</li>
<template repeat="{{model.attributes.artists}}">
<li><a href="#detail/{{href}}">{{name}}</a></li>
</template>
</ul>
</template>
</div>
</div>
</template>
<script type="text/javascript">
Polymer('detail-screen', {
observe: {
itemId: 'fetch',
itemType: 'fetch'
},
goBack: function() {
app.router.navigateBack();
},
fetch: function() {
if (!this.model || this.model.id != this.itemId) {
this.model = new app.models.MusicItem();
this.model.cache = app.caches[this.itemType];
this.model.cacheMode = SFDC.isOnline() ? Force.CACHE_MODE.SERVER_FIRST : Force.CACHE_MODE.CACHE_ONLY;
this.model.id = this.itemId;
this.model.set('thumbnail_url', 'large_loading.gif');
this.model.fetch();
}
}
});
</script>
</polymer-element>
<polymer-element name="mobile-app">
<template>
<force-ui-app id="force_ui_app" multipage="true" accesstoken="{{accesstoken}}" instanceurl="{{instanceurl}}" hideheader="true">
<search-screen id="search" class="page absolute"></search-screen>
<detail-screen id="detail" class="page absolute"></detail-screen>
<detail-screen id="detail2" class="page absolute"></detail-screen>
</force-ui-app>
<polymer-ui-toggle-button id="onlineToggler" value="{{onlinestatus}}" onCaption="Online" offCaption="Offline" style="position: fixed; bottom: 0; right: 0; margin: 20px;"></polymer-ui-toggle-button>
</template>
<script type="text/javascript">
var app = {models: {}};
Polymer('mobile-app', {
_onlineStatus: true,
get onlinestatus() {
return this._onlineStatus;
},
set onlinestatus(newStatus) {
SFDC.isOnline = function() { return newStatus }; // XXX
this._onlineStatus = newStatus;
},
setupModels: function() {
var app = this;
app.models = {};
app.models.MusicItem = Force.RemoteObject.extend({
syncRemoteObjectWithServer: function(method, id) {
if (method != "read") throw "Method not supported " + method;
var itemType = id.split(":")[1];
var extras = (itemType == 'artist' ? 'album' : (itemType == 'album' ? 'track' : ''));
var url = 'http://ws.spotify.com/lookup/1/.json?uri=' + id + '&extras=' + extras;
var embedUrl = 'https://embed.spotify.com/oembed/?url=' + id;
return $.when($.ajax({url: url}), $.ajax({url:embedUrl})).then(function(response, embedResponse) {
var record = _.extend({thumbnail_url: embedResponse[0].thumbnail_url}, response[0][itemType]);
record.artistId = record['artist-id']; // XXX template binding fails because of '-'
return record;
});
}
});
app.models.MusicItemsCollection = Force.RemoteObjectCollection.extend({
model: app.models.MusicItem,
fetchRemoteObjectsFromServer: function(config) {
var url = 'http://ws.spotify.com/search/1/' + config.itemType +'.json?q=' + encodeURI(config.query);
return $.ajax({url: url})
.then(function(data) {
return {
totalSize: data.info.num_results,
records: data[config.itemType + 's'],
hasMore: data.info.limit < data.info.num_results,
getMore: function() { /* FIXME */}
};
});
}
});
},
setupCaches: function() {
var app = this;
app.caches = {};
if (navigator.smartstore) {
SFDC.launcher.then(function() {
var albumCache = new Force.StoreCache("albums", [{path:"name", type:"string"}], "href");
var artistCache = new Force.StoreCache("artists", [{path:"name", type:"string"}], "href");
var trackCache = new Force.StoreCache("tracks", [{path:"name", type:"string"}], "href");
$.when(albumCache.init(), artistCache.init(), trackCache.init()).then(function() {
app.caches.album = albumCache;
app.caches.artist = artistCache;
app.caches.track = trackCache;
});
});
}
},
ready: function() {
var app = this;
this.setupModels();
this.setupCaches();
app.router = app.$.force_ui_app.setupRouter({
routes: {
"": "search",
"search": "search",
"detail/:itemId": "detail",
},
search: function() {
app.router.gotoPageId('#search');
},
detail: function(itemId) {
var onDetailAlready = app.$.force_ui_app.activepage && app.$.force_ui_app.activepage.id === 'detail';
var target = onDetailAlready ? app.$.detail2 : app.$.detail;
var targetId = onDetailAlready ? '#detail2' : '#detail';
target.itemType = itemId.split(":")[1];
target.itemId = itemId;
app.router.gotoPageId(targetId);
}
});
Backbone.history.start();
}
});
</script>
</polymer-element>
<mobile-app></mobile-app>
<script type="text/javascript">
window.addEventListener('WebComponentsReady', function() {
app = $("mobile-app")[0];
// Container
if (cordova.callbackId) document.addEventListener("deviceready", SFDC.launch, false)
// Local Testing
else SFDC.launch();
});
</script>
</body>
</html>