<commit_before>GoalGetter.Views.ChatView = GoalGetter.Views.ScreenBase.extend
  className: 'chat-window row'
  initialize: ->
    _.bindAll @, 'render'
    @chat_fetcher = new GoalGetter.Helpers.ChatRecordFetcher()
    @chat_fetcher.last_request_time(Date.now())
    
    # remove this later.
    window.cf = @chat_fetcher
    
    # The view will start the fetcher; in some future version, we need a way to have the
    # view stop the fetcher if it gets garbaged.

    @chat_fetcher.run()
    @listenTo @, 'add_sent_message', @append_message
    @listenTo @chat_fetcher, 'sync', @show_new_messages

  show_new_messages: ->
    view_self = @
    if @chat_fetcher.models.length > 0
      @chat_fetcher.pause()
      @chat_fetcher.models.forEach (model) ->
        view_self.append_message model.get('message'), {is_response: model.get('is_response')}
      @chat_fetcher.last_request_time(Date.now())
      @chat_fetcher.resume()
    
  append_message: (txt, opts) ->
    is_response = false
    if typeof opts != 'undefined'
      is_response = opts.is_response
      
    # emails might have newlines in them.
    txt = txt.replace(/\n/g, "<br>")
    div = $('<div>').addClass('chat-bubble').html(txt)
    if is_response
      div.addClass 'received'
    else
      div.addClass 'sent'
    
    @$el.find('.chat-list').append div
    # Scroll down to the last message
    d = @$('.chat-list')
    d.scrollTop d.prop("scrollHeight")

  send_message: (e) ->
      view_self = @
      tgt = $(e.target).siblings('.userinput')
      if tgt.length == 0
        tgt = $(e.target).closest('#sendit').siblings('.userinput')
        
      txt = $(tgt[0]).text().trim()
      if txt.length > 0
        $.ajax('/chat_records',
          method: 'post',
          data:
            message_to_counselor: txt
          success: (d, s, x) ->
            view_self.$el.find('.userinput').text ''
            view_self.$el.find('#chatarea').addClass 'empty'
            view_self.trigger 'add_sent_message', txt
        )

  events:
    'click #chatarea': (e) ->
      $(e.target).find('.userinput').focus()

    'click #sendit': 'send_message'
    'click .send-icon': 'send_message'
    'keyup .userinput': (e) ->
      if $(e.target).text().trim().length > 0
        $(e.target).closest('#chatarea').removeClass('empty')
      else
        $(e.target).closest('#chatarea').addClass('empty')
<commit_msg>Bugfix: spurious double click on chat message send<commit_after>GoalGetter.Views.ChatView = GoalGetter.Views.ScreenBase.extend
  className: 'chat-window row'
  initialize: ->
    _.bindAll @, 'render'
    @chat_fetcher = new GoalGetter.Helpers.ChatRecordFetcher()
    @chat_fetcher.last_request_time(Date.now())
    
    # remove this later.
    window.cf = @chat_fetcher
    
    # The view will start the fetcher; in some future version, we need a way to have the
    # view stop the fetcher if it gets garbaged.

    @chat_fetcher.run()
    @listenTo @, 'add_sent_message', @append_message
    @listenTo @chat_fetcher, 'sync', @show_new_messages

  show_new_messages: ->
    view_self = @
    if @chat_fetcher.models.length > 0
      @chat_fetcher.pause()
      @chat_fetcher.models.forEach (model) ->
        view_self.append_message model.get('message'), {is_response: model.get('is_response')}
      @chat_fetcher.last_request_time(Date.now())
      @chat_fetcher.resume()
    
  append_message: (txt, opts) ->
    is_response = false
    if typeof opts != 'undefined'
      is_response = opts.is_response
      
    # emails might have newlines in them.
    txt = txt.replace(/\n/g, "<br>")
    div = $('<div>').addClass('chat-bubble').html(txt)
    if is_response
      div.addClass 'received'
    else
      div.addClass 'sent'
    
    @$el.find('.chat-list').append div
    # Scroll down to the last message
    d = @$('.chat-list')
    d.scrollTop d.prop("scrollHeight")

  send_message: (e) ->
      view_self = @
      
      tgt = $(e.target).siblings('.userinput')
      if tgt.length == 0
        tgt = $(e.target).closest('#sendit').siblings('.userinput')
        
      txt = $(tgt[0]).text().trim()
      if txt.length > 0
        $.ajax('/chat_records',
          method: 'post',
          data:
            message_to_counselor: txt
          success: (d, s, x) ->
            view_self.$el.find('.userinput').text ''
            view_self.$el.find('#chatarea').addClass 'empty'
            view_self.trigger 'add_sent_message', txt
        )

  events:
    'click #chatarea': (e) ->
      $(e.target).find('.userinput').focus()

    'click .send-icon': 'send_message'
    'keyup .userinput': (e) ->
      if $(e.target).text().trim().length > 0
        $(e.target).closest('#chatarea').removeClass('empty')
      else
        $(e.target).closest('#chatarea').addClass('empty')

{'fraction_non_alphanumeric': 0.15254590984974958, 'fraction_numerical': 0.002086811352253756, 'mean_word_length': 2.6037593984962406, 'pattern_counts': {'":': 0, '<': 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': '2654221', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1572, 'n_words': 316}