<commit_before>$ ->
  num_hours = 12
  $(".active-channels").on "click", ".body img", ->
    $(this).toggleClass("full")
    false


  $(".active-channels").on "click", ".show", ->
    $(this).parents(".posts").find(".activity-post").removeClass("hide")
    false

  $(".activity-graph-data").each (i,a) ->
    graph = d3.select(a)
    data = $.map $(a).data("sparklines").split(","), (e) -> parseInt(e)
    svg = graph.select(".activity-graph").append("svg:svg").attr("width", "100%").attr("height", "100%")

    x = d3.scale.linear().domain([0, num_hours]).range([0, 60])
    y = d3.scale.linear().domain([0, d3.max(data) + 1]).range([22, 4])

    line = d3.svg.line()
      .x (d,i) ->
        console.log [d,i]
        x(i)
      .y (d) ->
        console.log [d]
        y(d)

    svg.append("svg:path").attr("d", line(data))

    # x = d3.scale.linear().domain([0, 10]).range([0, 50])

  postTemplate = (body, user) ->
    "
<div class=\"activity-post highlight\">
  <img src=\"#{user.avatar_url.replace(/22$/, '32')}\" class=\"avatar\" title=\"#{user.login}\" />
  <div class=\"body\">
    #{body}
  </div>
</div>
    "

  $(document).on 'click', ".post-header .faves .octicon", ->
    self = $(this)
    post = self.parents(".post").attr("data-post-id")
    $.ajax(url:"/posts/"+post+"/fave", dataType: "json", type: "post").done (msg) ->
      self.parents(".faves").toggleClass("active", msg.status).find(".count").text("#{msg.count}")
    return false

  syntax = $('#syntax').val()
  insertText = (text) ->
    if syntax && syntax == "html"
      $.markItUp
        target: $('.comment-box')
        placeHolder: text
    else
      $('.comment-box').append text
  insertImage = (url) ->
    if syntax && syntax == "html"
      $.markItUp
        target: $('.comment-box')
        placeHolder: "<img src=\"#{url}\" />"
    else
      t = $('.comment-box').val()
      t += "\n\n" if t != ''
      t += "![](#{url})"
      $('.comment-box').val(t)

  $(document).on 'click', ".upload-image", ->
    form = $(this).parents("form")
    file = form.find("input[type=file]")
    info = form.find(".info")
    filename = ""

    uploadComplete = (xhr) ->
      console.log xhr.responseText
      info.html("Finished uploading \"#{filename}\"")
      insertImage(JSON.parse(xhr.responseText).url)
    uploadError = (d) ->
      info.html("Error #{d}")
    uploadProgress = (progress) ->
      info.html("Uploading \"#{filename}\" (#{progress}%)")

    startUpload = () ->
      xhr = new XMLHttpRequest()
      xhr.open 'POST', $(form).attr('action'), true
      token = $('meta[name="_csrf"]').attr('content')
      xhr.setRequestHeader('X_CSRF_TOKEN', token)

      xhr.onreadystatechange = (e) =>
        if xhr.readyState == 4
          if xhr.status == 201
            uploadComplete(xhr)
          else if xhr.status == 202
            uploadComplete(xhr)
          else
            uploadError(xhr.responseText)
      xhr.onerror = (e) =>
        uploadError "error"

      if xhr.upload?
        xhr.upload.onprogress = (e) =>
          percentage = Math.round((e.loaded / e.total) * 100)
          uploadProgress(percentage)

      form = new FormData(form[0])
      xhr.send form

    file.on 'change', (e) ->
      f = $(this).val().split("\\")
      filename = f[f.length - 1]
      startUpload()

    file.trigger("click")
    false

  $(document).on 'click', ".post-reply", ->
    self = $(this)
    post = self.parents(".post").find(".body")
    text = decodeURIComponent(post.attr("data-raw-body"))
    if syntax && syntax == "html"
      text = "<blockquote>#{text}</blockquote>"
    else
      text = text.replace(/</g, '&amp;lt;').replace(/>/g, '&amp;gt;').replace(/(^|\n)/g, "\n> ").replace(/^\n/, '')
    $(".comment-box textarea").focus()
    insertText("#{text}\n\n")
    $(".comment-box textarea").focus()
    return false

  $(document).on 'click', ".post-unread", ->
    self = $(this)
    post = self.attr("data-prev-post-id")
    ourl = document.location.href.replace(/#.*$/, '')
    url = "#{ourl}/posts/#{post}/unread"
    $.ajax(url:url, dataType: "json", type: "post")
    return false

  $(".active-channels").on 'click', '.mark-read', ->
    if $(this).hasClass("all-read")
      $(this).removeClass("all-read").parents(".activity").find(".posts").removeClass("hide")
    else
      $(this).addClass("all-read").parents(".activity").find(".posts").addClass("hide").find(".activity-post").removeClass("highlight")
      channelId = parseInt $(this).parents(".activity").data("channel-id")
      $.ajax
        type: "POST"
        dataType: "json"
        url: "/channels/#{channelId}/visit"

  loadMorePosts = (include_id=0, cb=null, all=false) ->
    loader = $(".post-loader")
    loader.data("channel-id")
    last_id = $('.post:not(.preview)').first().attr("data-post-id")
    ourl = document.location.href.replace(/#.*$/, '')
    limit = ""
    if include_id == 0 && !all
      limit = "&limit=12"
    $.get "#{ourl}/posts?first_id=#{last_id}#{limit}", (data) ->
      count = $(".post").length
      d = $(data)
      d.insertAfter loader
      count = $(".post").length - count
      c = loader.find(".post-loader-count")
      c.text(parseInt(c.text() - count))
      if parseInt(c.text()) < 1
        loader.hide()
      cb?()

  loaderHidden = false
  $(document).on 'scroll', ->
    t = $(document).scrollTop()
    if !loaderHidden && t > 100
      loaderHidden = true

    if loaderHidden && t == 0
      loadMorePosts()
      loaderHidden = false

    return false

  $(document).on 'click', ".post-loader a.load-more", ->
    loadMorePosts()
    return false

  $(document).on 'click', ".post-loader a.load-all", ->
    loadMorePosts(0, null, true)
    return false

  if window.location.hash != "" && window.location.hash.lastIndexOf("#post_", 0) == 0
    post_id = parseInt(window.location.hash.replace(/^#post_/, ''))
    if $(".post-#{post_id}").length > 0
      $(".post-#{post_id}").addClass("highlight") if !$(".post-#{post_id}").hasClass("unread")
    else
      loadMorePosts post_id, () ->
        window.location.hash = "post_#{post_id}"
        $("body").scrollTop($(".post-#{post_id}").addClass("highlight").offset().top)


  if $('.comment-small').length
    $('.comment-small').on 'submit', 'form', ->
      textarea = $(this).find('textarea')
      text = textarea.val()
      textarea.val ''
      $.ajax
        type: "POST"
        dataType: "json"
        url: $(this).attr("action")
        data: {"post[body]": text}
        success: (data) ->
          console.log data
          u = $.grep window.Users, (i) -> console.log i ; i.id == data.user_id
          console.log u
          $(postTemplate(data.html_body, u[0])).insertBefore textarea.parents(".comment-small")
          # refreshPosts()
        error: () ->
          textarea.val text
          $(".upload_info").html("Error sending post. Please try again.")
      false
    $('.comment-box').keydown (e) ->
      if e.keyCode == 13 && (e.metaKey || e.ctrlKey)
        $(this).parents('form').submit()
<commit_msg>Only reload after scroll in posts view<commit_after>$ ->
  num_hours = 12
  $(".active-channels").on "click", ".body img", ->
    $(this).toggleClass("full")
    false


  $(".active-channels").on "click", ".show", ->
    $(this).parents(".posts").find(".activity-post").removeClass("hide")
    false

  $(".activity-graph-data").each (i,a) ->
    graph = d3.select(a)
    data = $.map $(a).data("sparklines").split(","), (e) -> parseInt(e)
    svg = graph.select(".activity-graph").append("svg:svg").attr("width", "100%").attr("height", "100%")

    x = d3.scale.linear().domain([0, num_hours]).range([0, 60])
    y = d3.scale.linear().domain([0, d3.max(data) + 1]).range([22, 4])

    line = d3.svg.line()
      .x (d,i) ->
        console.log [d,i]
        x(i)
      .y (d) ->
        console.log [d]
        y(d)

    svg.append("svg:path").attr("d", line(data))

    # x = d3.scale.linear().domain([0, 10]).range([0, 50])

  postTemplate = (body, user) ->
    "
<div class=\"activity-post highlight\">
  <img src=\"#{user.avatar_url.replace(/22$/, '32')}\" class=\"avatar\" title=\"#{user.login}\" />
  <div class=\"body\">
    #{body}
  </div>
</div>
    "

  $(document).on 'click', ".post-header .faves .octicon", ->
    self = $(this)
    post = self.parents(".post").attr("data-post-id")
    $.ajax(url:"/posts/"+post+"/fave", dataType: "json", type: "post").done (msg) ->
      self.parents(".faves").toggleClass("active", msg.status).find(".count").text("#{msg.count}")
    return false

  syntax = $('#syntax').val()
  insertText = (text) ->
    if syntax && syntax == "html"
      $.markItUp
        target: $('.comment-box')
        placeHolder: text
    else
      $('.comment-box').append text
  insertImage = (url) ->
    if syntax && syntax == "html"
      $.markItUp
        target: $('.comment-box')
        placeHolder: "<img src=\"#{url}\" />"
    else
      t = $('.comment-box').val()
      t += "\n\n" if t != ''
      t += "![](#{url})"
      $('.comment-box').val(t)

  $(document).on 'click', ".upload-image", ->
    form = $(this).parents("form")
    file = form.find("input[type=file]")
    info = form.find(".info")
    filename = ""

    uploadComplete = (xhr) ->
      console.log xhr.responseText
      info.html("Finished uploading \"#{filename}\"")
      insertImage(JSON.parse(xhr.responseText).url)
    uploadError = (d) ->
      info.html("Error #{d}")
    uploadProgress = (progress) ->
      info.html("Uploading \"#{filename}\" (#{progress}%)")

    startUpload = () ->
      xhr = new XMLHttpRequest()
      xhr.open 'POST', $(form).attr('action'), true
      token = $('meta[name="_csrf"]').attr('content')
      xhr.setRequestHeader('X_CSRF_TOKEN', token)

      xhr.onreadystatechange = (e) =>
        if xhr.readyState == 4
          if xhr.status == 201
            uploadComplete(xhr)
          else if xhr.status == 202
            uploadComplete(xhr)
          else
            uploadError(xhr.responseText)
      xhr.onerror = (e) =>
        uploadError "error"

      if xhr.upload?
        xhr.upload.onprogress = (e) =>
          percentage = Math.round((e.loaded / e.total) * 100)
          uploadProgress(percentage)

      form = new FormData(form[0])
      xhr.send form

    file.on 'change', (e) ->
      f = $(this).val().split("\\")
      filename = f[f.length - 1]
      startUpload()

    file.trigger("click")
    false

  $(document).on 'click', ".post-reply", ->
    self = $(this)
    post = self.parents(".post").find(".body")
    text = decodeURIComponent(post.attr("data-raw-body"))
    if syntax && syntax == "html"
      text = "<blockquote>#{text}</blockquote>"
    else
      text = text.replace(/</g, '&amp;lt;').replace(/>/g, '&amp;gt;').replace(/(^|\n)/g, "\n> ").replace(/^\n/, '')
    $(".comment-box textarea").focus()
    insertText("#{text}\n\n")
    $(".comment-box textarea").focus()
    return false

  $(document).on 'click', ".post-unread", ->
    self = $(this)
    post = self.attr("data-prev-post-id")
    ourl = document.location.href.replace(/#.*$/, '')
    url = "#{ourl}/posts/#{post}/unread"
    $.ajax(url:url, dataType: "json", type: "post")
    return false

  $(".active-channels").on 'click', '.mark-read', ->
    if $(this).hasClass("all-read")
      $(this).removeClass("all-read").parents(".activity").find(".posts").removeClass("hide")
    else
      $(this).addClass("all-read").parents(".activity").find(".posts").addClass("hide").find(".activity-post").removeClass("highlight")
      channelId = parseInt $(this).parents(".activity").data("channel-id")
      $.ajax
        type: "POST"
        dataType: "json"
        url: "/channels/#{channelId}/visit"

  loadMorePosts = (include_id=0, cb=null, all=false) ->
    loader = $(".post-loader")
    loader.data("channel-id")
    last_id = $('.post:not(.preview)').first().attr("data-post-id")
    ourl = document.location.href.replace(/#.*$/, '')
    limit = ""
    if include_id == 0 && !all
      limit = "&limit=12"
    $.get "#{ourl}/posts?first_id=#{last_id}#{limit}", (data) ->
      count = $(".post").length
      d = $(data)
      d.insertAfter loader
      count = $(".post").length - count
      c = loader.find(".post-loader-count")
      c.text(parseInt(c.text() - count))
      if parseInt(c.text()) < 1
        loader.hide()
      cb?()

  if $(".comment-box").length > 0
    loaderHidden = false
    $(document).on 'scroll', ->
      t = $(document).scrollTop()
      if !loaderHidden && t > 100
        loaderHidden = true

      if loaderHidden && t == 0
        loadMorePosts()
        loaderHidden = false

      return false

    $(document).on 'click', ".post-loader a.load-more", ->
      loadMorePosts()
      return false

    $(document).on 'click', ".post-loader a.load-all", ->
      loadMorePosts(0, null, true)
      return false

    if window.location.hash != "" && window.location.hash.lastIndexOf("#post_", 0) == 0
      post_id = parseInt(window.location.hash.replace(/^#post_/, ''))
      if $(".post-#{post_id}").length > 0
        $(".post-#{post_id}").addClass("highlight") if !$(".post-#{post_id}").hasClass("unread")
      else
        loadMorePosts post_id, () ->
          window.location.hash = "post_#{post_id}"
          $("body").scrollTop($(".post-#{post_id}").addClass("highlight").offset().top)


  if $('.comment-small').length
    $('.comment-small').on 'submit', 'form', ->
      textarea = $(this).find('textarea')
      text = textarea.val()
      textarea.val ''
      $.ajax
        type: "POST"
        dataType: "json"
        url: $(this).attr("action")
        data: {"post[body]": text}
        success: (data) ->
          console.log data
          u = $.grep window.Users, (i) -> console.log i ; i.id == data.user_id
          console.log u
          $(postTemplate(data.html_body, u[0])).insertBefore textarea.parents(".comment-small")
          # refreshPosts()
        error: () ->
          textarea.val text
          $(".upload_info").html("Error sending post. Please try again.")
      false
    $('.comment-box').keydown (e) ->
      if e.keyCode == 13 && (e.metaKey || e.ctrlKey)
        $(this).parents('form').submit()

{'fraction_non_alphanumeric': 0.22182330165434705, 'fraction_numerical': 0.008658922914466738, 'mean_word_length': 2.5268123138033762, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 98, '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': '2545240', 'n_tokens_mistral': 4829, 'n_tokens_neox': 4688, 'n_words': 863}