<commit_before>/**
 * @fileoverview This files provides a service for managing the application
 * state. The application state is written to both the URL and the local
 * storage.
 */
goog.provide('app.StateManager');

goog.require('app');
goog.require('goog.asserts');
goog.require('goog.math');
goog.require('goog.storage.mechanism.HTML5LocalStorage');
goog.require('ngeo.Location');


/**
 * @constructor
 * @param {ngeo.Location} ngeoLocation ngeo location service.
 * @ngInject
 */
app.StateManager = function(ngeoLocation) {

  /**
   * Object representing the application's initial state.
   * @type {Object.<string ,string>}
   * @private
   */
  this.initialState_ = {};

  /**
   * @type {ngeo.Location}
   * @private
   */
  this.ngeoLocation_ = ngeoLocation;

  /**
   * @type {goog.storage.mechanism.HTML5LocalStorage}
   * @private
   */
  this.localStorage_ = new goog.storage.mechanism.HTML5LocalStorage();

  /**
   * @type {number}
   * @private
   */
  this.version_ = -1;


  // Populate initialState_ with the application's initial state. The initial
  // state is read from the location URL, or from the local storage if there
  // is no state in the location URL.

  var paramKeys = ngeoLocation.getParamKeys();
  var i, key;

  if (paramKeys.length === 0 ||
      (paramKeys.length === 1 && (paramKeys.indexOf('debug') >= 0 ||
      paramKeys.indexOf('fid') >= 0 || paramKeys.indexOf('lang') >= 0)) ||
      (paramKeys.length === 2 &&
      ((paramKeys.indexOf('debug') >= 0 && paramKeys.indexOf('fid') >= 0) ||
      (paramKeys.indexOf('lang') >= 0 && paramKeys.indexOf('fid') >= 0) ||
      (paramKeys.indexOf('debug') >= 0 && paramKeys.indexOf('lang') >= 0))) ||
      (paramKeys.length === 3 &&
      paramKeys.indexOf('debug') >= 0 && paramKeys.indexOf('fid') >= 0 &&
      paramKeys.indexOf('lang') >= 0
      )) {
    if (this.localStorage_.isAvailable()) {
      var count = this.localStorage_.getCount();
      for (i = 0; i < count; ++i) {
        key = this.localStorage_.key(i);
        if (paramKeys.indexOf('lang') >= 0 && key === 'lang') {
          this.initialState_[key] = ngeoLocation.getParam(key);
        } else {
          goog.asserts.assert(!goog.isNull(key));
          this.initialState_[key] = this.localStorage_.get(key);
        }
      }
    }
    this.version_ = 3;
  } else {
    var keys = ngeoLocation.getParamKeys();
    for (i = 0; i < keys.length; ++i) {
      key = keys[i];
      this.initialState_[key] = ngeoLocation.getParam(key);
    }
    this.version_ = this.initialState_.hasOwnProperty('version') ?
        goog.math.clamp(+this.initialState_['version'], 2, 3) : 2;
  }
  goog.asserts.assert(this.version_ != -1);

  this.ngeoLocation_.updateParams({'version': 3});
};


/**
 * Return the version as set in the initial URL (the URL that user<commit_msg>Do not allow to start with a blank map<commit_after>/**
 * @fileoverview This files provides a service for managing the application
 * state. The application state is written to both the URL and the local
 * storage.
 */
goog.provide('app.StateManager');

goog.require('app');
goog.require('app.Notify');
goog.require('goog.asserts');
goog.require('goog.math');
goog.require('goog.storage.mechanism.HTML5LocalStorage');
goog.require('ngeo.Location');


/**
 * @constructor
 * @param {ngeo.Location} ngeoLocation ngeo location service.
 * @param {app.Notify} appNotify Notify service.
 * @param {angularGettext.Catalog} gettextCatalog Gettext service.
 * @ngInject
 */
app.StateManager = function(ngeoLocation, appNotify, gettextCatalog) {
  /**
   * @type {angularGettext.Catalog}
   * @private
   */
  this.gettextCatalog_ = gettextCatalog;

  /**
   * @type {app.Notify}
   * @private
   */
  this.notify_ = appNotify;

  /**
   * Object representing the application's initial state.
   * @type {Object.<string ,string>}
   * @private
   */
  this.initialState_ = {};

  /**
   * @type {ngeo.Location}
   * @private
   */
  this.ngeoLocation_ = ngeoLocation;

  /**
   * @type {goog.storage.mechanism.HTML5LocalStorage}
   * @private
   */
  this.localStorage_ = new goog.storage.mechanism.HTML5LocalStorage();

  /**
   * @type {number}
   * @private
   */
  this.version_ = -1;


  // Populate initialState_ with the application's initial state. The initial
  // state is read from the location URL, or from the local storage if there
  // is no state in the location URL.

  var paramKeys = ngeoLocation.getParamKeys();
  var i, key;

  if (paramKeys.length === 0 ||
      (paramKeys.length === 1 && (paramKeys.indexOf('debug') >= 0 ||
      paramKeys.indexOf('fid') >= 0 || paramKeys.indexOf('lang') >= 0)) ||
      (paramKeys.length === 2 &&
      ((paramKeys.indexOf('debug') >= 0 && paramKeys.indexOf('fid') >= 0) ||
      (paramKeys.indexOf('lang') >= 0 && paramKeys.indexOf('fid') >= 0) ||
      (paramKeys.indexOf('debug') >= 0 && paramKeys.indexOf('lang') >= 0))) ||
      (paramKeys.length === 3 &&
      paramKeys.indexOf('debug') >= 0 && paramKeys.indexOf('fid') >= 0 &&
      paramKeys.indexOf('lang') >= 0
      )) {
    if (this.localStorage_.isAvailable()) {
      var count = this.localStorage_.getCount();
      for (i = 0; i < count; ++i) {
        key = this.localStorage_.key(i);
        if (paramKeys.indexOf('lang') >= 0 && key === 'lang') {
          this.initialState_[key] = ngeoLocation.getParam(key);
        } else {
          goog.asserts.assert(!goog.isNull(key));
          this.initialState_[key] = this.localStorage_.get(key);
        }
      }
    }
    this.version_ = 3;
  } else {
    var keys = ngeoLocation.getParamKeys();
    for (i = 0; i < keys.length; ++i) {
      key = keys[i];
      this.initialState_[key] = ngeoLocation.getParam(key);
    }
    this.version_ = this.initialState_.hasOwnProperty('version') ?
        goog.math.clamp(+this.initialState_['version'], 2, 3) : 2;
  }

  if (!((this.initialState_.hasOwnProperty('bgLayer') &&
      this.initialState_['bgLayer'].length > 0 &&
      this.initialState_['bgLayer'] != 'blank') ||
      (this.initialState_.hasOwnProperty('layers') &&
      this.initialState_['layers'].length > 0) ||
      (this.initialState_.hasOwnProperty('fid') &&
      this.initialState_['fid'].length > 0))) {
    this.initialState_['bgLayer'] = 'basemap_2015_global';
    var msg = this.gettextCatalog_.getString(
        'Aucune couche n\'étant définie pour cette carte,' +
        ' une couche de fond a automatiquement été ajoutée.');
    this.notify_(msg, app.NotifyNotificationType.INFO);
  }

  goog.asserts.assert(this.version_ != -1);

  this.ngeoLocation_.updateParams({'version': 3});
};


/**
 * Return the version as set in the initial URL (the URL that user
{'fraction_non_alphanumeric': 0.1660179640718563, 'fraction_numerical': 0.009730538922155689, 'mean_word_length': 3.1445409429280398, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 34, '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': '6531508', 'n_tokens_mistral': 2211, 'n_tokens_neox': 2113, 'n_words': 464}