metric_segment.js 6.4 KB

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