Skip to content

Commit

Permalink
feat: add message events to the unit page container
Browse files Browse the repository at this point in the history
  • Loading branch information
ihor-romaniuk committed Oct 22, 2024
1 parent 2fa4ac9 commit d62e051
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions cms/static/js/views/pages/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ function($, _, Backbone, gettext, BasePage,

}

window.addEventListener('message', (event) => {
if (event.data && event.data.type === 'refreshXBlock') {
this.render();
}
});

this.listenTo(Backbone, 'move:onXBlockMoved', this.onXBlockMoved);
},

Expand Down Expand Up @@ -222,6 +228,7 @@ function($, _, Backbone, gettext, BasePage,
},

initializePasteButton() {
var self = this;
if (this.options.canEdit && !self.options.isIframeEmbed) {
// We should have the user's clipboard status.
const data = this.options.clipboardData;
Expand All @@ -237,6 +244,7 @@ function($, _, Backbone, gettext, BasePage,
* Given the latest information about the user's clipboard, hide or show the Paste button as appropriate.
*/
refreshPasteButton(data) {
var self = this;
// Do not perform any changes on paste button since they are not
// rendered on Library or LibraryContent pages
if (!this.isLibraryPage && !this.isLibraryContentPage && !self.options.isIframeEmbed) {
Expand Down Expand Up @@ -554,26 +562,40 @@ function($, _, Backbone, gettext, BasePage,
},

showMoveXBlockModal: function(event) {
var xblockElement = this.findXBlockElement(event.target),
parentXBlockElement = xblockElement.parents('.studio-xblock-wrapper'),
sourceXBlockInfo = XBlockUtils.findXBlockInfo(xblockElement, this.model),
sourceParentXBlockInfo = XBlockUtils.findXBlockInfo(parentXBlockElement, this.model),
modal = new MoveXBlockModal({
sourceXBlockInfo: sourceXBlockInfo,
sourceParentXBlockInfo: sourceParentXBlockInfo,
XBlockURLRoot: this.getURLRoot(),
outlineURL: this.options.outlineURL
});

try {
if (this.options.isIframeEmbed) {
window.parent.postMessage(
{
type: 'showMoveXBlockModal',
payload: {}
payload: {
sourceXBlockInfo: {
id: sourceXBlockInfo.attributes.id,
displayName: sourceXBlockInfo.attributes.display_name,
},
sourceParentXBlockInfo: {
id: sourceParentXBlockInfo.attributes.id,
category: sourceParentXBlockInfo.attributes.category,
hasChildren: sourceParentXBlockInfo.attributes.has_children,
},
},
}, document.referrer
);
return true;
}
} catch (e) {
console.error(e);
}
var xblockElement = this.findXBlockElement(event.target),
parentXBlockElement = xblockElement.parents('.studio-xblock-wrapper'),
modal = new MoveXBlockModal({
sourceXBlockInfo: XBlockUtils.findXBlockInfo(xblockElement, this.model),
sourceParentXBlockInfo: XBlockUtils.findXBlockInfo(parentXBlockElement, this.model),
XBlockURLRoot: this.getURLRoot(),
outlineURL: this.options.outlineURL
});

event.preventDefault();
modal.show();
Expand Down

0 comments on commit d62e051

Please sign in to comment.