metric_segment.js 6.9 KB

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