Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
evergreen / app / assets / javascripts / form.js
Size: Mime:
$(function() {
  $togglePathEditable = function() {
    $('input.custom-path').prop('disabled', !$('input.using-custom-path').prop('checked'));
  };

  $togglePathEditable();
  $(document).change('input.using-custom-path', $togglePathEditable);

  var formHelpers = {
    actionTaken: function(_this) {
      this.$action_taken = $(_this).find('input[clicked=true]');
      if (this.$action_taken.attr('name') === 'preview') {
        this.$preview = this.$action_taken;
      } else {
        this.$preview = null;
      }
    },
    saveInputMethod: function(_this) {
      var $method = '';
      this.$method_input = $(_this).find('input[name=_method]');

      if (this.$method_input.length === 0) {
        $method = $(_this).attr('method');
      } else {
        $method = this.$method_input.val();
      }

      if (!$.data(_this, 'original-method')) {
        $(_this).data('original-method', $method);
        $(_this).data('original-action', _this.action);
      }
    }
  };

  var $form = $('form.previewable');

  $form.on('submit', function(e) {
    formHelpers.actionTaken(this);
    formHelpers.saveInputMethod(this);

    if (formHelpers.$preview) {
      if (formHelpers.$method_input.length) {
        formHelpers.$method_input.val('post');
      } else {
        $(this).attr('method', 'post');
      }

      $(this).removeAttr('data-remote');
      $(this).removeData("remote");
      this.action = formHelpers.$preview.data('path');
      this.target = '_blank';

    } else {
      if (formHelpers.$method_input.length) {
        formHelpers.$method_input.val($(this).data('original-method'));
      } else {
        $(this).attr('method', $(this).data('original-method'));
      }

      $(this).attr('data-remote', true);
      this.action = $(this).data('original-action');
      this.target = '_self';
    }
  });

  $form.on('ajax:complete', function(e, xhr, status) {
    var is_valid = (/[\d]+$/).test(this.action);

    if (is_valid) {
      formHelpers.$method_input.val('put');

      $(this).data('original-action', this.action);
      $(this).data('original-method', 'put');

      if (window.history && window.history.pushState) {
        // resource_json comes from _toolbar.js.erb
        window.history.pushState({}, null, this.action + "/edit");
      }
    }
  });

});

$(document).on('click', 'form input[type=submit]', function() {
  $('[clicked=true]').removeAttr('clicked');
  $(this).attr('clicked', true);
});