github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/ui/javascripts/app/helpers.js (about)

     1  Ember.Handlebars.helper('panelBar', function(status) {
     2    var highlightClass;
     3  
     4    if (status == "passing") {
     5      highlightClass = "bg-green";
     6    } else {
     7      highlightClass = "bg-orange";
     8    }
     9    return new Handlebars.SafeString('<div class="panel-bar ' + highlightClass + '"></div>');
    10  });
    11  
    12  Ember.Handlebars.helper('listBar', function(status) {
    13    var highlightClass;
    14  
    15    if (status == "passing") {
    16      highlightClass = "bg-green";
    17    } else {
    18      highlightClass = "bg-orange";
    19    }
    20    return new Handlebars.SafeString('<div class="list-bar-horizontal ' + highlightClass + '"></div>');
    21  });
    22  
    23  Ember.Handlebars.helper('sessionName', function(session) {
    24    var name;
    25  
    26    if (session.Name === "") {
    27      name = '<span>' + Handlebars.Utils.escapeExpression(session.ID) + '</span>';
    28    } else {
    29      name = '<span>' + Handlebars.Utils.escapeExpression(session.Name) + '</span>' + ' <small>' + Handlebars.Utils.escapeExpression(session.ID) + '</small>';
    30    }
    31  
    32    return new Handlebars.SafeString(name);
    33  });
    34  
    35  Ember.Handlebars.helper('sessionMeta', function(session) {
    36    var meta = '<div class="metadata">' + Handlebars.Utils.escapeExpression(session.Behavior) + ' behavior</div>';
    37  
    38    if (session.TTL !== "") {
    39      meta = meta + '<div class="metadata">, ' + Handlebars.Utils.escapeExpression(session.TTL) + ' TTL</div>';
    40    }
    41  
    42    return new Handlebars.SafeString(meta);
    43  });
    44  
    45  Ember.Handlebars.helper('aclName', function(name, id) {
    46    if (name === "") {
    47      return id;
    48    } else {
    49      return new Handlebars.SafeString(Handlebars.Utils.escapeExpression(name) + ' <small class="pull-right no-case">' + Handlebars.Utils.escapeExpression(id) + '</small>');
    50    }
    51  });
    52  
    53  Ember.Handlebars.helper('formatRules', function(rules) {
    54    if (rules === "") {
    55      return "No rules defined";
    56    } else {
    57      return rules;
    58    }
    59  });
    60  
    61  Ember.Handlebars.helper('limit', function(str, limit) {
    62    if (str.length > limit)
    63      return str.substring(0, limit) + '...';
    64    return str;
    65  });
    66  
    67  // We need to do this because of our global namespace properties. The
    68  // service.Tags
    69  Ember.Handlebars.helper('serviceTagMessage', function(tags) {
    70    if (tags === null) {
    71      return "No tags";
    72    }
    73  });
    74  
    75  
    76  // Sends a new notification to the UI
    77  function notify(message, ttl) {
    78    if (window.notifications !== undefined && window.notifications.length > 0) {
    79      $(window.notifications).each(function(i, v) {
    80        v.dismiss();
    81      });
    82    }
    83    var notification = new NotificationFx({
    84      message : '<p>'+ message + '</p>',
    85      layout : 'growl',
    86      effect : 'slide',
    87      type : 'notice',
    88      ttl: ttl,
    89    });
    90  
    91    // show the notification
    92    notification.show();
    93  
    94    // Add the notification to the queue to be closed
    95    window.notifications = [];
    96    window.notifications.push(notification);
    97  }
    98  
    99  // Tomography
   100  
   101  // TODO: not sure how to how do to this more Ember.js-y
   102  function tomographyMouseOver(el) {
   103    var buf = el.getAttribute('data-node') + ' - ' + el.getAttribute('data-distance') + 'ms';
   104    var segment = el.getAttribute('data-segment');
   105    if (segment !== "") {
   106      buf += ' (Segment: ' + segment + ')';
   107    }
   108    document.getElementById('tomography-node-info').textContent = buf;
   109  
   110  }
   111  
   112  Ember.Handlebars.helper('tomographyGraph', function(tomography, size) {
   113  
   114    // This is ugly, but I'm working around bugs with Handlebars and templating
   115    // parts of svgs. Basically things render correctly the first time, but when
   116    // stuff is updated for subsequent go arounds the templated parts don't show.
   117    // It appears (based on google searches) that the replaced elements aren't
   118    // being interpreted as http://www.w3.org/2000/svg. Anyway, this works and
   119    // if/when Handlebars fixes the underlying issues all of this can be cleaned
   120    // up drastically.
   121  
   122    var max = -999999999;
   123    tomography.distances.forEach(function (d, i) {
   124      if (d.distance > max) {
   125        max = d.distance;
   126      }
   127    });
   128    var insetSize = size / 2 - 8;
   129    var buf = '' +
   130  '      <svg width="' + size + '" height="' + size + '">' +
   131  '        <g class="tomography" transform="translate(' + (size / 2) + ', ' + (size / 2) + ')">' +
   132  '          <g>' +
   133  '            <circle class="background" r="' + insetSize + '"/>' +
   134  '            <circle class="axis" r="' + (insetSize * 0.25) + '"/>' +
   135  '            <circle class="axis" r="' + (insetSize * 0.5) + '"/>' +
   136  '            <circle class="axis" r="' + (insetSize * 0.75) + '"/>' +
   137  '            <circle class="border" r="' + insetSize + '"/>' +
   138  '          </g>' +
   139  '          <g class="lines">';
   140    var distances = tomography.distances;
   141    var n = distances.length;
   142    if (tomography.n > 360) {
   143      // We have more nodes than we want to show, take a random sampling to keep
   144      // the number around 360.
   145      var sampling = 360 / tomography.n;
   146      distances = distances.filter(function (_, i) {
   147        return i == 0 || i == n - 1 || Math.random() < sampling
   148      });
   149      // Re-set n to the filtered size
   150      n = distances.length;
   151    }
   152    distances.forEach(function (d, i) {
   153      buf += '            <line transform="rotate(' + (i * 360 / n) + ')" y2="' + (-insetSize * (d.distance / max)) + '" ' +
   154        'data-node="' + Handlebars.Utils.escapeExpression(d.node) + '" data-distance="' + d.distance + '" data-segment="' + Handlebars.Utils.escapeExpression(d.segment) + '" onmouseover="tomographyMouseOver(this);"/>';
   155    });
   156    buf += '' +
   157  '          </g>' +
   158  '          <g class="labels">' +
   159  '            <circle class="point" r="5"/>' +
   160  '            <g class="tick" transform="translate(0, ' + (insetSize * -0.25 ) + ')">' +
   161  '              <line x2="70"/>' +
   162  '              <text x="75" y="0" dy=".32em">' + (max > 0 ? (parseInt(max * 25) / 100) : 0) + 'ms</text>' +
   163  '            </g>' +
   164  '            <g class="tick" transform="translate(0, ' + (insetSize * -0.5 ) + ')">' +
   165  '              <line x2="70"/>' +
   166  '              <text x="75" y="0" dy=".32em">' + (max > 0 ? (parseInt(max * 50) / 100) : 0)+ 'ms</text>' +
   167  '            </g>' +
   168  '            <g class="tick" transform="translate(0, ' + (insetSize * -0.75 ) + ')">' +
   169  '              <line x2="70"/>' +
   170  '              <text x="75" y="0" dy=".32em">' + (max > 0 ? (parseInt(max * 75) / 100) : 0) + 'ms</text>' +
   171  '            </g>' +
   172  '            <g class="tick" transform="translate(0, ' + (insetSize * -1) + ')">' +
   173  '              <line x2="70"/>' +
   174  '              <text x="75" y="0" dy=".32em">' + (max > 0 ? (parseInt(max * 100) / 100) : 0) + 'ms</text>' +
   175  '            </g>' +
   176  '          </g>' +
   177  '        </g>' +
   178  '      </svg>';
   179  
   180    return new Handlebars.SafeString(buf);
   181  });