Update Modello web samples from upstream; version up
[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 = (i+1)%2 !== 0 ? "lightColor " : "";
111         var nightColor = nightMode ? "night" : "";
112         var canvasW = Math.floor(mediaListItemW);
113         var canvasH = Math.floor(mediaListItemH * 0.95) ;
114
115         this.mediaListItems.append("<li id=" + this.type + "ListItem" + i + " style='width:" + mediaListItemW + "px;" + " height:" +  mediaListItemH + "px;" +
116                 " margin-bottom: 10px' ><a href='#' class='" + lightColor + nightColor + "' >" +
117                 "<canvas id=" + this.type + "CanvasNum" + i + " width=" + canvasW + " height=" + canvasH + "> </canvas>" +
118                 "</a></li>"
119         );
120
121         try
122         {
123                 var currentCanvas = document.getElementById(this.type + "CanvasNum" + i);
124                 var currentCTX = currentCanvas.getContext("2d");
125
126                 if (currentCanvas && currentCTX)
127                 {
128                         this.content[i].ctx = currentCTX;
129
130                         currentCTX.drawImage(icon, 0, 0, canvasH, canvasH );
131                         var artistText = new TextObject(currentCTX,artistTextObj);
132                         var trackText = new TextObject(currentCTX,trackTextObj);
133                         var trackTextTemp1 = screenOrientation === "portrait" ? mediaTextTemplate2 : mediaTextTemplate2Landscape;
134                         var trackTextTemp2 = screenOrientation === "portrait" ? mediaTextTemplate3 : mediaTextTemplate3Landscape;
135                         var artistTextTemp = screenOrientation === "portrait" ? trackTextTemplate : trackTextTemplateLandscape;
136
137                         trackText.applyTemplate(trackTextTemp1);
138                         trackText.drawObj();
139                         trackText.applyTemplate(trackTextTemp2);
140                         trackText.drawObj();
141                         artistText.applyTemplate(artistTextTemp);
142                         artistText.drawObj();
143                 }
144                 else
145                         console.log("MediaPlayer: Failed to draw media item for index " + i );
146         }
147         catch(err)
148         {
149             console.log("MediaPlayer: drawImage failed for " + this.type + " - reason -> " + err);
150             console.log("Source below");
151             console.log("Src = " + icon.src);
152         }
153 }
154
155 MediaPlayer.prototype.fillMediaList = function()
156 {
157     console.log("MediaPlayer in fillMediaList for content = " + this.type);
158         this.fillingList = true;
159
160     //Don't try and fill an empty content list
161     if (this.content === undefined || this.content === null || this.content.length <=0)
162                 return false;
163
164         this.emptyTimeouts();
165
166     this.mediaListItems.empty();
167     var timeOut = 1;
168
169     switch (this.type)
170     {
171                 case "audio":
172                     audioMediaListLoaded = true;
173                     break;
174                 case "video":
175                     videoMediaListLoaded = true;
176                     break;
177                 case "image":
178                     imageMediaListLoaded = true;
179                     break;
180                 default:
181                     break;
182     }
183
184     var jumpSize = 1;
185     var tmpClearTimeout;
186
187     for (var i=0; i < this.content.length; i++)
188     {
189                 tmpClearTimeout = setTimeout(this.makeListItem.bind(this, i, jumpSize), timeOut);
190                 this.clearDrawTimeouts.push(tmpClearTimeout);
191
192                 timeOut += 50;
193                 i+=jumpSize - 1;
194     }
195 }