<commit_before>class Views.QuestionView extends View
  @content: ->
    @div class: 'question', =>
      @div class: 'row', =>
        @div class: 'span12', =>
          @div class: 'body lead', outlet: 'body'
          @div class: 'new-answer-form hide', =>
            @textarea rows: 2, outlet: 'newAnswerTextarea'
            @button "Suggest Answer", class: 'btn pull-right', click: 'createAnswer'

      @div class: 'row', =>
        @div class: 'span4', =>
          @h5 "Collective Ranking"
          @subview 'collectiveVote', new Views.RelationView(
            attributes: { class: 'collective vote column' }
          )
        @div class: 'span4', =>
          @h5 class: 'pull-left', =>
            @a "Your Ranking", class: 'no-href disabled', click: 'showPersonalVote', outlet: 'showPersonalVoteLink'
          @h5 "|", class: 'pull-left separator'
          @h5 =>
            @a "All Rankings", class: 'no-href', click: 'showAllVotes', outlet: 'showAllVotesLink'

          @subview 'personalVote', new Views.RelationView(
            attributes: { class: 'personal vote column' }
          )

          @subview 'allVotes', new Views.RelationView(
            attributes: { class: 'all-votes column hide' }
            buildItem: (vote) -> new Views.VoteView(vote)
          )

        @div class: 'span4', =>

  initialize: (@question) ->
    @rankedItemsByAnswerId = {}
    @body.text(question.body())

    @collectiveVote.buildItem = (answer) => @buildAnswerItem(answer, draggable: true)
    @collectiveVote.setRelation(question.answers())

    @personalVote.buildItem = (ranking) =>
      @buildAnswerItem(ranking.answer(), position: ranking.position())
    @personalVote.onInsert = (item, ranking) =>
      @rankedItemsByAnswerId[ranking.answerId()]?.remove()
      @rankedItemsByAnswerId[ranking.answerId()] = item
    @personalVote.setRelation(Models.User.getCurrent().rankingsForQuestion(question))

    removeItem = null
    @personalVote.sortable(
      receive: -> removeItem = 0
      over: -> removeItem = 0
      out: -> removeItem = 1
      beforeStop: (event, ui) -> ui.item.detach() if removeItem
      stop: (event, ui) => @updateAnswerRanking(ui.item)
    )

    @allVotes.setRelation(@question.votes())

    @newAnswerTextarea.keydown (e) =>
      if e.keyCode == 13 # enter
        @createAnswer()
        e.preventDefault()

  buildAnswerItem: (answer, options={}) ->
    item = $$ -> @li answer.body(), class: 'answer', 'data-answer-id': answer.id()

    if options.draggable
      item.draggable(
        helper: -> $(this).clone().width($(this).width())
        appendTo: 'body'
        connectToSortable: '.personal.vote'
      )

    if position = options.position
      item.data('position', position)

    item

  showPersonalVote: ->
    @enableLink(@showAllVotesLink)
    @disableLink(@showPersonalVoteLink)
    @allVotes.hide()
    @personalVote.show()

  showAllVotes: ->
    @enableLink(@showPersonalVoteLink)
    @disableLink(@showAllVotesLink)<commit_msg>Show personal ranking when user starts dragging an answer<commit_after>class Views.QuestionView extends View
  @content: ->
    @div class: 'question', =>
      @div class: 'row', =>
        @div class: 'span12', =>
          @div class: 'body lead', outlet: 'body'
          @div class: 'new-answer-form hide', =>
            @textarea rows: 2, outlet: 'newAnswerTextarea'
            @button "Suggest Answer", class: 'btn pull-right', click: 'createAnswer'

      @div class: 'row', =>
        @div class: 'span4', =>
          @h5 "Collective Ranking"
          @subview 'collectiveVote', new Views.RelationView(
            attributes: { class: 'collective vote column' }
          )
        @div class: 'span4', =>
          @h5 class: 'pull-left', =>
            @a "Your Ranking", class: 'no-href disabled', click: 'showPersonalVote', outlet: 'showPersonalVoteLink'
          @h5 "|", class: 'pull-left separator'
          @h5 =>
            @a "All Rankings", class: 'no-href', click: 'showAllVotes', outlet: 'showAllVotesLink'

          @subview 'personalVote', new Views.RelationView(
            attributes: { class: 'personal vote column' }
          )

          @subview 'allVotes', new Views.RelationView(
            attributes: { class: 'all-votes column hide' }
            buildItem: (vote) -> new Views.VoteView(vote)
          )

        @div class: 'span4', =>

  initialize: (@question) ->
    @rankedItemsByAnswerId = {}
    @body.text(question.body())

    @collectiveVote.buildItem = (answer) => @buildAnswerItem(answer, draggable: true)
    @collectiveVote.setRelation(question.answers())

    @personalVote.buildItem = (ranking) =>
      @buildAnswerItem(ranking.answer(), position: ranking.position())
    @personalVote.onInsert = (item, ranking) =>
      @rankedItemsByAnswerId[ranking.answerId()]?.remove()
      @rankedItemsByAnswerId[ranking.answerId()] = item
    @personalVote.setRelation(Models.User.getCurrent().rankingsForQuestion(question))

    removeItem = null
    @personalVote.sortable(
      receive: -> removeItem = 0
      over: -> removeItem = 0
      out: -> removeItem = 1
      beforeStop: (event, ui) -> ui.item.detach() if removeItem
      stop: (event, ui) => @updateAnswerRanking(ui.item)
    )

    @allVotes.setRelation(@question.votes())

    @newAnswerTextarea.keydown (e) =>
      if e.keyCode == 13 # enter
        @createAnswer()
        e.preventDefault()

  buildAnswerItem: (answer, options={}) ->
    item = $$ -> @li answer.body(), class: 'answer', 'data-answer-id': answer.id()

    if options.draggable
      item.draggable(
        helper: -> $(this).clone().width($(this).width())
        appendTo: 'body'
        connectToSortable: '.personal.vote'
        delay: 1
      )
      item.mousedown => @showPersonalVote()

    if position = options.position
      item.data('position', position)

    item

  showPersonalVote: ->
    @enableLink(@showAllVotesLink)
    @disableLink(@showPersonalVoteLink)
    @allVotes.hide()
    @personalVote.show()

  showAllVotes: ->
    @enableLink(@showPersonalVoteLink)
    @disableLink(@showAllVotesLink)
{'fraction_non_alphanumeric': 0.14644897959183673, 'fraction_numerical': 0.005061224489795919, 'mean_word_length': 2.742211362248015, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 58, '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': '930493', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1872, 'n_words': 403}