This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,122 @@
define([
'jquery',
'Esun_Video/js/product-video-id-upload'
], function ($, initProductVideoUpload) {
'use strict';
return function (Widget) {
return $.widget('mage.newVideoDialog', Widget, {
/**
* HtmlContent can be inserted after Magento's data-mage-init scan,
* so initialize managed-video controls when the native dialog exists.
*/
_create: function () {
var integration;
this._super();
integration = this.element.find('#esun-product-video-integration');
if (integration.length) {
initProductVideoUpload({}, integration.get(0));
}
},
/**
* Managed MP4 videos do not require YouTube/Vimeo metadata lookup.
*/
_onGetVideoInformationEditClick: function () {
if (this._isHtml5Video()) {
this._videoRequestComplete = true;
this._renderHtml5Preview();
return;
}
this._super();
},
/**
* Prevent focus-out validation of an existing managed MP4 URL.
*/
_onGetVideoInformationFocusOut: function () {
if (this._isHtml5Video()) {
this._videoRequestComplete = true;
return;
}
this._super();
},
/**
* Keep native form validation, but skip provider URL validation for MP4.
*
* @param {Function} callback
*/
isValid: function (callback) {
var videoForm;
if (!this._isHtml5Video()) {
this._super(callback);
return;
}
videoForm = this.element.find(this._videoFormSelector);
this._blockActionButtons(true);
videoForm.mage('validation', {
errorPlacement: function (error, input) {
error.insertAfter(input);
}
});
videoForm.validation();
this._videoRequestComplete = true;
callback(videoForm.valid());
this._blockActionButtons(false);
},
/**
* Render an HTML5 preview when opening a managed MP4 gallery item.
*/
_onOpenDialog: function (event, imageData) {
this._super(event, imageData);
if (imageData && (
imageData.video_provider === 'html5' ||
/\.mp4(?:$|\?)/i.test(imageData.video_url || '')
)) {
this._videoRequestComplete = true;
this._renderHtml5Preview(imageData.video_url);
}
},
_isHtml5Video: function () {
var provider = this.element.find('[name="video_provider"]').val(),
url = this.element.find(this._videoUrlSelector).val();
return provider === 'html5' || /\.mp4(?:$|\?)/i.test(url || '');
},
_renderHtml5Preview: function (url) {
var container = this.element.find('.video-player-container'),
metadata;
url = url || this.element.find(this._videoUrlSelector).val();
container.empty();
if (!url) {
return;
}
$('<video controls preload="metadata" playsinline></video>')
.attr('src', url)
.css({width: '100%', maxHeight: '360px'})
.appendTo(container);
try {
metadata = JSON.parse(this.element.find('[name="video_metadata"]').val() || '{}');
} catch (error) {
metadata = {};
}
this.element.find('.video-information.title span')
.text(this.element.find('[name="video_title"]').val());
this.element.find('.video-information.duration span')
.text(metadata.duration ? metadata.duration + 's' : '');
this.element.find('.video-information.uploaded, .video-information.uploader').hide();
this.element.find('.video-information.title, .video-information.duration').show();
}
});
};
});
@@ -0,0 +1,210 @@
define([
'jquery',
'Magento_Ui/js/modal/modal',
'mage/translate'
], function ($, modal) {
'use strict';
return function (config, element) {
var $element = $(element),
options = $element.data('options'),
$dialog = $element.find('[data-role="mp4-upload-dialog"]'),
$form = $element.find('[data-role="esun-video-form"]'),
$progress = $element.find('[data-role="progress"]'),
$openUpload = $element.find('[data-role="open-mp4-upload"]'),
$coverInput = $element.find('[data-role="cover-image-input"]'),
$coverNote = $element.find('[data-role="cover-image-empty-note"]'),
$libraryId = $element.find('[data-role="esun-video-id-input-v2"]'),
$addLibraryVideo = $element.find('[data-role="esun-add-video-by-id-v2"]'),
libraryItems = {},
modalOptions;
if ($element.data('esunVideoInitialized')) {
return;
}
$element.data('esunVideoInitialized', true);
if (!options.enabled) {
$openUpload.prop('disabled', true)
.attr('title', $.mage.__('Video upload is disabled in configuration.'));
}
setLibraryItems(options.libraryItems || []);
$coverInput.on('change', function () {
$coverNote.toggle(!(this.files && this.files.length));
});
$libraryId.on('keydown', function (event) {
if (event.key === 'Enter') {
event.preventDefault();
addLibraryVideo();
}
});
modalOptions = {
type: 'slide',
title: $.mage.__('Upload MP4 Video'),
buttons: [{
text: $.mage.__('Upload and Add'),
class: 'action-primary',
click: upload
}, {
text: $.mage.__('Cancel'),
click: function () {
$dialog.modal('closeModal');
}
}]
};
modal(modalOptions, $dialog);
$openUpload.on('click', function () {
if (!options.enabled) {
return;
}
$dialog.modal('openModal');
});
$addLibraryVideo.on('click', addLibraryVideo);
function addLibraryVideo() {
var selectedId = $.trim($libraryId.val()),
item;
if (!/^[1-9]\d*$/.test(selectedId)) {
alert($.mage.__('Please enter a valid video ID.'));
return;
}
item = libraryItems[selectedId];
if (!item) {
$.getJSON(options.libraryUrl).done(function (response) {
setLibraryItems(response.items || []);
addLibraryItem(selectedId);
}).fail(function () {
alert($.mage.__('Unable to load the video library.'));
});
return;
}
addLibraryItem(selectedId);
}
function addLibraryItem(selectedId) {
var item = libraryItems[selectedId];
if (!item) {
alert($.mage.__('No enabled video was found for this ID.'));
return;
}
item.metadata = {
duration: item.duration,
size: item.size,
width: item.width,
height: item.height,
resolution: item.resolution
};
item.disabled = 0;
addToGallery(item);
closeDialogs();
$libraryId.val('');
}
function setLibraryItems(items) {
libraryItems = {};
$.each(items, function (index, item) {
libraryItems[String(item.id)] = item;
});
}
function upload() {
var title = $.trim($form.find('[name="title"]').val()),
file = $form.find('[name="video"]')[0].files[0],
cover = $form.find('[name="cover"]')[0].files[0],
uploadId = Date.now().toString(36) + Math.random().toString(36).slice(2),
total,
index = 0;
if (!title || !file) {
alert($.mage.__('Title and MP4 video are required.'));
return;
}
if (!/\.mp4$/i.test(file.name) || (file.type && file.type !== 'video/mp4')) {
alert($.mage.__('Only MP4 video files are supported.'));
return;
}
total = Math.ceil(file.size / options.chunkSize);
$progress.attr('hidden', false).val(0);
sendNext();
function sendNext() {
var start = index * options.chunkSize,
data = new FormData();
data.append('form_key', window.FORM_KEY);
data.append('upload_id', uploadId);
data.append('index', index);
data.append('total', total);
data.append('name', file.name);
data.append('title', title);
data.append('description', $form.find('[name="description"]').val());
data.append('status', $form.find('[name="status"]').val());
data.append('chunk', file.slice(start, Math.min(start + options.chunkSize, file.size)), file.name);
if (cover && index + 1 === total) {
data.append('cover', cover, cover.name);
}
$.ajax({
url: options.uploadUrl,
method: 'POST',
data: data,
processData: false,
contentType: false
}).done(function (response) {
index++;
$progress.val(Math.round(index / total * 100));
if (!response.completed) {
sendNext();
return;
}
addToGallery(response.video);
closeDialogs();
$form[0].reset();
$progress.attr('hidden', true).val(0);
}).fail(function (xhr) {
var response = xhr.responseJSON || {};
alert(response.message || $.mage.__('Video upload failed.'));
});
}
}
function addToGallery(video) {
var widget = $('#new-video').data('mageNewVideoDialog'),
metadata = $.extend({esun_video_id: video.id}, video.metadata),
imageData = {
file: video.gallery_file,
file_id: 'esun_video_' + video.id,
value_id: '',
label: video.title,
position: 0,
disabled: video.disabled,
removed: '',
url: video.cover_url,
media_type: 'external-video',
video_provider: 'html5',
video_url: video.video_url,
video_title: video.title,
video_description: video.description,
video_metadata: JSON.stringify(metadata),
sizeLabel: ''
};
if (!widget || typeof widget._setImage !== 'function') {
alert($.mage.__('The video was uploaded, but the product gallery could not be updated. Reload the page and select it from Video Management.'));
return;
}
widget._setImage(imageData.file, imageData);
}
function closeDialogs() {
$dialog.modal('closeModal');
$('#new-video').modal('closeModal');
}
};
});
@@ -0,0 +1,120 @@
define([
'jquery',
'Magento_Ui/js/modal/modal',
'mage/translate'
], function ($, modal) {
'use strict';
return function (config, element) {
var $element = $(element),
options = $element.data('options'),
$form = $element.find('[data-role="video-upload-form"]'),
$progress = $element.find('[data-role="upload-progress"]'),
$status = $element.find('[data-role="upload-status"]'),
$coverInput = $element.find('[data-role="cover-image-input"]'),
$coverNote = $element.find('[data-role="cover-image-empty-note"]'),
uploading = false;
$coverInput.on('change', function () {
$coverNote.toggle(!(this.files && this.files.length));
});
modal({
type: 'slide',
title: $.mage.__('Upload Video'),
buttons: [{
text: $.mage.__('Upload'),
class: 'action-primary',
click: upload
}, {
text: $.mage.__('Cancel'),
click: function () {
if (!uploading) {
$element.modal('closeModal');
}
}
}]
}, $element);
$('[data-role="open-video-upload"]').on('click', function () {
if (!options.enabled) {
alert($.mage.__('Video upload is disabled in configuration.'));
return;
}
$element.modal('openModal');
});
function upload() {
var title = $.trim($form.find('[name="title"]').val()),
fileInput = $form.find('[name="video"]')[0],
coverInput = $form.find('[name="cover"]')[0],
file = fileInput.files[0],
cover = coverInput.files[0],
uploadId,
total,
index = 0;
if (uploading) {
return;
}
if (!title || !file) {
alert($.mage.__('Title and MP4 video are required.'));
return;
}
if (!/\.mp4$/i.test(file.name) || (file.type && file.type !== 'video/mp4')) {
alert($.mage.__('Only MP4 video files are supported.'));
return;
}
uploading = true;
uploadId = Date.now().toString(36) + Math.random().toString(36).slice(2);
total = Math.ceil(file.size / options.chunkSize);
$progress.attr('hidden', false).val(0);
$status.text($.mage.__('Uploading...'));
sendNext();
function sendNext() {
var start = index * options.chunkSize,
data = new FormData();
data.append('form_key', window.FORM_KEY);
data.append('upload_id', uploadId);
data.append('index', index);
data.append('total', total);
data.append('name', file.name);
data.append('title', title);
data.append('description', $form.find('[name="description"]').val());
data.append('status', $form.find('[name="status"]').val());
data.append(
'chunk',
file.slice(start, Math.min(start + options.chunkSize, file.size)),
file.name
);
if (cover && index + 1 === total) {
data.append('cover', cover, cover.name);
}
$.ajax({
url: options.uploadUrl,
method: 'POST',
data: data,
processData: false,
contentType: false
}).done(function (response) {
index++;
$progress.val(Math.round(index / total * 100));
if (!response.completed) {
sendNext();
return;
}
$status.text($.mage.__('Upload completed. Refreshing...'));
window.location.replace(options.indexUrl);
}).fail(function (xhr) {
var response = xhr.responseJSON || {};
uploading = false;
$status.text(response.message || $.mage.__('Video upload failed.'));
});
}
}
};
});