Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve openDialog method initialization #27566

Merged
merged 28 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c839ecc
magento/adobe-stock-integration#935 : Loader stays over the media gal…
Apr 6, 2020
e5e23a8
magento/adobe-stock-integration#935 : Loader stays over the media gal…
Apr 6, 2020
7ba5410
magento/magento2#27566: Do not re-load the gallery app, just content
Apr 7, 2020
f8730c9
Refactor to change targetElementId dynamically
Nazar65 Apr 9, 2020
dba6863
cover new behavior with unit tests, static test fix
Nazar65 Apr 9, 2020
2da1025
corectly bind this
Nazar65 Apr 9, 2020
9b21471
update unit test
Nazar65 Apr 9, 2020
610cd62
use modalLoaded per review comments
Nazar65 Apr 9, 2020
26e4df5
correct unit tests
Nazar65 Apr 9, 2020
8a5def7
Merge branch '2.4-develop' into support-new-media-gallery
slavvka Apr 14, 2020
2c74e2f
Merge branch '2.4-develop' into support-new-media-gallery
slavvka Apr 15, 2020
67c2c0c
Improve opendialogurl configuration to be extensible
Nazar65 Apr 15, 2020
25e5df1
refactor pre review comments static test fix
Nazar65 Apr 15, 2020
e4cdee3
resolve dependency
Nazar65 Apr 15, 2020
d6fabe2
remove wrong added dependency
Nazar65 Apr 15, 2020
6f3cc44
Add openDialogConfig to all instance initialization
Nazar65 Apr 15, 2020
cbb1f4a
remove redundant dependency
Nazar65 Apr 15, 2020
b0afe12
Merge branch '2.4-develop' into support-new-media-gallery
slavvka Apr 15, 2020
8f338a9
Merge branch '2.4-develop' into improve-opendialogurl-config
slavvka Apr 15, 2020
9b75b8d
Merge branch '2.4-develop' into improve-opendialogurl-config
slavvka Apr 16, 2020
15a1861
Merge branch '2.4-develop' into support-new-media-gallery
slavvka Apr 16, 2020
cdcf858
Add DataObject as configuration param, to be more extesible
Nazar65 Apr 16, 2020
c94a81c
Merge branch 'improve-opendialogurl-config' of https://github.com/Naz…
Nazar65 Apr 16, 2020
edf4017
correct event listener to reload media gallery
Nazar65 Apr 16, 2020
0abc548
Merge branch 'support-new-media-gallery' of https://github.com/Nazar6…
Nazar65 Apr 16, 2020
9387809
Merge branch '2.4-develop' of https://github.com/magento/magento2 int…
Nazar65 Apr 16, 2020
aa82c97
Merge remote-tracking branch 'origin/improve-opendialogurl-config' in…
Nazar65 Apr 16, 2020
41f7115
Merge branch '2.4-develop' into support-new-media-gallery
lenaorobei Apr 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ define([
openDialogUrl += '&current_tree_path=' + Base64.mageEncode(this.mediaGallery.initialOpenSubpath);
}

browser.openDialog(openDialogUrl, null, null, this.mediaGallery.openDialogTitle);
browser.openDialog(
openDialogUrl,
null,
null,
this.mediaGallery.openDialogTitle,
{
targetElementId: $buttonEl.attr('id')
}
);
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ define([
'?isAjax=true&current_tree_path=d3lzaXd5Zw,,',
null,
null,
'Hello world'
'Hello world',
{
targetElementId: 'theTargetId'
}
);
});
});
Expand Down
5 changes: 3 additions & 2 deletions dev/tests/js/jasmine/tests/lib/mage/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ define([
*/
done: function () {
obj.targetElementId = 1;
obj.modalLoaded = true;
}
};
});
Expand All @@ -58,8 +59,8 @@ define([
}
};
});
obj.openDialog('instance/url', 100, 100, 'title', undefined);
obj.openDialog('instance/url', 100, 100, 'title', undefined);
obj.openDialog('instance/url/target_element_id/YDW2424/', 100, 100, 'title', undefined);
obj.openDialog('instance/target_element_id/Y45GDRg/', 100, 100, 'title', undefined);
expect($.ajax.calls.count()).toBe(1);
});
});
Expand Down
26 changes: 15 additions & 11 deletions lib/internal/Magento/Framework/Data/Form/Element/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,19 @@ protected function _getPluginButtonsHtml($visible = true)

// Button to media images insertion window
if ($this->getConfig('add_images')) {
$htmlId = $this->getHtmlId();
$url = $this->getConfig('files_browser_window_url')
. 'target_element_id/'
. $htmlId
. '/'
. (null !== $this->getConfig('store_id')
? 'store/' . $this->getConfig('store_id') . '/"'
: '');
$buttonsHtml .= $this->_getButtonHtml(
[
'title' => $this->translate('Insert Image...'),
'onclick' => "MediabrowserUtility.openDialog('"
. $this->getConfig('files_browser_window_url')
. "target_element_id/" . $this->getHtmlId() . "/"
. (null !== $this->getConfig('store_id') ? 'store/'
. $this->getConfig('store_id') . '/' : '')
. "')",
'onclick' => 'MediabrowserUtility.openDialog(\'' . $url
. '\', null, null, null, { \'targetElementId\': \'' . $htmlId . '\' })',
'class' => 'action-add-image plugin',
'style' => $visible ? '' : 'display:none',
]
Expand Down Expand Up @@ -496,13 +500,13 @@ protected function getInlineJs($jsSetupObject, $forceLoad)
$jsString = '
<script type="text/javascript">
//<![CDATA[
window.tinyMCE_GZ = window.tinyMCE_GZ || {};
window.tinyMCE_GZ = window.tinyMCE_GZ || {};
window.tinyMCE_GZ.loaded = true;
require([
"jquery",
"mage/translate",
"mage/adminhtml/events",
"mage/adminhtml/wysiwyg/tiny_mce/setup",
"jquery",
"mage/translate",
"mage/adminhtml/events",
"mage/adminhtml/wysiwyg/tiny_mce/setup",
"mage/adminhtml/wysiwyg/widget"
], function(jQuery){' .
"\n" .
Expand Down
85 changes: 43 additions & 42 deletions lib/web/mage/adminhtml/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ define([
window.MediabrowserUtility = {
windowId: 'modal_dialog_message',
modalLoaded: false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, re-use modaLoaded property, because removing it is backward-incompatible change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, but then we need create a separate PR to remove this line https://github.com/magento/magento2-page-builder/blob/1605bd01f19e4b503aa97a0e78e9e828a81db0b3/app/code/Magento/PageBuilder/view/adminhtml/web/js/form/element/image-uploader.js#L79 in PageBuilder Repository, because we have always overridden this property, and gallery will initialized second time

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done ✔️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@omiroshnichenko @Nazar65 This class is not API, and we shouldn't follow backward compatibility here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate PR opened at PageBuilder repo. magento/magento2-page-builder#449

targetElementId: false,

/**
* @return {Number}
Expand Down Expand Up @@ -50,56 +51,54 @@ define([
*/
openDialog: function (url, width, height, title, options) {
var windowId = this.windowId,
content = '<div class="popup-window" id="' + windowId + '"></div>',
self = this;
content = '<div class="popup-window" id="' + windowId + '"></div>';

if (options &&
self.targetElementId &&
self.targetElementId === options.targetElementId) {
if (typeof options.closed !== 'undefined') {
if (this.modalLoaded) {

if (!_.isUndefined(options)) {
this.modal.modal('option', 'closed', options.closed);
}
this.modal.modal('openModal');

return;
} else if (_.isUndefined(options) &&
self.modalLoaded === true &&
self.targetElementId === url
) {
this.modal.modal('openModal');
this.setTargetElementId(options, url);
$(window).trigger('reload.MediaGallery');

return;
}

if (this.modal) {
this.modal.html($(content).html());
this.modal = $(content).modal($.extend({
title: title || 'Insert File...',
modalClass: 'magento',
type: 'slide',
buttons: []
}, options));

if (options && typeof options.closed !== 'undefined') {
this.modal.modal('option', 'closed', options.closed);
}
} else {
this.modal = $(content).modal($.extend({
title: title || 'Insert File...',
modalClass: 'magento',
type: 'slide',
buttons: []
}, options));
}
this.modal.modal('openModal');

$.ajax({
url: url,
type: 'get',
context: $(this),
showLoader: true

}).done(function (data) {
self.modal.html(data).trigger('contentUpdated');
self.modalLoaded = true;
self.targetElementId = options ?
options.targetElementId
: url;
});
this.modal.html(data).trigger('contentUpdated');
this.modalLoaded = true;
this.setTargetElementId(options, url);
}.bind(this));

},

/**
* Setter for targetElementId property
*
* @param {Object} options
* @param {String} url
*/
setTargetElementId: function (options, url) {
this.targetElementId = options && options.targetElementId ?
options.targetElementId
: url.match(/\/target_element_id\/([\s\S].*?)\//)[1];
},

/**
Expand All @@ -113,7 +112,6 @@ define([
$.widget('mage.mediabrowser', {
eventPrefix: 'mediabrowser',
options: {
targetElementId: null,
contentsUrl: null,
onInsertUrl: null,
newFolderUrl: null,
Expand All @@ -140,7 +138,8 @@ define([
'click #delete_files': 'deleteFiles',
'click #insert_files': 'insertSelectedFiles',
'fileuploaddone': '_uploadDone',
'click [data-row=breadcrumb]': 'selectFolder'
'click [data-row=breadcrumb]': 'selectFolder',
'reload.MediaGallery': 'reload'
});
this.activeNode = null;
//tree dont use event bubbling
Expand Down Expand Up @@ -322,16 +321,18 @@ define([
* return {HTMLElement|null}
*/
getTargetElement: function () {
var opener, targetElementId;
var opener,
targetElementId,
mediaBrowser = window.MediabrowserUtility;

if (typeof wysiwyg != 'undefined' && wysiwyg.get(this.options.targetElementId)) {
if (!_.isUndefined(wysiwyg) && wysiwyg.get(mediaBrowser.targetElementId)) {
opener = this.getMediaBrowserOpener() || window;
targetElementId = tinyMceEditors.get(this.options.targetElementId).getMediaBrowserTargetElementId();
targetElementId = tinyMceEditors.get(mediaBrowser.targetElementId).getMediaBrowserTargetElementId();

return $(opener.document.getElementById(targetElementId));
}

return $('#' + this.options.targetElementId);
return $('#' + mediaBrowser.targetElementId);
},

/**
Expand All @@ -340,12 +341,12 @@ define([
* return {Object|null}
*/
getMediaBrowserOpener: function () {
if (typeof wysiwyg != 'undefined' &&
wysiwyg.get(this.options.targetElementId) &&
typeof tinyMceEditors != 'undefined' &&
!tinyMceEditors.get(this.options.targetElementId).getMediaBrowserOpener().closed
var targetElementId = window.MediabrowserUtility.targetElementId;

if (!_.isUndefined(wysiwyg) && wysiwyg.get(targetElementId) && !_.isUndefined(tinyMceEditors) &&
!tinyMceEditors.get(targetElementId).getMediaBrowserOpener().closed
) {
return tinyMceEditors.get(this.options.targetElementId).getMediaBrowserOpener();
return tinyMceEditors.get(targetElementId).getMediaBrowserOpener();
}

return null;
Expand Down