<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/

$(window).focus ->
  if active_var()
    update_display(active_var())
  return

@update_display = (id) ->
  request_job_data(id)
  update_destroy_button(id)

@request_job_data = (id) ->
  if id?
    $.ajax
      type: 'GET'
      url: Routes.workflow_path(id)
      contentType: "application/json; charset=utf-8"
      dataType: "json"
      error: (jqXHR, textStatus, errorThrown) ->
        show_job_panel()
        console.log jqXHR
      success: (data, textStatus, jqXHR) ->
        show_job_panel()
        update_status_label(id, data.status_label)
        update_job_details_panel(data)
        update_open_dir_button(data.fs_root)
        update_edit_button(id)
        update_copy_button(id)
        update_submit_button(id, data.status.char)
        update_stop_button(id, data.status.char)
        update_template_button(id)
        list_folder_contents(data)
        if missing_data_path()
          # If the template folder does not exist we need to disable certain buttons.
          update_open_dir_button()
          update_terminal_button()
          update_edit_button()
          update_submit_button()
          update_copy_button()
  else
    show_job_panel()
    disable_all_buttons()

@disable_all_buttons = ->
  update_job_details_panel()
  update_open_dir_button()
  update_edit_button()
  update_terminal_button()
  update_submit_button()
  update_stop_button()
  update_template_button()
  update_copy_button()
  update_destroy_button()
  list_folder_contents()

@show_job_panel = (show) ->
  if show?
    $("#job-details-panel").show()
  else
    $("#job-details-panel").hide()

@show_script_details_panel = (show) ->
  if show?
    $("#script-details-panel").show()
  else
    $("#script-details-panel").hide()

@update_status_label = (id, label) ->
  if label? && id?
    $("#status_label_#{id}").html(label)

@update_job_details_panel = (data) ->
  show_job_panel()
  if data?
    $("#job-details-name").text(data.name)
    $("#job-details-server").val(data.host_title)
    $("#job-details-staged-dir").text(data.staged_dir)
    if data.account == null or data.account == ""
      $("#job-details-account").addClass("text-muted")
      $("#job-details-account").text("Not specified")
    else
      $("#job-details-account").removeClass("text-muted")
      $("#job-details-account").text(data.account)
    show_job_panel(true)

@update_script_details_panel = (content) ->
  if content?
    $("#script-name").text(content.name)
    $("#open-script-dir-button").attr("href", "#{content.fs_base}")
    $("#open-terminal-dir-button").attr("href", "#{content.terminal_base}")
    $("#open-editor-button").attr("href", "#{content.editor_url}")
    update_terminal_button(content.terminal_base)
    $.ajax
      type: 'GET'
      url: content.apiurl
      contentType: "application/json; charset=utf-8"
      dataType: "text"
      error: (jqXHR, textStatus, errorThrown) ->
        show_script_details_panel()
        console.log jqXHR
      success: (filedata, textStatus, jqXHR) ->
        show_script_details_panel()
        $("#script-text-data").text(filedata)
        show_script_details_panel(true)
  else
    show_script_details_panel()


@update_open_dir_button = (path) ->
  if path?
    $("#open_dir_button").attr("href", path)
    $("#open_dir_button").removeAttr("disabled")
    $("#open_dir_button").unbind('click', false)
  else
    $("#open_dir_button").attr("href", "#")
    $("#open_dir_button").attr("disabled", true)
    $("#open_dir_button").bind('click', false)

@update_copy_button = (id) ->
  if id?
    $("#copy_button").attr("href", Routes.copy_workflow_path(id))
    $("#copy_button").data("method", "PUT")
    $("#copy_button").removeAttr("disabled")
    $("#copy_button").unbind('click', false)
  else
    $("#copy_button").attr("href", "#")
    $("#copy_button").attr("disabled", true)
    $("#copy_button").bind('click', false)

@update_copy_template_button = (path) ->
  if path?
    $("#copy_template_button").attr("href", Routes.new_template_path({ path: path }))
    $("#copy_template_button").data("method", "GET")
    $("#copy_template_button").removeAttr("disabled")
    $("#copy_template_button").unbind('click', false)
  else
    $("#copy_template_button").attr("href", "#")
    $("#copy_template_button").attr("disabled", true)
    $("#copy_template_button").bind('click', false)

@update_edit_button = (id) ->
  if id?
    $("#edit_button").attr("href", Routes.edit_workflow_path(id))
    $("#edit_button").removeAttr("disabled")
    $("#edit_button").unbind('click', false)
  else
    $("#edit_button").removeAttr("href")
    $("#edit_button").attr("disabled", true)
    $("#edit_button").bind('click', false)

@update_terminal_button = (path) ->
  if path?
    $("#terminal_button").attr("href", path)
    $("#terminal_button").removeAttr("disabled")
    $("#terminal_button").unbind('click', false)
  else
    $("#terminal_button").removeAttr("href")
    $("#terminal_button").attr("disabled", true)
    $("#terminal_button").bind('click', false)

@update_submit_button = (id, status_char) ->
  if id? && !status_char?
      $("#submit_button").attr("href", Routes.submit_workflow_path(id))
      $("#submit_button").data("method", "PUT")
      $("#submit_button").removeAttr("disabled")
      $("#submit_button").unbind('click', false)
  else<commit_msg>Re-enable submit for completed and failed jobs<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/

$(window).focus ->
  if active_var()
    update_display(active_var())
  return

@update_display = (id) ->
  request_job_data(id)
  update_destroy_button(id)

@request_job_data = (id) ->
  if id?
    $.ajax
      type: 'GET'
      url: Routes.workflow_path(id)
      contentType: "application/json; charset=utf-8"
      dataType: "json"
      error: (jqXHR, textStatus, errorThrown) ->
        show_job_panel()
        console.log jqXHR
      success: (data, textStatus, jqXHR) ->
        show_job_panel()
        update_status_label(id, data.status_label)
        update_job_details_panel(data)
        update_open_dir_button(data.fs_root)
        update_edit_button(id)
        update_copy_button(id)
        update_submit_button(id, data.status.char)
        update_stop_button(id, data.status.char)
        update_template_button(id)
        list_folder_contents(data)
        if missing_data_path()
          # If the template folder does not exist we need to disable certain buttons.
          update_open_dir_button()
          update_terminal_button()
          update_edit_button()
          update_submit_button()
          update_copy_button()
  else
    show_job_panel()
    disable_all_buttons()

@disable_all_buttons = ->
  update_job_details_panel()
  update_open_dir_button()
  update_edit_button()
  update_terminal_button()
  update_submit_button()
  update_stop_button()
  update_template_button()
  update_copy_button()
  update_destroy_button()
  list_folder_contents()

@show_job_panel = (show) ->
  if show?
    $("#job-details-panel").show()
  else
    $("#job-details-panel").hide()

@show_script_details_panel = (show) ->
  if show?
    $("#script-details-panel").show()
  else
    $("#script-details-panel").hide()

@update_status_label = (id, label) ->
  if label? && id?
    $("#status_label_#{id}").html(label)

@update_job_details_panel = (data) ->
  show_job_panel()
  if data?
    $("#job-details-name").text(data.name)
    $("#job-details-server").val(data.host_title)
    $("#job-details-staged-dir").text(data.staged_dir)
    if data.account == null or data.account == ""
      $("#job-details-account").addClass("text-muted")
      $("#job-details-account").text("Not specified")
    else
      $("#job-details-account").removeClass("text-muted")
      $("#job-details-account").text(data.account)
    show_job_panel(true)

@update_script_details_panel = (content) ->
  if content?
    $("#script-name").text(content.name)
    $("#open-script-dir-button").attr("href", "#{content.fs_base}")
    $("#open-terminal-dir-button").attr("href", "#{content.terminal_base}")
    $("#open-editor-button").attr("href", "#{content.editor_url}")
    update_terminal_button(content.terminal_base)
    $.ajax
      type: 'GET'
      url: content.apiurl
      contentType: "application/json; charset=utf-8"
      dataType: "text"
      error: (jqXHR, textStatus, errorThrown) ->
        show_script_details_panel()
        console.log jqXHR
      success: (filedata, textStatus, jqXHR) ->
        show_script_details_panel()
        $("#script-text-data").text(filedata)
        show_script_details_panel(true)
  else
    show_script_details_panel()


@update_open_dir_button = (path) ->
  if path?
    $("#open_dir_button").attr("href", path)
    $("#open_dir_button").removeAttr("disabled")
    $("#open_dir_button").unbind('click', false)
  else
    $("#open_dir_button").attr("href", "#")
    $("#open_dir_button").attr("disabled", true)
    $("#open_dir_button").bind('click', false)

@update_copy_button = (id) ->
  if id?
    $("#copy_button").attr("href", Routes.copy_workflow_path(id))
    $("#copy_button").data("method", "PUT")
    $("#copy_button").removeAttr("disabled")
    $("#copy_button").unbind('click', false)
  else
    $("#copy_button").attr("href", "#")
    $("#copy_button").attr("disabled", true)
    $("#copy_button").bind('click', false)

@update_copy_template_button = (path) ->
  if path?
    $("#copy_template_button").attr("href", Routes.new_template_path({ path: path }))
    $("#copy_template_button").data("method", "GET")
    $("#copy_template_button").removeAttr("disabled")
    $("#copy_template_button").unbind('click', false)
  else
    $("#copy_template_button").attr("href", "#")
    $("#copy_template_button").attr("disabled", true)
    $("#copy_template_button").bind('click', false)

@update_edit_button = (id) ->
  if id?
    $("#edit_button").attr("href", Routes.edit_workflow_path(id))
    $("#edit_button").removeAttr("disabled")
    $("#edit_button").unbind('click', false)
  else
    $("#edit_button").removeAttr("href")
    $("#edit_button").attr("disabled", true)
    $("#edit_button").bind('click', false)

@update_terminal_button = (path) ->
  if path?
    $("#terminal_button").attr("href", path)
    $("#terminal_button").removeAttr("disabled")
    $("#terminal_button").unbind('click', false)
  else
    $("#terminal_button").removeAttr("href")
    $("#terminal_button").attr("disabled", true)
    $("#terminal_button").bind('click', false)

@update_submit_button = (id, status_char) ->
  if id? && status_char != "R" && status_char != "Q"
      $("#submit_button").attr("href", Routes.submit_workflow_path(id))
      $("#submit_button").data("method", "PUT")
      $("#submit_button").removeAttr("disabled")
      $("#submit_button").unbind('click', false)
  else
{'fraction_non_alphanumeric': 0.1756224252194161, 'fraction_numerical': 0.0003582303421099767, 'mean_word_length': 4.012118491921005, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, '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': '2389591', 'n_tokens_mistral': 3593, 'n_tokens_neox': 3668, 'n_words': 578}