<commit_before>/*
 * RailsAdmin remote form @VERSION
 *
 * License
 *
 * http://www.railsadmin.org
 *
 * Depends:
 *   jquery.ui.core.js
 *   jquery.ui.widget.js
 *   jquery.ui.dialog.js
 */
(function($) {
  $.widget("ra.remoteForm", {
    dialog: null,
    options: {
      dialogClass: "",
      width: 720
    },

    _create: function() {
      var widget = this;
      $(widget.element).bind("click", function(e){
        e.preventDefault();
        var dialog = widget._getDialog();
        $.ajax({
          url: $(this).attr("href"),
          beforeSend: function(xhr) {
            xhr.setRequestHeader("Accept", "text/javascript");
          },
          success: function(data, status, xhr) {
            /* Todo: Parse header to dialog title */
            dialog.html(data);
            widget._bindFormEvents();
          },
          error: function(xhr, status, error) {
            dialog.html(xhr.responseText);
          }
        });
      });
    },

    _bindFormEvents: function() {
      var dialog = this._getDialog(),
          form = dialog.find("form"),
          widget = this;

      form.bind("ajax:success", function(e, data, status, xhr) {
        var input = widget.element.prev(), json = $.parseJSON(data);
        input.append('<option value="' + json["id"] + '">' + json["label"] + '</option>' );
        dialog.dialog("close");
      });

      form.bind("ajax:error", function(e, xhr, status, error) {
        dialog.html(xhr.responseText);
        widget._bindFormEvents();
      });

      form.find("input[name=_save]").bind("click", function(e) {
          e.preventDefault();
          form.callRemote();
      });

      form.find("input[name=_continue]").bind("click", function(e) {
          e.preventDefault();
          dialog.dialog("close");
      });
    },

    _getDialog: function() {
      if (!this.dialog) {
        var widget = this;
        this.dialog = $('<div class="' + this.options["dialogClass"] + '"></div>').dialog({
          autoShow: false,
          close: function(e, ui) {<commit_msg>Set remote form's header as dialog's title, use dialog buttons and render only form content + javascript and style directions.<commit_after>/*
 * RailsAdmin remote form @VERSION
 *
 * License
 *
 * http://www.railsadmin.org
 *
 * Depends:
 *   jquery.ui.core.js
 *   jquery.ui.widget.js
 *   jquery.ui.dialog.js
 */
(function($) {
  $.widget("ra.remoteForm", {
    dialog: null,
    options: {
      dialogClass: "",
      width: 720
    },

    _create: function() {
      var widget = this;
      $(widget.element).bind("click", function(e){
        e.preventDefault();
        var dialog = widget._getDialog();
        $.ajax({
          url: $(this).attr("href"),
          beforeSend: function(xhr) {
            xhr.setRequestHeader("Accept", "text/javascript");
          },
          success: function(data, status, xhr) {
            dialog.html(data);
            widget._bindFormEvents();
          },
          error: function(xhr, status, error) {
            dialog.html(xhr.responseText);
          }
        });
      });
    },

    _bindFormEvents: function() {
      var dialog = this._getDialog(),
          form = dialog.find("form"),
          widget = this,
          saveButtonText = dialog.find("input[name=_save]").val(),
          cancelButtonText = dialog.find("input[name=_continue]").val();

      dialog.dialog("option", "title", $(".ui-widget-header", dialog).remove().text());

      form.attr("data-remote", true);
      dialog.find(".submit").remove();
      dialog.find(".ra-block-content").removeClass("ra-block-content");

      var buttons = {}

      buttons[saveButtonText] = function() {
        dialog.find("form").submit();
      }

      buttons[cancelButtonText] = function() {
        dialog.dialog("close");
      }

      dialog.dialog("option", "buttons", buttons);

      form.bind("ajax:success", function(e, data, status, xhr) {
        var input = widget.element.prev(), json = $.parseJSON(data);
        input.append('<option value="' + json["id"] + '">' + json["label"] + '</option>' );
        dialog.dialog("close");
      });

      form.bind("ajax:error", function(e, xhr, status, error) {
        dialog.html(xhr.responseText);
        widget._bindFormEvents();
      });
    },

    _getDialog: function() {
      if (!this.dialog) {
        var widget = this;
        this.dialog = $('<div class="' + this.options["dialogClass"] + '"></div>').dialog({
          autoShow: false,
          close: function(e, ui) {
{'fraction_non_alphanumeric': 0.17570546737213405, 'fraction_numerical': 0.0013227513227513227, 'mean_word_length': 2.20409604519774, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6030877', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1316, 'n_words': 249}