Advertisement
dimkiriaoks

Η JavaScript (jQuery)

Sep 22nd, 2022
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import $ from "jquery"
  2.  
  3. class Search {
  4.   // 1. describe and create/initiate our object
  5.   constructor() {
  6.     this.addSearchHTML()
  7.     this.resultsDiv = $("#search-overlay__results")
  8.     this.openButton = $(".js-search-trigger")
  9.     this.closeButton = $(".search-overlay__close")
  10.     this.searchOverlay = $(".search-overlay")
  11.     this.searchField = $("#search-term")
  12.     this.events()
  13.     this.isOverlayOpen = false
  14.     this.isSpinnerVisible = false
  15.     this.previousValue
  16.     this.typingTimer
  17.   }
  18.  
  19.   // 2. events
  20.   events() {
  21.     this.openButton.on("click", this.openOverlay.bind(this))
  22.     this.closeButton.on("click", this.closeOverlay.bind(this))
  23.     $(document).on("keydown", this.keyPressDispatcher.bind(this))
  24.     this.searchField.on("keyup", this.typingLogic.bind(this))
  25.   }
  26.  
  27.   // 3. methods (function, action...)
  28.   typingLogic() {
  29.     if (this.searchField.val() != this.previousValue) {
  30.       clearTimeout(this.typingTimer)
  31.  
  32.       if (this.searchField.val()) {
  33.         if (!this.isSpinnerVisible) {
  34.           this.resultsDiv.html('<div class="spinner-loader"></div>')
  35.           this.isSpinnerVisible = true
  36.         }
  37.         this.typingTimer = setTimeout(this.getResults.bind(this), 750)
  38.       } else {
  39.         this.resultsDiv.html("")
  40.         this.isSpinnerVisible = false
  41.       }
  42.     }
  43.  
  44.     this.previousValue = this.searchField.val()
  45.   }
  46.  
  47.   getResults() {
  48.     $.getJSON(universityData.root_url + "/wp-json/university/v1/search?term=" + this.searchField.val(), results => {
  49.       this.resultsDiv.html(`
  50.         <div class="row">
  51.           <div class="one-third">
  52.             <h2 class="search-overlay__section-title">General Information</h2>
  53.             ${results.generalInfo.length ? '<ul class="link-list min-list">' : "<p>No general information matches that search.</p>"}
  54.               ${results.generalInfo.map(item => `<li><a href="${item.permalink}">${item.title}</a> ${item.postType == "post" ? `by ${item.authorName}` : ""}</li>`).join("")}
  55.             ${results.generalInfo.length ? "</ul>" : ""}
  56.           </div>
  57.           <div class="one-third">
  58.             <h2 class="search-overlay__section-title">Programs</h2>
  59.             ${results.programs.length ? '<ul class="link-list min-list">' : `<p>No programs match that search. <a href="${universityData.root_url}/programs">View all programs</a></p>`}
  60.               ${results.programs.map(item => `<li><a href="${item.permalink}">${item.title}</a></li>`).join("")}
  61.             ${results.programs.length ? "</ul>" : ""}
  62.            
  63.             <h2 class="search-overlay__section-title">Professors</h2>
  64.               ${results.professors.length ? '<ul class="professor-cards">' : `<p>No professors match that search. <a href="${universityData.root_url}/professors">View all professors</a></p>`}
  65.                 ${results.professors.map(item => `
  66.                 <li class="professor-card__list-item">
  67.                     <a class="professor-card" href="${item.permalink}">
  68.                     <img class="professor-card__image" src="${item.image}" alt="">
  69.                     <span class="professor-card__name">${item.title}</span>
  70.                     </a>
  71.                 </li>
  72.                 `).join("")}
  73.               ${results.professors.length ? "</ul>" : ""}
  74.  
  75.           </div>
  76.           <div class="one-third">
  77.             <h2 class="search-overlay__section-title">Campuses</h2>
  78.             ${results.campuses.length ? '<ul class="link-list min-list">' : `<p>No campuses match that search. <a href="${universityData.root_url}/campuses">View all campuses</a></p>`}
  79.               ${results.campuses.map(item => `<li><a href="${item.permalink}">${item.title}</a></li>`).join("")}
  80.             ${results.campuses.length ? "</ul>" : ""}
  81.  
  82.             <h2 class="search-overlay__section-title">Events</h2>
  83.             ${results.events.length ? '' : `<p>No events match that search. <a href="${universityData.root_url}/events">View all events</a></p>`}
  84.             ${results.events.map(item => `
  85.             <div class="event-summary">
  86.                 <a class="event-summary__date t-center" href="${item.permalink}">
  87.                     <span class="event-summary__month">${item.month} </span>
  88.                     <span class="event-summary__day">${item.day}</span>
  89.                 </a>
  90.                 <div class="event-summary__content">
  91.                     <h5 class="event-summary__title headline headline--tiny"><a href="${item.permalink}"></a>${item.title}</h5>
  92.                     <p> ${item.excerpt} <a href="${item.permalink}" class="nu gray">Read more</a></p>
  93.                 </div>
  94.             </div>
  95.             `).join("")}
  96.            
  97.           </div>
  98.         </div>
  99.       `)
  100.       this.isSpinnerVisible = false
  101.     })
  102.   }
  103.  
  104.   keyPressDispatcher(e) {
  105.     if (e.keyCode == 83 && !this.isOverlayOpen && !$("input, textarea").is(":focus")) {
  106.       this.openOverlay()
  107.     }
  108.  
  109.     if (e.keyCode == 27 && this.isOverlayOpen) {
  110.       this.closeOverlay()
  111.     }
  112.   }
  113.  
  114.   openOverlay() {
  115.     this.searchOverlay.addClass("search-overlay--active")
  116.     $("body").addClass("body-no-scroll")
  117.     this.searchField.val("")
  118.     setTimeout(() => this.searchField.focus(), 301)
  119.     console.log("our open method just ran!")
  120.     this.isOverlayOpen = true
  121.     return false; // this prevent default action eg click on a link
  122.   }
  123.  
  124.   closeOverlay() {
  125.     this.searchOverlay.removeClass("search-overlay--active")
  126.     $("body").removeClass("body-no-scroll")
  127.     console.log("our close method just ran!")
  128.     this.isOverlayOpen = false
  129.   }
  130.  
  131.   addSearchHTML() {
  132.     $("body").append(`
  133.       <div class="search-overlay">
  134.         <div class="search-overlay__top">
  135.           <div class="container">
  136.             <i class="fa fa-search search-overlay__icon" aria-hidden="true"></i>
  137.             <input type="text" class="search-term" placeholder="What are you looking for?" id="search-term">
  138.             <div class="search-overlay__close">
  139.               <i class="fa fa-window-close" aria-hidden="true"></i>
  140.             </div>
  141.           </div>
  142.         </div>
  143.        
  144.         <div class="container">
  145.           <div id="search-overlay__results"></div>
  146.         </div>
  147.  
  148.       </div>
  149.     `)
  150.   }
  151. }
  152.  
  153. export default Search
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement