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; } $('') .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(); } }); }; });