Skip to content

Commit

Permalink
refactor: refactored iframe post msg logic
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang committed May 3, 2024
1 parent b077870 commit 912a036
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 27 deletions.
6 changes: 2 additions & 4 deletions cms/djangoapps/contentstore/views/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@
CREATE_IF_NOT_FOUND = ["course_info"]

# Useful constants for defining predicates
def NEVER(x):
return False
def ALWAYS(x):
return True
NEVER = lambda x: False
ALWAYS = lambda x: True


# Disable atomic requests so transactions made during the request commit immediately instead of waiting for the end of
Expand Down
4 changes: 0 additions & 4 deletions cms/static/js/views/modals/move_xblock_modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,6 @@ function($, Backbone, _, gettext, BaseView, XBlockViewUtils, MoveXBlockUtils, Ht
targetParentLocator: this.targetParentXBlockInfo.id
}
);
window.parent.postMessage({
method: 'close_edit_modal',
msg: 'Sends a message when the modal window is closed'
}, '*');
}
});

Expand Down
25 changes: 21 additions & 4 deletions cms/static/js/views/utils/move_xblock_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,25 @@ function($, _, Backbone, Feedback, AlertView, XBlockViewUtils, MoveXBlockUtils,
.done(function(response) {
// hide modal
Backbone.trigger('move:hideMoveModal');
// hide xblock element
data.sourceXBlockElement.hide();
if (data.sourceXBlockElement) {
// hide xblock element
data.sourceXBlockElement.hide();
}

window.parent.postMessage({
method: 'move_xblock',
msg: 'Sends a message when the xblock is moved',
params: {
sourceDisplayName: data.sourceDisplayName,
sourceLocator: data.sourceLocator,
targetParentLocator: data.targetParentLocator,
}
}, '*');
window.parent.postMessage({
method: 'close_edit_modal',
msg: 'Sends a message when the modal window is closed'
}, '*');

showMovedNotification(
StringUtils.interpolate(
gettext('Success! "{displayName}" has been moved.'),
Expand All @@ -36,7 +53,7 @@ function($, _, Backbone, Feedback, AlertView, XBlockViewUtils, MoveXBlockUtils,
}
),
{
sourceXBlockElement: data.sourceXBlockElement,
sourceXBlockElement: data.sourceXBlockElement ? data.sourceXBlockElement : null,
sourceDisplayName: data.sourceDisplayName,
sourceLocator: data.sourceLocator,
sourceParentLocator: data.sourceParentLocator,
Expand Down Expand Up @@ -78,7 +95,7 @@ function($, _, Backbone, Feedback, AlertView, XBlockViewUtils, MoveXBlockUtils,
click: function() {
undoMoveXBlock(
{
sourceXBlockElement: data.sourceXBlockElement,
sourceXBlockElement: data.sourceXBlockElement ? data.sourceXBlockElement : null,
sourceDisplayName: data.sourceDisplayName,
sourceLocator: data.sourceLocator,
sourceParentLocator: data.sourceParentLocator,
Expand Down
36 changes: 22 additions & 14 deletions cms/templates/container_editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,28 @@
require(['js/models/xblock_info', 'js/views/xblock', 'js/views/utils/xblock_utils',
'common/js/components/utils/view_utils', 'gettext', 'js/views/modals/edit_xblock', 'js/views/modals/move_xblock_modal'],
function (XBlockInfo, XBlockView, XBlockUtils, ViewUtils, gettext, EditXBlockModal, MoveXBlockModal) {
if ('${action_name|n, decode.utf8}' === 'move') {
var parentModel = new XBlockInfo({ id: '${unit.location|n, decode.utf8}', category: 'vertical' });
var moveModal = new MoveXBlockModal({
sourceXBlockInfo: new XBlockInfo(${xblock_info | n, dump_js_escaped_json}),
sourceParentXBlockInfo: parentModel,
XBlockURLRoot: "/xblock",
outlineURL: '${outline_url | n, js_escaped_string}',
});
moveModal.show();
} else if ('${action_name|n, decode.utf8}' === 'edit') {
var editModal = new EditXBlockModal();

editModal.edit([], new XBlockInfo(${xblock_info | n, dump_js_escaped_json}), {});
}
function showMoveModal(unitLocation, xblockInfo, outlineUrl) {
var parentModel = new XBlockInfo({ id: unitLocation, category: 'vertical' });
var moveModal = new MoveXBlockModal({
sourceXBlockInfo: new XBlockInfo(xblockInfo),
sourceParentXBlockInfo: parentModel,
XBlockURLRoot: '/xblock',
outlineURL: outlineUrl,
});
moveModal.show();
}

function showEditModal(xblockInfo) {
var editModal = new EditXBlockModal();
editModal.edit([], new XBlockInfo(xblockInfo), {});
}

var actionName = '${action_name|n, decode.utf8}';
if (actionName === 'move') {
showMoveModal('${unit.location|n, decode.utf8}', ${xblock_info | n, dump_js_escaped_json}, '${outline_url | n, js_escaped_string}');
} else if (actionName === 'edit') {
showEditModal(${xblock_info | n, dump_js_escaped_json});
}
});
</%static:webpack>
</%block>
Expand Down
2 changes: 1 addition & 1 deletion cms/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
name='xblock_outline_handler'),
re_path(fr'^xblock/container/{settings.USAGE_KEY_PATTERN}$', contentstore_views.xblock_container_handler,
name='xblock_container_handler'),
re_path(fr'^xblock/{settings.USAGE_KEY_PATTERN}/actions/(?P<action_name>[^/]+)$$', xblock_actions_view,
re_path(fr'^xblock/{settings.USAGE_KEY_PATTERN}/actions/(?P<action_name>[^/]+)$', xblock_actions_view,
name='xblock_actions_handler'),
re_path(fr'^xblock/{settings.USAGE_KEY_PATTERN}/(?P<view_name>[^/]+)$', contentstore_views.xblock_view_handler,
name='xblock_view_handler'),
Expand Down

0 comments on commit 912a036

Please sign in to comment.