<commit_before>Namespaced.declare('Scheddo')

$('.votable').delegate '.vote[data-role=create]', 'ajax:success', (e, data, textStatus, jqXHR) ->
  Scheddo.voteCallback.call(this, data)

$('.votable').delegate '.vote[data-role=delete]', 'ajax:success', (e, data, textStatus, jqXHR) ->
  Scheddo.unvoteCallback.call(this, data)

$('.votable').delegate '.vote', 'ajax:error', (e, data, textStatus, jqXHR) ->
  Scheddo.voteErrorCallback.call(this, data)

$('.votable').delegate '.vote', 'ajax:beforeSend', (e, data, textStatus, jqXHR) ->
  if $('body').hasClass('loading')
    return false
  $('body').addClass('loading')

$('.votable').on 'click', (e, data, status, jqXhr) ->


Scheddo.voteCallback = (data) ->
  $('body').removeClass('loading')
  $(this).attr('data-role','delete')
  $(this).attr('action',"/votes/#{data.vote.id}")
  $(this).find('#vote_id').val(data.vote.id)
  $(this).append($('<input name=_method type=hidden value=delete />'))
  if($(this).data('hover'))
    $(this).one 'mouseout', ->
      $(this).find('input.vote').removeClass('vote').addClass('unvote')
  else
    $(this).find('input.vote').removeClass('vote').addClass('unvote')
  id = $(this).parent().data('id')
  $(".vote-count[data-id=#{id}]").text(parseInt($(".vote-count[data-id=#{id}]").text()) + 1 )


Scheddo.unvoteCallback = (data) ->
  $('body').removeClass('loading')
  $(this).attr('data-role','create')
  $(this).attr('action','/votes')
  $(this).find('input[value=delete]').remove()
  if($(this).data('hover'))
    $(this).one 'mouseout', ->
      $(this).find('input.unvote').removeClass('unvote').addClass('vote')
  else
    $(this).find('input.unvote').removeClass('unvote').addClass('vote')
  id = $(this).parent().data('id')
  $(".vote-count[data-id=#{id}]").text(parseInt($(".vote-count[data-id=#{id}]").text()) - 1 )

Scheddo.voteErrorCallback = (data) ->
  $('body').removeClass('loading')
  $(".flash").append($("<div id=flash-error>#{data.statusText}</div>"))<commit_msg>Fix "choose winner" button placement<commit_after>Namespaced.declare('Scheddo')

$('.votable').delegate '.vote[data-role=create]', 'ajax:success', (e, data, textStatus, jqXHR) ->
  Scheddo.voteCallback.call(this, data)

$('.votable').delegate '.vote[data-role=delete]', 'ajax:success', (e, data, textStatus, jqXHR) ->
  Scheddo.unvoteCallback.call(this, data)

$('.votable').delegate '.vote', 'ajax:error', (e, data, textStatus, jqXHR) ->
  Scheddo.voteErrorCallback.call(this, data)

$('.votable').delegate '.vote', 'ajax:beforeSend', (e, data, textStatus, jqXHR) ->
  if $('body').hasClass('loading')
    return false
  $('body').addClass('loading')

$('.votable').on 'click', (e, data, status, jqXhr) ->


Scheddo.voteCallback = (data) ->
  $('body').removeClass('loading')
  $(this).attr('data-role','delete')
  $(this).attr('action',"/votes/#{data.vote.id}")
  $(this).find('#vote_id').val(data.vote.id)
  $(this).append($('<input name=_method type=hidden value=delete />'))
  if($(this).data('hover'))
    $(this).one 'mouseout', ->
      $(this).find('input.vote').removeClass('vote').addClass('unvote')
  else
    $(this).find('input.vote').removeClass('vote').addClass('unvote')
  id = $(this).parent().data('id')
  newVoteCount = parseInt($(".vote-count[data-id=#{id}]").text(), 10) + 1
  $(".vote-count[data-id=#{id}]").html("<span>#{newVoteCount}</span>")

Scheddo.unvoteCallback = (data) ->
  $('body').removeClass('loading')
  $(this).attr('data-role','create')
  $(this).attr('action','/votes')
  $(this).find('input[value=delete]').remove()
  if($(this).data('hover'))
    $(this).one 'mouseout', ->
      $(this).find('input.unvote').removeClass('unvote').addClass('vote')
  else
    $(this).find('input.unvote').removeClass('unvote').addClass('vote')
  id = $(this).parent().data('id')
  newVoteCount = parseInt($(".vote-count[data-id=#{id}]").text(), 10) - 1
  $(".vote-count[data-id=#{id}]").html("<span>#{newVoteCount}</span>")

Scheddo.voteErrorCallback = (data) ->
  $('body').removeClass('loading')
  $(".flash").append($("<div id=flash-error>#{data.statusText}</div>"))
{'fraction_non_alphanumeric': 0.2721456692913386, 'fraction_numerical': 0.001968503937007874, 'mean_word_length': 5.556451612903226, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, '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': '1539206', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1452, 'n_words': 183}