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