Initial package
[profile/ivi/sdk/web-sample-build.git] / samples / web / Sample / Tizen / Web App / MediaPlayer / project / js / player.js
1 /*
2  * Copyright (c) 2013, Intel Corporation.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 MediaPlayer = function(type)
11 {
12         this.type = type;
13         this.content = new Array();
14         this.mediaList = $("#" + type + "MediaList");
15         this.mediaListItems = $("#" + type + "MediaListItems");
16         this.playerControls = undefined;
17         this.mediaListLoaded = false;
18         this.listIndex = 0;
19         this.imagesLoaded = false;
20         this.loadAndPlay = false;
21         this.loadPrev = false;
22         this.currentFileLoaded = false;
23         this.fillingList = false;
24         this.clearDrawTimeouts = new Array();
25
26         switch(type)
27         {
28                 case "audio":
29                         console.log("MediaPlayer creating AudioPlayer");
30                         audioPlayer = new AudioPlayer;
31                         $.extend(this, audioPlayer);
32                         this.playerControls = document.getElementById("audioPlayer");
33                 break;
34                 case "video":
35                         console.log("MediaPlayer creating VideoPlayer");
36                         videoPlayer = new VideoPlayer;
37                         $.extend(this, videoPlayer);
38                         this.playerControls = document.getElementById("videoPlayer");
39                 break;
40                 case "image":
41                         console.log("MediaPlayer creating ImagePlayer");
42                         imagePlayer = new ImagePlayer;
43                         $.extend(this, imagePlayer);
44                         this.playerControls = new ImageControls();
45                 break;
46                 default:
47                         console.log("MediaPlayer: Trying to make an invalid player type " + type);
48                 break;
49         }
50 }
51
52 MediaPlayer.prototype.updateContent = function(newContent, append)
53 {
54         console.log("MediaPlayer: updating content for " + this.type);
55         if (newContent && newContent !== undefined && newContent.length > 0)
56         {
57                 if (append)
58                 {
59                         this.content = this.content.concat(newContent);
60                 }
61                 else
62                 {
63                         this.content.length = 0;
64                         this.content = newContent;
65                 }
66         }
67         else
68                 console.log("MediaPlayer: invalid content update for " + this.type);
69 }
70
71 MediaPlayer.prototype.getContent = function()
72 {
73         return this.content;
74 }
75
76 MediaPlayer.prototype.contentLength = function()
77 {
78         return this.content.length;
79 }
80
81 MediaPlayer.prototype.show = function()
82 {
83         $("#" + this.type + "Player").show();
84 }
85
86 MediaPlayer.prototype.clearContent = function()
87 {
88         console.log("MediaPlayer: clearing content for " + this.type);
89         this.emptyTimeouts();
90         this.content.length = 0;
91 }
92
93 MediaPlayer.prototype.currentIndex = function()
94 {
95         return this.listIndex;
96 }
97
98 MediaPlayer.prototype.emptyTimeouts = function()
99 {
100     var clearItem;
101
102     while (clearItem = this.clearDrawTimeouts.pop())
103     {
104                 clearTimeout(clearItem);
105     }
106 }
107
108 MediaPlayer.prototype.makeListBar = function(icon, i, artistTextObj, trackTextObj)
109 {
110         var lightColor = "";
111         var canvasW = mediaListItemW;
112         var canvasH = mediaListItemH * 0.95 ;
113
114         if ((i+1)%2 !== 0)
115         {
116                 lightColor = "lightColor ";
117         }
118
119         if (nightMode)
120         {
121                 this.mediaListItems.append("<li id=" + this.type + "ListItem" + i + " style='width:" + mediaListItemW + "px;" + " height:" +  mediaListItemH + "px;" +
122                         " margin-bottom: 10px' ><a href='#' class='" + lightColor + "night' >" +
123                         "<canvas id=" + this.type + "CanvasNum" + i + " width=" + canvasW + " height=" + canvasH + "> </canvas>" +
124                         "</a></li>"
125                 );
126          }
127         else
128         {
129                 this.mediaListItems.append("<li id=" + this.type + "ListItem" + i + " style='width:" + mediaListItemW + "px;" + " height:" +  mediaListItemH + "px;" +
130                         " margin-bottom: 10px' ><a href='#' class='" + lightColor + "night' >" +
131                         "<canvas id=" + this.type + "CanvasNum" + i + " width=" + canvasW + " height=" + canvasH + "> </canvas>" +
132                         "</a></li>"
133                 );
134         }
135
136         try
137         {
138                 var currentCanvas = document.getElementById(this.type + "CanvasNum" + i);
139                 var currentCTX = currentCanvas.getContext("2d");
140
141                 this.content[i].ctx = currentCTX;
142
143                 currentCTX.drawImage(icon, 0, 0, canvasH, canvasH );
144                 var artistText = new TextObject(currentCTX,artistTextObj);
145                 var trackText = new TextObject(currentCTX,trackTextObj);
146                 var mediaTextTemp1 = screenOrientation === "portrait" ? mediaTextTemplate2 : mediaTextTemplate2Landscape;
147                 var mediaTextTemp2 = screenOrientation === "portrait" ? mediaTextTemplate3 : mediaTextTemplate3Landscape;
148                 var trackTextTemp = screenOrientation === "portrait" ? trackTextTemplate : trackTextTemplateLandscape;
149
150                 trackText.applyTemplate(mediaTextTemp1);
151                 trackText.drawObj();
152                 trackText.applyTemplate(mediaTextTemp2);
153                 trackText.drawObj();
154                 artistText.applyTemplate(trackTextTemp);
155                 artistText.drawObj();
156         }
157         catch(err)
158         {
159             console.log("MediaPlayer: drawImage failed for " + this.type + " - reason -> " + err);
160         }
161 }
162
163 MediaPlayer.prototype.fillMediaList = function()
164 {
165     console.log("MediaPlayer in fillMediaList for content = " + this.type);
166         this.fillingList = true;
167
168     //Don't try and fill an empty content list
169     if (this.content === undefined || this.content === null || this.content.length <=0)
170                 return false;
171
172         this.emptyTimeouts();
173
174     this.mediaListItems.empty();
175     var timeOut = 1;
176
177     switch (this.type)
178     {
179                 case "audio":
180                     audioMediaListLoaded = true;
181                     break;
182                 case "video":
183                     videoMediaListLoaded = true;
184                     break;
185                 case "image":
186                     imageMediaListLoaded = true;
187                     break;
188                 default:
189                     break;
190     }
191
192     var jumpSize = 1;
193     var tmpClearTimeout;
194
195     for (var i=0; i < this.content.length; i++)
196     {
197                 tmpClearTimeout = setTimeout(this.makeListItem.bind(this, i, jumpSize), timeOut);
198                 this.clearDrawTimeouts.push(tmpClearTimeout);
199
200                 timeOut += 50;
201                 i+=jumpSize - 1;
202     }
203 }