github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/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( 23 (property) => `listToSort.@each.${property}` 24 ); 25 26 // eslint-disable-next-line ember/no-new-mixins 27 return Mixin.create({ 28 // Override in mixin consumer 29 sortProperty: null, 30 sortDescending: true, 31 listToSort: computed(function () { 32 return []; 33 }), 34 35 _sortableFactoryWarningPrinted: false, 36 37 listSorted: computed( 38 ...eachProperties, 39 '_sortableFactoryWarningPrinted', 40 'listToSort.[]', 41 'sortDescending', 42 'sortProperty', 43 function () { 44 if (!this._sortableFactoryWarningPrinted && !Ember.testing) { 45 let message = 46 'Using SortableFactory without property keys means the list will only sort when the members change, not when any of their properties change.'; 47 48 if (fromSortableMixin) { 49 message += 50 ' The Sortable mixin is deprecated in favor of SortableFactory.'; 51 } 52 53 warn(message, properties.length > 0, { 54 id: 'nomad.no-sortable-properties', 55 }); 56 // eslint-disable-next-line ember/no-side-effects 57 this.set('_sortableFactoryWarningPrinted', true); 58 } 59 60 const sorted = this.listToSort.compact().sortBy(this.sortProperty); 61 if (this.sortDescending) { 62 return sorted.reverse(); 63 } 64 return sorted; 65 } 66 ), 67 }); 68 }