Tizen 2.1 base
[samples/web/Mediacontent.git] / js / main.js
1 /*
2  *      Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  *      Licensed under the Flora License, Version 1.0 (the "License");
5  *      you may not use this file except in compliance with the License.
6  *      You may obtain a copy of the License at
7  *
8  *              http://www.tizenopensource.org/license
9  *
10  *      Unless required by applicable law or agreed to in writing, software
11  *      distributed under the License is distributed on an "AS IS" BASIS,
12  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *      See the License for the specific language governing permissions and
14  *      limitations under the License.
15  */
16
17 var gMediaSource, gMediaFolders, gMediaItems, gItemId, gFolderId, gMediaType = "ALL";
18
19 $(document).delegate("#main", "pageinit", function() {
20         $("#main .ui-btn-back").bind("vclick", function() {
21                 tizen.application.getCurrentApplication().exit();
22                 return false;
23         });
24
25         $("#folder-list input").bind("change", function() {
26                 if ($(this).prop("checked") == true) {
27                         showFolderList($(this).attr("value"));
28                 }
29         });
30
31         $("#folder-list").delegate("li", "vclick", function() {
32                 var id = $(this).data("id");
33                 if (id != null) {
34                         getFolderItems(Number(id), gMediaType);
35                 }
36                 return false;
37         });
38
39         $("#all-items").bind("vclick", function() {
40                 getFolderItems(null, gMediaType);
41                 return false;
42         });
43
44         try {
45                 gMediaSource = tizen.content;
46         } catch (exc) {
47                 alert("getLocalMediaSource exc:" + exc.message);
48         }
49
50         getFolders();
51 });
52
53 $(document).delegate("#items", "pageinit", function() {
54         $("#items").bind("pagebeforeshow", showItemList);
55
56         $("#items-list input").bind("change", function() {
57                 if ($(this).prop("checked") == true) {
58                         gMediaType = $(this).attr("value");
59                         getFolderItems(gFolderId, gMediaType);
60                 }
61         });
62
63         $("#items-list").delegate("li", "vclick", function() {
64                 var id = $(this).data("id");
65                 if (id != null) {
66                         gItemId = Number(id);
67                         $.mobile.changePage("#detail");
68                 }
69                 return false;
70         });
71
72         $("#batch-update").bind("vclick", function() {
73                 updateLast3Ratings();
74                 return false;
75         });
76 });
77
78 $(document).delegate("#detail", "pageinit", function() {
79         $("#detail").bind("pagebeforeshow", showMediaDetail);
80
81         $("#update").bind("vclick", function() {
82                 updateRating();
83                 window.history.back();
84                 return false;
85         });
86 });
87
88 function getFolders() {
89         function onGetFoldersSuccess(folders) {
90                 gMediaFolders = folders;
91                 showFolderList("ALL");
92         }
93
94         function onGetFoldersError(err) {
95                 alert("GetFolders failed:" + err.message);
96         }
97
98         try {
99                 gMediaSource.getDirectories(onGetFoldersSuccess, onGetFoldersError);
100         } catch (exc) {
101                 alert("getFolders exc:" + exc.message);
102         }
103 }
104
105 function makeListItem(id, mainText, subText) {
106         return '<li class="ui-li-has-multiline ui-li-text-ellipsis"'
107                         + (id == null ? '' : ' data-id="'+ id +'"')
108                         + '>'
109                         + mainText
110                         + '<span class="ui-li-text-sub">'
111                         + subText
112                         + '</span></li>';
113 }
114
115 function showFolderList(storageType) {
116         var str = '';
117         for (var i = 0; i < gMediaFolders.length; i++) {
118                 if (storageType == "ALL" || storageType == gMediaFolders[i].storageType) {
119                         str += makeListItem(i, gMediaFolders[i].title, gMediaFolders[i].folderURI);
120                 }
121         }
122         $("#folder-list>li[data-id]").remove();
123         $("#folder-list").append(str).trigger("create").listview("refresh");
124 }
125
126
127
128 function showItemList() {
129         var str = '';
130
131         $("#items-title").html(gFolderId == null ? "All" : gMediaFolders[gFolderId].title);
132
133         for (var i = 0; i < gMediaItems.length; i++) {
134                 str += makeListItem(i, gMediaItems[i].title,
135                                 gMediaItems[i].type + "  Rating: " + gMediaItems[i].rating);
136         }
137
138         $("#items-list>li[data-id]").remove();
139         $("#items-list").append(str).trigger("create").listview("refresh");
140 }
141
142 function getFolderItems(folderId, mediaType) {
143         function onFindItemsSuccess(items) {
144                 gMediaItems = items;
145                 gFolderId = folderId;
146                 gMediaType = mediaType;
147
148                 if($.mobile.activePage.attr("id") == "items") {
149                         showItemList();
150                 } else {
151                         $.mobile.changePage("#items");
152                 }
153         }
154
155         function onFindItemsError(err) {
156                 alert("getFolderItems findItems failed: " + err.message);
157         }
158
159         try {
160                 gMediaSource.find(onFindItemsSuccess, onFindItemsError,
161                                 folderId == null ? null : gMediaFolders[folderId].id,
162                                 mediaType == "ALL" ? null : new tizen.AttributeFilter("type", "EXACTLY", mediaType),
163                                 new tizen.SortMode("title", "ASC"));
164         } catch (exc) {
165                 //alert("findItems exc:" + exc.message);
166         }
167 }
168
169 function makeCommonPropertiesListItems(item) {
170         return makeListItem(null, "Title", item.title)
171                         + makeListItem(null, "Size", item.size + " Bytes")
172                         + makeListItem(null, "Type", item.type)
173                         + makeListItem(null, "Identifier", item.id)
174                         + makeListItem(null, "Item URI", item.contentURI)
175                         + makeListItem(null, "MIME type", item.mimeType)
176                         + makeListItem(null, "Release date", item.releaseDate)
177                         + makeListItem(null, "Modified date", item.modifiedDate)
178                         + makeListItem(null, "Description", item.description)
179                         + makeListItem(null, "Rating", item.rating)
180                         + makeListItem(null, "Thumbnail URI", item.thumbnailURIs[0]);
181 }
182
183 function showMediaDetail() {
184         var str, item = gMediaItems[gItemId];
185
186         if (item.type == "IMAGE") {
187                 str = makeCommonPropertiesListItems(item)
188                         + makeListItem(null, "Width", item.width)
189                         + makeListItem(null, "Height", item.height)
190                         + makeListItem(null, "Latitude", item.geolocation.latitude)
191                         + makeListItem(null, "Longitude", item.geolocation.longitude);
192         } else if (item.type == "VIDEO") {
193                 str = makeCommonPropertiesListItems(item)
194                         + makeListItem(null, "Width", item.width)
195                         + makeListItem(null, "Height", item.height)
196                         + makeListItem(null, "Album", item.album)
197                         + makeListItem(null, "Artist", item.artists[0])
198                         + makeListItem(null, "Duration", item.duration + " Milliseconds");
199         } else if (item.type == "AUDIO") {
200                 str = makeCommonPropertiesListItems(item)
201                         + makeListItem(null, "Album", item.album)
202                         + makeListItem(null, "Artist", item.artists[0])
203                         + makeListItem(null, "Composer", item.composers[0])
204                         + makeListItem(null, "Genre", item.genres[0])
205                         + makeListItem(null, "Copyright", item.copyright)
206                         + makeListItem(null, "Bitrate", item.bitrate + " Bps")
207                         + makeListItem(null, "Track number", item.trackNumber)
208                         + makeListItem(null, "Duration", item.duration + " Milliseconds");
209         } else {
210                 alert("Invalid type");
211         }
212
213         $("#media-details").html(str).trigger("create").listview("refresh");
214 }
215
216 function updateRating() {
217         var item = gMediaItems[gItemId];
218
219         item.rating += 1; /* increase by 1 */
220         try {
221                 gMediaSource.update(item);
222         } catch (exc) {
223                 alert("updateItem exc: " + exc.message);
224         }
225 }
226
227 function updateLast3Ratings() {
228         function onSuccess() {
229                 getFolderItems(gFolderId, gMediaType);
230         }
231
232         function onError(err) {
233                 alert("updateItemsBatch failed:" + err.message);
234         }
235
236         var itemArray = [];
237
238         for (var i = gMediaItems.length - 1, j = 0; i >= 0 && j < 3; i--, j++) {
239                 gMediaItems[i].rating += 1;
240                 itemArray.push(gMediaItems[i]);
241         }
242
243         try {
244                 gMediaSource.updateBatch(itemArray, onSuccess, onError);
245         } catch (exc) {
246                 alert("updateItemsBatch exc:" + exc.message);
247         }
248 }