thresholds.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import 'jquery.flot';
  3. import $ from 'jquery';
  4. import _ from 'lodash';
  5. export class ThresholdControls {
  6. plot: any;
  7. placeholder: any;
  8. height: any;
  9. thresholds: any;
  10. constructor(private panelCtrl) {
  11. this.thresholds = this.panelCtrl.panel.thresholds;
  12. }
  13. getHandleInnerHtml(handleName, op, value) {
  14. if (op === '>') { op = '&gt;'; }
  15. if (op === '<') { op = '&lt;'; }
  16. return `
  17. <div class="alert-handle-line">
  18. </div>
  19. <div class="alert-handle">
  20. ${op} ${value}
  21. </div>`;
  22. }
  23. getFullHandleHtml(handleName, op, value) {
  24. var innerTemplate = this.getHandleInnerHtml(handleName, op, value);
  25. return `<div class="alert-handle-wrapper alert-handle-wrapper--${handleName}">${innerTemplate}</div>`;
  26. }
  27. setupDragging(handleElem, threshold, handleIndex) {
  28. var isMoving = false;
  29. var lastY = null;
  30. var posTop;
  31. var plot = this.plot;
  32. var panelCtrl = this.panelCtrl;
  33. function dragging(evt) {
  34. if (lastY === null) {
  35. lastY = evt.clientY;
  36. } else {
  37. var diff = evt.clientY - lastY;
  38. posTop = posTop + diff;
  39. lastY = evt.clientY;
  40. handleElem.css({top: posTop + diff});
  41. }
  42. }
  43. function stopped() {
  44. isMoving = false;
  45. // calculate graph level
  46. var graphValue = plot.c2p({left: 0, top: posTop}).y;
  47. graphValue = parseInt(graphValue.toFixed(0));
  48. threshold.value = graphValue;
  49. var valueCanvasPos = plot.p2c({x: 0, y: graphValue});
  50. handleElem.off("mousemove", dragging);
  51. handleElem.off("mouseup", dragging);
  52. // trigger digest and render
  53. panelCtrl.$scope.$apply(function() {
  54. panelCtrl.render();
  55. panelCtrl.events.emit('threshold-changed', {threshold: threshold, index: handleIndex});
  56. });
  57. }
  58. handleElem.bind('mousedown', function() {
  59. isMoving = true;
  60. lastY = null;
  61. posTop = handleElem.position().top;
  62. handleElem.on("mousemove", dragging);
  63. handleElem.on("mouseup", stopped);
  64. });
  65. }
  66. cleanUp() {
  67. if (this.placeholder) {
  68. this.placeholder.find(".alert-handle-wrapper").remove();
  69. }
  70. }
  71. renderHandle(handleIndex, model, defaultHandleTopPos) {
  72. var handleName = 'T' + (handleIndex+1);
  73. var handleElem = this.placeholder.find(`.alert-handle-wrapper--${handleName}`);
  74. var value = model.value;
  75. var valueStr = value;
  76. var handleTopPos = 0;
  77. // handle no value
  78. if (!_.isNumber(value)) {
  79. valueStr = '';
  80. handleTopPos = defaultHandleTopPos;
  81. } else {
  82. var valueCanvasPos = this.plot.p2c({x: 0, y: value});
  83. handleTopPos = Math.min(Math.max(valueCanvasPos.top, 0), this.height) - 6;
  84. }
  85. if (handleElem.length === 0) {
  86. handleElem = $(this.getFullHandleHtml(handleName, model.op, valueStr));
  87. this.placeholder.append(handleElem);
  88. this.setupDragging(handleElem, model, handleIndex);
  89. } else {
  90. handleElem.html(this.getHandleInnerHtml(handleName, model.op, valueStr));
  91. }
  92. handleElem.toggleClass('alert-handle-wrapper--no-value', valueStr === '');
  93. handleElem.css({top: handleTopPos});
  94. }
  95. draw(plot) {
  96. this.plot = plot;
  97. this.placeholder = plot.getPlaceholder();
  98. this.height = plot.height();
  99. if (this.thresholds.length > 0) {
  100. this.renderHandle(0, this.thresholds[0], 10);
  101. }
  102. if (this.thresholds.length > 1) {
  103. this.renderHandle(1, this.thresholds[1], this.height-30);
  104. }
  105. }
  106. }