{#
* Plugin Name : ProductPlus
*
* Copyright (C) BraTech Co., Ltd. All Rights Reserved.
* http://www.bratech.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
#}
<script>
$(function() {
{% for ProductItem in ProductItems %}
{% if ProductItem.input_type == constant('Plugin\\ProductPlus42\\Entity\\ProductItem::IMAGE_TYPE') %}
{% set name = 'productplus_' ~ ProductItem.id %}
{% set image_name = 'productplus_' ~ ProductItem.id ~ '_images' %}
{% set add_name = 'productplus_' ~ ProductItem.id ~ '_add_images' %}
{% set delete_name = 'productplus_' ~ ProductItem.id ~ '_delete_images' %}
// ファイルアップロード
var inputFileElement = document.querySelector('#admin_product_productplus_{{ ProductItem.id}}');
FilePond.setOptions({
server: {
process: {
url: '{{ path('admin_product_image_process') }}',
headers: {
'ECCUBE-CSRF-TOKEN': $('meta[name="eccube-csrf-token"]').attr('content'),
'X-Requested-With': 'XMLHttpRequest'
}
},
load: {
url: '{{ path('admin_productplus_image_load') }}?source=',
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
},
revert: {
url: '{{ path('admin_product_image_revert') }}',
headers: {
'ECCUBE-CSRF-TOKEN': $('meta[name="eccube-csrf-token"]').attr('content'),
'X-Requested-With': 'XMLHttpRequest'
}
}
}
});
var pond_{{ name }} = FilePond.create(inputFileElement, {
allowFileTypeValidation: true,
acceptedFileTypes: [
'image/gif',
'image/png',
'image/jpeg'
],
allowFileSizeValidation: true,
maxFileSize: 10000000,
maxFiles: 10,
allowBrowse: true,
allowDrop: true,
allowReorder: true,
labelIdle: '<i class="fa fa-cloud-upload fa-3x text-ec-lightGray mx-3 align-middle" aria-hidden="true" style="font-size: 40px"></i>{{ 'admin.common.drag_and_drop_image_description'|trans }}<span class="filepond--label-action">{{ 'admin.common.file_select'|trans }}</span>',
// 保存されている画像のロード
files: [
{% for image in form[image_name] %}
{
source: '{{ image.vars.value }}',
options: {
type: 'local'
}
},
{% endfor %}
// 追加してすぐの画像のロード. バリデーションエラーの場合など.
{% for add_image in form[add_name] %}
{
source: '{{ add_image.vars.value }}',
options: {
type: 'local'
}
},
{% endfor %}
]
});
// 画像が追加されたら add_images にファイル名を追加する
var proto_add_{{ name }} = '{{ form_widget(form[add_name].vars.prototype) }}';
pond_{{ name }}.on('processfile', function(error, file) {
if (error) {
console.log(error);
} else {
$('#upload-zone-{{ProductItem.id}}').append(
$(proto_add_{{ name }}.replace(/__name__/g, file.id))
.val(file.serverId)
.addClass('productplus_{{ProductItem.id}}_add_images')
);
}
});
// 画像が削除されたら delete_images にファイル名を追加する
var proto_del_{{ name }} = '{{ form_widget(form[delete_name].vars.prototype) }}';
pond_{{ name }}.on('removefile', function(error, file) {
if (error) {
console.log(error);
} else {
// file.serverId にはアップロードしたファイル名が格納される.
// DBに登録されると URL path が入るためファイル名のみ抜き出す.
if (file.serverId) {
$('#upload-zone-{{ProductItem.id}}').append(
$(proto_del_{{ name }}.replace(/__name__/g, file.id))
.val(file.serverId.split('/').pop())
.addClass('productplus_{{ProductItem.id}}_del_images')
);
}
// 追加してすぐ削除した画像があれば削除する
$('#upload-zone-{{ProductItem.id}}').find('#admin_product_productplus_{{ProductItem.id}}_add_images_' + file.id).remove(); // 追加してすぐ削除した画像
$('#upload-zone-{{ProductItem.id}}').find('.productplus_{{ProductItem.id}}_add_images[value="' + file.filename + '"]').remove(); // 追加後, バリデーションエラーが発生した後に削除した画像
}
});
pond_{{ name }}.on('reorderfiles', function(files, origin, target) {
$image_files = $('[name^="admin_product[productplus_{{ProductItem.id}}_images]"]');
$target = $image_files.parent();
$image_files.remove();
for(index in files){
$target.append('<input type="hidden" id="admin_product_productplus_{{ProductItem.id}}_images_' + index + '" name="admin_product[productplus_{{ProductItem.id}}_images]['+ index +']" value="'+ files[index].filename +'" class="productplus_{{ProductItem.id}}_images">');
}
});
// バリデーションエラーが出た場合に画像を保持するための hidden を追加しておく
var proto_image_{{ name }} = '{{ form_widget(form[image_name].vars.prototype) }}';
{% for image in form[image_name] %}
$('#upload-zone-{{ProductItem.id}}').append(
$(proto_image_{{ name }}.replace(/__name__/g, '{{ loop.index0 }}'))
.val('{{ image.vars.value }}')
.addClass('productplus_{{ProductItem.id}}_images')
);
{% endfor %}
{% for add_image in form[add_name] %}
$('#upload-zone-{{ProductItem.id}}').append(
$('{{ form_widget(add_image) }}')
.val('{{ add_image.vars.value }}')
.addClass('productplus_{{ProductItem.id}}_add_images')
);
{% endfor %}
{% for delete_image in form[delete_name] %}
$('#upload-zone-{{ProductItem.id}}').append(
$('{{ form_widget(delete_image) }}').addClass('productplus_{{ProductItem.id}}_del_images')
);
{% endfor %}
{% endif %}
{% endfor %}
})
</script>