5
5
*/
6
6
7
7
/** @var $block \Magento\Cms\Block\Adminhtml\Wysiwyg\Images\Content\Uploader */
8
- /** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
9
8
10
- $ filters = $ block ->getConfig ()->getFilters () ?? [];
11
- $ allowedExtensions = [];
12
9
$ blockHtmlId = $ block ->getHtmlId ();
13
-
14
- $ listExtensions = [];
15
- foreach ($ filters as $ media_type ) {
16
- $ listExtensions [] = array_map (function ($ fileExt ) {
17
- return ltrim ($ fileExt , '.* ' );
18
- }, $ media_type ['files ' ]);
19
- }
20
-
21
- $ allowedExtensions = array_merge ([], ...$ listExtensions );
22
-
23
- $ resizeConfig = $ block ->getImageUploadConfigData ()->getIsResizeEnabled ()
24
- ? "{action: 'resize', maxWidth: "
25
- . $ block ->escapeHtml ($ block ->getImageUploadMaxWidth ())
26
- . ", maxHeight: "
27
- . $ block ->escapeHtml ($ block ->getImageUploadMaxHeight ())
28
- . "} "
29
- : "{action: 'resize'} " ;
30
10
?>
31
11
32
- <div id="<?= /* @noEscape */ $ blockHtmlId ?> " class="uploader">
12
+ <div id="<?= /* @noEscape */ $ blockHtmlId ?> " class="uploader"
13
+ data-mage-init='{
14
+ "Magento_Backend/js/media-uploader" : {
15
+ "maxFileSize": <?= /* @noEscape */ $ block ->getFileSizeService ()->getMaxFileSize () ?> ,
16
+ "maxWidth": <?= /* @noEscape */ $ block ->getImageUploadMaxWidth () ?> ,
17
+ "maxHeight": <?= /* @noEscape */ $ block ->getImageUploadMaxHeight () ?> ,
18
+ "isResizeEnabled": <?= /* @noEscape */ $ block ->getImageUploadConfigData ()->getIsResizeEnabled () ?>
19
+ }
20
+ }'
21
+ >
33
22
<span class="fileinput-button form-buttons">
34
23
<span><?= $ block ->escapeHtml (__ ('Upload Images ' )) ?> </span>
35
24
<input class="fileupload" type="file"
@@ -46,130 +35,4 @@ $resizeConfig = $block->getImageUploadConfigData()->getIsResizeEnabled()
46
35
<div class="clear"></div>
47
36
</div>
48
37
</script>
49
- <?php $ intMaxSize = $ block ->getFileSizeService ()->getMaxFileSize ();
50
- $ resizeConfig = /* @noEscape */ $ resizeConfig ;
51
- $ blockHtmlId = /* @noEscape */ $ blockHtmlId ;
52
- $ scriptString = <<<script
53
-
54
- require([
55
- 'jquery',
56
- 'mage/template',
57
- 'Magento_Ui/js/lib/validation/validator',
58
- 'Magento_Ui/js/modal/alert',
59
- 'jquery/file-uploader',
60
- 'domReady!',
61
- 'mage/translate'
62
- ], function ($, mageTemplate, validator, uiAlert) {
63
- var maxFileSize = {$ block ->escapeJs ($ block ->getFileSizeService ()->getMaxFileSize ())},
64
- allowedExtensions = ' {$ block ->escapeJs (implode (' ' , $ allowedExtensions ))}';
65
-
66
- $('# {$ blockHtmlId } .fileupload').fileupload({
67
- dataType: 'json',
68
- formData: {
69
- isAjax: 'true',
70
- form_key: FORM_KEY
71
- },
72
- sequentialUploads: true,
73
- acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
74
- allowedExtensions: allowedExtensions,
75
- maxFileSize: maxFileSize,
76
- dropZone: $('# {$ blockHtmlId }').closest('[role="dialog"]'),
77
- add: function (e, data) {
78
- var progressTmpl = mageTemplate('# {$ blockHtmlId }-template'),
79
- fileSize,
80
- tmpl,
81
- validationResult;
82
-
83
- data.files = data.files.filter(function (file) {
84
- fileSize = typeof file.size == "undefined" ?
85
- $.mage.__('We could not detect a size.') :
86
- byteConvert(file.size);
87
-
88
- if (maxFileSize) {
89
- validationResult = validator('validate-max-size', file.size, maxFileSize);
90
-
91
- if (!validationResult.passed) {
92
- uiAlert({
93
- content: validationResult.message
94
- });
95
-
96
- return false;
97
- }
98
- }
99
-
100
- if (allowedExtensions) {
101
- validationResult = validator('validate-file-type', file.name, allowedExtensions);
102
-
103
- if (!validationResult.passed) {
104
- uiAlert({
105
- content: validationResult.message
106
- });
107
-
108
- return false;
109
- }
110
- }
111
-
112
- data.fileId = Math.random().toString(36).substr(2, 9);
113
-
114
- tmpl = progressTmpl({
115
- data: {
116
- name: file.name,
117
- size: fileSize,
118
- id: data.fileId
119
- }
120
- });
121
-
122
- $(tmpl).data('image', data).appendTo('# {$ blockHtmlId }');
123
-
124
- return true;
125
- });
126
-
127
- if (data.files.length) {
128
- $(this).fileupload('process', data).done(function () {
129
- data.submit();
130
- });
131
- }
132
- },
133
- done: function (e, data) {
134
- var progressSelector = '#' + data.fileId + ' .progressbar-container .progressbar';
135
- var tempErrorMessage = document.createElement("div");
136
- $(progressSelector).css('width', '100%');
137
- $('[data-action="show-error"]').children(".message").remove();
138
- if (data.result && !data.result.hasOwnProperty('errorcode')) {
139
- $(progressSelector).removeClass('upload-progress').addClass('upload-success');
140
- } else {
141
- tempErrorMessage.className = "message message-warning warning";
142
- tempErrorMessage.innerHTML = data.result.error;
143
-
144
- $('[data-action="show-error"]').append(tempErrorMessage);
145
- $(progressSelector).removeClass('upload-progress').addClass('upload-failure');
146
- }
147
- },
148
- progress: function (e, data) {
149
- var progress = parseInt(data.loaded / data.total * 100, 10);
150
- var progressSelector = '#' + data.fileId + ' .progressbar-container .progressbar';
151
- $(progressSelector).css('width', progress + '%');
152
- },
153
- fail: function (e, data) {
154
- var progressSelector = '#' + data.fileId + ' .progressbar-container .progressbar';
155
- $(progressSelector).removeClass('upload-progress').addClass('upload-failure');
156
- }
157
- });
158
-
159
- $('# {$ blockHtmlId } .fileupload').fileupload('option', {
160
- process: [{
161
- action: 'load',
162
- fileTypes: /^image\/(gif|jpeg|png)$/,
163
- maxFileSize: {$ intMaxSize } * 10
164
- },
165
- {$ resizeConfig },
166
- {
167
- action: 'save'
168
- }]
169
- });
170
- });
171
-
172
- script ;
173
- ?>
174
- <?= /* @noEscape */ $ secureRenderer ->renderTag ('script ' , [], $ scriptString , false ) ?>
175
38
</div>
0 commit comments