metric_segment.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. let str = this.query;
  102. if (str[0] === '/') {
  103. str = str.substring(1);
  104. }
  105. if (str[str.length - 1] === '/') {
  106. str = str.substring(0, str.length - 1);
  107. }
  108. try {
  109. return item.toLowerCase().match(str.toLowerCase());
  110. } catch (e) {
  111. return false;
  112. }
  113. };
  114. $input.attr('data-provide', 'typeahead');
  115. $input.typeahead({
  116. source: $scope.source,
  117. minLength: 0,
  118. items: 10000,
  119. updater: $scope.updater,
  120. matcher: $scope.matcher,
  121. });
  122. const typeahead = $input.data('typeahead');
  123. typeahead.lookup = function() {
  124. this.query = this.$element.val() || '';
  125. const items = this.source(this.query, $.proxy(this.process, this));
  126. return items ? this.process(items) : items;
  127. };
  128. if (debounceLookup) {
  129. typeahead.lookup = _.debounce(typeahead.lookup, 500, { leading: true });
  130. }
  131. $button.keydown(evt => {
  132. // trigger typeahead on down arrow or enter key
  133. if (evt.keyCode === 40 || evt.keyCode === 13) {
  134. $button.click();
  135. }
  136. });
  137. $button.click(() => {
  138. options = null;
  139. $input.css('width', Math.max($button.width(), 80) + 16 + 'px');
  140. $button.hide();
  141. $input.show();
  142. $input.focus();
  143. linkMode = false;
  144. const typeahead = $input.data('typeahead');
  145. if (typeahead) {
  146. $input.val('');
  147. typeahead.lookup();
  148. }
  149. });
  150. $input.blur($scope.inputBlur);
  151. $compile(elem.contents())($scope);
  152. },
  153. };
  154. }
  155. /** @ngInject */
  156. export function metricSegmentModel(uiSegmentSrv, $q) {
  157. return {
  158. template:
  159. '<metric-segment segment="segment" get-options="getOptionsInternal()" on-change="onSegmentChange()"></metric-segment>',
  160. restrict: 'E',
  161. scope: {
  162. property: '=',
  163. options: '=',
  164. getOptions: '&',
  165. onChange: '&',
  166. },
  167. link: {
  168. pre: function postLink($scope, elem, attrs) {
  169. let cachedOptions;
  170. $scope.valueToSegment = value => {
  171. const option = _.find($scope.options, { value: value });
  172. const segment = {
  173. cssClass: attrs.cssClass,
  174. custom: attrs.custom,
  175. value: option ? option.text : value,
  176. selectMode: attrs.selectMode,
  177. };
  178. return uiSegmentSrv.newSegment(segment);
  179. };
  180. $scope.getOptionsInternal = () => {
  181. if ($scope.options) {
  182. cachedOptions = $scope.options;
  183. return $q.when(
  184. _.map($scope.options, option => {
  185. return { value: option.text };
  186. })
  187. );
  188. } else {
  189. return $scope.getOptions().then(options => {
  190. cachedOptions = options;
  191. return _.map(options, option => {
  192. if (option.html) {
  193. return option;
  194. }
  195. return { value: option.text };
  196. });
  197. });
  198. }
  199. };
  200. $scope.onSegmentChange = () => {
  201. if (cachedOptions) {
  202. const option = _.find(cachedOptions, { text: $scope.segment.value });
  203. if (option && option.value !== $scope.property) {
  204. $scope.property = option.value;
  205. } else if (attrs.custom !== 'false') {
  206. $scope.property = $scope.segment.value;
  207. }
  208. } else {
  209. $scope.property = $scope.segment.value;
  210. }
  211. // needs to call this after digest so
  212. // property is synced with outerscope
  213. $scope.$$postDigest(() => {
  214. $scope.$apply(() => {
  215. $scope.onChange();
  216. });
  217. });
  218. };
  219. $scope.segment = $scope.valueToSegment($scope.property);
  220. },
  221. },
  222. };
  223. }
  224. coreModule.directive('metricSegment', metricSegment);
  225. coreModule.directive('metricSegmentModel', metricSegmentModel);