<commit_before>### define 
jquery : $
underscore : _
../libs/toast : Toast
###

$ ->

  # Progresssive enhancements
  $(document).on "click", "[data-newwindow]", (e) ->

    [ width, height ] = $(this).data("newwindow").split("x")
    window.open(this.href, "_blank", "width=#{width},height=#{height},location=no,menubar=no")
    e.preventDefault()


  $(document).on "click", "a[data-ajax]", (event) ->
    
    event.preventDefault()
    $this = $(this)
    $form = $this.parents("form").first()

    options = {}
    for action in $this.data("ajax").split(",")
      [ key, value ] = action.split("=")
      options[key] = value ? true

    ajaxOptions = 
      url : this.href
      dataType : "json"

    if options["confirm"]
      return unless confirm("Are you sure?")

    if options["method"]
      ajaxOptions["type"] = options.method

    if options["submit"]
      $validationGroup = $this.parents("form, [data-validation-group]").first()
      isValid = true
      $validationGroup.find(":input")
        .each( ->
          unless this.checkValidity()
            isValid = false
            Toast.error( $(this).data("invalid-message") || this.validationMessage )
        )
      return unless isValid
      ajaxOptions["type"] = options.method ? $form[0].method ? "POST"
      ajaxOptions["data"] = $form.serialize()

    if options["busy-class"]
      $this.addClass("busy")

    $.ajax(ajaxOptions).then(

      ({ html, messages }) ->

        if messages?
          Toast.message(messages)
        else
          Toast.success("Success :-)")

        $this.trigger("ajax-success", html, messages)

        if options["replace-row"] 
          $this.parents("tr").first().replaceWith(html)

        if options["delete-row"]
          if $this.parents("table").first().hasClass("table-details")
            $this.parents("tr").first().next().remove()
          $this.parents("tr").first().remove()

        if options["add-row"]
          $(options["add-row"]).find("tbody").append(html)

        if options["replace-table"]
          $(options["replace-table"]).replaceWith(html)

        if options["delete-parent"]?
          if options["delete-parent"] != true
            $this.parents(options["delete-parent"]).first().remove()
          else
            $this.parent().remove()

        if options["reload"]
          window.location.reload()

        if options["redirect"]
          setTimeout(
            -> window.location.href = options["redirect"]
            500
          )

        return

      (jqXHR) ->
        try
          data = JSON.parse(jqXHR.responseText)

        catch error
          return Toast.error("Internal Error :-(")

        if (messages = data.messages)?
          Toast.message(messages)
        else
          Toast.error("Error :-/")

        $this.trigger("ajax-error", messages)
    ).always(
      -> 
        $this.removeClass("busy")
    )
  

  $(document).on "change", "table input.select-all-rows", ->

    $this = $(this)
    $this.parents("table").find("tbody input.select-row").prop("checked", this.checked)
    return


  do ->

    shiftKeyPressed = false
    $(document).on 
      "keydown" : (event) ->
        shiftKeyPressed = event.shiftKey
        return<commit_msg>OX-355 added [data-ajax] for forms<commit_after>### define 
jquery : $
underscore : _
../libs/toast : Toast
###

$ ->

  # Progresssive enhancements
  $(document).on "click", "[data-newwindow]", (e) ->

    [ width, height ] = $(this).data("newwindow").split("x")
    window.open(this.href, "_blank", "width=#{width},height=#{height},location=no,menubar=no")
    e.preventDefault()


  dataAjaxHandler = (event) ->
    
    event.preventDefault()
    $this = $(this)
    $form = if $this.is("form") then $this else $this.parents("form").first()

    options = {}
    for action in $this.data("ajax").split(",")
      [ key, value ] = action.split("=")
      options[key] = value ? true

    ajaxOptions = 
      url : if $this.is("form") then this.action else this.href
      dataType : "json"

    if options["confirm"]
      return unless confirm("Are you sure?")

    if options["method"]
      ajaxOptions["type"] = options.method

    if options["submit"]
      $validationGroup = if $this.is("form") then $this else $this.parents("form, [data-validation-group]").first()
      isValid = true
      $validationGroup.find(":input")
        .each( ->
          unless this.checkValidity()
            isValid = false
            Toast.error( $(this).data("invalid-message") || this.validationMessage )
        )
      return unless isValid
      ajaxOptions["type"] = options.method ? $form[0].method ? "POST"
      ajaxOptions["data"] = $form.serialize()

    if options["busy-class"]
      $this.addClass("busy")

    $.ajax(ajaxOptions).then(

      ({ html, messages }) ->

        if messages?
          Toast.message(messages)
        else
          Toast.success("Success :-)")

        $this.trigger("ajax-success", html, messages)

        if options["replace-row"] 
          $this.parents("tr").first().replaceWith(html)

        if options["delete-row"]
          if $this.parents("table").first().hasClass("table-details")
            $this.parents("tr").first().next().remove()
          $this.parents("tr").first().remove()

        if options["add-row"]
          $(options["add-row"]).find("tbody").append(html)

        if options["replace-table"]
          $(options["replace-table"]).replaceWith(html)

        if options["delete-parent"]?
          if options["delete-parent"] != true
            $this.parents(options["delete-parent"]).first().remove()
          else
            $this.parent().remove()

        if options["reload"]
          window.location.reload()

        if options["redirect"]
          setTimeout(
            -> window.location.href = options["redirect"]
            500
          )

        return

      (jqXHR) ->
        try
          data = JSON.parse(jqXHR.responseText)

        catch error
          return Toast.error("Internal Error :-(")

        if (messages = data.messages)?
          Toast.message(messages)
        else
          Toast.error("Error :-/")

        $this.trigger("ajax-error", messages)
    ).always(
      -> 
        $this.removeClass("busy")
    )

  $(document).on "click", "a[data-ajax]", dataAjaxHandler
  $(document).on "submit", "form[data-ajax]", dataAjaxHandler
  

  $(document).on "change", "table input.select-all-rows", ->

    $this = $(this)
    $this.parents("table").find("tbody input.select-row").prop("checked", this.checked)
    return


  do ->

    shiftKeyPressed = false
    $(document).on 
      "keydown" : (event) ->
        shiftKeyPressed = event.shiftKey
        return
{'fraction_non_alphanumeric': 0.17191679049034175, 'fraction_numerical': 0.0016344725111441308, 'mean_word_length': 2.2930528375733856, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 25, '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': '3130221', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1944, 'n_words': 354}