graphiteSegment.js 4.2 KB

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