diff --git a/mod/forum/amd/build/inpage_reply.min.js b/mod/forum/amd/build/inpage_reply.min.js index c3bade2bf26..b25f8e1c809 100644 --- a/mod/forum/amd/build/inpage_reply.min.js +++ b/mod/forum/amd/build/inpage_reply.min.js @@ -1 +1 @@ -define(["jquery","core/templates","core/notification","mod_forum/repository","mod_forum/selectors"],function(a,b,c,d,e){var f={THREADED:2,NESTED:3,FLAT_OLDEST_FIRST:1,FLAT_NEWEST_FIRST:-1},g=function(g){g.on("click",e.post.inpageSubmitBtn,function(h){h.preventDefault();var i,j=a(h.currentTarget).parents(e.post.inpageReplyForm).get(0),k=j.elements.post.value.trim(),l=j.elements.reply.value,m=j.elements.subject.value,n=a(h.currentTarget).parents(e.post.forumContent),o=parseInt(g.find(e.post.modeSelect).get(0).value);k.length&&d.addDiscussionPost(l,m,k).then(function(a){var b=a.messages.reduce(function(a,b){return"success"==b.type&&(a+="

"+b.message+"

"),a},"");return c.addNotification({message:b,type:"success"}),a}).then(function(a){j.reset();var c=a.post;switch(i=c.id,o){case f.THREADED:return b.render("mod_forum/forum_discussion_threaded_post",c);case f.NESTED:return b.render("mod_forum/forum_discussion_nested_post",c);default:return b.render("mod_forum/forum_discussion_post",c)}}).then(function(a,c){var d;return o!=f.FLAT_OLDEST_FIRST&&o!=f.FLAT_NEWEST_FIRST||(d=n.parents(e.post.repliesContainer).children().get(0)),void 0==d&&(d=n.siblings(e.post.repliesContainer).children().get(0)),o==f.FLAT_NEWEST_FIRST?b.prependNodeContents(d,a,c):b.appendNodeContents(d,a,c)}).then(function(){return n.find(e.post.inpageReplyContent).hide()}).then(function(){location.href="#p"+i}).fail(c.exception)})};return{init:function(a){g(a)}}}); \ No newline at end of file +define(["jquery","core/templates","core/notification","mod_forum/repository","mod_forum/selectors"],function(a,b,c,d,e){var f={THREADED:2,NESTED:3,FLAT_OLDEST_FIRST:1,FLAT_NEWEST_FIRST:-1},g=function(a){var b=a.find(e.post.inpageSubmitBtnText),c=a.find(e.post.loadingIconContainer),d=a.outerWidth();a.css("width",d),b.addClass("hidden"),c.removeClass("hidden")},h=function(a){var b=a.find(e.post.inpageSubmitBtnText),c=a.find(e.post.loadingIconContainer);a.css("width",""),b.removeClass("hidden"),c.addClass("hidden")},i=function(i){i.on("click",e.post.inpageSubmitBtn,function(j){j.preventDefault();var k,l=a(j.currentTarget),m=l.parent().find(e.post.inpageReplyButton),n=l.parents(e.post.inpageReplyForm).get(0),o=n.elements.post.value.trim(),p=n.elements.reply.value,q=n.elements.subject.value,r=l.parents(e.post.forumContent),s=parseInt(i.find(e.post.modeSelect).get(0).value);o.length&&(g(l),m.prop("disabled",!0),d.addDiscussionPost(p,q,o).then(function(a){var b=a.messages.reduce(function(a,b){return"success"==b.type&&(a+="

"+b.message+"

"),a},"");return c.addNotification({message:b,type:"success"}),a}).then(function(a){n.reset();var c=a.post;switch(k=c.id,s){case f.THREADED:return b.render("mod_forum/forum_discussion_threaded_post",c);case f.NESTED:return b.render("mod_forum/forum_discussion_nested_post",c);default:return b.render("mod_forum/forum_discussion_post",c)}}).then(function(a,c){var d;return s!=f.FLAT_OLDEST_FIRST&&s!=f.FLAT_NEWEST_FIRST||(d=r.parents(e.post.repliesContainer).children().get(0)),void 0==d&&(d=r.siblings(e.post.repliesContainer).children().get(0)),s==f.FLAT_NEWEST_FIRST?b.prependNodeContents(d,a,c):b.appendNodeContents(d,a,c)}).then(function(){return h(l),m.prop("disabled",!1),r.find(e.post.inpageReplyContent).hide()}).then(function(){location.href="#p"+k})["catch"](function(a){return h(l),m.prop("disabled",!1),c.exception(a)}))})};return{init:function(a){i(a)}}}); \ No newline at end of file diff --git a/mod/forum/amd/build/selectors.min.js b/mod/forum/amd/build/selectors.min.js index 6cdaf85499a..d5a48dcc650 100644 --- a/mod/forum/amd/build/selectors.min.js +++ b/mod/forum/amd/build/selectors.min.js @@ -1 +1 @@ -define([],function(){return{subscription:{toggle:"[data-type='subscription-toggle'][data-action='toggle']"},post:{post:'[data-region="post"]',action:'[data-region="post-action"]',actionsContainer:'[data-region="post-actions-container"]',forumCoreContent:"[data-region-content='forum-post-core']",forumContent:"[data-content='forum-post']",forumSubject:"[data-region-content='forum-post-core-subject']",inpageReplyLink:"[data-action='collapsible-link']",inpageReplyContent:"[data-content='inpage-reply-content']",inpageReplyForm:"form[data-content='inpage-reply-form']",inpageSubmitBtn:"[data-action='forum-inpage-submit']",repliesContainer:"[data-region='replies-container']",modeSelect:"select[name='mode']"},lock:{toggle:"[data-action='toggle'][data-type='lock-toggle']"},favourite:{toggle:"[data-type='favorite-toggle'][data-action='toggle']"},pin:{toggle:"[data-type='pin-toggle'][data-action='toggle']"}}}); \ No newline at end of file +define([],function(){return{subscription:{toggle:"[data-type='subscription-toggle'][data-action='toggle']"},post:{post:'[data-region="post"]',action:'[data-region="post-action"]',actionsContainer:'[data-region="post-actions-container"]',forumCoreContent:"[data-region-content='forum-post-core']",forumContent:"[data-content='forum-post']",forumSubject:"[data-region-content='forum-post-core-subject']",inpageReplyButton:"button",inpageReplyLink:"[data-action='collapsible-link']",inpageReplyContent:"[data-content='inpage-reply-content']",inpageReplyForm:"form[data-content='inpage-reply-form']",inpageSubmitBtn:"[data-action='forum-inpage-submit']",inpageSubmitBtnText:"[data-region='submit-text']",loadingIconContainer:"[data-region='loading-icon-container']",repliesContainer:"[data-region='replies-container']",modeSelect:"select[name='mode']"},lock:{toggle:"[data-action='toggle'][data-type='lock-toggle']"},favourite:{toggle:"[data-type='favorite-toggle'][data-action='toggle']"},pin:{toggle:"[data-type='pin-toggle'][data-action='toggle']"}}}); \ No newline at end of file diff --git a/mod/forum/amd/src/inpage_reply.js b/mod/forum/amd/src/inpage_reply.js index 4c74341d2f6..06decf40955 100644 --- a/mod/forum/amd/src/inpage_reply.js +++ b/mod/forum/amd/src/inpage_reply.js @@ -14,13 +14,9 @@ // along with Moodle. If not, see . /** - * This module is the highest level module for the calendar. It is - * responsible for initialising all of the components required for - * the calendar to run. It also coordinates the interaction between - * components by listening for and responding to different events - * triggered within the calendar UI. + * This module handles the in page replying to forum posts. * - * @module mod_forum/posts_list + * @module mod_forum/inpage_reply * @package mod_forum * @copyright 2019 Peter Dias * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -46,18 +42,57 @@ define([ FLAT_NEWEST_FIRST: -1 }; + /** + * Show the loading icon for the submit button. + * + * @param {Object} button The submit button element + */ + var showSubmitButtonLoadingIcon = function(button) { + var textContainer = button.find(Selectors.post.inpageSubmitBtnText); + var loadingIconContainer = button.find(Selectors.post.loadingIconContainer); + var width = button.outerWidth(); + // Fix the width so that the button size doesn't change when we show the loading icon. + button.css('width', width); + textContainer.addClass('hidden'); + loadingIconContainer.removeClass('hidden'); + }; + + /** + * Hide the loading icon for the submit button. + * + * @param {Object} button The submit button element + */ + var hideSubmitButtonLoadingIcon = function(button) { + var textContainer = button.find(Selectors.post.inpageSubmitBtnText); + var loadingIconContainer = button.find(Selectors.post.loadingIconContainer); + // Reset the width back to it's default. + button.css('width', ''); + textContainer.removeClass('hidden'); + loadingIconContainer.addClass('hidden'); + }; + + /** + * Register the event listeners for the submit button of the in page reply. + * + * @param {Object} root The discussion container element. + */ var registerEventListeners = function(root) { root.on('click', Selectors.post.inpageSubmitBtn, function(e) { e.preventDefault(); - var form = $(e.currentTarget).parents(Selectors.post.inpageReplyForm).get(0); + var submitButton = $(e.currentTarget); + var allButtons = submitButton.parent().find(Selectors.post.inpageReplyButton); + var form = submitButton.parents(Selectors.post.inpageReplyForm).get(0); var message = form.elements.post.value.trim(); var postid = form.elements.reply.value; var subject = form.elements.subject.value; - var currentRoot = $(e.currentTarget).parents(Selectors.post.forumContent); + var currentRoot = submitButton.parents(Selectors.post.forumContent); var mode = parseInt(root.find(Selectors.post.modeSelect).get(0).value); var newid; if (message.length) { + showSubmitButtonLoadingIcon(submitButton); + allButtons.prop('disabled', true); + Repository.addDiscussionPost(postid, subject, message) .then(function(context) { var message = context.messages.reduce(function(carry, message) { @@ -105,13 +140,19 @@ define([ } }) .then(function() { + hideSubmitButtonLoadingIcon(submitButton); + allButtons.prop('disabled', false); return currentRoot.find(Selectors.post.inpageReplyContent).hide(); }) .then(function() { location.href = "#p" + newid; return; }) - .fail(Notification.exception); + .catch(function(error) { + hideSubmitButtonLoadingIcon(submitButton); + allButtons.prop('disabled', false); + return Notification.exception(error); + }); } }); }; @@ -119,7 +160,6 @@ define([ return { init: function(root) { registerEventListeners(root); - } }; }); diff --git a/mod/forum/amd/src/selectors.js b/mod/forum/amd/src/selectors.js index 65509610ff2..7773b002ac4 100644 --- a/mod/forum/amd/src/selectors.js +++ b/mod/forum/amd/src/selectors.js @@ -33,10 +33,13 @@ define([], function() { forumCoreContent: "[data-region-content='forum-post-core']", forumContent: "[data-content='forum-post']", forumSubject: "[data-region-content='forum-post-core-subject']", + inpageReplyButton: "button", inpageReplyLink: "[data-action='collapsible-link']", inpageReplyContent: "[data-content='inpage-reply-content']", inpageReplyForm: "form[data-content='inpage-reply-form']", inpageSubmitBtn: "[data-action='forum-inpage-submit']", + inpageSubmitBtnText: "[data-region='submit-text']", + loadingIconContainer: "[data-region='loading-icon-container']", repliesContainer: "[data-region='replies-container']", modeSelect: "select[name='mode']" }, diff --git a/mod/forum/templates/inpage_reply.mustache b/mod/forum/templates/inpage_reply.mustache index 3cd5ac95fe5..da44f5e6757 100644 --- a/mod/forum/templates/inpage_reply.mustache +++ b/mod/forum/templates/inpage_reply.mustache @@ -46,12 +46,13 @@
-