blob: 4eebd89117668782699dab4f7556910d044f8a97 [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];
48 var layout = droid.layout;
49 var imgDiv = document.getElementById("bulletinImg");
50 var descDiv = document.getElementById("bulletinDesc");
51
52 if (layout == "imgLeft") {
53 imgDiv.className = "img-left";
54 descDiv.className = "desc-right";
55 } else if (layout == "imgTop") {
56 imgDiv.className = "img-top";
57 descDiv.className = "desc-bottom";
58 } else if (layout == "imgRight") {
59 imgDiv.className = "img-right";
60 descDiv.className = "desc-left";
61 }
62
63 imgDiv.innerHTML = "<img src='" + toRoot + assetsRoot + "images/home/" + droid.img + "'>";
64 descDiv.innerHTML = (droid.title != "") ? "<h3>" + droid.title + "</h3>" + droid.desc : droid.desc;
65
66 if(oldDroid)
67 oldDroid.removeClass("selected");
68
69 $("#droidlink-"+appName).addClass("selected");
70}
71
72
73// -- * build the carousel based on the droidList * -- //
74function buildCarousel() {
75 var appList = document.getElementById("app-list");
76 for (var x in droidList) {
77 var droid = droidList[x];
78 var icon = droid.icon;
79 var name = droid.name;
80 var a = document.createElement("a");
81 var img = document.createElement("img");
82 var br = document.createElement("br");
83 var span = document.createElement("span");
84 var text = document.createTextNode(droid.name);
85
86 a.setAttribute("id", "droidlink-" + x);
87 a.className = x;
88 a.setAttribute("href", "#");
89 a.onclick = function() { showPreview(this.className); return false; }
90 img.setAttribute("src", toRoot + assetsRoot + "images/home/" + droid.icon);
91 img.setAttribute("alt", "");
92
93 span.appendChild(text);
94 a.appendChild(img);
95 a.appendChild(br);
96 a.appendChild(span);
97 appList.appendChild(a);
98 }
99}
100
101// -- * slider * -- //
102
103// -- dependencies:
104// (1) div containing slides, (2) a "clip" div to hide the scroller
105// (3) control arrows
106
107// -- * config below * -- //
108
109var slideCode = droidList; // the dictionary of slides
110var slideList = 'app-list'; // the div containing the slides
111var arrowRight = 'arrow-right'; // the right control arrow
112var arrowLeft = 'arrow-left'; // the left control arrow
113
114
115function showPreview(slideName) {
116 centerSlide(slideName);
117 if (slideName.indexOf('selected') != -1) {
118 return false;
119 }
120 droid(slideName); // do this function when slide is clicked
121}
122
123var thumblist = document.getElementById(slideList);// the div containing the slides
124
125var slideWidth = 144; // width of a slide including all margins, etc.
126var slidesAtOnce = 3; // no. of slides to appear at once (requires odd number to have a centered slide)
127
128// -- * no editing should be needed below * -- //
129
130var originPosition = {};
131var is_animating = 0;
132var currentStripPosition = 0;
133var centeringPoint = 0;
134var rightScrollLimit = 0;
135
136// makeSlideStrip()
137// - figures out how many slides there are
138// - determines the centering point of the slide strip
139function makeSlideStrip() {
140 var slideTotal = 0;
141 centeringPoint = Math.ceil(slidesAtOnce/2);
142 for (var x in slideCode) {
143 slideTotal++;
144 }
145 var i = 0;
146 for (var code in slideCode) {
147 if (i <= centeringPoint-1) {
148 originPosition[code] = 0;
149 } else {
150 if (i >= slideTotal-centeringPoint+1) {
151 originPosition[code] = (slideTotal-slidesAtOnce)*slideWidth;
152 } else {
153 originPosition[code] = (i-centeringPoint+1)*slideWidth;
154 }
155 }
156 i++;
157 }
158 rightScrollLimit = -1*(slideTotal-slidesAtOnce)*slideWidth;
159}
160
161// slides with acceleration
162function slide(goal, id, go_left, cp) {
163 var div = document.getElementById(id);
164 var animation = {};
165 animation.time = 0.5; // in seconds
166 animation.fps = 60;
167 animation.goal = goal;
168 origin = 0.0;
169 animation.origin = Math.abs(origin);
170 animation.frames = (animation.time * animation.fps) - 1.0;
171 var current_frame = 0;
172 var motions = Math.abs(animation.goal - animation.origin);
173 function animate() {
174 var ease_right = function (t) { return (1 - Math.cos(t * Math.PI))/2.0; };
175 var ease = ease_right;
176 if (go_left == 1) {
177 ease = function(t) { return 1.0 - ease_right(t); };
178 }
179 var left = (ease(current_frame/animation.frames) * Math.abs(animation.goal - animation.origin)) - cp;
180 if(left < 0) {
181 left = 0;
182 }
183 if(!isNaN(left)) {
184 div.style.left = '-' + Math.round(left) + 'px';
185 }
186 current_frame += 1;
187 if (current_frame == animation.frames) {
188 is_animating = 0;
189 window.clearInterval(timeoutId)
190 }
191 }
192 var timeoutId = window.setInterval(animate, animation.time/animation.fps * 1000);
193}
194
195//Get style property
196function getStyle(element, cssProperty){
197 var elem = document.getElementById(element);
198 if(elem.currentStyle){
199 return elem.currentStyle[cssProperty]; //IE
200 } else{
201 var style = document.defaultView.getComputedStyle(elem, null); //firefox, Opera
202 return style.getPropertyValue(cssProperty);
203 }
204}
205
206// Left and right arrows
207function page_left() {
208 var amount = slideWidth;
209 animateSlide(amount, 'left');
210}
211
212function page_right() {
213 var amount = slideWidth;
214 animateSlide(amount, 'right');
215}
216
217
218// animates the strip
219// - sets arrows to on or off
220function animateSlide(amount,dir) {
221 var currentStripPosition = parseInt(getStyle(slideList,'left'));
222 var motionDistance;
223 if (amount == slideWidth ) {
224 motionDistance = slideWidth;
225 } else {
226 motionDistance = amount;
227 }
228
229 var rightarrow = document.getElementById(arrowRight);
230 var leftarrow = document.getElementById(arrowLeft);
231
232 function aToggle(state,aDir) {
233 if (state == 'on') {
234 if (aDir =='right') {
235 rightarrow.className = 'arrow-right-on';
236 rightarrow.href = "javascript:page_right()";
237 } else {
238 leftarrow.className = 'arrow-left-on';
239 leftarrow.href = "javascript:page_left()";
240 }
241 } else {
242 if (aDir =='right') {
243 rightarrow.href = "javascript:{}";
244 rightarrow.className = 'arrow-right-off';
245 } else {
246 leftarrow.href = "javascript:{}";
247 leftarrow.className = 'arrow-left-off';
248 }
249 }
250 }
251
252 function arrowChange(rP) {
253 if (rP >= rightScrollLimit) {
254 aToggle('on','right');
255 }
256 if (rP <= rightScrollLimit) {
257 aToggle('off','right');
258 }
259 if (rP <= slideWidth) {
260 aToggle('on','left');
261 }
262 if (rP >= 0) {
263 aToggle('off','left');
264 }
265 }
266
267 if (dir == 'right' && is_animating == 0) {
268 arrowChange(currentStripPosition-motionDistance);
269 is_animating = 1;
270 slide(motionDistance, slideList, 0, currentStripPosition);
271 } else if (dir == 'left' && is_animating == 0) {
272 arrowChange(currentStripPosition+motionDistance);
273 is_animating = 1;
274 rightStripPosition = currentStripPosition + motionDistance;
275 slide(motionDistance, slideList, 1, rightStripPosition);
276 }
277}
278
279function centerSlide(slideName) {
280 var currentStripPosition = parseInt(getStyle(slideList,'left'));
281 var dir = 'left';
282 var originpoint = Math.abs(currentStripPosition);
283 if (originpoint <= originPosition[slideName]) {
284 dir = 'right';
285 }
286 var motionValue = Math.abs(originPosition[slideName]-originpoint);
287 animateSlide(motionValue,dir);
288}
289
290
291function initCarousel(def) {
292 buildCarousel();
293 showPreview(def);
294 makeSlideStrip();
295}