<commit_before>templates = {}

actions = 
  sendFile: ->
    textField = $('#messageInput')
    sendFile encodeURI textField.val()
    textField.val ''

user =
  displayName: 'Orange Colander'
  screenName: 'orangecolander'

$.get 'incoming/Content.html', (text) -> templates.message = text
$.get 'Status.html', (text) -> templates.status = text  
$.get 'FileTransferRequest.html', (text) -> templates.fileTransfer = text

$('button[data-action]').live 'click', (event) ->
  actionName = $(this).data('action')
  if actionName of actions
    actions[actionName].apply this, arguments
  else
    console.error "undefined action #{actionName}"

$ ->
  $('#messageInput').bind 'keydown', (event) ->
    if event.which is 13 and not event.altKey
      event.preventDefault()
      sendMessage $(this).val()
      $(this).val ''
  
  $(messageView).bind 'load', ->
    $ = jQuery = messageView.jQuery
    
    $('a').live 'click', (event) -> event.preventDefault()
    $('body').css fontSize: '8pt'
    
    _ajax = jQuery.ajax
    jQuery.ajax = (url, options) ->
      options.data ?= {}
      options.data._url = url
      _ajax.call this, '/ajax', options

formatTime = (date = new Date) ->
  [ date.getHours(), date.getMinutes() ].join(':')

uriPattern = /[^:\/?\#]+:(?:\/\/(?:(?:[^:@\/]*(?::[^:@\/]*)?)?@)?[^:\/?\#]*(?::\d*)?)?(?:[^?\#]*)*(?:\?[^\#]*)?(?:\#.*)?/g
markup = (text) ->
  $('<div>').text(text).html() # escape HTML characters
  .replace(/\n/g, '<br>') # convert line breaks
  .replace(uriPattern, (uri) -> """<a href="#{uri}">#{uri}</a>""") # convert URIs to links

fillTemplate = (template, data) ->
  template.replace /%([A-Za-z]+)%/g, (m, key) -> data[key]

sendMessage = (text, options = {}) ->
  type = options.type ? 'message'
  
  classNames = [ type ]
  classNames.push options.status if type is 'status'
  classNames.push if options.outgoing then 'outgoing' else 'incoming'
  
  data =
    time: formatTime()
    sender: user.displayName
    senderScreenName: user.screenName
    messageClasses: classNames.join(' ')
    message: markup text
  
  html = fillTemplate templates.message, data
  messageView.appendMessage html

sendFile = (fileName, options = {}) ->
  html = fillTemplate templates.fileTransfer,
    sender: user.displayName
    senderScreenName: user.screenName<commit_msg>add mention class to mockup messages containing the test user screen name<commit_after>templates = {}

actions = 
  sendFile: ->
    textField = $('#messageInput')
    sendFile encodeURI textField.val()
    textField.val ''

user =
  displayName: 'Orange Colander'
  screenName: 'orangecolander'

$.get 'incoming/Content.html', (text) -> templates.message = text
$.get 'Status.html', (text) -> templates.status = text  
$.get 'FileTransferRequest.html', (text) -> templates.fileTransfer = text

$('button[data-action]').live 'click', (event) ->
  actionName = $(this).data('action')
  if actionName of actions
    actions[actionName].apply this, arguments
  else
    console.error "undefined action #{actionName}"

$ ->
  $('#messageInput').bind 'keydown', (event) ->
    if event.which is 13 and not event.altKey
      event.preventDefault()
      sendMessage $(this).val()
      $(this).val ''
  
  $(messageView).bind 'load', ->
    $ = jQuery = messageView.jQuery
    
    $('a').live 'click', (event) -> event.preventDefault()
    $('body').css fontSize: '8pt'
    
    _ajax = jQuery.ajax
    jQuery.ajax = (url, options) ->
      options.data ?= {}
      options.data._url = url
      _ajax.call this, '/ajax', options

formatTime = (date = new Date) ->
  [ date.getHours(), date.getMinutes() ].join(':')

uriPattern = /[^:\/?\#]+:(?:\/\/(?:(?:[^:@\/]*(?::[^:@\/]*)?)?@)?[^:\/?\#]*(?::\d*)?)?(?:[^?\#]*)*(?:\?[^\#]*)?(?:\#.*)?/g
markup = (text) ->
  $('<div>').text(text).html() # escape HTML characters
  .replace(/\n/g, '<br>') # convert line breaks
  .replace(uriPattern, (uri) -> """<a href="#{uri}">#{uri}</a>""") # convert URIs to links

fillTemplate = (template, data) ->
  template.replace /%([A-Za-z]+)%/g, (m, key) -> data[key]

containsWord = (text, word) -> ///\b#{word}\b///.test text

sendMessage = (text, options = {}) ->
  type = options.type ? 'message'
  
  classNames = [ type ]
  classNames.push 'mention' if containsWord text, user.screenName
  classNames.push options.status if type is 'status'
  classNames.push if options.outgoing then 'outgoing' else 'incoming'
  
  data =
    time: formatTime()
    sender: user.displayName
    senderScreenName: user.screenName
    messageClasses: classNames.join(' ')
    message: markup text
  
  html = fillTemplate templates.message, data
  messageView.appendMessage html

sendFile = (fileName, options = {}) ->
  html = fillTemplate templates.fileTransfer,
    sender: user.displayName
    senderScreenName: user.screenName
{'fraction_non_alphanumeric': 0.20203911776945485, 'fraction_numerical': 0.0012484394506866417, 'mean_word_length': 3.4757914338919926, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 46, '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': '7363632', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1588, 'n_words': 359}