<commit_before>import Ember from 'ember';
import layout from '../templates/components/simple-table-header-row';

export default Ember.Component.extend({
  layout,
  tagName: 'thead',
  columnsArray: null,

  sortingCriteria: Ember.Object.create({}),
  sortingCriteriaOrdered: Ember.A([]),

  didReceiveAttrs({oldAttrs, newAttrs}) {
    this._super(...arguments);

    if (!oldAttrs || oldAttrs.columns !== newAttrs.columns) {
      let columns      = this.get('columns');
      let columnsArray = this._processColumns(columns);
      this.set('columnsArray', columnsArray);
    }
  },

  actions: {
    sortBy(key) {
      let sortAction = this.get('sortAction');
      let sortBy     = this.get('sortBy');

      this._setOrderForColumn(key);

      let criteria = this._formatedCriteria();

      if (sortBy) {
        return sortBy(criteria);
      } else {
        return sortAction(criteria);
      }
    }
  },

  _processColumns(columns) {
    return columns.map((item) => {
      if (Ember.isArray(item)) {
        let { style, classes } = item[0];
        return { hasMultipleColumns: true, items: item, style, classes };
      } else {
        let { style, classes } = item;
        return { hasMultipleColumns: false, items: item, style, classes };
      }
    });
  },

  _setOrderForColumn(sortingKey) {
    let criteria = this.get('sortingCriteria');
    let ordered = this.get('sortingCriteriaOrdered');

    let oldOrder = criteria.get(sortingKey);
    let newOrder = this._toggleSortingOrder(oldOrder);
    criteria.set(sortingKey, newOrder);

    ordered.removeObject(sortingKey);
    ordered.insertAt(0, sortingKey);

    return newOrder;
  },

  _toggleSortingOrder(order) {
    switch(order) {
    case null:
      return 'asc';
    case 'asc':
      return 'desc';
    case 'desc':
      return null;
    default:
      return 'asc';
    }
  },

  _formatedCriteria() {
    let criteria = this.get('sortingCriteria');
    let ordered = this.get('sortingCriteriaOrdered');

    return ordered.map((orderKey) => {
      let order = criteria.get(orderKey);
      if (order) {
        return `${orderKey}:${order}`;
      }
    });
  }

});
<commit_msg>Fix issue with sorting states<commit_after>import Ember from 'ember';
import layout from '../templates/components/simple-table-header-row';

export default Ember.Component.extend({
  layout,
  tagName: 'thead',
  columnsArray: null,

  sortingCriteria: null,
  sortingCriteriaOrdered: null,

  init() {
    this._super(...arguments);

    this.set('sortingCriteriaOrdered', Ember.A([]));
    this.set('sortingCriteria', Ember.Object.create({}));
  },

  didReceiveAttrs({oldAttrs, newAttrs}) {
    this._super(...arguments);

    if (!oldAttrs || oldAttrs.columns !== newAttrs.columns) {
      let columns      = this.get('columns');
      let columnsArray = this._processColumns(columns);
      this.set('columnsArray', columnsArray);
    }
  },

  actions: {
    sortBy(key) {
      let sortAction = this.get('sortAction');
      let sortBy     = this.get('sortBy');

      this._setOrderForColumn(key);

      let criteria = this._formatedCriteria();

      if (sortBy) {
        return sortBy(criteria);
      } else {
        return sortAction(criteria);
      }
    }
  },

  _processColumns(columns) {
    return columns.map((item) => {
      if (Ember.isArray(item)) {
        let { style, classes } = item[0];
        return { hasMultipleColumns: true, items: item, style, classes };
      } else {
        let { style, classes } = item;
        return { hasMultipleColumns: false, items: item, style, classes };
      }
    });
  },

  _setOrderForColumn(sortingKey) {
    let criteria = this.get('sortingCriteria');
    let ordered = this.get('sortingCriteriaOrdered');

    let oldOrder = criteria.get(sortingKey);
    let newOrder = this._toggleSortingOrder(oldOrder);
    criteria.set(sortingKey, newOrder);

    ordered.removeObject(sortingKey);
    if (newOrder) {
      ordered.insertAt(0, sortingKey);
    }

    return newOrder;
  },

  _toggleSortingOrder(order) {
    switch(order) {
    case null:
      return 'asc';
    case 'asc':
      return 'desc';
    case 'desc':
      return null;
    default:
      return 'asc';
    }
  },

  _formatedCriteria() {
    let criteria = this.get('sortingCriteria');
    let ordered = this.get('sortingCriteriaOrdered');

    return ordered.map((orderKey) => {
      let order = criteria.get(orderKey);
      if (order) {
        return `${orderKey}:${order}`;
      }
    });
  }

});

{'fraction_non_alphanumeric': 0.1415929203539823, 'fraction_numerical': 0.0008849557522123894, 'mean_word_length': 2.802354920100925, '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': '7139339', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1391, 'n_words': 282}