metric.segment.js 6.8 KB

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