metric_segment.js 7.4 KB

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