The Android Open Source Project | b6c1cf6 | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | /* 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 |
|
| 10 | var oldVid = "multi"; // set the default video
|
| 11 | var nowPlayingString = "Now playing:";
|
| 12 | var assetsRoot = "/gae/assets/";
|
| 13 |
|
| 14 |
|
| 15 | /* -- app thumbnail switcher -- */
|
| 16 |
|
| 17 | var currentDroid;
|
| 18 | var oldDroid;
|
| 19 |
|
| 20 | // shows a random application
|
| 21 | function 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
|
| 42 | function 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='" + 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 * -- //
|
| 74 | function 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 text = document.createTextNode(droid.name);
|
| 84 |
|
| 85 | a.setAttribute("id", "droidlink-" + x);
|
| 86 | a.className = x;
|
| 87 | a.setAttribute("href", "#");
|
| 88 | a.onclick = function() { showPreview(this.className); return false; }
|
| 89 | img.setAttribute("src", assetsRoot + "images/home/" + droid.icon);
|
| 90 | img.setAttribute("alt", "");
|
| 91 |
|
| 92 | a.appendChild(img);
|
| 93 | a.appendChild(br);
|
| 94 | a.appendChild(text);
|
| 95 | appList.appendChild(a);
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | // -- * slider * -- //
|
| 100 |
|
| 101 | // -- dependencies:
|
| 102 | // (1) div containing slides, (2) a "clip" div to hide the scroller
|
| 103 | // (3) control arrows
|
| 104 |
|
| 105 | // -- * config below * -- //
|
| 106 |
|
| 107 | var slideCode = droidList; // the dictionary of slides
|
| 108 | var slideList = 'app-list'; // the div containing the slides
|
| 109 | var arrowRight = 'arrow-right'; // the right control arrow
|
| 110 | var arrowLeft = 'arrow-left'; // the left control arrow
|
| 111 |
|
| 112 |
|
| 113 | function showPreview(slideName) {
|
| 114 | // centerSlide(slideName);
|
| 115 | droid(slideName); // do this function when slide is clicked
|
| 116 |
|
| 117 | }
|
| 118 |
|
| 119 | var thumblist = document.getElementById(slideList);// the div containing the slides
|
| 120 |
|
| 121 | var slideWidth = 144; // width of a slide including all margins, etc.
|
| 122 | var slidesAtOnce = 3; // no. of slides to appear at once (requires odd number to have a centered slide)
|
| 123 |
|
| 124 | // -- * no editing should be needed below * -- //
|
| 125 |
|
| 126 | var originPosition = {};
|
| 127 | var is_animating = 0;
|
| 128 | var currentStripPosition = 0;
|
| 129 | var centeringPoint = 0;
|
| 130 | var rightScrollLimit = 0;
|
| 131 |
|
| 132 | // makeSlideStrip()
|
| 133 | // - figures out how many slides there are
|
| 134 | // - determines the centering point of the slide strip
|
| 135 | function makeSlideStrip() {
|
| 136 | var slideTotal = 0;
|
| 137 | centeringPoint = Math.ceil(slidesAtOnce/2);
|
| 138 | for (var x in slideCode) {
|
| 139 | slideTotal++;
|
| 140 | }
|
| 141 | var i = 0;
|
| 142 | for (var code in slideCode) {
|
| 143 | if (i <= centeringPoint-1) {
|
| 144 | originPosition[code] = 0;
|
| 145 | } else {
|
| 146 | if (i >= slideTotal-centeringPoint+1) {
|
| 147 | originPosition[code] = (slideTotal-slidesAtOnce)*slideWidth;
|
| 148 | } else {
|
| 149 | originPosition[code] = (i-centeringPoint+1)*slideWidth;
|
| 150 | }
|
| 151 | }
|
| 152 | i++;
|
| 153 | }
|
| 154 | rightScrollLimit = -1*(slideTotal-slidesAtOnce)*slideWidth;
|
| 155 | }
|
| 156 |
|
| 157 | // slides with acceleration
|
| 158 | function slide(goal, id, go_left, cp) {
|
| 159 | var div = document.getElementById(id);
|
| 160 | var animation = {};
|
| 161 | animation.time = 0.5; // in seconds
|
| 162 | animation.fps = 60;
|
| 163 | animation.goal = goal;
|
| 164 | origin = 0.0;
|
| 165 | animation.origin = Math.abs(origin);
|
| 166 | animation.frames = (animation.time * animation.fps) - 1.0;
|
| 167 | var current_frame = 0;
|
| 168 | var motions = Math.abs(animation.goal - animation.origin);
|
| 169 | function animate() {
|
| 170 | var ease_right = function (t) { return (1 - Math.cos(t * Math.PI))/2.0; };
|
| 171 | var ease = ease_right;
|
| 172 | if (go_left == 1) {
|
| 173 | ease = function(t) { return 1.0 - ease_right(t); };
|
| 174 | }
|
| 175 | var left = (ease(current_frame/animation.frames) * Math.abs(animation.goal - animation.origin)) - cp;
|
| 176 | if(left < 0) {
|
| 177 | left = 0;
|
| 178 | }
|
| 179 | if(!isNaN(left)) {
|
| 180 | div.style.left = '-' + Math.round(left) + 'px';
|
| 181 | }
|
| 182 | current_frame += 1;
|
| 183 | if (current_frame == animation.frames) {
|
| 184 | is_animating = 0;
|
| 185 | window.clearInterval(timeoutId)
|
| 186 | }
|
| 187 | }
|
| 188 | var timeoutId = window.setInterval(animate, animation.time/animation.fps * 1000);
|
| 189 | }
|
| 190 |
|
| 191 | //Get style property
|
| 192 | function getStyle(element, cssProperty){
|
| 193 | var elem = document.getElementById(element);
|
| 194 | if(elem.currentStyle){
|
| 195 | return elem.currentStyle[cssProperty]; //IE
|
| 196 | } else{
|
| 197 | var style = document.defaultView.getComputedStyle(elem, null); //firefox, Opera
|
| 198 | return style.getPropertyValue(cssProperty);
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | // Left and right arrows
|
| 203 | function page_left() {
|
| 204 | var amount = slideWidth;
|
| 205 | animateSlide(amount, 'left');
|
| 206 | }
|
| 207 |
|
| 208 | function page_right() {
|
| 209 | var amount = slideWidth;
|
| 210 | animateSlide(amount, 'right');
|
| 211 | }
|
| 212 |
|
| 213 |
|
| 214 | // animates the strip
|
| 215 | // - sets arrows to on or off
|
| 216 | function animateSlide(amount,dir) {
|
| 217 | var currentStripPosition = parseInt(getStyle(slideList,'left'));
|
| 218 | var motionDistance;
|
| 219 | if (amount == slideWidth ) {
|
| 220 | motionDistance = slideWidth;
|
| 221 | } else {
|
| 222 | motionDistance = amount;
|
| 223 | }
|
| 224 |
|
| 225 | var rightarrow = document.getElementById(arrowRight);
|
| 226 | var leftarrow = document.getElementById(arrowLeft);
|
| 227 |
|
| 228 | function aToggle(state,aDir) {
|
| 229 | if (state == 'on') {
|
| 230 | if (aDir =='right') {
|
| 231 | rightarrow.className = 'arrow-right-on';
|
| 232 | rightarrow.href = "javascript:page_right()";
|
| 233 | } else {
|
| 234 | leftarrow.className = 'arrow-left-on';
|
| 235 | leftarrow.href = "javascript:page_left()";
|
| 236 | }
|
| 237 | } else {
|
| 238 | if (aDir =='right') {
|
| 239 | rightarrow.href = "javascript:{}";
|
| 240 | rightarrow.className = 'arrow-right-off';
|
| 241 | } else {
|
| 242 | leftarrow.href = "javascript:{}";
|
| 243 | leftarrow.className = 'arrow-left-off';
|
| 244 | }
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | function arrowChange(rP) {
|
| 249 | if (rP >= rightScrollLimit) {
|
| 250 | aToggle('on','right');
|
| 251 | }
|
| 252 | if (rP <= rightScrollLimit) {
|
| 253 | aToggle('off','right');
|
| 254 | }
|
| 255 | if (rP <= slideWidth) {
|
| 256 | aToggle('on','left');
|
| 257 | }
|
| 258 | if (rP >= 0) {
|
| 259 | aToggle('off','left');
|
| 260 | }
|
| 261 | }
|
| 262 |
|
| 263 | if (dir == 'right' && is_animating == 0) {
|
| 264 | arrowChange(currentStripPosition-motionDistance);
|
| 265 | is_animating = 1;
|
| 266 | slide(motionDistance, slideList, 0, currentStripPosition);
|
| 267 | } else if (dir == 'left' && is_animating == 0) {
|
| 268 | arrowChange(currentStripPosition+motionDistance);
|
| 269 | is_animating = 1;
|
| 270 | rightStripPosition = currentStripPosition + motionDistance;
|
| 271 | slide(motionDistance, slideList, 1, rightStripPosition);
|
| 272 | }
|
| 273 | }
|
| 274 |
|
| 275 | function centerSlide(slideName) {
|
| 276 | var currentStripPosition = parseInt(getStyle(slideList,'left'));
|
| 277 | var dir = 'left';
|
| 278 | var originpoint = Math.abs(currentStripPosition);
|
| 279 | if (originpoint <= originPosition[slideName]) {
|
| 280 | dir = 'right';
|
| 281 | }
|
| 282 | var motionValue = Math.abs(originPosition[slideName]-originpoint);
|
| 283 | animateSlide(motionValue,dir);
|
| 284 | }
|
| 285 |
|
| 286 |
|
| 287 | function initCarousel(def) {
|
| 288 | buildCarousel();
|
| 289 | showPreview(def);
|
| 290 | makeSlideStrip();
|
| 291 | } |