metric_segment.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. define([
  2. 'lodash',
  3. 'jquery',
  4. '../core_module',
  5. ],
  6. function (_, $, coreModule) {
  7. 'use strict';
  8. coreModule.default.directive('metricSegment', function($compile, $sce) {
  9. var inputTemplate = '<input type="text" data-provide="typeahead" ' +
  10. ' class="gf-form-input input-medium"' +
  11. ' spellcheck="false" style="display:none"></input>';
  12. var linkTemplate = '<a class="gf-form-label" ng-class="segment.cssClass" ' +
  13. 'tabindex="1" give-focus="segment.focus" ng-bind-html="segment.html"></a>';
  14. var selectTemplate = '<a class="gf-form-input gf-form-input--dropdown" ng-class="segment.cssClass" ' +
  15. 'tabindex="1" give-focus="segment.focus" ng-bind-html="segment.html"></a>';
  16. return {
  17. scope: {
  18. segment: "=",
  19. getOptions: "&",
  20. onChange: "&",
  21. debounce: "@",
  22. },
  23. link: function($scope, elem) {
  24. var $input = $(inputTemplate);
  25. var segment = $scope.segment;
  26. var $button = $(segment.selectMode ? selectTemplate : linkTemplate);
  27. var options = null;
  28. var cancelBlur = null;
  29. var linkMode = true;
  30. var debounceLookup = $scope.debounce;
  31. $input.appendTo(elem);
  32. $button.appendTo(elem);
  33. $scope.updateVariableValue = function(value) {
  34. if (value === '' || segment.value === value) {
  35. return;
  36. }
  37. value = _.unescape(value);
  38. $scope.$apply(function() {
  39. var selected = _.find($scope.altSegments, {value: value});
  40. if (selected) {
  41. segment.value = selected.value;
  42. segment.html = selected.html || selected.value;
  43. segment.fake = false;
  44. segment.expandable = selected.expandable;
  45. if (selected.type) {
  46. segment.type = selected.type;
  47. }
  48. }
  49. else if (segment.custom !== 'false') {
  50. segment.value = value;
  51. segment.html = $sce.trustAsHtml(value);
  52. segment.expandable = true;
  53. segment.fake = false;
  54. }
  55. $scope.onChange();
  56. });
  57. };
  58. $scope.switchToLink = function(fromClick) {
  59. if (linkMode && !fromClick) { return; }
  60. clearTimeout(cancelBlur);
  61. cancelBlur = null;
  62. linkMode = true;
  63. $input.hide();
  64. $button.show();
  65. $scope.updateVariableValue($input.val());
  66. };
  67. $scope.inputBlur = function() {
  68. // happens long before the click event on the typeahead options
  69. // need to have long delay because the blur
  70. cancelBlur = setTimeout($scope.switchToLink, 200);
  71. };
  72. $scope.source = function(query, callback) {
  73. $scope.$apply(function() {
  74. $scope.getOptions({ $query: query }).then(function(altSegments) {
  75. $scope.altSegments = altSegments;
  76. options = _.map($scope.altSegments, function(alt) {
  77. return _.escape(alt.value);
  78. });
  79. // add custom values
  80. if (segment.custom !== 'false') {
  81. if (!segment.fake && _.indexOf(options, segment.value) === -1) {
  82. options.unshift(segment.value);
  83. }
  84. }
  85. callback(options);
  86. });
  87. });
  88. };
  89. $scope.updater = function(value) {
  90. if (value === segment.value) {
  91. clearTimeout(cancelBlur);
  92. $input.focus();
  93. return value;
  94. }
  95. $input.val(value);
  96. $scope.switchToLink(true);
  97. return value;
  98. };
  99. $scope.matcher = function(item) {
  100. var str = this.query;
  101. if (str[0] === '/') { str = str.substring(1); }
  102. if (str[str.length - 1] === '/') { str = str.substring(0, str.length-1); }
  103. try {
  104. return item.toLowerCase().match(str.toLowerCase());
  105. } catch(e) {
  106. return false;
  107. }
  108. };
  109. $input.attr('data-provide', 'typeahead');
  110. $input.typeahead({ source: $scope.source, minLength: 0, items: 10000, updater: $scope.updater, matcher: $scope.matcher });
  111. var typeahead = $input.data('typeahead');
  112. typeahead.lookup = function () {
  113. this.query = this.$element.val() || '';
  114. var items = this.source(this.query, $.proxy(this.process, this));
  115. return items ? this.process(items) : items;
  116. };
  117. if (debounceLookup) {
  118. typeahead.lookup = _.debounce(typeahead.lookup, 500, {leading: true});
  119. }
  120. $button.keydown(function(evt) {
  121. // trigger typeahead on down arrow or enter key
  122. if (evt.keyCode === 40 || evt.keyCode === 13) {
  123. $button.click();
  124. }
  125. });
  126. $button.click(function() {
  127. options = null;
  128. $input.css('width', (Math.max($button.width(), 80) + 16) + 'px');
  129. $button.hide();
  130. $input.show();
  131. $input.focus();
  132. linkMode = false;
  133. var typeahead = $input.data('typeahead');
  134. if (typeahead) {
  135. $input.val('');
  136. typeahead.lookup();
  137. }
  138. });
  139. $input.blur($scope.inputBlur);
  140. $compile(elem.contents())($scope);
  141. }
  142. };
  143. });
  144. coreModule.default.directive('metricSegmentModel', function(uiSegmentSrv, $q) {
  145. return {
  146. template: '<metric-segment segment="segment" get-options="getOptionsInternal()" on-change="onSegmentChange()"></metric-segment>',
  147. restrict: 'E',
  148. scope: {
  149. property: "=",
  150. options: "=",
  151. getOptions: "&",
  152. onChange: "&",
  153. },
  154. link: {
  155. pre: function postLink($scope, elem, attrs) {
  156. var cachedOptions;
  157. $scope.valueToSegment = function(value) {
  158. var option = _.find($scope.options, {value: value});
  159. var segment = {
  160. cssClass: attrs.cssClass,
  161. custom: attrs.custom,
  162. value: option ? option.text : value,
  163. selectMode: attrs.selectMode,
  164. };
  165. return uiSegmentSrv.newSegment(segment);
  166. };
  167. $scope.getOptionsInternal = function() {
  168. if ($scope.options) {
  169. cachedOptions = $scope.options;
  170. return $q.when(_.map($scope.options, function(option) {
  171. return {value: option.text};
  172. }));
  173. } else {
  174. return $scope.getOptions().then(function(options) {
  175. cachedOptions = options;
  176. return _.map(options, function(option) {
  177. if (option.html) {
  178. return option;
  179. }
  180. return {value: option.text};
  181. });
  182. });
  183. }
  184. };
  185. $scope.onSegmentChange = function() {
  186. if (cachedOptions) {
  187. var option = _.find(cachedOptions, {text: $scope.segment.value});
  188. if (option && option.value !== $scope.property) {
  189. $scope.property = option.value;
  190. } else if (attrs.custom !== 'false') {
  191. $scope.property = $scope.segment.value;
  192. }
  193. } else {
  194. $scope.property = $scope.segment.value;
  195. }
  196. // needs to call this after digest so
  197. // property is synced with outerscope
  198. $scope.$$postDigest(function() {
  199. $scope.$apply(function() {
  200. $scope.onChange();
  201. });
  202. });
  203. };
  204. $scope.segment = $scope.valueToSegment($scope.property);
  205. }
  206. }
  207. };
  208. });
  209. });