From 5b879da1631fddd3367bc7d2f69cc19a2597733b Mon Sep 17 00:00:00 2001 From: PKulkoRaccoonGang Date: Mon, 8 Apr 2024 14:37:26 +0300 Subject: [PATCH] refactor: removed errors handling --- cms/templates/container_editor.html | 44 +++++++++++------------------ 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/cms/templates/container_editor.html b/cms/templates/container_editor.html index 7864b63cce8b..6f1345e1a7a7 100644 --- a/cms/templates/container_editor.html +++ b/cms/templates/container_editor.html @@ -175,10 +175,6 @@ // Serves to initialize the rendering of a xblock edit modal window. setTimeout(() => $('.button-edit').trigger('click'), 300); - const iframeMessageHandler = (method, msg) => { - return window.parent.postMessage({ method, msg }, '*'); - } - /** * Callback function for the MutationObserver to handle mutations * and send information when the modal window close logic is triggered. @@ -188,41 +184,33 @@ */ const xblockEditModalObserver = new MutationObserver((mutations) => { const modalClassName = 'wrapper-modal-window-edit-xblock'; - // The index of the current modal window in the removed nodes. - const currentModalIndex = 1; - // Filter mutations to find removed nodes that match the current modal class name. - const editModalRemovedNodes = mutations + // When a modal window is opened while the template is rendering, + // an element with class modalClassName is rendered, + // the MutationObserver defines this process in removedNodes. + const modalElementMutationRecords = mutations .filter(({ removedNodes }) => { - return removedNodes.length > 0 && removedNodes[currentModalIndex].className.includes(modalClassName) + const filteredModalClassName = Array.from(removedNodes).filter((node) => + node.className && node.className.includes(modalClassName)); + + return filteredModalClassName.length > 0; }); - // If removed nodes match the current modal, post a message to close the edit modal. - if (editModalRemovedNodes.length > 0) { - iframeMessageHandler('close_edit_modal', 'Sends a message when the modal window is closed'); + // If the element was present and deleted, close the modal window. + if (modalElementMutationRecords.length > 0 && !$('.' + modalClassName).length) { + window.parent.postMessage({ + method: 'close_edit_modal', + msg: 'Sends a message when the modal window is closed' + }, '*'); } - }) + }); - xblockEditModalObserver.observe(document.body, { + xblockEditModalObserver.observe(document, { childList: true, subtree: true, attributes: true, attributeFilter: ['class'] }); - - const originalAjax = $.ajax; - // Notifies the parent element that a server error has occurred. - $.ajax = function(options) { - const errorHandler = options.error; - options.error = function(xhr, textStatus, errorThrown) { - iframeMessageHandler('edit_modal-error', 'Sends a message in case of server error'); - - if (errorHandler) { - errorHandler(xhr, textStatus, errorThrown); - } - }; - return originalAjax.call(this, options); - }; });