<commit_before>var CoreView = require('backbone/core-view');
var _ = require('underscore');
var CarouselFormView = require('../../../components/carousel-form-view');
var CarouselCollection = require('../../../components/custom-carousel/custom-carousel-collection');
var WidgetFormFactory = require('./widgets-form-factory');
var WidgetsFormFieldsView = require('./widgets-form-fields-view');
var checkAndBuildOpts = require('../../../helpers/required-opts');
var loadingTemplate = require('../../layers/panel-loading-template.tpl');

var REQUIRED_OPTS = [
  'userActions',
  'widgetDefinitionModel',
  'modals',
  'querySchemaModel',
  'configModel',
  'userModel'
];

module.exports = CoreView.extend({

  initialize: function (opts) {
    checkAndBuildOpts(opts, REQUIRED_OPTS, this);
    this._initBinds();

    if (this._querySchemaModel.shouldFetch()) {
      this._querySchemaModel.fetch();
    }
  },

  render: function () {
    this.clearSubViews();
    this.$el.empty();

    if (!this._querySchemaModel.isFetched()) {
      this.$el.html(loadingTemplate());
    } else {
      this._renderCarousel();
      this._renderForm();
    }

    return this;
  },

  _initBinds: function () {
    this.listenTo(this._widgetDefinitionModel, 'change:type', this._renderFormAndValidate);
    this.listenTo(this._querySchemaModel, 'change:status', this.render);
  },

  _renderCarousel: function () {
    var carouselCollection = new CarouselCollection(
      _.map(WidgetFormFactory.getDataTypes(this._querySchemaModel), function (type) {
        return {
          selected: this._widgetDefinitionModel.get('type') === type.value,
          val: type.value,
          label: type.label,
          template: function () {
            return (type.iconTemplate && type.iconTemplate({ makeItBig: true })) || type.value;
          }
        };
      }, this)
    );

    carouselCollection.bind('change:selected', function (mdl) {
      if (mdl.get('selected')) {
        this._widgetDefinitionModel.changeType(mdl.getValue());
      }
    }, this);

    var view = new CarouselFormView({
      collection: carouselCollection,
      template: require('./widgets-form-types.tpl')
    });
    this.addView(view);
    this.$el.append(view.render().el);
  },

  _renderFormAndValidate: function () {
    this._renderForm();

    if (this._formView.validateForm() === null) {
      this._userActions.saveWidget(this._widgetDefinitionModel);
    }
  },

  _renderForm: function () {
    if (this._formView) {
      this.removeView(this._formView);
      this._formView.clean();
    }
    this._formView = new WidgetsFormFieldsView({
      userActions: this._userActions,
      widgetDefinitionModel: this._widgetDefinitionModel,
      querySchemaModel: this._querySchemaModel,
      modals: this._modals,
      userModel: this._userModel,
      configModel: this._configModel
    });
    this.addView(this._formView);
    this.$el.append(this._formView.render().el);
  }
});
<commit_msg>Do not allow to change a widget to TimeSeries if there is already a TimeSeries widget<commit_after>var CoreView = require('backbone/core-view');
var _ = require('underscore');
var CarouselFormView = require('../../../components/carousel-form-view');
var CarouselCollection = require('../../../components/custom-carousel/custom-carousel-collection');
var WidgetFormFactory = require('./widgets-form-factory');
var WidgetsFormFieldsView = require('./widgets-form-fields-view');
var checkAndBuildOpts = require('../../../helpers/required-opts');
var loadingTemplate = require('../../layers/panel-loading-template.tpl');

var TIME_SERIES_VALUE = 'time-series';
var REQUIRED_OPTS = [
  'userActions',
  'widgetDefinitionModel',
  'modals',
  'querySchemaModel',
  'configModel',
  'userModel'
];

module.exports = CoreView.extend({

  initialize: function (opts) {
    checkAndBuildOpts(opts, REQUIRED_OPTS, this);
    this._initBinds();

    if (this._querySchemaModel.shouldFetch()) {
      this._querySchemaModel.fetch();
    }
  },

  render: function () {
    this.clearSubViews();
    this.$el.empty();

    if (!this._querySchemaModel.isFetched()) {
      this.$el.html(loadingTemplate());
    } else {
      this._renderCarousel();
      this._renderForm();
    }

    return this;
  },

  _initBinds: function () {
    this.listenTo(this._widgetDefinitionModel, 'change:type', this._renderFormAndValidate);
    this.listenTo(this._querySchemaModel, 'change:status', this.render);
  },

  _renderCarousel: function () {
    var containsTimeSeries = _.any(this._widgetDefinitionModel.collection.models, function (model) {
      return model.get('type') === TIME_SERIES_VALUE;
    });
    var filteredDataTypes = _.filter(WidgetFormFactory.getDataTypes(this._querySchemaModel), function (type) {
      // Do not allow to change the widget to TimeSeries if there is already a TimeSeries widget
      return (!containsTimeSeries ||
              type.value !== TIME_SERIES_VALUE ||
              this._widgetDefinitionModel.get('type') === TIME_SERIES_VALUE);
    }, this);
    var carouselCollection = new CarouselCollection(
      _.map(filteredDataTypes, function (type) {
        return {
          selected: this._widgetDefinitionModel.get('type') === type.value,
          val: type.value,
          label: type.label,
          template: function () {
            return (type.iconTemplate && type.iconTemplate({ makeItBig: true })) || type.value;
          }
        };
      }, this)
    );

    carouselCollection.bind('change:selected', function (mdl) {
      if (mdl.get('selected')) {
        this._widgetDefinitionModel.changeType(mdl.getValue());
      }
    }, this);

    var view = new CarouselFormView({
      collection: carouselCollection,
      template: require('./widgets-form-types.tpl')
    });
    this.addView(view);
    this.$el.append(view.render().el);
  },

  _renderFormAndValidate: function () {
    this._renderForm();

    if (this._formView.validateForm() === null) {
      this._userActions.saveWidget(this._widgetDefinitionModel);
    }
  },

  _renderForm: function () {
    if (this._formView) {
      this.removeView(this._formView);
      this._formView.clean();
    }
    this._formView = new WidgetsFormFieldsView({
      userActions: this._userActions,
      widgetDefinitionModel: this._widgetDefinitionModel,
      querySchemaModel: this._querySchemaModel,
      modals: this._modals,
      userModel: this._userModel,
      configModel: this._configModel
    });
    this.addView(this._formView);
    this.$el.append(this._formView.render().el);
  }
});

{'fraction_non_alphanumeric': 0.14635258358662614, 'fraction_numerical': 0.0, 'mean_word_length': 3.2900912646675358, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5281645', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1928, 'n_words': 326}