metric.segment.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. define([
  2. 'angular',
  3. 'app',
  4. 'lodash',
  5. 'jquery',
  6. ],
  7. function (angular, app, _, $) {
  8. 'use strict';
  9. angular
  10. .module('grafana.directives')
  11. .directive('metricSegment', function($compile, $sce) {
  12. var inputTemplate = '<input type="text" data-provide="typeahead" ' +
  13. ' class="tight-form-clear-input input-medium"' +
  14. ' spellcheck="false" style="display:none"></input>';
  15. var buttonTemplate = '<a class="tight-form-item" ng-class="segment.cssClass" ' +
  16. 'tabindex="1" focus-me="segment.focus" ng-bind-html="segment.html"></a>';
  17. return {
  18. scope: {
  19. segment: "=",
  20. getAltSegments: "&",
  21. onValueChanged: "&"
  22. },
  23. link: function($scope, elem) {
  24. var $input = $(inputTemplate);
  25. var $button = $(buttonTemplate);
  26. var segment = $scope.segment;
  27. var options = null;
  28. var cancelBlur = null;
  29. $input.appendTo(elem);
  30. $button.appendTo(elem);
  31. $scope.updateVariableValue = function(value) {
  32. if (value === '' || segment.value === value) {
  33. return;
  34. }
  35. $scope.$apply(function() {
  36. var selected = _.findWhere($scope.altSegments, { value: value });
  37. if (selected) {
  38. segment.value = selected.value;
  39. segment.html = selected.html;
  40. segment.fake = false;
  41. segment.expandable = selected.expandable;
  42. }
  43. else {
  44. segment.value = value;
  45. segment.html = $sce.trustAsHtml(value);
  46. segment.expandable = true;
  47. segment.fake = false;
  48. }
  49. $scope.onValueChanged();
  50. });
  51. };
  52. $scope.switchToLink = function(now) {
  53. if (now === true || cancelBlur) {
  54. clearTimeout(cancelBlur);
  55. cancelBlur = null;
  56. $input.hide();
  57. $button.show();
  58. $scope.updateVariableValue($input.val());
  59. }
  60. else {
  61. // need to have long delay because the blur
  62. // happens long before the click event on the typeahead options
  63. cancelBlur = setTimeout($scope.switchToLink, 50);
  64. }
  65. };
  66. $scope.source = function(query, callback) {
  67. if (options) { return options; }
  68. $scope.$apply(function() {
  69. $scope.getAltSegments().then(function(altSegments) {
  70. $scope.altSegments = altSegments;
  71. options = _.map($scope.altSegments, function(alt) { return alt.value; });
  72. // add custom values
  73. if (!segment.fake && _.indexOf(options, segment.value) === -1) {
  74. options.unshift(segment.value);
  75. }
  76. callback(options);
  77. });
  78. });
  79. };
  80. $scope.updater = function(value) {
  81. if (value === segment.value) {
  82. clearTimeout(cancelBlur);
  83. $input.focus();
  84. return value;
  85. }
  86. $input.val(value);
  87. $scope.switchToLink(true);
  88. return value;
  89. };
  90. $input.attr('data-provide', 'typeahead');
  91. $input.typeahead({ source: $scope.source, minLength: 0, items: 10000, updater: $scope.updater });
  92. var typeahead = $input.data('typeahead');
  93. typeahead.lookup = function () {
  94. this.query = this.$element.val() || '';
  95. var items = this.source(this.query, $.proxy(this.process, this));
  96. return items ? this.process(items) : items;
  97. };
  98. $button.keydown(function(evt) {
  99. // trigger typeahead on down arrow or enter key
  100. if (evt.keyCode === 40 || evt.keyCode === 13) {
  101. $button.click();
  102. }
  103. });
  104. $button.click(function() {
  105. options = null;
  106. $input.css('width', ($button.width() + 16) + 'px');
  107. $button.hide();
  108. $input.show();
  109. $input.focus();
  110. var typeahead = $input.data('typeahead');
  111. if (typeahead) {
  112. $input.val('');
  113. typeahead.lookup();
  114. }
  115. });
  116. $input.blur($scope.switchToLink);
  117. $compile(elem.contents())($scope);
  118. }
  119. };
  120. });
  121. });