metric_segment.js 6.6 KB

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