<commit_before>import Ember from 'ember';

const {on, computed, inject } = Ember;

/***
 * Turns a mix of string and objects into objects with key/value so we can use consistently.
 */
var adjustPropertyDefinitions = function(properties) {
  return properties.map(function(propertyDefinition) {
    if (typeof propertyDefinition === 'string') {
      return {
        mixeePropertyName: propertyDefinition,
        persistedPropertyName: propertyDefinition
      };
    }
    var key = Object.keys(propertyDefinition)[0];
    return {
      mixeePropertyName: key,
      persistedPropertyName: propertyDefinition[key]
    };
  });
};
/***
 * Synchronizes mixee properties with persisted user ViewState.
 * some of the API is inspired by concept's used by Ember's [query-params]
 * (http://emberjs.com/guides/routing/query-params/), please read it for a deeper understanding
 */
export default Ember.Mixin.create({
  viewStateRepository: inject.service(),
  /***
   * key used to uniquely identify the persisted properties.
   * Required, must be defined by mixee
   */
  viewStateKey: null,
  /***
   * List of properties to persist
   * Specify an array with strings or objects. If using an object, the key refers to the mixee property
   * and the string value to the persisted property
   * ```
   * viewStateKey: ['sort', 'group', {componentProperty: 'persistedProperty'}]
   * ```
   * To specify a default value, simply define it in the mixee.
   * For more information see [query-params Map a controller's property to a different query param
   * key](http://emberjs.com/guides/routing/query-params/)
   */
  viewStateProperties: computed(function() {
    return [];
  }),

  loadViewState: on('init', function() {
    const viewState = this.get('viewStateRepository').getViewStateFor(this.get('viewStateKey'));
    const propertyDefinitions = adjustPropertyDefinitions(this.get('viewStateProperties'));

    propertyDefinitions.forEach(propertyDefinition=> {
      if (viewState.hasOwnProperty(propertyDefinition.persistedPropertyName)) {
        // If we have a persisted value, we set it in the mixee. Otherwise, leave the default
        this.set(propertyDefinition.mixeePropertyName,
          viewState[propertyDefinition.persistedPropertyName]);
      }
    });
  }),

  persistViewState: on('willDestroyElement', function() {
    const viewState = this.get('viewStateRepository').getViewStateFor(this.get('viewStateKey')),
    const propertyDefinitions = adjustPropertyDefinitions(this.get('viewStateProperties'));

    propertyDefinitions.forEach(propertyDefinition=> {
      Ember.set(viewState, propertyDefinition.persistedPropertyName,
        this.get(propertyDefinition.mixeePropertyName));
    });
    viewState.lastUpdatedAt = new Date();
    this.get('viewStateRepository').flush();
  })
});<commit_msg>Fixes jshint<commit_after>import Ember from 'ember';

const {on, computed, inject } = Ember;

/***
 * Turns a mix of string and objects into objects with key/value so we can use consistently.
 */
var adjustPropertyDefinitions = function(properties) {
  return properties.map(function(propertyDefinition) {
    if (typeof propertyDefinition === 'string') {
      return {
        mixeePropertyName: propertyDefinition,
        persistedPropertyName: propertyDefinition
      };
    }
    var key = Object.keys(propertyDefinition)[0];
    return {
      mixeePropertyName: key,
      persistedPropertyName: propertyDefinition[key]
    };
  });
};
/***
 * Synchronizes mixee properties with persisted user ViewState.
 * some of the API is inspired by concept's used by Ember's [query-params]
 * (http://emberjs.com/guides/routing/query-params/), please read it for a deeper understanding
 */
export default Ember.Mixin.create({
  viewStateRepository: inject.service(),
  /***
   * key used to uniquely identify the persisted properties.
   * Required, must be defined by mixee
   */
  viewStateKey: null,
  /***
   * List of properties to persist
   * Specify an array with strings or objects. If using an object, the key refers to the mixee property
   * and the string value to the persisted property
   * ```
   * viewStateKey: ['sort', 'group', {componentProperty: 'persistedProperty'}]
   * ```
   * To specify a default value, simply define it in the mixee.
   * For more information see [query-params Map a controller's property to a different query param
   * key](http://emberjs.com/guides/routing/query-params/)
   */
  viewStateProperties: computed(function() {
    return [];
  }),

  loadViewState: on('init', function() {
    const viewState = this.get('viewStateRepository').getViewStateFor(this.get('viewStateKey'));
    const propertyDefinitions = adjustPropertyDefinitions(this.get('viewStateProperties'));

    propertyDefinitions.forEach(propertyDefinition=> {
      if (viewState.hasOwnProperty(propertyDefinition.persistedPropertyName)) {
        // If we have a persisted value, we set it in the mixee. Otherwise, leave the default
        this.set(propertyDefinition.mixeePropertyName,
          viewState[propertyDefinition.persistedPropertyName]);
      }
    });
  }),

  persistViewState: on('willDestroyElement', function() {
    const viewState = this.get('viewStateRepository').getViewStateFor(this.get('viewStateKey'));
    const propertyDefinitions = adjustPropertyDefinitions(this.get('viewStateProperties'));

    propertyDefinitions.forEach(propertyDefinition=> {
      Ember.set(viewState, propertyDefinition.persistedPropertyName,
        this.get(propertyDefinition.mixeePropertyName));
    });
    viewState.lastUpdatedAt = new Date();
    this.get('viewStateRepository').flush();
  })
});
{'fraction_non_alphanumeric': 0.11073766141871573, 'fraction_numerical': 0.0003537944454272068, 'mean_word_length': 4.048214285714286, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4119891', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1489, 'n_words': 415}