From 7a6346429b790984487966630c8d65973b2abbc7 Mon Sep 17 00:00:00 2001 From: Ryan Wyllie Date: Wed, 27 Apr 2016 01:48:48 +0000 Subject: [PATCH] MDL-53921 gradingform: Destroy frequently used comment dialog on hide --- .../guide/amd/build/comment_chooser.min.js | 2 +- .../form/guide/amd/src/comment_chooser.js | 99 ++++++++++--------- 2 files changed, 51 insertions(+), 50 deletions(-) diff --git a/grade/grading/form/guide/amd/build/comment_chooser.min.js b/grade/grading/form/guide/amd/build/comment_chooser.min.js index acb88091429..abeda372d0a 100644 --- a/grade/grading/form/guide/amd/build/comment_chooser.min.js +++ b/grade/grading/form/guide/amd/build/comment_chooser.min.js @@ -1 +1 @@ -define(["jquery","core/templates","core/notification","core/yui"],function(a,b,c){return{initialise:function(d,e,f,g){function h(b,c){var e="",g="comment-chooser-"+d+"-cancel",h='";"undefined"==typeof j&&(j=new M.core.dialogue({modal:!0,headerContent:e,bodyContent:b,footerContent:h,focusAfterHide:"#"+f,id:"comments-chooser-dialog-"+d}),a("#"+g).click(function(){"undefined"!=typeof j&&j.hide()}),a.each(c,function(b,c){var e="#comment-option-"+d+"-"+c.id;a(e).click(function(){var b=a("#"+f),d=b.val();""!==a.trim(d)&&(d+="\n"),d+=c.description,b.val(d),"undefined"!=typeof j&&j.hide()}),a(document).off("keypress",e).on("keypress",e,function(){var b=event.which||event.keyCode;(13==b||32==b)&&a(e).click()})})),j.show()}function i(){var a={criterionId:d,comments:g};b.render("gradingform_guide/comment_chooser",a).done(function(a){h(a,g)}).fail(c.exception)}var j;a("#"+e).click(function(a){a.preventDefault(),i()})}}}); \ No newline at end of file +define(["jquery","core/templates","core/notification","core/yui"],function(a,b,c){return{initialise:function(d,e,f,g){function h(b,c){var e="",g="comment-chooser-"+d+"-cancel",h='",i=new M.core.dialogue({modal:!0,headerContent:e,bodyContent:b,footerContent:h,focusAfterHide:"#"+f,id:"comments-chooser-dialog-"+d});a("#"+g).click(function(){i.hide()}),a.each(c,function(b,c){var e="#comment-option-"+d+"-"+c.id;a(e).click(function(){var b=a("#"+f),d=b.val();""!==a.trim(d)&&(d+="\n"),d+=c.description,b.val(d),i.hide()}),a(document).off("keypress",e).on("keypress",e,function(){var b=event.which||event.keyCode;(13==b||32==b)&&a(e).click()})}),i.after("visibleChange",function(a){a.prevVal&&!a.newVal&&this.destroy()},i),i.show()}function i(){var a={criterionId:d,comments:g};b.render("gradingform_guide/comment_chooser",a).done(function(a){h(a,g)}).fail(c.exception)}a("#"+e).click(function(a){a.preventDefault(),i()})}}}); \ No newline at end of file diff --git a/grade/grading/form/guide/amd/src/comment_chooser.js b/grade/grading/form/guide/amd/src/comment_chooser.js index fdee3cb9478..6f9b158d408 100644 --- a/grade/grading/form/guide/amd/src/comment_chooser.js +++ b/grade/grading/form/guide/amd/src/comment_chooser.js @@ -40,8 +40,6 @@ define(['jquery', 'core/templates', 'core/notification', 'core/yui'], function ( * @param commentOptions The array of frequently used comments to be used as options. */ initialise: function (criterionId, buttonId, remarkId, commentOptions) { - var chooserDialog; - /** * Display the chooser dialog using the compiled HTML from the mustache template * and binds onclick events for the generated comment options. @@ -54,58 +52,61 @@ define(['jquery', 'core/templates', 'core/notification', 'core/yui'], function ( var cancelButtonId = 'comment-chooser-' + criterionId + '-cancel'; var cancelButton = ''; - if (typeof chooserDialog === 'undefined') { - // Set dialog's body content. - chooserDialog = new M.core.dialogue({ - modal: true, - headerContent: titleLabel, - bodyContent: compiledSource, - footerContent: cancelButton, - focusAfterHide: '#' + remarkId, - id: "comments-chooser-dialog-" + criterionId + // Set dialog's body content. + var chooserDialog = new M.core.dialogue({ + modal: true, + headerContent: titleLabel, + bodyContent: compiledSource, + footerContent: cancelButton, + focusAfterHide: '#' + remarkId, + id: "comments-chooser-dialog-" + criterionId + }); + + // Bind click event to the cancel button. + $("#" + cancelButtonId).click(function() { + chooserDialog.hide(); + }); + + // Loop over each comment item and bind click events. + $.each(comments, function (index, comment) { + var commentOptionId = '#comment-option-' + criterionId + '-' + comment.id; + + // Delegate click event for the generated option link. + $(commentOptionId).click(function () { + var remarkTextArea = $('#' + remarkId); + var remarkText = remarkTextArea.val(); + + // Add line break if the current value of the remark text is not empty. + if ($.trim(remarkText) !== '') { + remarkText += '\n'; + } + remarkText += comment.description; + + remarkTextArea.val(remarkText); + + chooserDialog.hide(); }); - // Bind click event to the cancel button. - $("#" + cancelButtonId).click(function() { - if (typeof chooserDialog !== 'undefined') { - chooserDialog.hide(); + // Handle keypress on list items. + $(document).off('keypress', commentOptionId).on('keypress', commentOptionId, function () { + var keyCode = event.which || event.keyCode; + + // Enter or space key. + if (keyCode == 13 || keyCode == 32) { + // Trigger click event. + $(commentOptionId).click(); } }); + }); - // Loop over each comment item and bind click events. - $.each(comments, function (index, comment) { - var commentOptionId = '#comment-option-' + criterionId + '-' + comment.id; - - // Delegate click event for the generated option link. - $(commentOptionId).click(function () { - var remarkTextArea = $('#' + remarkId); - var remarkText = remarkTextArea.val(); - - // Add line break if the current value of the remark text is not empty. - if ($.trim(remarkText) !== '') { - remarkText += '\n'; - } - remarkText += comment.description; - - remarkTextArea.val(remarkText); - - if (typeof chooserDialog !== 'undefined') { - chooserDialog.hide(); - } - }); - - // Handle keypress on list items. - $(document).off('keypress', commentOptionId).on('keypress', commentOptionId, function () { - var keyCode = event.which || event.keyCode; - - // Enter or space key. - if (keyCode == 13 || keyCode == 32) { - // Trigger click event. - $(commentOptionId).click(); - } - }); - }); - } + // Destroy the dialog when it is hidden to allow the grading section to + // be loaded as a fragment multiple times within the same page. + chooserDialog.after('visibleChange', function(e) { + // Going from visible to hidden. + if (e.prevVal && !e.newVal) { + this.destroy(); + } + }, chooserDialog); // Show dialog. chooserDialog.show();