blob: 57eaff77cb75434cc2d9c2576336dd3bb0bfc4ea [file] [log] [blame]
The Android Open Source Project88b60792009-03-03 19:28:42 -08001/* file: carousel.js
2 date: oct 2008
3 author: jeremydw,smain
4 info: operates the carousel widget for announcements on
5 the android developers home page. modified from the
6 original market.js from jeremydw. */
7
8/* -- video switcher -- */
9
10var oldVid = "multi"; // set the default video
11var nowPlayingString = "Now playing:";
12var assetsRoot = "assets/";
13
14
15/* -- app thumbnail switcher -- */
16
17var currentDroid;
18var oldDroid;
19
20// shows a random application
21function randomDroid(){
22
23 // count the total number of apps
24 var droidListLength = 0;
25 for (var k in droidList)
26 droidListLength++;
27
28 // pick a random app and show it
29 var j = 0;
30 var i = Math.floor(droidListLength*Math.random());
31 for (var x in droidList) {
32 if(j++ == i){
33 currentDroid = x;
34 showPreview(x);
35 centerSlide(x);
36 }
37 }
38
39}
40
41// shows a bulletin, swaps the carousel highlighting
42function droid(appName){
43
44 oldDroid = $("#droidlink-"+currentDroid);
45 currentDroid = appName;
46
47 var droid = droidList[appName];
Scott Main5db40c62009-07-21 10:52:11 -070048
49 $("#"+appName).show().siblings().hide();
The Android Open Source Project88b60792009-03-03 19:28:42 -080050
51 if(oldDroid)
52 oldDroid.removeClass("selected");
53
54 $("#droidlink-"+appName).addClass("selected");
55}
56
57
58// -- * build the carousel based on the droidList * -- //
59function buildCarousel() {
60 var appList = document.getElementById("app-list");
61 for (var x in droidList) {
62 var droid = droidList[x];
63 var icon = droid.icon;
64 var name = droid.name;
65 var a = document.createElement("a");
66 var img = document.createElement("img");
67 var br = document.createElement("br");
68 var span = document.createElement("span");
69 var text = document.createTextNode(droid.name);
70
71 a.setAttribute("id", "droidlink-" + x);
72 a.className = x;
73 a.setAttribute("href", "#");
74 a.onclick = function() { showPreview(this.className); return false; }
75 img.setAttribute("src", toRoot + assetsRoot + "images/home/" + droid.icon);
76 img.setAttribute("alt", "");
77
78 span.appendChild(text);
79 a.appendChild(img);
80 a.appendChild(br);
81 a.appendChild(span);
82 appList.appendChild(a);
Scott Main5db40c62009-07-21 10:52:11 -070083
84
85 /* add the bulletins */
86 var layout = droid.layout;
87 var div = document.createElement("div");
88 var imgDiv = document.createElement("div");
89 var descDiv = document.createElement("div");
90
91 div.setAttribute("id", x);
92 div.setAttribute("style", "display:none");
93 imgDiv.setAttribute("class", "bulletinImg");
94 descDiv.setAttribute("class", "bulletinDesc");
95
96 if (layout == "imgLeft") {
97 $(imgDiv).addClass("img-left");
98 $(descDiv).addClass("desc-right");
99 } else if (layout == "imgTop") {
100 $(imgDiv).addClass("img-top");
101 $(descDiv).addClass("desc-bottom");
102 } else if (layout == "imgRight") {
103 $(imgDiv).addClass("img-right");
104 $(descDiv).addClass("desc-left");
105 }
106
107 imgDiv.innerHTML = "<img src='" + toRoot + assetsRoot + "images/home/" + droid.img + "'>";
108 descDiv.innerHTML = (droid.title != "") ? "<h3>" + droid.title + "</h3>" + droid.desc : droid.desc;
109 $(div).append(imgDiv);
110 $(div).append(descDiv);
111
112 $("#carouselMain").append(div);
113
The Android Open Source Project88b60792009-03-03 19:28:42 -0800114 }
Scott Main5db40c62009-07-21 10:52:11 -0700115
116
117
The Android Open Source Project88b60792009-03-03 19:28:42 -0800118}
119
120// -- * slider * -- //
121
122// -- dependencies:
123// (1) div containing slides, (2) a "clip" div to hide the scroller
124// (3) control arrows
125
126// -- * config below * -- //
127
128var slideCode = droidList; // the dictionary of slides
129var slideList = 'app-list'; // the div containing the slides
130var arrowRight = 'arrow-right'; // the right control arrow
131var arrowLeft = 'arrow-left'; // the left control arrow
132
133
134function showPreview(slideName) {
135 centerSlide(slideName);
136 if (slideName.indexOf('selected') != -1) {
137 return false;
138 }
139 droid(slideName); // do this function when slide is clicked
140}
141
142var thumblist = document.getElementById(slideList);// the div containing the slides
143
144var slideWidth = 144; // width of a slide including all margins, etc.
145var slidesAtOnce = 3; // no. of slides to appear at once (requires odd number to have a centered slide)
146
147// -- * no editing should be needed below * -- //
148
149var originPosition = {};
150var is_animating = 0;
151var currentStripPosition = 0;
152var centeringPoint = 0;
153var rightScrollLimit = 0;
154
155// makeSlideStrip()
156// - figures out how many slides there are
157// - determines the centering point of the slide strip
158function makeSlideStrip() {
159 var slideTotal = 0;
160 centeringPoint = Math.ceil(slidesAtOnce/2);
161 for (var x in slideCode) {
162 slideTotal++;
163 }
164 var i = 0;
165 for (var code in slideCode) {
166 if (i <= centeringPoint-1) {
167 originPosition[code] = 0;
168 } else {
169 if (i >= slideTotal-centeringPoint+1) {
170 originPosition[code] = (slideTotal-slidesAtOnce)*slideWidth;
171 } else {
172 originPosition[code] = (i-centeringPoint+1)*slideWidth;
173 }
174 }
175 i++;
176 }
177 rightScrollLimit = -1*(slideTotal-slidesAtOnce)*slideWidth;
178}
179
180// slides with acceleration
181function slide(goal, id, go_left, cp) {
182 var div = document.getElementById(id);
183 var animation = {};
184 animation.time = 0.5; // in seconds
185 animation.fps = 60;
186 animation.goal = goal;
187 origin = 0.0;
188 animation.origin = Math.abs(origin);
189 animation.frames = (animation.time * animation.fps) - 1.0;
190 var current_frame = 0;
191 var motions = Math.abs(animation.goal - animation.origin);
192 function animate() {
193 var ease_right = function (t) { return (1 - Math.cos(t * Math.PI))/2.0; };
194 var ease = ease_right;
195 if (go_left == 1) {
196 ease = function(t) { return 1.0 - ease_right(t); };
197 }
198 var left = (ease(current_frame/animation.frames) * Math.abs(animation.goal - animation.origin)) - cp;
199 if(left < 0) {
200 left = 0;
201 }
202 if(!isNaN(left)) {
203 div.style.left = '-' + Math.round(left) + 'px';
204 }
205 current_frame += 1;
206 if (current_frame == animation.frames) {
207 is_animating = 0;
208 window.clearInterval(timeoutId)
209 }
210 }
211 var timeoutId = window.setInterval(animate, animation.time/animation.fps * 1000);
212}
213
214//Get style property
215function getStyle(element, cssProperty){
216 var elem = document.getElementById(element);
217 if(elem.currentStyle){
218 return elem.currentStyle[cssProperty]; //IE
219 } else{
220 var style = document.defaultView.getComputedStyle(elem, null); //firefox, Opera
221 return style.getPropertyValue(cssProperty);
222 }
223}
224
225// Left and right arrows
226function page_left() {
227 var amount = slideWidth;
228 animateSlide(amount, 'left');
229}
230
231function page_right() {
232 var amount = slideWidth;
233 animateSlide(amount, 'right');
234}
235
236
237// animates the strip
238// - sets arrows to on or off
239function animateSlide(amount,dir) {
240 var currentStripPosition = parseInt(getStyle(slideList,'left'));
241 var motionDistance;
242 if (amount == slideWidth ) {
243 motionDistance = slideWidth;
244 } else {
245 motionDistance = amount;
246 }
247
248 var rightarrow = document.getElementById(arrowRight);
249 var leftarrow = document.getElementById(arrowLeft);
250
251 function aToggle(state,aDir) {
252 if (state == 'on') {
253 if (aDir =='right') {
254 rightarrow.className = 'arrow-right-on';
255 rightarrow.href = "javascript:page_right()";
256 } else {
257 leftarrow.className = 'arrow-left-on';
258 leftarrow.href = "javascript:page_left()";
259 }
260 } else {
261 if (aDir =='right') {
262 rightarrow.href = "javascript:{}";
263 rightarrow.className = 'arrow-right-off';
264 } else {
265 leftarrow.href = "javascript:{}";
266 leftarrow.className = 'arrow-left-off';
267 }
268 }
269 }
270
271 function arrowChange(rP) {
272 if (rP >= rightScrollLimit) {
273 aToggle('on','right');
274 }
275 if (rP <= rightScrollLimit) {
276 aToggle('off','right');
277 }
278 if (rP <= slideWidth) {
279 aToggle('on','left');
280 }
281 if (rP >= 0) {
282 aToggle('off','left');
283 }
284 }
285
286 if (dir == 'right' && is_animating == 0) {
287 arrowChange(currentStripPosition-motionDistance);
288 is_animating = 1;
289 slide(motionDistance, slideList, 0, currentStripPosition);
290 } else if (dir == 'left' && is_animating == 0) {
291 arrowChange(currentStripPosition+motionDistance);
292 is_animating = 1;
293 rightStripPosition = currentStripPosition + motionDistance;
294 slide(motionDistance, slideList, 1, rightStripPosition);
295 }
296}
297
298function centerSlide(slideName) {
299 var currentStripPosition = parseInt(getStyle(slideList,'left'));
300 var dir = 'left';
301 var originpoint = Math.abs(currentStripPosition);
302 if (originpoint <= originPosition[slideName]) {
303 dir = 'right';
304 }
305 var motionValue = Math.abs(originPosition[slideName]-originpoint);
306 animateSlide(motionValue,dir);
307}
308
309
310function initCarousel(def) {
311 buildCarousel();
312 showPreview(def);
313 makeSlideStrip();
314}