diff --git a/lms/static/coffee/src/discussion.coffee b/lms/static/coffee/src/discussion.coffee index e2645bcd8e..0da9b18ff7 100644 --- a/lms/static/coffee/src/discussion.coffee +++ b/lms/static/coffee/src/discussion.coffee @@ -16,7 +16,6 @@ $ -> Discussion.initializeDiscussion(discussion) Discussion.bindDiscussionEvents(discussion) - generateLocal = (elem) -> (selector) -> $(elem).find(selector) @@ -104,6 +103,14 @@ Discussion = retrieve_single_thread : "/courses/#{$$course_id}/discussion/forum/#{param}/threads/#{param1}" }[name] + safeAjax: (params) -> + $elem = params.$elem + if $elem.attr("disabled") + return + $elem.attr("disabled", "disabled") + $.ajax(params).always -> + $elem.removeAttr("disabled") + handleAnchorAndReload: (response) -> #window.location = window.location.pathname + "#" + response['id'] window.location.reload() @@ -113,13 +120,10 @@ Discussion = $local = generateLocal($discussionModule) handleShowDiscussion = (elem) -> $elem = $(elem) - if $elem.attr("disabled") - return if not $local("section.discussion").length - $elem.attr("disabled", "disabled") discussion_id = $elem.attr("discussion_id") url = Discussion.urlFor 'retrieve_discussion', discussion_id - $.ajax( + Discussion.safeAjax url: url method: "GET" success: (data, textStatus, xhr) -> @@ -131,14 +135,12 @@ Discussion = $elem.unbind('click').click -> handleHideDiscussion(this) dataType: 'html' - ).always -> - $elem.removeAttr("disabled") - else $local("section.discussion").show() $elem.html("Hide Discussion") $elem.unbind('click').click -> handleHideDiscussion(this) + handleHideDiscussion = (elem) -> $local("section.discussion").hide() $elem = $(elem) @@ -381,25 +383,32 @@ Discussion = , 'json' handleHideSingleThread = (elem) -> - $elem = $(elem) + $threadTitle = $local(".thread-title") + $showComments = $local(".discussion-show-comments") $content.children(".comments").hide() - $elem.unbind('click').click -> - handleShowSingleThread(this) + $threadTitle.unbind('click').click handleShowSingleThread + $showComments.unbind('click').click handleShowSingleThread + prevHtml = $showComments.html() + $showComments.html prevHtml.replace "Hide", "Show" + + handleShowSingleThread = -> + $threadTitle = $local(".thread-title") + $showComments = $local(".discussion-show-comments") + + rebindHideEvents = -> + $threadTitle.unbind('click').click handleHideSingleThread + $showComments.unbind('click').click handleHideSingleThread + prevHtml = $showComments.html() + $showComments.html prevHtml.replace "Show", "Hide" - handleShowSingleThread = (elem) -> - $elem = $(elem) - if $elem.attr("disabled") - return if $content.children(".comments").length $content.children(".comments").show() - $elem.unbind('click').click -> - handleHideSingleThread(this) + rebindHideEvents() else - $elem.attr("disabled", "disabled") - discussion_id = $elem.parents(".discussion").attr("_id") + discussion_id = $threadTitle.parents(".discussion").attr("_id") url = Discussion.urlFor('retrieve_single_thread', discussion_id, id) - console.log url - $.ajax( + Discussion.safeAjax + $elem: $.merge($threadTitle, $showComments) url: url method: "GET" success: (response, textStatus) -> @@ -412,15 +421,12 @@ Discussion = $content.find(".comment").each (index, comment) -> Discussion.initializeContent(comment) Discussion.bindContentEvents(comment) - $elem.unbind('click').click -> - handleHideSingleThread(this) + rebindHideEvents() dataType: 'json' - ).always -> - $elem.removeAttr("disabled") - $local(".thread-title").click -> - handleShowSingleThread(this) + $local(".thread-title").click handleShowSingleThread + $local(".discussion-show-comments").click handleShowSingleThread $local(".discussion-reply").click -> handleShowSingleThread($local(".thread-title")) diff --git a/lms/templates/discussion/_thread.html b/lms/templates/discussion/_thread.html index 806b0ee579..8ea341edaf 100644 --- a/lms/templates/discussion/_thread.html +++ b/lms/templates/discussion/_thread.html @@ -93,7 +93,7 @@ user No.${content['user_id']} % endif % if content.get('comments_count', -1) >= 0: - , ${content['comments_count']} comment(s) + , Show ${content['comments_count']} comment(s) % endif