Advertisement
Guest User

mainjs

a guest
Jun 11th, 2017
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. console.log('Hello TrustPilot!');
  2. // THE REVIEWS DATA
  3. var reviews = getReviews();
  4. // tell js where is the widget
  5. var widget = $('#widget');
  6. // total number of reviewers
  7. var total = reviews.length;
  8. // initialize reviewSum variable to add all review ratings in it
  9. var reviewSum = 0;
  10. var carousalHtml = '';
  11. // loop through each review
  12. $(reviews).each(function(index, value) {
  13.     // add current review's rating in global reviewSum variable
  14.     reviewSum = reviewSum + parseInt(value.starRating);
  15.     // review Body if text is bigger then 200 characters then add more functionality
  16.     var reviewBody = value.reviewBody.length > 180 ? value.reviewBody.substr(0, 180) + '... <span class="showFullReview" onclick="javascript: showFullReviewBody($(this) ,' + index + ');">more</span>' : value.reviewBody;
  17.     // create class for Bad, Middle and good review for styling purposes
  18.     var reviewStarsClass = '';
  19.     if (parseInt(value.starRating) > 2 && parseInt(value.starRating) <= 3) {
  20.         reviewStarsClass = 'middle';
  21.     } else if (parseInt(value.starRating) >= 0 && parseInt(value.starRating) <= 2) {
  22.         reviewStarsClass = 'bad';
  23.     } else {
  24.         reviewStarsClass = 'good';
  25.     }
  26.     // Create HTML for carousal
  27.     carousalHtml += `
  28.     <div class="testimonial">
  29.       <div class="top">
  30.         <span class="name">` + value.fullName + `</span>
  31.         <div class="star-rating ` + reviewStarsClass + `">
  32.                 <span style="width:` + 20 * parseInt(value.starRating) + '%' + '" id="starRatingPresenter"><strong>' + value.starRating + `</strong> out of 5</span>
  33.             </div>
  34.      </div>
  35.      <div class="content">
  36.        <span class="firstQuote fa fa-quote-left"></span>
  37.        <span class="main">
  38.        ` + reviewBody + `
  39.        </span>
  40.        <span class="lastQuote fa fa-quote-right"></span>
  41.      </div>
  42.      <div class="footer">
  43.      ` + value.location + `
  44.      </div>
  45.    </div>
  46.      `;
  47. });
  48. // calculate average rating
  49. var averageRating = reviewSum / total;
  50. // Calculate happiness percentage
  51. var averageRatePercentage = 20 * averageRating;
  52. // calculate average rating's class for styling purposes
  53. var reviewStarsClass = '';
  54. if (parseInt(averageRating) > 2 && parseInt(averageRating) <= 3) {
  55.     reviewStarsClass = 'middle';
  56. } else if (parseInt(averageRating) >= 0 && parseInt(averageRating) <= 2) {
  57.     reviewStarsClass = 'bad';
  58. } else {
  59.     reviewStarsClass = 'good';
  60. }
  61. // Set dynamic text for banner
  62. var bannerText = '';
  63. if(reviewStarsClass == 'good') {
  64.   bannerText = 'We\'ve got our customers happy !';
  65. } else if(reviewStarsClass == 'middle') {
  66.   bannerText = 'Sometimes we\'re unable to fullfil needs of our customers';
  67. } else {
  68.   bannerText = 'Customers blame us for everything';
  69. }
  70. // Create HTML for widget
  71. var html = `
  72.    <div class="half first">
  73.       <span class="widgetTitle `+reviewStarsClass+`">
  74.         `+bannerText+`
  75.       </span>
  76.       <div class="averageRatingInfo">
  77.          <div class="star-rating ` + reviewStarsClass + `">
  78.             <span style="width:` + averageRatePercentage + '%' + '" id="starRatingPresenter"><strong>' + averageRating + `</strong> out of 5</span>
  79.         </div><br>
  80.         <div class="averageRatingWrap">
  81.            <span class="averageRating">` + averageRating + ` <span class="light">Rating</span></span>
  82.         </div>
  83.         <div class="reviewersWrap">
  84.            <span class="reviewers">` + total + ` <span class="light">Reviewers</span></span>
  85.         </div>
  86.         <div class="clear"></div>
  87.         <hr />
  88.         <div class="reviewFooterIndicator">
  89.            <span class="fa fa-star ` + reviewStarsClass + `"></span> Reviews
  90.         </div>
  91.      </div>
  92.   </div>
  93.   <div class="half second">
  94.        <div class="owl-carousel testimonials">` + carousalHtml + `</div>
  95.   </div>
  96.      `;
  97. // Apply widget html
  98. widget.html(html);
  99. // Initialize Carousel
  100. $('.owl-carousel').owlCarousel({
  101.    loop: true,
  102.    margin: 20,
  103.    autoplay: true,
  104.    autoplayTimeout: 5000,
  105.    nav: true,
  106.    navText: [
  107.        '<i class="fa fa-chevron-left"></i>',
  108.        '<i class="fa fa-chevron-right"></i>'
  109.    ],
  110.    autoplayHoverPause: true,
  111.    responsive: {
  112.        0: {
  113.            items: 1
  114.        },
  115.        480: {
  116.            items: 1
  117.        },
  118.        600: {
  119.            items: 2
  120.        },
  121.        1000: {
  122.            items: 2
  123.        }
  124.    }
  125. });
  126.  
  127. // Function to show all review text when clicked on more button
  128. function showFullReviewBody(e, id) {
  129.    console.log(id);
  130.    var contentPlace = e.closest('span.main');
  131.    var fullText = reviews[id].reviewBody+' <span class=\'showFullReview\' onclick=\'javascript: showLessReviewBody($(this),' + id + ');\'\'>less</span>';
  132.     contentPlace.html(fullText);
  133. }
  134. function showLessReviewBody(e, id) {
  135.     console.log(id);
  136.     var contentPlace = e.closest('span.main');
  137.     var reviewBody = reviews[id].reviewBody.substr(0, 180) + '... <span class="showFullReview" onclick="javascript: showFullReviewBody($(this) ,' + id + ');">more</span>';
  138.     contentPlace.html(reviewBody);
  139. }
  140. // Function to get reviews
  141. function getReviews() {
  142.     var toReturn = $.ajax({
  143.         url: 'API/reviews.json',
  144.         async: false
  145.     }).responseJSON;
  146.     return toReturn;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement