graphiteSegment.js 4.2 KB

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