metric_segment.ts 7.7 KB

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