metric_segment.ts 7.4 KB

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