/** * When dijit dialogs get created they are moved out of their original location to be directly under the document body * As a result when we try and remove widgets within a container at a later point (eg in hyf.util.insertContent) the dialogs * dont get removed, leading to potential script issues. * These script functions attach to the common scripts to hopefully resolve these issues. */ require(["dojo/aspect", 'dojo/topic'], function(aspect, topic) { //before adding any new content work out how many dialog widgets there are already aspect.before(hyf.util, 'insertContent', function(target, content, evalScripts) { var currentDialogs = hyf.util.getDijitWidgets(document.body, 'dijit.Dialog'); var dialogIds = new Array(); for (var i = 0; i < currentDialogs.length; ++i) { dialogIds.push(currentDialogs[i].id); } target._hyfCurrentDialogIds = dialogIds; }); //after parsing widgets in new content, check again which dialogs exist //assume any new ones were added from ths content so note them for correct removal later topic.subscribe('hyf/hooks/widgetsParsed', function(container){ var newDialogs = hyf.util.getDijitWidgets(document.body, 'dijit.Dialog'); var newDialogIds = new Array(); for (var i = 0; i < newDialogs.length; ++i) { var dialogId = newDialogs[i].id; var found = false; if (container._hyfCurrentDialogIds) { for (var j = 0; j < container._hyfCurrentDialogIds.length; j++) { if (container._hyfCurrentDialogIds[j] == dialogId) { found = true; break; } } delete container._hyfCurrentDialogIds; } if (!found) newDialogIds.push(dialogId) } if (newDialogIds.length > 0) { container._hyfAddedDialogIds = newDialogIds; dojo.addClass(container, 'hyf-added-dialog'); } }); //when destroying widgets in a container, also destroy and dialogs initially //added from within this container aspect.after(hyf.util, 'destroyDojoWidgets', function(target) { var containersToProcess = dojo.query('.hyf-added-dialog', target); containersToProcess.push(target); containersToProcess.forEach(function(item){ if (item._hyfAddedDialogIds) { for (var i = 0; i < item._hyfAddedDialogIds.length; ++i) { var id = item._hyfAddedDialogIds[i]; var d = dijit.byId(id); if (d) { d.destroyRecursive(); delete d; } } } }); }, true); });