PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/droiddoc/templates/assets/carousel.js

https://github.com/jsherman/platform_build
JavaScript | 293 lines | 221 code | 44 blank | 28 comment | 54 complexity | 9f1d3cd19cfbdaa5c6ddb6f365b60e1a MD5 | raw file
  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. /* -- video switcher -- */
  8. var oldVid = "multi"; // set the default video
  9. var nowPlayingString = "Now playing:";
  10. var assetsRoot = "/assets/";
  11. /* -- app thumbnail switcher -- */
  12. var currentDroid;
  13. var oldDroid;
  14. // shows a random application
  15. function randomDroid(){
  16. // count the total number of apps
  17. var droidListLength = 0;
  18. for (var k in droidList)
  19. droidListLength++;
  20. // pick a random app and show it
  21. var j = 0;
  22. var i = Math.floor(droidListLength*Math.random());
  23. for (var x in droidList) {
  24. if(j++ == i){
  25. currentDroid = x;
  26. showPreview(x);
  27. centerSlide(x);
  28. }
  29. }
  30. }
  31. // shows a bulletin, swaps the carousel highlighting
  32. function droid(appName){
  33. oldDroid = $("#droidlink-"+currentDroid);
  34. currentDroid = appName;
  35. var droid = droidList[appName];
  36. var layout = droid.layout;
  37. var imgDiv = document.getElementById("bulletinImg");
  38. var descDiv = document.getElementById("bulletinDesc");
  39. if (layout == "imgLeft") {
  40. imgDiv.className = "img-left";
  41. descDiv.className = "desc-right";
  42. } else if (layout == "imgTop") {
  43. imgDiv.className = "img-top";
  44. descDiv.className = "desc-bottom";
  45. } else if (layout == "imgRight") {
  46. imgDiv.className = "img-right";
  47. descDiv.className = "desc-left";
  48. }
  49. imgDiv.innerHTML = "<img src='" + assetsRoot + "images/home/" + droid.img + "'>";
  50. descDiv.innerHTML = (droid.title != "") ? "<h3>" + droid.title + "</h3>" + droid.desc : droid.desc;
  51. if(oldDroid)
  52. oldDroid.removeClass("selected");
  53. $("#droidlink-"+appName).addClass("selected");
  54. }
  55. // -- * build the carousel based on the droidList * -- //
  56. function buildCarousel() {
  57. var appList = document.getElementById("app-list");
  58. for (var x in droidList) {
  59. var droid = droidList[x];
  60. var icon = droid.icon;
  61. var name = droid.name;
  62. var a = document.createElement("a");
  63. var img = document.createElement("img");
  64. var br = document.createElement("br");
  65. var text = document.createTextNode(droid.name);
  66. a.setAttribute("id", "droidlink-" + x);
  67. a.className = x;
  68. a.setAttribute("href", "#");
  69. a.onclick = function() { showPreview(this.className); return false; }
  70. img.setAttribute("src", assetsRoot + "images/home/" + droid.icon);
  71. img.setAttribute("alt", "");
  72. a.appendChild(img);
  73. a.appendChild(br);
  74. a.appendChild(text);
  75. appList.appendChild(a);
  76. }
  77. }
  78. // -- * slider * -- //
  79. // -- dependencies:
  80. // (1) div containing slides, (2) a "clip" div to hide the scroller
  81. // (3) control arrows
  82. // -- * config below * -- //
  83. var slideCode = droidList; // the dictionary of slides
  84. var slideList = 'app-list'; // the div containing the slides
  85. var arrowRight = 'arrow-right'; // the right control arrow
  86. var arrowLeft = 'arrow-left'; // the left control arrow
  87. function showPreview(slideName) {
  88. // centerSlide(slideName);
  89. if (slideName.indexOf('selected') != -1) {
  90. return false;
  91. }
  92. droid(slideName); // do this function when slide is clicked
  93. }
  94. var thumblist = document.getElementById(slideList);// the div containing the slides
  95. var slideWidth = 144; // width of a slide including all margins, etc.
  96. var slidesAtOnce = 3; // no. of slides to appear at once (requires odd number to have a centered slide)
  97. // -- * no editing should be needed below * -- //
  98. var originPosition = {};
  99. var is_animating = 0;
  100. var currentStripPosition = 0;
  101. var centeringPoint = 0;
  102. var rightScrollLimit = 0;
  103. // makeSlideStrip()
  104. // - figures out how many slides there are
  105. // - determines the centering point of the slide strip
  106. function makeSlideStrip() {
  107. var slideTotal = 0;
  108. centeringPoint = Math.ceil(slidesAtOnce/2);
  109. for (var x in slideCode) {
  110. slideTotal++;
  111. }
  112. var i = 0;
  113. for (var code in slideCode) {
  114. if (i <= centeringPoint-1) {
  115. originPosition[code] = 0;
  116. } else {
  117. if (i >= slideTotal-centeringPoint+1) {
  118. originPosition[code] = (slideTotal-slidesAtOnce)*slideWidth;
  119. } else {
  120. originPosition[code] = (i-centeringPoint+1)*slideWidth;
  121. }
  122. }
  123. i++;
  124. }
  125. rightScrollLimit = -1*(slideTotal-slidesAtOnce)*slideWidth;
  126. }
  127. // slides with acceleration
  128. function slide(goal, id, go_left, cp) {
  129. var div = document.getElementById(id);
  130. var animation = {};
  131. animation.time = 0.5; // in seconds
  132. animation.fps = 60;
  133. animation.goal = goal;
  134. origin = 0.0;
  135. animation.origin = Math.abs(origin);
  136. animation.frames = (animation.time * animation.fps) - 1.0;
  137. var current_frame = 0;
  138. var motions = Math.abs(animation.goal - animation.origin);
  139. function animate() {
  140. var ease_right = function (t) { return (1 - Math.cos(t * Math.PI))/2.0; };
  141. var ease = ease_right;
  142. if (go_left == 1) {
  143. ease = function(t) { return 1.0 - ease_right(t); };
  144. }
  145. var left = (ease(current_frame/animation.frames) * Math.abs(animation.goal - animation.origin)) - cp;
  146. if(left < 0) {
  147. left = 0;
  148. }
  149. if(!isNaN(left)) {
  150. div.style.left = '-' + Math.round(left) + 'px';
  151. }
  152. current_frame += 1;
  153. if (current_frame == animation.frames) {
  154. is_animating = 0;
  155. window.clearInterval(timeoutId)
  156. }
  157. }
  158. var timeoutId = window.setInterval(animate, animation.time/animation.fps * 1000);
  159. }
  160. //Get style property
  161. function getStyle(element, cssProperty){
  162. var elem = document.getElementById(element);
  163. if(elem.currentStyle){
  164. return elem.currentStyle[cssProperty]; //IE
  165. } else{
  166. var style = document.defaultView.getComputedStyle(elem, null); //firefox, Opera
  167. return style.getPropertyValue(cssProperty);
  168. }
  169. }
  170. // Left and right arrows
  171. function page_left() {
  172. var amount = slideWidth;
  173. animateSlide(amount, 'left');
  174. }
  175. function page_right() {
  176. var amount = slideWidth;
  177. animateSlide(amount, 'right');
  178. }
  179. // animates the strip
  180. // - sets arrows to on or off
  181. function animateSlide(amount,dir) {
  182. var currentStripPosition = parseInt(getStyle(slideList,'left'));
  183. var motionDistance;
  184. if (amount == slideWidth ) {
  185. motionDistance = slideWidth;
  186. } else {
  187. motionDistance = amount;
  188. }
  189. var rightarrow = document.getElementById(arrowRight);
  190. var leftarrow = document.getElementById(arrowLeft);
  191. function aToggle(state,aDir) {
  192. if (state == 'on') {
  193. if (aDir =='right') {
  194. rightarrow.className = 'arrow-right-on';
  195. rightarrow.href = "javascript:page_right()";
  196. } else {
  197. leftarrow.className = 'arrow-left-on';
  198. leftarrow.href = "javascript:page_left()";
  199. }
  200. } else {
  201. if (aDir =='right') {
  202. rightarrow.href = "javascript:{}";
  203. rightarrow.className = 'arrow-right-off';
  204. } else {
  205. leftarrow.href = "javascript:{}";
  206. leftarrow.className = 'arrow-left-off';
  207. }
  208. }
  209. }
  210. function arrowChange(rP) {
  211. if (rP >= rightScrollLimit) {
  212. aToggle('on','right');
  213. }
  214. if (rP <= rightScrollLimit) {
  215. aToggle('off','right');
  216. }
  217. if (rP <= slideWidth) {
  218. aToggle('on','left');
  219. }
  220. if (rP >= 0) {
  221. aToggle('off','left');
  222. }
  223. }
  224. if (dir == 'right' && is_animating == 0) {
  225. arrowChange(currentStripPosition-motionDistance);
  226. is_animating = 1;
  227. slide(motionDistance, slideList, 0, currentStripPosition);
  228. } else if (dir == 'left' && is_animating == 0) {
  229. arrowChange(currentStripPosition+motionDistance);
  230. is_animating = 1;
  231. rightStripPosition = currentStripPosition + motionDistance;
  232. slide(motionDistance, slideList, 1, rightStripPosition);
  233. }
  234. }
  235. function centerSlide(slideName) {
  236. var currentStripPosition = parseInt(getStyle(slideList,'left'));
  237. var dir = 'left';
  238. var originpoint = Math.abs(currentStripPosition);
  239. if (originpoint <= originPosition[slideName]) {
  240. dir = 'right';
  241. }
  242. var motionValue = Math.abs(originPosition[slideName]-originpoint);
  243. animateSlide(motionValue,dir);
  244. }
  245. function initCarousel(def) {
  246. buildCarousel();
  247. showPreview(def);
  248. makeSlideStrip();
  249. }