metric_segment.js 7.3 KB

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