thresholds.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. needsCleanup: boolean;
  11. constructor(private panelCtrl) {}
  12. getHandleHtml(handleIndex, model, valueStr) {
  13. var colorClass = 'crit';
  14. if (model.colorMode === 'warning') {
  15. colorClass = 'warn';
  16. }
  17. return `
  18. <div class="alert-handle-wrapper alert-handle-wrapper--T${handleIndex}">
  19. <div class="alert-handle-line alert-handle-line--${colorClass}">
  20. </div>
  21. <div class="alert-handle" data-handle-index="${handleIndex}">
  22. <i class="icon-gf icon-gf-${colorClass} alert-icon-${colorClass}"></i>
  23. <span class="alert-handle-value">${valueStr}<i class="alert-handle-grip"></i></span>
  24. </div>
  25. </div>`;
  26. }
  27. initDragging(evt) {
  28. var handleElem = $(evt.currentTarget).parents(".alert-handle-wrapper");
  29. var handleIndex = $(evt.currentTarget).data("handleIndex");
  30. var isMoving = false;
  31. var lastY = null;
  32. var posTop;
  33. var plot = this.plot;
  34. var panelCtrl = this.panelCtrl;
  35. var model = this.thresholds[handleIndex];
  36. function dragging(evt) {
  37. if (lastY === null) {
  38. lastY = evt.clientY;
  39. } else {
  40. var diff = evt.clientY - lastY;
  41. posTop = posTop + diff;
  42. lastY = evt.clientY;
  43. handleElem.css({top: posTop + diff});
  44. }
  45. }
  46. function stopped() {
  47. isMoving = false;
  48. // calculate graph level
  49. var graphValue = plot.c2p({left: 0, top: posTop}).y;
  50. graphValue = parseInt(graphValue.toFixed(0));
  51. model.value = graphValue;
  52. var valueCanvasPos = plot.p2c({x: 0, y: graphValue});
  53. handleElem.off("mousemove", dragging);
  54. handleElem.off("mouseup", dragging);
  55. handleElem.off("mouseleave", dragging);
  56. // trigger digest and render
  57. panelCtrl.$scope.$apply(function() {
  58. panelCtrl.render();
  59. panelCtrl.events.emit('threshold-changed', {threshold: model, index: handleIndex});
  60. });
  61. }
  62. isMoving = true;
  63. lastY = null;
  64. posTop = handleElem.position().top;
  65. handleElem.on("mousemove", dragging);
  66. handleElem.on("mouseup", stopped);
  67. handleElem.on("mouseleave", stopped);
  68. }
  69. cleanUp() {
  70. this.placeholder.find(".alert-handle-wrapper").remove();
  71. this.needsCleanup = false;
  72. }
  73. renderHandle(handleIndex, defaultHandleTopPos) {
  74. var model = this.thresholds[handleIndex];
  75. var value = model.value;
  76. var valueStr = value;
  77. var handleTopPos = 0;
  78. // handle no value
  79. if (!_.isNumber(value)) {
  80. valueStr = '';
  81. handleTopPos = defaultHandleTopPos;
  82. } else {
  83. var valueCanvasPos = this.plot.p2c({x: 0, y: value});
  84. handleTopPos = Math.min(Math.max(valueCanvasPos.top, 0), this.height) - 6;
  85. }
  86. var handleElem = $(this.getHandleHtml(handleIndex, model, valueStr));
  87. this.placeholder.append(handleElem);
  88. handleElem.toggleClass('alert-handle-wrapper--no-value', valueStr === '');
  89. handleElem.css({top: handleTopPos});
  90. }
  91. prepare(elem) {
  92. if (this.panelCtrl.editingThresholds) {
  93. var thresholdMargin = this.panelCtrl.panel.thresholds.length > 1 ? '220px' : '110px';
  94. elem.css('margin-right', thresholdMargin);
  95. } else if (this.needsCleanup) {
  96. elem.css('margin-right', '0');
  97. }
  98. }
  99. draw(plot) {
  100. this.thresholds = this.panelCtrl.panel.thresholds;
  101. this.plot = plot;
  102. this.placeholder = plot.getPlaceholder();
  103. if (this.needsCleanup) {
  104. this.cleanUp();
  105. }
  106. // if no thresholds or not editing alerts skip rendering handles
  107. if (this.thresholds.length === 0 || !this.panelCtrl.editingThresholds) {
  108. return;
  109. }
  110. this.height = plot.height();
  111. if (this.thresholds.length > 0) {
  112. this.renderHandle(0, 10);
  113. }
  114. if (this.thresholds.length > 1) {
  115. this.renderHandle(1, this.height-30);
  116. }
  117. this.placeholder.off('mousedown', '.alert-handle');
  118. this.placeholder.on('mousedown', '.alert-handle', this.initDragging.bind(this));
  119. this.needsCleanup = true;
  120. }
  121. }