Advertisement
Guest User

Updated code

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