process-reviews.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery'
  7. ], function ($) {
  8. 'use strict';
  9. /**
  10. * @param {String} url
  11. * @param {*} fromPages
  12. */
  13. function processReviews(url, fromPages) {
  14. $.ajax({
  15. url: url,
  16. cache: true,
  17. dataType: 'html',
  18. showLoader: false,
  19. loaderContext: $('.product.data.items')
  20. }).done(function (data) {
  21. $('#product-review-container').html(data);
  22. $('[data-role="product-review"] .pages a').each(function (index, element) {
  23. $(element).click(function (event) { //eslint-disable-line max-nested-callbacks
  24. processReviews($(element).attr('href'), true);
  25. event.preventDefault();
  26. });
  27. });
  28. }).complete(function () {
  29. if (fromPages == true) { //eslint-disable-line eqeqeq
  30. $('html, body').animate({
  31. scrollTop: $('#reviews').offset().top - 50
  32. }, 300);
  33. }
  34. });
  35. }
  36. return function (config) {
  37. var reviewTab = $(config.reviewsTabSelector),
  38. requiredReviewTabRole = 'tab';
  39. if (reviewTab.attr('role') === requiredReviewTabRole && reviewTab.hasClass('active')) {
  40. processReviews(config.productReviewUrl);
  41. } else {
  42. reviewTab.one('beforeOpen', function () {
  43. processReviews(config.productReviewUrl);
  44. });
  45. }
  46. $(function () {
  47. $('.product-info-main .reviews-actions a').click(function (event) {
  48. var anchor;
  49. event.preventDefault();
  50. anchor = $(this).attr('href').replace(/^.*?(#|$)/, '');
  51. $('.product.data.items [data-role="content"]').each(function (index) { //eslint-disable-line
  52. if (this.id == 'reviews') { //eslint-disable-line eqeqeq
  53. $('.product.data.items').tabs('activate', index);
  54. $('html, body').animate({
  55. scrollTop: $('#' + anchor).offset().top - 50
  56. }, 300);
  57. }
  58. });
  59. });
  60. });
  61. };
  62. });