(hoofbeats) Reorg library code.
[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         // Private stuff
5
6         var p = {
7             initialized: false,
8             deferred: undefined,
9             // Whether items should be resolved on MusicBrainz
10             resolve: false,
11             fetchCount: 100,
12             fetchOffset: 0,
13             oneTime: false,
14             mediaItems: [],
15
16             errorCB: function(error) {
17                 console.log("HoofbeatsLibrary.errorCB: " + error.name);
18                 this.deferred.reject();
19                 throw new Error(error.name);
20             },
21
22             findCB: function(items) {
23                 items.forEach(function(item, index, items) {
24                     p.mediaItems.push(item);
25                     if (p.resolve) {
26                         win.MusicBrainz.getArtist(item.artists[0]).done(
27                             function(data) {
28                                 console.log(
29                                     "HoofbeatsLibrary.findCB: " +
30                                     "item resolved on MusicBrainz: " +
31                                     data.name);
32                             }
33                         );
34                     }
35                 });
36
37                 if (items.length == p.fetchCount && !p.oneTime) {
38                     // There *might* be more items.
39                     p.fetchOffset += p.fetchCount;
40                     tizen.content.find(
41                         p.findCB.bind(this),
42                         p.errorCB.bind(this),
43                         null,
44                         p.typeFilter,
45                         null,
46                         p.fetchCount,
47                         p.fetchOffset);
48                 } else {
49                     p.deferred.resolve();
50                 }
51             }
52         }; // p
53
54
55         // Public stuff
56
57         return {
58             initialize: function() {
59                 if (p.initialized)
60                     return;
61
62                 if (win.tizen === undefined) {
63                     throw Error("You need the Tizen web API  to run Hoofbeats.");
64                 }
65
66                 p.audioTypeFilter = new tizen.AttributeFilter(
67                     "type", "EXACTLY", "AUDIO");
68                 p.videoTypeFilter = new tizen.AttributeFilter(
69                     "type", "EXACTLY", "VIDEO");
70                 p.typeFilter = new tizen.CompositeFilter(
71                     "UNION", [p.audioTypeFilter, p.videoTypeFilter]);
72             },
73
74             scan: function(options) {
75                 var opts = options || {};
76                 if (opts.resolve !== undefined) {
77                     p.resolve = opts.resolve;
78                 }
79
80                 p.deferred = new $.Deferred();
81                 this.initialize();
82
83                 p.mediaItems = [];
84                 p.fetchOffset = 0;
85
86                 if (opts.count !== undefined) {
87                     p.fetchCount = opts.count;
88                     p.oneTime = true;
89                 }
90
91                 tizen.content.find(
92                     p.findCB.bind(this),
93                     p.errorCB.bind(this),
94                     null,
95                     p.typeFilter,
96                     null,
97                     p.fetchCount,
98                     p.fetchOffset);
99
100                 return p.deferred.promise();
101             },
102
103             item: function(id) {
104                 var ret;
105
106                 p.mediaItems.forEach(function(item, index, items) {
107                     if (item.id == id)
108                         ret = item;
109                 });
110
111                 return ret;
112             },
113
114             // Some simple getters
115             getInitialized: function() { return p.initialized; },
116             getItems: function() { return p.mediaItems; },
117             getSize: function() { return p.mediaItems.length; },
118             getResolve: function() {return p.resolve; }
119         };
120     };
121
122     win.HoofbeatsLibrary = library;
123 }(window, jQuery));