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