<commit_before>var CoreView = require('backbone/core-view');
var template = require('./analysis-controls.tpl');
var AnalysesQuotaFactory = require('./analyses-quota-factory');
var InfoboxView = require('../../../../components/infobox/infobox-view');
var InfoboxFactory = require('../../../../components/infobox/infobox-factory');
var InfoboxModel = require('../../../../components/infobox/infobox-model');
var InfoboxCollection = require('../../../../components/infobox/infobox-collection');

/**
 * View representing the apply button for a form
 */
module.exports = CoreView.extend({
  className: 'Options-bar Options-bar--right u-flex',

  events: {
    'click .js-save': '_onSaveClicked'
  },

  initialize: function (opts) {
    if (!opts.formModel) throw new Error('formModel is required');
    if (!opts.userActions) throw new Error('userActions is required');
    if (!opts.userModel) throw new Error('userModel is required');
    if (!opts.stackLayoutModel) throw new Error('stackLayoutModel is required');

    this._formModel = opts.formModel;
    this._userActions = opts.userActions;
    this._userModel = opts.userModel;
    this._userFetchModelState = 'idle';
    this._stackLayoutModel = opts.stackLayoutModel;

    this._isNew = !opts.analysisNode;
    this._analysisNode = opts.analysisNode;

    this._formModel.on('change', this.render, this);
    this.add_related_model(this._formModel);

    if (this._analysisNode) {
      this._analysisNode.on('change:status', this.render, this);
      this.add_related_model(this._analysisNode);
    }
  },

  render: function () {
    this.clearSubViews();
    var requiresQuota = AnalysesQuotaFactory.requiresQuota(this._formModel.get('type'), this._userModel);
    var html;

    if (requiresQuota) {
      html = this._createQuotaView();
    } else {
      html = this._html();
    }

    this.$el.html(html);
    return this;
  },

  _html: function () {
    return template({
      label: this._formModel.get('persisted')
        ? _t('editor.layers.analysis-form.apply-btn')
        : _t('editor.layers.analysis-form.create-btn'),
      isDisabled: !this._canSave()
    });
  },

  _createQuotaView: function () {
    var quotaInfo = AnalysesQuotaFactory.getQuotaInfo(this._formModel.get('type'), this._userModel);
    var creditsLeft = quotaInfo.totalQuota - quotaInfo.usedQuota;
    var canRunAnalysis = creditsLeft > 0 || quotaInfo.hardLimit === false;

    var states = [
      {
        state: 'loading',
        createContentView: function () {
          return InfoboxFactory.createLoading({
            title: _t('editor.layers.analysis-form.quota.title'),
            body: _t('editor.layers.analysis-form.quota.loading')
          });
        }
      },
      {
        state: 'ready',
        createContentView: function () {
          var confirmLabel = this._formModel.get('persisted')
            ? _t('editor.layers.analysis-form.apply-btn')
            : _t('editor.layers.analysis-form.create-btn');

          var quotaMessage = '';
          var blockPrice = (quotaInfo.blockPrice / 100);
          if (creditsLeft > 0) {
            quotaMessage = _t('editor.layers.analysis-form.quota.credits-left-message', { smart_count: creditsLeft });
          } else {
            quotaMessage = _t('editor.layers.analysis-form.quota.no-credits-message');
          }
          quotaInfo.quotaMessage = quotaMessage;

          return InfoboxFactory.createQuota({
            type: 'alert',
            title: _t('editor.layers.analysis-form.quota.title'),
            body: creditsLeft > 0
              ? _t('editor.layers.analysis-form.quota.credits-left-body', {
                blockSize: quotaInfo.blockSize,
                blockPrice: blockPrice
              })
              : _t('editor.layers.analysis-form.quota.no-credits-body', {
                blockSize: quotaInfo.blockSize,
                blockPrice: blockPrice
              }),
            confirmLabel: confirmLabel,
            confirmType: 'button',
            confirmDisabled: !this._formModel.isValid() || !canRunAnalysis,
            cancelLabel: _t('editor.layers.analysis-form.quota.cancel'),
            cancelType: 'link',
            quota: quotaInfo
          });
        }.bind(this),
        mainAction: function () {
          this._stackLayoutModel.prevStep('layers');
        }.bind(this),
        secondAction: function () {
          if (this._formModel.isValid() && canRunAnalysis) {
            this._userActions.saveAnalysis(this._formModel);
          }
        }.bind(this)
      },
      {
        state: 'error',<commit_msg>Don't render quota things until analysis is not ready to be changed<commit_after>var CoreView = require('backbone/core-view');
var template = require('./analysis-controls.tpl');
var AnalysesQuotaFactory = require('./analyses-quota-factory');
var InfoboxView = require('../../../../components/infobox/infobox-view');
var InfoboxFactory = require('../../../../components/infobox/infobox-factory');
var InfoboxModel = require('../../../../components/infobox/infobox-model');
var InfoboxCollection = require('../../../../components/infobox/infobox-collection');

/**
 * View representing the apply button for a form
 */
module.exports = CoreView.extend({
  className: 'Options-bar Options-bar--right u-flex',

  events: {
    'click .js-save': '_onSaveClicked'
  },

  initialize: function (opts) {
    if (!opts.formModel) throw new Error('formModel is required');
    if (!opts.userActions) throw new Error('userActions is required');
    if (!opts.userModel) throw new Error('userModel is required');
    if (!opts.stackLayoutModel) throw new Error('stackLayoutModel is required');

    this._formModel = opts.formModel;
    this._userActions = opts.userActions;
    this._userModel = opts.userModel;
    this._userFetchModelState = 'idle';
    this._stackLayoutModel = opts.stackLayoutModel;

    this._isNew = !opts.analysisNode;
    this._analysisNode = opts.analysisNode;

    this._formModel.on('change', this.render, this);
    this.add_related_model(this._formModel);

    if (this._analysisNode) {
      this._analysisNode.on('change:status', this.render, this);
      this.add_related_model(this._analysisNode);
    }
  },

  render: function () {
    this.clearSubViews();
    var requiresQuota = AnalysesQuotaFactory.requiresQuota(this._formModel.get('type'), this._userModel);
    var html;

    if (this._canSave() && requiresQuota) {
      html = this._createQuotaView();
    } else {
      html = this._html();
    }

    this.$el.html(html);
    return this;
  },

  _html: function () {
    return template({
      label: this._formModel.get('persisted')
        ? _t('editor.layers.analysis-form.apply-btn')
        : _t('editor.layers.analysis-form.create-btn'),
      isDisabled: !this._canSave()
    });
  },

  _createQuotaView: function () {
    var quotaInfo = AnalysesQuotaFactory.getQuotaInfo(this._formModel.get('type'), this._userModel);
    var creditsLeft = quotaInfo.totalQuota - quotaInfo.usedQuota;
    var canRunAnalysis = creditsLeft > 0 || quotaInfo.hardLimit === false;

    var states = [
      {
        state: 'loading',
        createContentView: function () {
          return InfoboxFactory.createLoading({
            title: _t('editor.layers.analysis-form.quota.title'),
            body: _t('editor.layers.analysis-form.quota.loading')
          });
        }
      },
      {
        state: 'ready',
        createContentView: function () {
          var confirmLabel = this._formModel.get('persisted')
            ? _t('editor.layers.analysis-form.apply-btn')
            : _t('editor.layers.analysis-form.create-btn');

          var quotaMessage = '';
          var blockPrice = (quotaInfo.blockPrice / 100);
          if (creditsLeft > 0) {
            quotaMessage = _t('editor.layers.analysis-form.quota.credits-left-message', { smart_count: creditsLeft });
          } else {
            quotaMessage = _t('editor.layers.analysis-form.quota.no-credits-message');
          }
          quotaInfo.quotaMessage = quotaMessage;

          return InfoboxFactory.createQuota({
            type: 'alert',
            title: _t('editor.layers.analysis-form.quota.title'),
            body: creditsLeft > 0
              ? _t('editor.layers.analysis-form.quota.credits-left-body', {
                blockSize: quotaInfo.blockSize,
                blockPrice: blockPrice
              })
              : _t('editor.layers.analysis-form.quota.no-credits-body', {
                blockSize: quotaInfo.blockSize,
                blockPrice: blockPrice
              }),
            confirmLabel: confirmLabel,
            confirmType: 'button',
            confirmDisabled: !this._canSave() || !canRunAnalysis,
            cancelLabel: _t('editor.layers.analysis-form.quota.cancel'),
            cancelType: 'link',
            quota: quotaInfo
          });
        }.bind(this),
        mainAction: function () {
          this._stackLayoutModel.prevStep('layers');
        }.bind(this),
        secondAction: function () {
          if (this._canSave() && canRunAnalysis) {
            this._userActions.saveAnalysis(this._formModel);
          }
        }.bind(this)
      },
      {
        state: 'error',
{'fraction_non_alphanumeric': 0.1409882149421559, 'fraction_numerical': 0.0012974375608173856, 'mean_word_length': 2.499810821036701, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4903206', 'n_tokens_mistral': 2722, 'n_tokens_neox': 2606, 'n_words': 459}