From 912a0365e31116b4b2ebdc0f2de501e756ca2dd2 Mon Sep 17 00:00:00 2001 From: PKulkoRaccoonGang Date: Fri, 3 May 2024 19:42:26 +0300 Subject: [PATCH] refactor: refactored iframe post msg logic --- cms/djangoapps/contentstore/views/block.py | 6 ++-- .../js/views/modals/move_xblock_modal.js | 4 --- .../js/views/utils/move_xblock_utils.js | 25 ++++++++++--- cms/templates/container_editor.html | 36 +++++++++++-------- cms/urls.py | 2 +- 5 files changed, 46 insertions(+), 27 deletions(-) diff --git a/cms/djangoapps/contentstore/views/block.py b/cms/djangoapps/contentstore/views/block.py index 0623151d1a10..58d0c048f12e 100644 --- a/cms/djangoapps/contentstore/views/block.py +++ b/cms/djangoapps/contentstore/views/block.py @@ -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 diff --git a/cms/static/js/views/modals/move_xblock_modal.js b/cms/static/js/views/modals/move_xblock_modal.js index 872a21eae05d..8b3abad0703b 100644 --- a/cms/static/js/views/modals/move_xblock_modal.js +++ b/cms/static/js/views/modals/move_xblock_modal.js @@ -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' - }, '*'); } }); diff --git a/cms/static/js/views/utils/move_xblock_utils.js b/cms/static/js/views/utils/move_xblock_utils.js index 9bde9c6d087e..178185fe6b73 100644 --- a/cms/static/js/views/utils/move_xblock_utils.js +++ b/cms/static/js/views/utils/move_xblock_utils.js @@ -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.'), @@ -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, @@ -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, diff --git a/cms/templates/container_editor.html b/cms/templates/container_editor.html index a06e4565a1c6..43f23d2100a4 100644 --- a/cms/templates/container_editor.html +++ b/cms/templates/container_editor.html @@ -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}); + } }); diff --git a/cms/urls.py b/cms/urls.py index b5077073ad30..aff79f5ca763 100644 --- a/cms/urls.py +++ b/cms/urls.py @@ -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[^/]+)$$', xblock_actions_view, + re_path(fr'^xblock/{settings.USAGE_KEY_PATTERN}/actions/(?P[^/]+)$', xblock_actions_view, name='xblock_actions_handler'), re_path(fr'^xblock/{settings.USAGE_KEY_PATTERN}/(?P[^/]+)$', contentstore_views.xblock_view_handler, name='xblock_view_handler'),