github.com/manicqin/nomad@v0.9.5/ui/app/mixins/sortable-factory.js (about)

     1  import Mixin from '@ember/object/mixin';
     2  import Ember from 'ember';
     3  import { computed } from '@ember/object';
     4  import { warn } from '@ember/debug';
     5  
     6  /**
     7    Sortable mixin factory
     8  
     9    Simple sorting behavior for a list of objects. Pass the list of properties
    10    you want the list to be live-sorted based on, or use the generic sortable.js
    11    if you don’t need that.
    12  
    13    Properties to override:
    14      - sortProperty: the property to sort by
    15      - sortDescending: when true, the list is reversed
    16      - listToSort: the list of objects to sort
    17  
    18    Properties provided:
    19      - listSorted: a copy of listToSort that has been sorted
    20  */
    21  export default function sortableFactory(properties, fromSortableMixin) {
    22    const eachProperties = properties.map(property => `listToSort.@each.${property}`);
    23  
    24    return Mixin.create({
    25      // Override in mixin consumer
    26      sortProperty: null,
    27      sortDescending: true,
    28      listToSort: computed(() => []),
    29  
    30      _sortableFactoryWarningPrinted: false,
    31  
    32      listSorted: computed(
    33        ...eachProperties,
    34        'listToSort.[]',
    35        'sortProperty',
    36        'sortDescending',
    37        function() {
    38          if (!this._sortableFactoryWarningPrinted && !Ember.testing) {
    39            let message =
    40              'Using SortableFactory without property keys means the list will only sort when the members change, not when any of their properties change.';
    41  
    42            if (fromSortableMixin) {
    43              message += ' The Sortable mixin is deprecated in favor of SortableFactory.';
    44            }
    45  
    46            warn(message, properties.length > 0, { id: 'nomad.no-sortable-properties' });
    47            this.set('_sortableFactoryWarningPrinted', true);
    48          }
    49  
    50          const sorted = this.listToSort.compact().sortBy(this.sortProperty);
    51          if (this.sortDescending) {
    52            return sorted.reverse();
    53          }
    54          return sorted;
    55        }
    56      ),
    57    });
    58  }