Advertisement
handikaputra20

main.js

Apr 14th, 2020
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. import '../component/search-bar.js';
  2. import DataSource from '../data/data-source.js';
  3. import rockets from './../data/rockets.js'
  4.  
  5. function SetVar(){
  6. this.contentSearch = document.querySelector("#content-search");
  7. this.rockelement = document.querySelector("#rocketlist");
  8.  
  9. this.SearchElement = document.querySelector("search-bar");
  10.  
  11. this.refreshElement = document.querySelector("#refreshButtonElement");
  12. this.cbox = document.querySelector(".cbox");
  13. this.btnview = document.querySelector('#btnview');
  14. this.contentSearch = document.querySelector("#content-search");
  15. this.pagination = document.querySelector("#pagination");
  16. this.seacrhcontainer = document.querySelector("#search-container");
  17. }
  18.  
  19. const main = () => {
  20. const getVar = new SetVar();
  21. // Tampilan halam utama tampilkan seluruh data rocket
  22. rockets.forEach(rocket => {
  23. getVar.contentSearch.style.display = 'none';
  24.  
  25. const {mission,launch_date,imgrocket} = rocket;
  26.  
  27. const createListRocket = document.createElement("div");
  28. createListRocket.setAttribute("class","cbox");
  29. createListRocket.innerHTML =
  30. `<img src="${imgrocket}" alt="" class=" imglist" width="200">
  31. <hr>
  32. <p> <b>Mission Name : ${mission}</b>$</p>
  33. <p> <b>Launch Date Utc :</b> ${launch_date}</p>
  34. <button type="button" class="btnview" id="btnview" value="${mission}" onclick="viewData(this)">View</button>
  35. </div>`;
  36. getVar.rockelement.appendChild(createListRocket);
  37. })
  38.  
  39. // aksi ketika pencarian
  40. const onButtonSearchClicked = () => {
  41. if(getVar.SearchElement.value == ""){
  42. alert("Please input you search!");
  43. }else{
  44. getVar.rockelement.innerHTML = "";
  45. DataSource.searchRocket(getVar.SearchElement.value)
  46. .then(renderResult)
  47. .catch(fallbackResult)
  48. }
  49. };
  50.  
  51. // aksi refresh halaman utama
  52. const refreshPage = () => {
  53. window.location.reload();
  54. }
  55.  
  56. // fungsi mencari data pencarian kata kunci
  57. const renderResult = (results => {
  58. getVar.rockelement.innerHTML = "";
  59. results.forEach(function(rocket) {
  60. const {mission,launch_date,imgrocket} = rocket;
  61.  
  62. const RocketElement = document.createElement("div");
  63. RocketElement.setAttribute("class","cbox");
  64. RocketElement.innerHTML =
  65. `<img src="${imgrocket}" alt="" class="imglist" width="200">
  66. <hr>
  67. <p> <b>Mission Name :</b> ${mission} </p>
  68. <p> <b>Launch Date Utc :</b> ${launch_date} </p>
  69. <button type="button" class="btnview" id="btnview" value="${mission}" onclick="viewData(this)">View</button>
  70. </div>`;
  71. getVar.rockelement.appendChild(RocketElement);
  72. })
  73. })
  74.  
  75. const fallbackResult = (message => {
  76. getVar.rockelement.innerHTML = "";
  77. getVar.rockelement.innerHTML =`<h2>${message}</h2>`;
  78. })
  79.  
  80. getVar.SearchElement.clickEvent = onButtonSearchClicked;
  81. getVar.SearchElement.clickRefresh = refreshPage;
  82.  
  83. // panggil fungsi pagination
  84. page();
  85.  
  86. };
  87.  
  88. // fungsi value dari button view rocket
  89. const viewData = (key =>{
  90. DataSource.searchRocket(key.value)
  91. .then(renderResult)
  92. })
  93.  
  94. // tampilkan data ketika click view view
  95. const renderResult = (results => {
  96. const getVar = new SetVar();
  97. getVar.rockelement.remove();
  98. getVar.pagination.style.display = "none";
  99. getVar.seacrhcontainer.style.display = "none";
  100. getVar.contentSearch.style.display = "block";
  101. results.forEach(rocket => {
  102. const {mission,launch_date,youtube} = rocket;
  103.  
  104. const ytChange = youtube.replace('https://www.youtube.com/watch?v=','https://www.youtube.com/embed/');
  105. getVar.contentSearch.innerHTML =
  106. `<button id="back" class="back" onclick="back()">&laquo;</button>&nbsp; Detail Mission<br><br>
  107. <iframe src="${ytChange}"></iframe>`;
  108. const contentView = document.createElement("div");
  109. contentView.setAttribute("class","content");
  110. contentView.innerHTML =
  111. `<p>Mission Name :</p>
  112. <p>Rocket Name : </p>
  113. <p>Rocket Type :</p>
  114. <p>Launch Date UTC : </p>
  115. <p>Launch Date Local : </p>
  116. <p>National : </p>
  117. <p>Playload Type : </p>
  118. <p>Detail : </p>
  119. </div>`;
  120. getVar.contentSearch.appendChild(contentView);
  121. })
  122. })
  123.  
  124.  
  125. const back = () => {
  126. window.location.reload();
  127. }
  128.  
  129. //// Pagination sampe kebawah
  130.  
  131. const page = () =>{
  132. var Items = $('#rocketlist .cbox').length;
  133. var limitPerItem = 4;
  134. $("#rocketlist .cbox:gt(" +(limitPerItem -1)+ ")").hide();
  135. var totalItem = Math.round(Items / limitPerItem);
  136. $('.item').append("<a href='javascript:void(0)'class='current-page active'> " + 1 +"</a>");
  137.  
  138. for(var i = 2 ; i <= totalItem; i++){
  139. $('.item').append("<a class='current-page' href='javascript:void(0)'>" + i +"</a>");
  140. }
  141. $('.item').append("<a id='next-page' href='javascript:void(0)'>&raquo;</a>");
  142.  
  143. $(".item a.current-page").on("click", function(){
  144. if($(this).hasClass("active")) {
  145. return false;
  146. } else {
  147. var currentPage = $(this).index();
  148. $(".item a").removeClass("active");
  149. $(this).addClass("active");
  150. $("#rocketlist .cbox").hide();
  151.  
  152. var getTotal = limitPerItem * currentPage;
  153. for(var i = getTotal - limitPerItem; i < getTotal; i++){
  154. $("#rocketlist .cbox:eq(" + i +")").show();
  155. }
  156. }
  157. });
  158.  
  159. $("#next-page").on("click", function() {
  160. var currentPage = $(".item a.active").index();
  161. if(currentPage === totalItem){
  162. return false;
  163. } else {
  164. currentPage++;
  165. $(".item a").removeClass("active");
  166. $("#rocketlist .cbox").hide();
  167.  
  168. var getTotal = limitPerItem * currentPage;
  169. for(var i = getTotal - limitPerItem; i < getTotal; i++){
  170. $("#rocketlist .cbox:eq("+i+")").show();
  171. }
  172. $(".item a.current-page:eq("+(currentPage-1) +")").addClass("active");
  173. }
  174.  
  175. });
  176.  
  177. $("#previous-page").on("click", function() {
  178. var currentPage = $(".item a.active").index();
  179. if(currentPage === 1){
  180. return false;
  181. } else {
  182. currentPage--;
  183. $(".item a").removeClass("active");
  184. $("#rocketlist .cbox").hide();
  185.  
  186. var getTotal = limitPerItem * currentPage;
  187. for(var i = getTotal - limitPerItem; i < getTotal; i++){
  188. $("#rocketlist .cbox:eq("+i+")").show();
  189. }
  190. $(".item a.current-page:eq("+(currentPage-1) +")").addClass("active");
  191. }
  192.  
  193. });
  194. }
  195. // Akhir pagination
  196. export default main;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement