init
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
define([
|
||||
'jquery'
|
||||
], function ($) {
|
||||
'use strict';
|
||||
|
||||
function loadReviews(url, fromPages) {
|
||||
$.ajax({
|
||||
url: url,
|
||||
cache: true,
|
||||
dataType: 'html',
|
||||
showLoader: false,
|
||||
loaderContext: $('.product-reviews-section')
|
||||
}).done(function (data) {
|
||||
$('#product-review-container').html(data).trigger('contentUpdated');
|
||||
$('[data-role="product-review"] .pages a').on('click', function (event) {
|
||||
event.preventDefault();
|
||||
loadReviews($(this).attr('href'), true);
|
||||
});
|
||||
}).always(function () {
|
||||
if (fromPages) {
|
||||
$('html, body').animate({
|
||||
scrollTop: $('#reviews').offset().top - 50
|
||||
}, 300);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return function (config) {
|
||||
$(function () {
|
||||
if (config.productReviewUrl) {
|
||||
loadReviews(config.productReviewUrl, location.hash === '#reviews');
|
||||
}
|
||||
|
||||
$('.product-info-main .reviews-actions a').on('click', function (event) {
|
||||
var anchor = $(this).attr('href').replace(/^.*?(#|$)/, ''),
|
||||
target = $('#' + anchor);
|
||||
|
||||
if (target.length) {
|
||||
event.preventDefault();
|
||||
$('html, body').animate({
|
||||
scrollTop: target.offset().top - 50
|
||||
}, 300);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,73 @@
|
||||
define(['jquery', 'mage/translate'], function ($, $t) {
|
||||
'use strict';
|
||||
|
||||
return function (config, element) {
|
||||
var $root = $(element),
|
||||
$modal = $root.find('[data-role="esun-review-form-modal"]');
|
||||
|
||||
function openModal() {
|
||||
if (!$modal.length) {
|
||||
return;
|
||||
}
|
||||
$modal.removeAttr('hidden');
|
||||
$('body').addClass('esun-review-form-modal-open');
|
||||
}
|
||||
|
||||
function closeModal() {
|
||||
if (!$modal.length) {
|
||||
return;
|
||||
}
|
||||
$modal.attr('hidden', true);
|
||||
$('body').removeClass('esun-review-form-modal-open');
|
||||
}
|
||||
|
||||
$root.on('click', '[data-role="esun-write-review-trigger"]', openModal);
|
||||
$root.on('click', '[data-role="esun-review-form-close"]', closeModal);
|
||||
|
||||
$(document).on('keyup.esunReviewFormModal', function (event) {
|
||||
if (event.key === 'Escape') {
|
||||
closeModal();
|
||||
}
|
||||
});
|
||||
|
||||
function renderImagePreview(input) {
|
||||
var files = Array.prototype.slice.call(input.files || []),
|
||||
$preview = $(input).closest('form').find('[data-role="review-image-preview"]');
|
||||
|
||||
$preview.empty();
|
||||
$.each(files, function (index, file) {
|
||||
var reader = new FileReader();
|
||||
reader.onload = function (event) {
|
||||
$('<img/>', {
|
||||
src: event.target.result,
|
||||
alt: file.name
|
||||
}).appendTo($preview);
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
});
|
||||
}
|
||||
|
||||
$root.on('change', '#review_images', function () {
|
||||
renderImagePreview(this);
|
||||
});
|
||||
|
||||
$(document)
|
||||
.off('change.esunReviewImages', '#review_images')
|
||||
.on('change.esunReviewImages', '#review_images', function () {
|
||||
renderImagePreview(this);
|
||||
});
|
||||
|
||||
$root.on('submit', '#review-form', function () {
|
||||
var $form = $(this),
|
||||
detail = $.trim($form.find('#review_field').val() || ''),
|
||||
nickname = $.trim($form.find('#nickname_field').val() || '');
|
||||
|
||||
if (!nickname) {
|
||||
nickname = $.trim($('.customer-name, .logged-in').first().text()) || $t('Customer');
|
||||
$form.find('#nickname_field').val(nickname);
|
||||
}
|
||||
|
||||
$form.find('#summary_field').val(detail.substring(0, 120) || $t('Review'));
|
||||
});
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,104 @@
|
||||
define(['jquery'], function ($) {
|
||||
'use strict';
|
||||
|
||||
var $modal;
|
||||
|
||||
function ensureModal() {
|
||||
if ($modal) {
|
||||
return $modal;
|
||||
}
|
||||
|
||||
$modal = $('<div/>', {'class': 'esun-review-lightbox', hidden: true})
|
||||
.append($('<button/>', {'class': 'esun-review-lightbox__close', type: 'button', text: '×'}))
|
||||
.append($('<button/>', {'class': 'esun-review-lightbox__prev', type: 'button', text: '‹'}))
|
||||
.append($('<img/>', {'class': 'esun-review-lightbox__image', alt: ''}))
|
||||
.append($('<video/>', {
|
||||
'class': 'esun-review-lightbox__video',
|
||||
controls: true,
|
||||
hidden: true,
|
||||
playsinline: true,
|
||||
preload: 'metadata'
|
||||
}))
|
||||
.append($('<button/>', {'class': 'esun-review-lightbox__next', type: 'button', text: '›'}))
|
||||
.appendTo(document.body);
|
||||
|
||||
$modal.on('click', '.esun-review-lightbox__close', close);
|
||||
$modal.on('click', '.esun-review-lightbox__prev', function () {
|
||||
show(($modal.data('index') || 0) - 1);
|
||||
});
|
||||
$modal.on('click', '.esun-review-lightbox__next', function () {
|
||||
show(($modal.data('index') || 0) + 1);
|
||||
});
|
||||
|
||||
return $modal;
|
||||
}
|
||||
|
||||
function stopVideo() {
|
||||
var video = $modal && $modal.find('.esun-review-lightbox__video').get(0);
|
||||
|
||||
if (!video) {
|
||||
return;
|
||||
}
|
||||
video.pause();
|
||||
video.removeAttribute('src');
|
||||
video.load();
|
||||
}
|
||||
|
||||
function show(index) {
|
||||
var media = $modal.data('media') || [],
|
||||
item,
|
||||
video;
|
||||
|
||||
if (!media.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
index = (index + media.length) % media.length;
|
||||
$modal.data('index', index);
|
||||
item = media[index];
|
||||
|
||||
if (item.type === 'video') {
|
||||
$modal.find('.esun-review-lightbox__image').attr('hidden', true).removeAttr('src');
|
||||
video = $modal.find('.esun-review-lightbox__video')
|
||||
.removeAttr('hidden')
|
||||
.attr('src', item.url)
|
||||
.get(0);
|
||||
video.load();
|
||||
video.play().catch(function () {});
|
||||
return;
|
||||
}
|
||||
|
||||
stopVideo();
|
||||
$modal.find('.esun-review-lightbox__video').attr('hidden', true);
|
||||
$modal.find('.esun-review-lightbox__image').removeAttr('hidden').attr('src', item.url);
|
||||
}
|
||||
|
||||
function close() {
|
||||
stopVideo();
|
||||
$modal.attr('hidden', true);
|
||||
}
|
||||
|
||||
function getMedia($button) {
|
||||
var media = $button.data('media'),
|
||||
images;
|
||||
|
||||
if (Array.isArray(media)) {
|
||||
return media;
|
||||
}
|
||||
images = $button.data('images') || [];
|
||||
return images.map(function (url) {
|
||||
return {type: 'image', url: url, thumb: url};
|
||||
});
|
||||
}
|
||||
|
||||
return function (config, element) {
|
||||
$(element).on('click', '.esun-review-images__thumb', function () {
|
||||
var $button = $(this),
|
||||
media = getMedia($button);
|
||||
|
||||
ensureModal();
|
||||
$modal.data('media', media).attr('hidden', false);
|
||||
show(parseInt($button.data('index'), 10) || 0);
|
||||
});
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,39 @@
|
||||
define(['jquery'], function ($) {
|
||||
'use strict';
|
||||
|
||||
return function (config, element) {
|
||||
var $root = $(element),
|
||||
voteUrl = config.voteUrl || '';
|
||||
|
||||
$root.on('click', '.esun-review-vote', function () {
|
||||
var $button = $(this),
|
||||
$votes = $button.closest('.esun-review-votes'),
|
||||
reviewId = parseInt($votes.data('review-id'), 10) || 0,
|
||||
voteType = parseInt($button.data('vote-type'), 10) || 0;
|
||||
|
||||
if (!voteUrl || !reviewId || $votes.data('can-vote') !== 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
$.ajax({
|
||||
url: voteUrl,
|
||||
type: 'POST',
|
||||
dataType: 'json',
|
||||
data: {
|
||||
review_id: reviewId,
|
||||
vote_type: voteType,
|
||||
form_key: $('input[name="form_key"]').first().val() || ($.mage && $.mage.cookies ? $.mage.cookies.get('form_key') : '')
|
||||
}
|
||||
}).done(function (response) {
|
||||
if (!response || !response.success) {
|
||||
return;
|
||||
}
|
||||
|
||||
$votes.find('[data-role="vote-up-count"]').text(response.up || 0);
|
||||
$votes.find('[data-role="vote-down-count"]').text(response.down || 0);
|
||||
$votes.find('.esun-review-vote').removeClass('is-active');
|
||||
$votes.find('[data-vote-type="' + response.customer_vote + '"]').addClass('is-active');
|
||||
});
|
||||
});
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user