Skip to content

Commit

Permalink
Merge pull request #443 from magento-folks/product_media
Browse files Browse the repository at this point in the history
[Folks] Media Gallery on Product Page
  • Loading branch information
irenelagno committed Mar 18, 2016
2 parents 044bada + 6d2bcb0 commit 8320035
Show file tree
Hide file tree
Showing 18 changed files with 429 additions and 329 deletions.
12 changes: 12 additions & 0 deletions app/code/Magento/Backend/view/adminhtml/requirejs-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
/*eslint no-unused-vars: 0*/
var config = {
map: {
'*': {
'mediaUploader': 'Magento_Backend/js/media-uploader'
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@
/** @var $block \Magento\Backend\Block\Media\Uploader */
?>

<div id="<?php echo $block->getHtmlId() ?>" class="uploader">
<div id="<?php echo $block->getHtmlId() ?>" class="uploader"
data-mage-init='{
"Magento_Backend/js/media-uploader" : {
"maxFileSize": <?php /* @escapeNotVerified */ echo $block->getFileSizeService()->getMaxFileSize() ?>,
"maxWidth":<?php /* @escapeNotVerified */ echo \Magento\Framework\File\Uploader::MAX_IMAGE_WIDTH ?> ,
"maxHeight": <?php /* @escapeNotVerified */ echo \Magento\Framework\File\Uploader::MAX_IMAGE_HEIGHT ?>
}
}'
>
<div class="fileinput-button form-buttons button">
<span><?php /* @escapeNotVerified */ echo __('Browse Files...') ?></span>
<input id="fileupload" type="file" name="<?php /* @escapeNotVerified */ echo $block->getConfig()->getFileField() ?>"
data-url="<?php /* @escapeNotVerified */ echo $block->getConfig()->getUrl() ?>" multiple="multiple" />
</div>
<div class="clear"></div>
<script id="<?php echo $block->getHtmlId() ?>-template" type="text/x-magento-template">
<script id="<?php echo $block->getHtmlId() ?>-template" type="text/x-magento-template" data-template="uploader">
<div id="<%- data.id %>" class="file-row">
<span class="file-info"><%- data.name %> (<%- data.size %>)</span>
<div class="progressbar-container">
Expand All @@ -27,88 +35,3 @@
</script>
</div>

<script>
require([
"jquery",
'mage/template',
'Magento_Ui/js/modal/alert',
"mage/translate",
"jquery/file-uploader"
], function ($, mageTemplate, alert) {

$('#fileupload').fileupload({
dataType: 'json',
formData: {
'form_key': window.FORM_KEY
},
dropZone: '[data-tab-panel=image-management]',
sequentialUploads: true,
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
maxFileSize: <?php /* @escapeNotVerified */ echo $block->getFileSizeService()->getMaxFileSize() ?> ,
add: function (e, data) {
var progressTmpl = mageTemplate('#<?php echo $block->getHtmlId(); ?>-template'),
fileSize,
tmpl;

$.each(data.files, function (index, file) {
fileSize = typeof file.size == "undefined" ?
$.mage.__('We could not detect a size.') :
byteConvert(file.size);

data.fileId = Math.random().toString(33).substr(2, 18);

tmpl = progressTmpl({
data: {
name: file.name,
size: fileSize,
id: data.fileId
}
});

$(tmpl).appendTo('#<?php echo $block->getHtmlId() ?>');
});

$(this).fileupload('process', data).done(function () {
data.submit();
});
},
done: function (e, data) {
if (data.result && !data.result.error) {
$('#<?php echo $block->getParentBlock()->getHtmlId() ?>').trigger('addItem', data.result);
} else {
$('#' + data.fileId)
.delay(2000)
.hide('highlight');
alert({
content: $.mage.__('We don\'t recognize or support this file extension type.')
});
}
$('#' + data.fileId).remove();
},
progress: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
var progressSelector = '#' + data.fileId + ' .progressbar-container .progressbar';
$(progressSelector).css('width', progress + '%');
},
fail: function (e, data) {
var progressSelector = '#' + data.fileId;
$(progressSelector).removeClass('upload-progress').addClass('upload-failure')
.delay(2000)
.hide('highlight')
.remove();
}
});
$('#fileupload').fileupload('option', {
process: [{
action: 'load',
fileTypes: /^image\/(gif|jpeg|png)$/
}, {
action: 'resize',
maxWidth: <?php /* @escapeNotVerified */ echo \Magento\Framework\File\Uploader::MAX_IMAGE_WIDTH ?> ,
maxHeight: <?php /* @escapeNotVerified */ echo \Magento\Framework\File\Uploader::MAX_IMAGE_HEIGHT ?>
}, {
action: 'save'
}]
});
});
</script>
125 changes: 125 additions & 0 deletions app/code/Magento/Backend/view/adminhtml/web/js/media-uploader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
/*global byteConvert*/
define([
'jquery',
'mage/template',
'Magento_Ui/js/modal/alert',
'mage/translate',
'jquery/file-uploader'
], function ($, mageTemplate, alert) {
'use strict';

$.widget('mage.mediaUploader', {

/**
*
* @private
*/
_create: function () {
var
self = this,
progressTmpl = mageTemplate('[data-template="uploader"]');

this.element.find('input[type=file]').fileupload({
dataType: 'json',
formData: {
'form_key': window.FORM_KEY
},
dropZone: '[data-tab-panel=image-management]',
sequentialUploads: true,
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
maxFileSize: this.options.maxFileSize,

/**
* @param {Object} e
* @param {Object} data
*/
add: function (e, data) {
var
fileSize,
tmpl;

$.each(data.files, function (index, file) {
fileSize = typeof file.size == 'undefined' ?
$.mage.__('We could not detect a size.') :
byteConvert(file.size);

data.fileId = Math.random().toString(33).substr(2, 18);

tmpl = progressTmpl({
data: {
name: file.name,
size: fileSize,
id: data.fileId
}
});

$(tmpl).appendTo(self.element);
});

$(this).fileupload('process', data).done(function () {
data.submit();
});
},

/**
* @param {Object} e
* @param {Object} data
*/
done: function (e, data) {
if (data.result && !data.result.error) {
self.element.trigger('addItem', data.result);
} else {
alert({
content: $.mage.__('We don\'t recognize or support this file extension type.')
});
}
self.element.find('#' + data.fileId).remove();
},

/**
* @param {Object} e
* @param {Object} data
*/
progress: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10),
progressSelector = '#' + data.fileId + ' .progressbar-container .progressbar';

self.element.find(progressSelector).css('width', progress + '%');
},

/**
* @param {Object} e
* @param {Object} data
*/
fail: function (e, data) {
var progressSelector = '#' + data.fileId;

self.element.find(progressSelector).removeClass('upload-progress').addClass('upload-failure')
.delay(2000)
.hide('highlight')
.remove();
}
});

this.element.find('input[type=file]').fileupload('option', {
process: [{
action: 'load',
fileTypes: /^image\/(gif|jpeg|png)$/
}, {
action: 'resize',
maxWidth: this.options.maxWidth,
maxHeight: this.options.maxHeight
}, {
action: 'save'
}]
});
}
});

return $.mage.mediaUploader;
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,36 @@ class Gallery extends \Magento\Framework\View\Element\AbstractBlock
{
/**
* Gallery field name suffix
*
* @var string
*/
private static $FIELD_NAME_SUFFIX = 'product';
protected $fieldNameSuffix = 'product';

/**
* Gallery html id
*
* @var string
*/
private static $HTML_ID = 'media_gallery';
protected $htmlId = 'media_gallery';

/**
* Gallery name
*
* @var string
*/
private static $NAME = 'product[media_gallery]';
protected $name = 'product[media_gallery]';

/**
* Html id for data scope
*
* @var string
*/
protected $image = 'image';

/**
* @var string
*/
private static $IMAGE = 'image';
protected $formName = 'product_form';

/**
* @var \Magento\Store\Model\StoreManagerInterface
Expand Down Expand Up @@ -102,8 +115,9 @@ public function getImages()
public function getContentHtml()
{
/* @var $content \Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery\Content */
$content = $this->_layout->createBlock('Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery\Content');
$content = $this->getChildBlock('content');
$content->setId($this->getHtmlId() . '_content')->setElement($this);
$content->setFormName($this->formName);
$galleryJs = $content->getJsObjectName();
$content->getUploader()->getConfig()->setMegiaGallery($galleryJs);
return $content->toHtml();
Expand All @@ -114,31 +128,31 @@ public function getContentHtml()
*/
protected function getHtmlId()
{
return static::$HTML_ID;
return $this->htmlId;
}

/**
* @return string
*/
public function getName()
{
return static::$NAME;
return $this->name;
}

/**
* @return string
*/
public function getFieldNameSuffix()
{
return static::$FIELD_NAME_SUFFIX;
return $this->fieldNameSuffix;
}

/**
* @return string
*/
public function getDataScopeHtmlId()
{
return static::$IMAGE;
return $this->image;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions app/code/Magento/Catalog/Model/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\Exception\ValidatorException;
use Magento\Framework\Exception\CouldNotSaveException;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand Down Expand Up @@ -521,6 +523,8 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
$product->getData($exception->getAttributeCode()),
$exception
);
} catch (ValidatorException $e) {
throw new CouldNotSaveException(__($e->getMessage()));
} catch (\Exception $e) {
throw new \Magento\Framework\Exception\CouldNotSaveException(__('Unable to save product'));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ protected function setUp()
'getProductLinks',
'setProductLinks',
'validate',
'save'
'save',
'getMediaGalleryEntries'
],
[],
'',
Expand Down Expand Up @@ -1248,7 +1249,7 @@ public function testSaveExistingWithMediaGalleryEntries()
->method('setMediaAttribute')
->with($this->initializedProductMock, ['image', 'small_image'], 'filename1');
$this->initializedProductMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);

$this->productMock->expects($this->any())->method('getMediaGalleryEntries')->willReturn(null);
$this->model->save($this->productMock);
$this->assertEquals($expectedResult, $this->initializedProductMock->getMediaGallery('images'));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<item name="componentType" xsi:type="string">fieldset</item>
</argument>
</arguments>
<block class="Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery\Content" as="content">
<block class="Magento\ProductVideo\Block\Adminhtml\Product\Edit\NewVideo" name="new-video"
template="Magento_ProductVideo::product/edit/slideout/form.phtml"/>
</block>
</block>
</referenceContainer>
</referenceContainer>
Expand Down
Loading

0 comments on commit 8320035

Please sign in to comment.