<commit_before>if Backbone?
  class @DiscussionModuleView extends Backbone.View
    events:
      "click .discussion-show": "toggleDiscussion"
      "click .new-post-btn": "toggleNewPost"
      "click .new-post-cancel": "hideNewPost"
      "click .discussion-paginator a": "navigateToPage"

    paginationTemplate: -> DiscussionUtil.getTemplate("_pagination")
    page_re: /\?discussion_page=(\d+)/
    initialize: ->
      # Set the page if it was set in the URL. This is used to allow deep linking to pages
      match = @page_re.exec(window.location.href)
      if match
        @page = parseInt(match[1])
      else
        @page = 1

    toggleNewPost: (event) ->
      event.preventDefault()
      if @newPostForm.is(':hidden')
        @newPostForm.slideDown(300)
      else
        @newPostForm.slideUp(300)

    hideNewPost: (event) ->
      event.preventDefault()
      @newPostForm.slideUp(300)

    toggleDiscussion: (event) ->
      thisButton = $(event.target).closest('a')
      if @showed
        @$("section.discussion").slideUp()
        thisButton.removeClass('shown')
        thisButton.find('.button-text').html("Show Discussion")
        @showed = false
      else
        thisButton.addClass('shown')
        thisButton.find('.button-text').html("Hide Discussion")

        console.log($(event.target))

        if @retrieved
          @$("section.discussion").slideDown()
          @showed = true
        else
          $elem = $(event.target)
          @loadPage $elem

    loadPage: ($elem)=>
      discussionId = @$el.data("discussion-id")
      url = DiscussionUtil.urlFor('retrieve_discussion', discussionId) + "?page=#{@page}"
      DiscussionUtil.safeAjax
        $elem: $elem
        $loading: $elem
        url: url
        type: "GET"
        dataType: 'json'
        success: (response, textStatus, jqXHR) => @renderDiscussion(event, response, textStatus, discussionId)

    renderDiscussion: (event, response, textStatus, discussionId) =>
      window.user = new DiscussionUser(response.user_info)
      Content.loadContentInfos(response.annotated_content_info)
      $(event.target).html("Hide Discussion")
      @discussion = new Discussion()
      @discussion.reset(response.discussion_data, {silent: false})
      $discussion = $(Mustache.render $("script#_inline_discussion").html(), {'threads':response.discussion_data, 'discussionId': discussionId})
      if @$('section.discussion').length
        @$('section.discussion').replaceWith($discussion)
      else
        $(".discussion-module").append($discussion)
      @newPostForm = $('.new-post-article')
      @threadviews = @discussion.map (thread) ->
        new DiscussionThreadInlineView el: @$("article#thread_#{thread.id}"), model: thread
      _.each @threadviews, (dtv) -> dtv.render()
      DiscussionUtil.bulkUpdateContentInfo(window.$$annotated_content_info)
      @newPostView = new NewPostInlineView el: @$('.new-post-article'), collection: @discussion
      @discussion.on "add", @addThread
      @retrieved = true
      @showed = true
      @renderPagination(2, response.num_pages)

    addThread: (thread, collection, options) =>
      # TODO: When doing pagination, this will need to repaginate. Perhaps just reload page 1?
      article = $("<article class='discussion-thread' id='thread_#{thread.id}'></article>")<commit_msg>Fix discussion module<commit_after>if Backbone?
  class @DiscussionModuleView extends Backbone.View
    events:
      "click .discussion-show": "toggleDiscussion"
      "click .new-post-btn": "toggleNewPost"
      "click .new-post-cancel": "hideNewPost"
      "click .discussion-paginator a": "navigateToPage"

    paginationTemplate: -> DiscussionUtil.getTemplate("_pagination")
    page_re: /\?discussion_page=(\d+)/
    initialize: ->
      # Set the page if it was set in the URL. This is used to allow deep linking to pages
      match = @page_re.exec(window.location.href)
      if match
        @page = parseInt(match[1])
      else
        @page = 1

    toggleNewPost: (event) ->
      event.preventDefault()
      if @newPostForm.is(':hidden')
        @newPostForm.slideDown(300)
      else
        @newPostForm.slideUp(300)

    hideNewPost: (event) ->
      event.preventDefault()
      @newPostForm.slideUp(300)

    toggleDiscussion: (event) ->
      thisButton = $(event.target).closest('a')
      if @showed
        @$("section.discussion").slideUp()
        thisButton.removeClass('shown')
        thisButton.find('.button-text').html("Show Discussion")
        @showed = false
      else
        thisButton.addClass('shown')
        thisButton.find('.button-text').html("Hide Discussion")

        console.log($(event.target))

        if @retrieved
          @$("section.discussion").slideDown()
          @showed = true
        else
          $elem = $(event.target)
          @loadPage $elem

    loadPage: ($elem)=>
      discussionId = @$el.data("discussion-id")
      url = DiscussionUtil.urlFor('retrieve_discussion', discussionId) + "?page=#{@page}"
      DiscussionUtil.safeAjax
        $elem: $elem
        $loading: $elem
        url: url
        type: "GET"
        dataType: 'json'
        success: (response, textStatus, jqXHR) => @renderDiscussion($elem, response, textStatus, discussionId)

    renderDiscussion: ($elem, response, textStatus, discussionId) =>
      window.user = new DiscussionUser(response.user_info)
      Content.loadContentInfos(response.annotated_content_info)
      $elem.html("Hide Discussion")
      @discussion = new Discussion()
      @discussion.reset(response.discussion_data, {silent: false})
      $discussion = $(Mustache.render $("script#_inline_discussion").html(), {'threads':response.discussion_data, 'discussionId': discussionId})
      if @$('section.discussion').length
        @$('section.discussion').replaceWith($discussion)
      else
        $(".discussion-module").append($discussion)
      @newPostForm = $('.new-post-article')
      @threadviews = @discussion.map (thread) ->
        new DiscussionThreadInlineView el: @$("article#thread_#{thread.id}"), model: thread
      _.each @threadviews, (dtv) -> dtv.render()
      DiscussionUtil.bulkUpdateContentInfo(window.$$annotated_content_info)
      @newPostView = new NewPostInlineView el: @$('.new-post-article'), collection: @discussion
      @discussion.on "add", @addThread
      @retrieved = true
      @showed = true
      @renderPagination(2, response.num_pages)

    addThread: (thread, collection, options) =>
      # TODO: When doing pagination, this will need to repaginate. Perhaps just reload page 1?
      article = $("<article class='discussion-thread' id='thread_#{thread.id}'></article>")
{'fraction_non_alphanumeric': 0.13919688342822895, 'fraction_numerical': 0.003895714713814804, 'mean_word_length': 3.0750915750915753, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1371332', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1908, 'n_words': 403}