github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/ui/app/components/job-version.js (about)

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