thresholds.ts 3.2 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(type, 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. <i class="icon-gf icon-gf-${type} alert-icon-${type}"></i>
  21. ${op} ${value}
  22. </div>`;
  23. }
  24. getFullHandleHtml(type, op, value) {
  25. var innerTemplate = this.getHandleInnerHtml(type, op, value);
  26. return `
  27. <div class="alert-handle-wrapper alert-handle-wrapper--${type}">
  28. ${innerTemplate}
  29. </div>
  30. `;
  31. }
  32. setupDragging(handleElem, threshold) {
  33. var isMoving = false;
  34. var lastY = null;
  35. var posTop;
  36. var plot = this.plot;
  37. var panelCtrl = this.panelCtrl;
  38. function dragging(evt) {
  39. if (lastY === null) {
  40. lastY = evt.clientY;
  41. } else {
  42. var diff = evt.clientY - lastY;
  43. posTop = posTop + diff;
  44. lastY = evt.clientY;
  45. handleElem.css({top: posTop + diff});
  46. }
  47. }
  48. function stopped() {
  49. isMoving = false;
  50. // calculate graph level
  51. var graphValue = plot.c2p({left: 0, top: posTop}).y;
  52. graphValue = parseInt(graphValue.toFixed(0));
  53. threshold.value = graphValue;
  54. var valueCanvasPos = plot.p2c({x: 0, y: graphValue});
  55. handleElem.off("mousemove", dragging);
  56. handleElem.off("mouseup", dragging);
  57. // trigger digest and render
  58. panelCtrl.$scope.$apply(function() {
  59. panelCtrl.render();
  60. });
  61. }
  62. handleElem.bind('mousedown', function() {
  63. isMoving = true;
  64. lastY = null;
  65. posTop = handleElem.position().top;
  66. handleElem.on("mousemove", dragging);
  67. handleElem.on("mouseup", stopped);
  68. });
  69. }
  70. cleanUp() {
  71. if (this.placeholder) {
  72. this.placeholder.find(".alert-handle-wrapper").remove();
  73. }
  74. }
  75. renderHandle(type, model, defaultHandleTopPos) {
  76. var handleElem = this.placeholder.find(`.alert-handle-wrapper--${type}`);
  77. var value = model.value;
  78. var valueStr = value;
  79. var handleTopPos = 0;
  80. // handle no value
  81. if (!_.isNumber(value)) {
  82. valueStr = '';
  83. handleTopPos = defaultHandleTopPos;
  84. } else {
  85. var valueCanvasPos = this.plot.p2c({x: 0, y: value});
  86. handleTopPos = Math.min(Math.max(valueCanvasPos.top, 0), this.height) - 6;
  87. }
  88. if (handleElem.length === 0) {
  89. handleElem = $(this.getFullHandleHtml(type, model.op, valueStr));
  90. this.placeholder.append(handleElem);
  91. this.setupDragging(handleElem, model);
  92. } else {
  93. handleElem.html(this.getHandleInnerHtml(type, model.op, valueStr));
  94. }
  95. handleElem.toggleClass('alert-handle-wrapper--no-value', valueStr === '');
  96. handleElem.css({top: handleTopPos});
  97. }
  98. draw(plot) {
  99. this.plot = plot;
  100. this.placeholder = plot.getPlaceholder();
  101. this.height = plot.height();
  102. if (this.thresholds.length > 0) {
  103. this.renderHandle('crit', this.thresholds[0], 10);
  104. }
  105. }
  106. }