github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/components/job-version.js (about)

     1  import Component from '@ember/component';
     2  import { action, computed } from '@ember/object';
     3  import { classNames } from '@ember-decorators/component';
     4  import classic from 'ember-classic-decorator';
     5  
     6  const changeTypes = ['Added', 'Deleted', 'Edited'];
     7  
     8  @classic
     9  @classNames('job-version', 'boxed-section')
    10  export default class JobVersion extends Component {
    11    version = null;
    12    isOpen = false;
    13  
    14    // Passes through to the job-diff component
    15    verbose = true;
    16  
    17    @computed('version.diff')
    18    get changeCount() {
    19      const diff = this.get('version.diff');
    20      const taskGroups = diff.TaskGroups || [];
    21  
    22      if (!diff) {
    23        return 0;
    24      }
    25  
    26      return (
    27        fieldChanges(diff) +
    28        taskGroups.reduce(arrayOfFieldChanges, 0) +
    29        (taskGroups.mapBy('Tasks') || []).reduce(flatten, []).reduce(arrayOfFieldChanges, 0)
    30      );
    31    }
    32  
    33    @action
    34    toggleDiff() {
    35      this.toggleProperty('isOpen');
    36    }
    37  }
    38  
    39  const flatten = (accumulator, array) => accumulator.concat(array);
    40  const countChanges = (total, field) => (changeTypes.includes(field.Type) ? total + 1 : total);
    41  
    42  function fieldChanges(diff) {
    43    return (
    44      (diff.Fields || []).reduce(countChanges, 0) +
    45      (diff.Objects || []).reduce(arrayOfFieldChanges, 0)
    46    );
    47  }
    48  
    49  function arrayOfFieldChanges(count, diff) {
    50    if (!diff) {
    51      return count;
    52    }
    53  
    54    return count + fieldChanges(diff);
    55  }