<commit_before>/**
 * The AnalysisResultsPresenter class for the AnalysisResultsView.
 *
 * @return AnalysisResultsPresenter class.
 */
define([
  'map/presenters/PresenterClass',
  'underscore',
  'backbone',
  'moment',
  'mps',
  'helpers/geojsonUtilsHelper'
], function(PresenterClass, _, Backbone, moment, mps, geojsonUtilsHelper) {

  'use strict';

  var StatusModel = Backbone.Model.extend({
    defaults: {
      baselayer: null,
      both: false,
      analysis: false,
      isoTotalArea: null,
      resource: null // analysis resource
    }
  });

  var SUBSCRIPTION_ALLOWED = [
    'loss',
    'imazon',
    'terrailoss',
    'prodes',
    'guyra',
    'umd_as_it_happens',
    'modis',
    'viirs_fires_alerts'
  ];

  var AnalysisResultsPresenter = PresenterClass.extend({

    /**
     * Layers that support email subscriptions.
     */
    _alertsSubscriptionLayers: [
      'forma'
    ],

    datasets: {
      'loss': 'umd-loss-gain',
      'forestgain': 'umd-loss-gain',
      'forma': 'forma-alerts',
      'imazon': 'imazon-alerts',
      'fires': 'nasa-active-fires',
      'modis': 'quicc-alerts',
      'terrailoss': 'terrai-alerts',
      'prodes': 'prodes-loss',
      'guyra': 'guyra-loss',
      'forest2000': 'umd-loss-gain',
      'umd_as_it_happens':'glad-alerts',
      'viirs_fires_alerts': 'viirs-active-fires'
    },

    init: function(view) {
      this.view = view;
      this.status = new StatusModel();
      this._super();
    },

    /**
     * Application subscriptions.
     */
    _subscriptions: [{
      'Place/go': function(place) {
        this._setBaselayer(place.layerSpec.getBaselayers());
        if ( place.params.subscribe_alerts ) this.subscribeAnalysis();
      }
    }, {
      'LayerNav/change': function(layerSpec) {
        this._setBaselayer(layerSpec.getBaselayers());
      }
    }, {
      'AnalysisService/results': function(results) {
        this._renderResults(results);
      }
    }, {
      'AnalysisResults/totalArea': function(area) {
        this._setTotalArea(area);
      }
    }, {
      'AnalysisTool/iso-drawn': function(multipolygon) {
        var isoTotalArea = geojsonUtilsHelper.getHectares(
          multipolygon);
        this.status.set('isoTotalArea', isoTotalArea);
      }
    }, {
      /**
       * Get the analysis resource so we can
       * get the data for the subscribe button.
       *
       * @param  {Object} resource Analysis resource
       */
      'AnalysisService/get': function(resource) {
        this.status.set('resource', resource);
      }
    }, {
      'AnalysisResults/Delete': function() {
        this.view._deleteAnalysisView();
      }
    },{
      'Analysis/toggle': function() {
        this.view.toogleAnalysis($('#analysis-tab').hasClass('is-analysis'));
      }
    },{
      'DownloadView/create': function(downloadView) {
        this.view.downloadView = downloadView;
      }
    }],

    /**
     * Set the status.baselayer from layerSpec.
     *
     * @param {Object} baselayers Current active baselayers
     */
    _setBaselayer: function(baselayers) {
      var baselayer;

      if (baselayers['loss']) {
        this.loss = true;
        baselayer = baselayers['loss'];
        this.status.set('both', (baselayers['forestgain']) ? true : false);
      }else{
        this.loss = false;
        baselayer = baselayers[_.first(_.intersection(
          _.pluck(baselayers, 'slug'),
          _.keys(this.datasets)))];
      }

      this.status.set('baselayer', baselayer);
    },

    /**
     * Set the subscribe button to disabled if alerts
     * are not supported for the current layers.
     */
    _setSubscribeButton: function() {
      // var supported = false;
      // var baselayer = this.status.get('baselayer');

      // Subscriptions not supported for regions yet.
      // if (baselayer && !this.status.get('resource').id1) {
      //   supported = _.indexOf(this._alertsSubscriptionLayers,
      //     baselayer.slug) >= 0;
      // }

      this.view.toggleSubscribeButton(false);
    },

    /**
     * Handle analysis results from the supplied object.
     *
     * @param  {Object} results [description]
     */
    _renderResults: function(results) {
      // Even if the result is a failure or unavailable message, we render
      // the widget results and keep the polygon.
      this.status.set('analysis', true);
      if (results.unavailable) {
        mps.publish('Spinner/stop');
        this.view.renderFailure();
      } else if (results.failure) {
        mps.publish('Spinner/stop');
        this.view.renderFailure();
      } else {
        mps.publish('Spinner/stop');
        this._renderAnalysis(results);
        // Subscribe button just should be activated
        // when a analysis is succesfully rendered.
        this.view.$tab.addClass('is-analysis');
        mps.publish('Analysis/toggle');
        this._setSubscribeButton();
      }<commit_msg>call proper method for when the API doesnt give any answer back<commit_after>/**
 * The AnalysisResultsPresenter class for the AnalysisResultsView.
 *
 * @return AnalysisResultsPresenter class.
 */
define([
  'map/presenters/PresenterClass',
  'underscore',
  'backbone',
  'moment',
  'mps',
  'helpers/geojsonUtilsHelper'
], function(PresenterClass, _, Backbone, moment, mps, geojsonUtilsHelper) {

  'use strict';

  var StatusModel = Backbone.Model.extend({
    defaults: {
      baselayer: null,
      both: false,
      analysis: false,
      isoTotalArea: null,
      resource: null // analysis resource
    }
  });

  var SUBSCRIPTION_ALLOWED = [
    'loss',
    'imazon',
    'terrailoss',
    'prodes',
    'guyra',
    'umd_as_it_happens',
    'modis',
    'viirs_fires_alerts'
  ];

  var AnalysisResultsPresenter = PresenterClass.extend({

    /**
     * Layers that support email subscriptions.
     */
    _alertsSubscriptionLayers: [
      'forma'
    ],

    datasets: {
      'loss': 'umd-loss-gain',
      'forestgain': 'umd-loss-gain',
      'forma': 'forma-alerts',
      'imazon': 'imazon-alerts',
      'fires': 'nasa-active-fires',
      'modis': 'quicc-alerts',
      'terrailoss': 'terrai-alerts',
      'prodes': 'prodes-loss',
      'guyra': 'guyra-loss',
      'forest2000': 'umd-loss-gain',
      'umd_as_it_happens':'glad-alerts',
      'viirs_fires_alerts': 'viirs-active-fires'
    },

    init: function(view) {
      this.view = view;
      this.status = new StatusModel();
      this._super();
    },

    /**
     * Application subscriptions.
     */
    _subscriptions: [{
      'Place/go': function(place) {
        this._setBaselayer(place.layerSpec.getBaselayers());
        if ( place.params.subscribe_alerts ) this.subscribeAnalysis();
      }
    }, {
      'LayerNav/change': function(layerSpec) {
        this._setBaselayer(layerSpec.getBaselayers());
      }
    }, {
      'AnalysisService/results': function(results) {
        this._renderResults(results);
      }
    }, {
      'AnalysisResults/totalArea': function(area) {
        this._setTotalArea(area);
      }
    }, {
      'AnalysisTool/iso-drawn': function(multipolygon) {
        var isoTotalArea = geojsonUtilsHelper.getHectares(
          multipolygon);
        this.status.set('isoTotalArea', isoTotalArea);
      }
    }, {
      /**
       * Get the analysis resource so we can
       * get the data for the subscribe button.
       *
       * @param  {Object} resource Analysis resource
       */
      'AnalysisService/get': function(resource) {
        this.status.set('resource', resource);
      }
    }, {
      'AnalysisResults/Delete': function() {
        this.view._deleteAnalysisView();
      }
    },{
      'Analysis/toggle': function() {
        this.view.toogleAnalysis($('#analysis-tab').hasClass('is-analysis'));
      }
    },{
      'DownloadView/create': function(downloadView) {
        this.view.downloadView = downloadView;
      }
    }],

    /**
     * Set the status.baselayer from layerSpec.
     *
     * @param {Object} baselayers Current active baselayers
     */
    _setBaselayer: function(baselayers) {
      var baselayer;

      if (baselayers['loss']) {
        this.loss = true;
        baselayer = baselayers['loss'];
        this.status.set('both', (baselayers['forestgain']) ? true : false);
      }else{
        this.loss = false;
        baselayer = baselayers[_.first(_.intersection(
          _.pluck(baselayers, 'slug'),
          _.keys(this.datasets)))];
      }

      this.status.set('baselayer', baselayer);
    },

    /**
     * Set the subscribe button to disabled if alerts
     * are not supported for the current layers.
     */
    _setSubscribeButton: function() {
      // var supported = false;
      // var baselayer = this.status.get('baselayer');

      // Subscriptions not supported for regions yet.
      // if (baselayer && !this.status.get('resource').id1) {
      //   supported = _.indexOf(this._alertsSubscriptionLayers,
      //     baselayer.slug) >= 0;
      // }

      this.view.toggleSubscribeButton(false);
    },

    /**
     * Handle analysis results from the supplied object.
     *
     * @param  {Object} results [description]
     */
    _renderResults: function(results) {
      // Even if the result is a failure or unavailable message, we render
      // the widget results and keep the polygon.
      this.status.set('analysis', true);
      if (results.unavailable) {
        mps.publish('Spinner/stop');
        this.view.renderFailure();
      } else if (results.failure) {
        mps.publish('Spinner/stop');
        this.view.renderFailureOnApi();
      } else {
        mps.publish('Spinner/stop');
        this._renderAnalysis(results);
        // Subscribe button just should be activated
        // when a analysis is succesfully rendered.
        this.view.$tab.addClass('is-analysis');
        mps.publish('Analysis/toggle');
        this._setSubscribeButton();
      }
{'fraction_non_alphanumeric': 0.1385220284302853, 'fraction_numerical': 0.0012097993749369896, 'mean_word_length': 2.3558863328822732, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4232330', 'n_tokens_mistral': 3123, 'n_tokens_neox': 3014, 'n_words': 579}