Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. function ajaxReq() {
  2. var request = new XMLHttpRequest();
  3. return request;
  4. }
  5.  
  6.  
  7. function makeLinks(projects) { // result = getJsonData > request.responseText
  8. var projectList = document.getElementById("project-list");
  9.  
  10. for (var project in projects) {
  11. if (projects[project].status !== "DNU") {
  12. var projectId = projects[project].id;
  13. var listItem = "<li><a class="project-link" id=""+projects[project].project+"" href="#">" + projects[project].project + "</a></li>";
  14. projectList.innerHTML += listItem;
  15. } // if !DNU
  16. }
  17.  
  18. // ADD EVENT LISTENERS
  19. var projLink = document.getElementsByClassName("project-link");
  20. for (var i = 0; i < projLink.length; i++) {
  21. var projId = projLink[i].id;
  22. //projLink[i].dataset.projIx = [i];
  23. projLink[i].addEventListener("click", showProject, false);
  24. }
  25.  
  26. var showNext = document.getElementById("show-next");
  27. var showPrev = document.getElementById("show-previous");
  28. showNext.addEventListener("click", showProject, false);
  29. showPrev.addEventListener("click", showProject, false);
  30.  
  31. // ARROW KEYS [not invoking the showProject function]
  32. $(document).keydown(function(e) {
  33. if(e.which==37) { // LEFT arrow
  34. $(showPrev).click(showProject);
  35. console.log("previous");
  36. } else
  37. if(e.which==39) { // RIGHT arrow
  38. $(showNext).click(showProject);
  39. console.log("next");
  40. }
  41. })
  42.  
  43.  
  44. function showProject(projId) {
  45.  
  46. var intro = document.getElementById("intro");
  47. if (intro) {
  48. intro.parentNode.removeChild(intro);
  49. }
  50. projId.preventDefault();
  51.  
  52. var projLinks = document.getElementsByClassName("project-link"); // array
  53.  
  54. var selIx = $(".selected").index();
  55.  
  56.  
  57. // ###### CLICK PREVIOUS/NEXT ######
  58. if (this.id === "show-previous" || this.id === "show-next") {
  59.  
  60. // 1a. if nothing is .selected
  61. if (selIx < 0) {
  62. if (this.id === "show-previous") {
  63. var selIx = projLinks.length-1;
  64. }
  65. else if (this.id === "show-next") {
  66. var selIx = 0;
  67. }
  68. }
  69.  
  70. // 1b. if .selected:
  71. else if (selIx > -1) {
  72. if (this.id === "show-previous") {
  73. if (selIx === 0) { // if @ first slide
  74. selIx = projLinks.length-1;
  75. }
  76. else {
  77. selIx --;
  78. }
  79. }
  80. else if (this.id === "show-next") {
  81. if (selIx === projLinks.length-1) { // if @ last slide
  82. selIx = 0;
  83. }
  84. else {
  85. selIx ++;
  86. }
  87. }
  88. }
  89. var selProjLi = projLinks[selIx]; // => li
  90. } // click previous/next
  91.  
  92.  
  93. // ###### CLICK .project-link ######
  94. else if (this.id !== "show-previous" && this.id !== "show-next") {
  95. var selIx = $(this).closest("li").index();
  96. }
  97.  
  98.  
  99. // FADE OUT, CALLBACK: LOAD NEW PROJECT
  100. $("#project-display").fadeTo(450, 0.0, function() {
  101.  
  102. // ###### ALL ######
  103. $(".selected").removeClass("selected");
  104.  
  105.  
  106. var projId = projLink[selIx].id;
  107. var selProjLi = projLink[selIx].parentElement;
  108. selProjLi.className = "selected";
  109.  
  110. var projectDisplay = document.getElementById("project-display");
  111.  
  112. // set vars for the project display elements:
  113. var projName = document.getElementById("project-name"); // h3
  114. var projTools = document.getElementById("project-tools");
  115. var projNotes = document.getElementById("project-notes");
  116. var projImages = document.getElementById("project-images");
  117.  
  118. // disappear the metadata elements 'cause sometimes they'll be empty
  119. projTools.style.display = "none";
  120. projNotes.style.display = "none";
  121. testimonial.style.display = "none";
  122.  
  123. for (var project in projects) { // 'Projects array' -> project
  124. if (projects[project].project === projId) {
  125.  
  126. var activeProj = projects[project];
  127.  
  128. projName.innerHTML = activeProj.project;
  129.  
  130. // maintain centered display of project-metadata: check for a value, else the element remains hidden
  131. if(activeProj["tools used"]) {
  132. projTools.style.display = "inline-block";
  133. projTools.innerHTML = activeProj["tools used"];
  134. }
  135. if(activeProj.notes) {
  136. projNotes.style.display = "inline-block";
  137. projNotes.innerHTML = activeProj.notes;
  138. }
  139. if(activeProj.testimonial) {
  140. testimonial.style.display = "inline-block";
  141. testimonial.innerHTML = activeProj.testimonial;
  142. }
  143.  
  144. // HOW TO ENSURE THESE ARE ALREADY LOADED ***BEFORE #project-display FADES IN***
  145. projImages.innerHTML = "";
  146. for (var i = 0; i < activeProj.images.length; i++ ) {
  147. projImages.innerHTML += "<img src="" + activeProj.images[i].url + "" />";
  148. }
  149. } // if project id ...
  150. } // for (var obj in data)
  151. }) // fade out
  152.  
  153. $("#project-display").fadeTo(600, 1.0);
  154.  
  155. } // showProject
  156. } // makeLinks
  157.  
  158. function getJsonData() {
  159. var request = ajaxReq();
  160. request.open("GET", "/json/projects.json", true);
  161. request.setRequestHeader("content-type", "application/json");
  162. request.send(null);
  163. request.onreadystatechange = function() {
  164. if (request.readyState === 4) {
  165. if (request.status === 200) {
  166. //makeLinks(request.responseText);
  167. var projects = JSON.parse(request.responseText);
  168. var projects = projects["Projects"];
  169. makeLinks(projects); // makeLinks = callback
  170. return projects;
  171. }
  172. }
  173. } // onreadystatechange
  174. } // getJsonData
  175.  
  176. getJsonData(makeLinks);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement