github.com/outbrain/consul@v1.4.5/ui-v2/app/controllers/dc/services/index.js (about)

     1  import Controller from '@ember/controller';
     2  import { get, computed } from '@ember/object';
     3  import { htmlSafe } from '@ember/string';
     4  import WithHealthFiltering from 'consul-ui/mixins/with-health-filtering';
     5  const max = function(arr, prop) {
     6    return arr.reduce(function(prev, item) {
     7      return Math.max(prev, get(item, prop));
     8    }, 0);
     9  };
    10  const chunk = function(str, size) {
    11    const num = Math.ceil(str.length / size);
    12    const chunks = new Array(num);
    13    for (let i = 0, o = 0; i < num; ++i, o += size) {
    14      chunks[i] = str.substr(o, size);
    15    }
    16    return chunks;
    17  };
    18  const width = function(num) {
    19    const str = num.toString();
    20    const len = str.length;
    21    const commas = chunk(str, 3).length - 1;
    22    return commas * 4 + len * 10;
    23  };
    24  const widthDeclaration = function(num) {
    25    return htmlSafe(`width: ${num}px`);
    26  };
    27  export default Controller.extend(WithHealthFiltering, {
    28    filter: function(item, { s = '', status = '' }) {
    29      const term = s.toLowerCase();
    30      return (
    31        (get(item, 'Name')
    32          .toLowerCase()
    33          .indexOf(term) !== -1 ||
    34          (get(item, 'Tags') || []).some(function(item) {
    35            return item.toLowerCase().indexOf(term) !== -1;
    36          })) &&
    37        item.hasStatus(status)
    38      );
    39    },
    40    maxWidth: computed('{maxPassing,maxWarning,maxCritical}', function() {
    41      const PADDING = 32 * 3 + 13;
    42      return ['maxPassing', 'maxWarning', 'maxCritical'].reduce((prev, item) => {
    43        return prev + width(get(this, item));
    44      }, PADDING);
    45    }),
    46    totalWidth: computed('maxWidth', function() {
    47      return widthDeclaration(get(this, 'maxWidth'));
    48    }),
    49    remainingWidth: computed('maxWidth', function() {
    50      return htmlSafe(`width: calc(50% - ${Math.round(get(this, 'maxWidth') / 2)}px)`);
    51    }),
    52    maxPassing: computed('items', function() {
    53      return max(get(this, 'items'), 'ChecksPassing');
    54    }),
    55    maxWarning: computed('items', function() {
    56      return max(get(this, 'items'), 'ChecksWarning');
    57    }),
    58    maxCritical: computed('items', function() {
    59      return max(get(this, 'items'), 'ChecksCritical');
    60    }),
    61    passingWidth: computed('maxPassing', function() {
    62      return widthDeclaration(width(get(this, 'maxPassing')));
    63    }),
    64    warningWidth: computed('maxWarning', function() {
    65      return widthDeclaration(width(get(this, 'maxWarning')));
    66    }),
    67    criticalWidth: computed('maxCritical', function() {
    68      return widthDeclaration(width(get(this, 'maxCritical')));
    69    }),
    70  });