(hoofbeats) Migrates to Tizen 2.0 'content' webapi.
[profile/ivi/cowhide.git] / examples / hoofbeats / javascripts / library.js
1 /* vi: set et sw=4 ts=4 si: */
2 $(function() {
3     var library = function() {
4         // For readability:
5         this.initialized = false;
6
7         this.fetchCount = 100;
8         this.fetchOffset = 0;
9         this.mediaItems = [];
10
11         this.initialize = function() {
12             if (this.initialized)
13                 return;
14
15             if (window.tizen === undefined) {
16                 throw Error("You need the Tizen web API  to run Hoofbeats.");
17             }
18
19             this.audioTypeFilter = new tizen.AttributeFilter(
20                 "type", "EXACTLY", "AUDIO");
21             this.videoTypeFilter = new tizen.AttributeFilter(
22                 "type", "EXACTLY", "VIDEO");
23             this.typeFilter = new tizen.CompositeFilter(
24                 "UNION", [this.audioTypeFilter, this.videoTypeFilter]);
25
26             this.sortMode = new tizen.SortMode("trackNumber", "ASC");
27         };
28
29         this.scan = function() {
30             this.initialize()
31
32             this.mediaItems = [];
33             this.fetchOffset = 0;
34
35             tizen.content.find(
36                 this.findCB.bind(this),
37                 this.errorCB.bind(this),
38                 null,
39                 this.typeFilter,
40                 this.sortMode,
41                 this.fetchCount,
42                 this.fetchOffset);
43         };
44
45         this.errorCB = function(error) {
46             console.log("Error: " + error.name);
47             throw new Error(error.name);
48         };
49
50         this.findCB = function(items) {
51             var self = this;
52
53             items.forEach(function(item, index, items) {
54                 self.mediaItems.push(item);
55                 window.MusicBrainz.getArtist(item.name).done(function(data) {
56                     console.log(data);
57                 });
58             });
59
60             if (items.length == this.fetchCount) {
61                 // There *might* be more items.
62                 this.fetchOffset += this.fetchCount;
63                 tizen.content.find(
64                     this.findCB.bind(this),
65                     this.errorCB.bind(this),
66                     null,
67                     this.typeFilter,
68                     this.sortMode,
69                     this.fetchCount,
70                     this.fetchOffset);
71             }
72         };
73     };
74
75     library.prototype = {
76         set initialized(value) {this._initialized = value; },
77         get initialized() { return this._initialized; }
78     };
79
80     window.HoofbeatsLibrary = library;
81 });