<commit_before># Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

# create_overlay = ->
#   d = document.createElement('div');
#   $(d).css(
#     background: 'lightgray',
# #     opacity: '0.4',
#     width: '50%',
#     height: '50%',
#     position: 'absolute',
#     top: '25%',
#     left: '25%',
#     zIndex: 100
#   ).attr('id', 'cash_overlay')
#   $('body').append(d)
#
# display_overlay = ->
#   if $('#cash_overlay').length == 0
#     create_overlay()
#   $('#cash_overlay').html \
#     """
#     <p>
#     #{$("#total_price").html()}
#     </p>
#     """
#   window.prompt "Cash given (in EUR):"
#   bind_overlay_hotkeys()

handle_errors = ->
  if $("#error_explanation").length > 0
    $("audio").trigger "play"

set_focus = (focus_id) ->
  if $(".field_with_errors").length > 0
    $(".field_with_errors input:first").focus()
  else
    if focus_id
      $("#" + focus_id).focus()
    else
      $(".field input").filter(-> $(this).val() == "").first().focus()

register_handler = ->
  $(".field input").blur ->
    if $(this).attr("size") == "7" && /^\s*$/.test $(this).val()
      $(this).val("a")
    $.ajax "/transactions/validate", { type: "POST", data: $("#transaction_form form").serialize(), timeout: 2000, success: replace_transaction_form }

replace_transaction_form = (data, status, jqXHR) ->
  focus_id = $(":focus").attr("id")
  focus_value = $(":focus").val()
#   if focus_id != "transaction_submit"
  $("#transaction_form").html data
  $("#" + focus_id).val focus_value
  process_page_change(focus_id)

process_page_change = (focus_id) ->
  bind_hotkeys()
  if $("#transaction_form").length > 0
    $(".field input").focus(-> $(this).select())
    bind_transaction_form_hotkeys()
    handle_errors()
    set_focus(focus_id)
    register_handler()
#   bind_overlay_hotkeys()

bind_element_to_hotkey = (selector, key, handler) ->
  element = $(selector)
  if element.length > 0
    if !handler
      handler = -> element[0].click()
    $(document).on('keypress', null, key, handler)

bind_link_to_hotkey = (href, key) ->
  bind_element_to_hotkey 'a[href="' + href + '"]', key

bind_hotkeys = ->
  bind_link_to_hotkey "/transactions", "l"
  bind_link_to_hotkey "/transactions/new", "n"
#   bind_element_to_hotkey "#cash_given_link", "g", display_overlay

bind_transaction_form_hotkeys = ->
  $("input").keyup (e) ->
    if e.keyCode == 27
      window.location.href = "/transactions"

# bind_overlay_hotkeys = ->
#   $(document).keyup (e) ->
#     if e.keyCode == 27
#       $("#cash_overlay").remove()
#   bind_element_to_hotkey "#cash_given_link", "g", display_overlay


TransactionsController = Paloma.controller "Transactions"
TransactionsController::default = ->
  jQuery ->
    process_page_change()<commit_msg>change method of hotkey-binding<commit_after># Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

# create_overlay = ->
#   d = document.createElement('div');
#   $(d).css(
#     background: 'lightgray',
# #     opacity: '0.4',
#     width: '50%',
#     height: '50%',
#     position: 'absolute',
#     top: '25%',
#     left: '25%',
#     zIndex: 100
#   ).attr('id', 'cash_overlay')
#   $('body').append(d)
#
# display_overlay = ->
#   if !$('#cash_overlay').length
#     create_overlay()
#   $('#cash_overlay').html \
#     """
#     <p>
#     #{$("#total_price").html()}
#     </p>
#     """
#   window.prompt "Cash given (in EUR):"
#   bind_overlay_hotkeys()

handle_errors = ->
  if $("#error_explanation").length
    $("audio").trigger "play"

set_focus = (focus_id) ->
  if $(".field_with_errors").length
    $(".field_with_errors input:first").focus()
  else
    if focus_id
      $("#" + focus_id).focus()
    else
      $(".field input").filter(-> $(this).val() == "").first().focus()

register_handler = ->
  $(".field input").blur ->
    if $(this).attr("size") == "7" && /^\s*$/.test $(this).val()
      $(this).val("a")
    $.ajax "/transactions/validate", { type: "POST", data: $("#transaction_form form").serialize(), timeout: 2000, success: replace_transaction_form }

replace_transaction_form = (data, status, jqXHR) ->
  focus_id = $(":focus").attr("id")
  focus_value = $(":focus").val()
#   if focus_id != "transaction_submit"
  $("#transaction_form").html data
  $("#" + focus_id).val focus_value
  process_page_change(focus_id)

process_page_change = (focus_id) ->
  bind_hotkeys()
  if $("#transaction_form").length
    $(".field input").focus(-> $(this).select())
    handle_errors()
    set_focus(focus_id)
    register_handler()
#   bind_overlay_hotkeys()

bind_hotkeys = ->
  $(document).keydown (event) ->
    target = switch event.keyCode
      when 'L'.charCodeAt(0) then "/transactions"
      when 'N'.charCodeAt(0) then "/transactions/new"
    if target
      event.stopPropagation()
      window.location.href = target
  $("#transaction_form input").keydown (event) ->
    if event.keyCode == 27
      event.preventDefault()
      event.stopPropagation()
      window.location.href = "/transactions"


# bind_overlay_hotkeys = ->
#   $(document).keyup (e) ->
#     if e.keyCode == 27
#       $("#cash_overlay").remove()
#   bind_element_to_hotkey "#cash_given_link", "g", display_overlay


TransactionsController = Paloma.controller "Transactions"
TransactionsController::default = ->
  jQuery ->
    process_page_change()
{'fraction_non_alphanumeric': 0.18929016189290163, 'fraction_numerical': 0.009251022949653086, 'mean_word_length': 3.6309719934102143, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 48, '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': '519031', 'n_tokens_mistral': 1928, 'n_tokens_neox': 1877, 'n_words': 383}