Sha256: 0d5e50f1fd1e919103538962b6cfc3dc84aa396d4c401ce2f4bb883bc4c26453

Contents?: true

Size: 1.6 KB

Versions: 16

Compression:

Stored size: 1.6 KB

Contents

pageflow.JsonInputView = Backbone.Marionette.ItemView.extend({
  mixins: [pageflow.inputView],

  template: 'pageflow/ui/templates/inputs/json_input',
  className: 'json_input',

  ui: {
    input: 'textarea'
  },

  events: {
    'change': 'onChange',
    'keyup': 'validate'
  },

  onRender: function() {
    this.load();
    this.validate();

    this.listenTo(this.model, 'change:' + this.options.propertyName, this.load);
  },

  onChange: function() {
    if (this.validate()) {
      this.save();
    }
  },

  onClose: function() {
    if (this.validate()) {
      this.save();
    }
  },

  save: function() {
    this.model.set(this.options.propertyName,
                   this.ui.input.val() ? JSON.parse(this.ui.input.val()) : null);
  },

  load: function() {
    var input = this.ui.input;
    var value = this.model.get(this.options.propertyName);

    input.val(value ? JSON.stringify(value, null, 2) : '');
  },

  validate: function() {
    var input = this.ui.input;

    if (input.val() && !this.isValidJson(input.val())) {
      this.displayValidationError(I18n.t('pageflow.ui.views.inputs.json_input_view.invalid'));
      return false;
    }
    else {
      this.resetValidationError();
      return true;
    }
  },

  displayValidationError: function(message) {
    this.$el.addClass('invalid');
    this.ui.input.attr('title', message);
  },

  resetValidationError: function(message) {
    this.$el.removeClass('invalid');
    this.ui.input.attr('title', '');
  },

  isValidJson: function(text) {
    try {
      JSON.parse(text);
      return true;
    }
    catch(e) {
      return false;
    }
  }
});

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
pageflow-14.5.2 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js
pageflow-15.0.2 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js
pageflow-15.0.1 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js
pageflow-15.0.0 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js
pageflow-14.5.1 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js
pageflow-14.5.0 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js
pageflow-15.0.0.rc2 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js
pageflow-15.0.0.rc1 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js
pageflow-15.0.0.beta4 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js
pageflow-15.0.0.beta3 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js
pageflow-14.4.0 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js
pageflow-15.0.0.beta2 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js
pageflow-15.0.0.beta1 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js
pageflow-14.3.0 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js
pageflow-14.2.1 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js
pageflow-14.2.0 app/assets/javascripts/pageflow/ui/views/inputs/json_input_view.js