metric.segment.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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" give-focus="segment.focus" ng-bind-html="segment.html"></a>';
  17. return {
  18. scope: {
  19. segment: "=",
  20. getOptions: "&",
  21. onChange: "&",
  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 if (segment.custom !== 'false') {
  44. segment.value = value;
  45. segment.html = $sce.trustAsHtml(value);
  46. segment.expandable = true;
  47. segment.fake = false;
  48. }
  49. $scope.onChange();
  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, 100);
  64. }
  65. };
  66. $scope.source = function(query, callback) {
  67. if (options) { return options; }
  68. $scope.$apply(function() {
  69. $scope.getOptions().then(function(altSegments) {
  70. $scope.altSegments = altSegments;
  71. options = _.map($scope.altSegments, function(alt) { return alt.value; });
  72. // add custom values
  73. if (segment.custom !== 'false') {
  74. if (!segment.fake && _.indexOf(options, segment.value) === -1) {
  75. options.unshift(segment.value);
  76. }
  77. }
  78. callback(options);
  79. });
  80. });
  81. };
  82. $scope.updater = function(value) {
  83. if (value === segment.value) {
  84. clearTimeout(cancelBlur);
  85. $input.focus();
  86. return value;
  87. }
  88. $input.val(value);
  89. $scope.switchToLink(true);
  90. return value;
  91. };
  92. $scope.matcher = function(item) {
  93. var str = this.query;
  94. if (str[0] === '/') { str = str.substring(1); }
  95. if (str[str.length - 1] === '/') { str = str.substring(0, str.length-1); }
  96. try {
  97. return item.toLowerCase().match(str);
  98. } catch(e) {
  99. return false;
  100. }
  101. };
  102. $input.attr('data-provide', 'typeahead');
  103. $input.typeahead({ source: $scope.source, minLength: 0, items: 10000, updater: $scope.updater, matcher: $scope.matcher });
  104. var typeahead = $input.data('typeahead');
  105. typeahead.lookup = function () {
  106. this.query = this.$element.val() || '';
  107. var items = this.source(this.query, $.proxy(this.process, this));
  108. return items ? this.process(items) : items;
  109. };
  110. $button.keydown(function(evt) {
  111. // trigger typeahead on down arrow or enter key
  112. if (evt.keyCode === 40 || evt.keyCode === 13) {
  113. $button.click();
  114. }
  115. });
  116. $button.click(function() {
  117. options = null;
  118. $input.css('width', ($button.width() + 16) + 'px');
  119. $button.hide();
  120. $input.show();
  121. $input.focus();
  122. var typeahead = $input.data('typeahead');
  123. if (typeahead) {
  124. $input.val('');
  125. typeahead.lookup();
  126. }
  127. });
  128. $input.blur($scope.switchToLink);
  129. $compile(elem.contents())($scope);
  130. }
  131. };
  132. });
  133. angular
  134. .module('grafana.directives')
  135. .directive('metricSegmentModel', function(uiSegmentSrv, $q) {
  136. return {
  137. template: '<metric-segment segment="segment" get-options="getOptionsInternal()" on-change="onSegmentChange()"></metric-segment>',
  138. restrict: 'E',
  139. scope: {
  140. property: "=",
  141. options: "=",
  142. getOptions: "&",
  143. onChange: "&",
  144. },
  145. link: {
  146. pre: function postLink($scope, elem, attrs) {
  147. $scope.valueToSegment = function(value) {
  148. var option = _.findWhere($scope.options, {value: value});
  149. var segment = {
  150. cssClass: attrs.cssClass,
  151. custom: attrs.custom,
  152. value: option ? option.text : value,
  153. };
  154. return uiSegmentSrv.newSegment(segment);
  155. };
  156. $scope.getOptionsInternal = function() {
  157. if ($scope.options) {
  158. var optionSegments = _.map($scope.options, function(option) {
  159. return uiSegmentSrv.newSegment({value: option.text});
  160. });
  161. return $q.when(optionSegments);
  162. } else {
  163. return $scope.getOptions();
  164. }
  165. };
  166. $scope.onSegmentChange = function() {
  167. if ($scope.options) {
  168. var option = _.findWhere($scope.options, {text: $scope.segment.value});
  169. if (option && option.value !== $scope.property) {
  170. $scope.property = option.value;
  171. }
  172. } else {
  173. $scope.property = $scope.segment.value;
  174. }
  175. // needs to call this after digest so
  176. // property is synced with outerscope
  177. $scope.$$postDigest(function() {
  178. $scope.onChange();
  179. });
  180. };
  181. $scope.segment = $scope.valueToSegment($scope.property);
  182. }
  183. }
  184. };
  185. });
  186. });