add_graphite_func.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import _ from 'lodash';
  2. import $ from 'jquery';
  3. // @ts-ignore
  4. import rst2html from 'rst2html';
  5. // @ts-ignore
  6. import Drop from 'tether-drop';
  7. import coreModule from 'app/core/core_module';
  8. import { FuncDef } from './gfunc';
  9. /** @ngInject */
  10. export function graphiteAddFunc($compile: any) {
  11. const inputTemplate =
  12. '<input type="text"' + ' class="gf-form-input"' + ' spellcheck="false" style="display:none"></input>';
  13. const buttonTemplate =
  14. '<a class="gf-form-label query-part dropdown-toggle"' +
  15. ' tabindex="1" gf-dropdown="functionMenu" data-toggle="dropdown">' +
  16. '<i class="fa fa-plus"></i></a>';
  17. return {
  18. link: function($scope: any, elem: JQuery) {
  19. const ctrl = $scope.ctrl;
  20. const $input = $(inputTemplate);
  21. const $button = $(buttonTemplate);
  22. $input.appendTo(elem);
  23. $button.appendTo(elem);
  24. ctrl.datasource.getFuncDefs().then((funcDefs: FuncDef[]) => {
  25. const allFunctions = _.map(funcDefs, 'name').sort();
  26. $scope.functionMenu = createFunctionDropDownMenu(funcDefs);
  27. $input.attr('data-provide', 'typeahead');
  28. $input.typeahead({
  29. source: allFunctions,
  30. minLength: 1,
  31. items: 10,
  32. updater: (value: any) => {
  33. let funcDef: any = ctrl.datasource.getFuncDef(value);
  34. if (!funcDef) {
  35. // try find close match
  36. value = value.toLowerCase();
  37. funcDef = _.find(allFunctions, funcName => {
  38. return funcName.toLowerCase().indexOf(value) === 0;
  39. });
  40. if (!funcDef) {
  41. return '';
  42. }
  43. }
  44. $scope.$apply(() => {
  45. ctrl.addFunction(funcDef);
  46. });
  47. $input.trigger('blur');
  48. return '';
  49. },
  50. });
  51. $button.click(() => {
  52. $button.hide();
  53. $input.show();
  54. $input.focus();
  55. });
  56. $input.keyup(() => {
  57. elem.toggleClass('open', $input.val() === '');
  58. });
  59. $input.blur(() => {
  60. // clicking the function dropdown menu won't
  61. // work if you remove class at once
  62. setTimeout(() => {
  63. $input.val('');
  64. $input.hide();
  65. $button.show();
  66. elem.removeClass('open');
  67. }, 200);
  68. });
  69. $compile(elem.contents())($scope);
  70. });
  71. let drop: any;
  72. const cleanUpDrop = () => {
  73. if (drop) {
  74. drop.destroy();
  75. drop = null;
  76. }
  77. };
  78. $(elem)
  79. .on('mouseenter', 'ul.dropdown-menu li', () => {
  80. cleanUpDrop();
  81. let funcDef;
  82. try {
  83. funcDef = ctrl.datasource.getFuncDef($('a', this).text());
  84. } catch (e) {
  85. // ignore
  86. }
  87. if (funcDef && funcDef.description) {
  88. let shortDesc = funcDef.description;
  89. if (shortDesc.length > 500) {
  90. shortDesc = shortDesc.substring(0, 497) + '...';
  91. }
  92. const contentElement = document.createElement('div');
  93. contentElement.innerHTML = '<h4>' + funcDef.name + '</h4>' + rst2html(shortDesc);
  94. drop = new Drop({
  95. target: this,
  96. content: contentElement,
  97. classes: 'drop-popover',
  98. openOn: 'always',
  99. tetherOptions: {
  100. attachment: 'bottom left',
  101. targetAttachment: 'bottom right',
  102. },
  103. });
  104. }
  105. })
  106. .on('mouseout', 'ul.dropdown-menu li', () => {
  107. cleanUpDrop();
  108. });
  109. $scope.$on('$destroy', cleanUpDrop);
  110. },
  111. };
  112. }
  113. coreModule.directive('graphiteAddFunc', graphiteAddFunc);
  114. function createFunctionDropDownMenu(funcDefs: FuncDef[]) {
  115. const categories: any = {};
  116. _.forEach(funcDefs, funcDef => {
  117. if (!funcDef.category) {
  118. return;
  119. }
  120. if (!categories[funcDef.category]) {
  121. categories[funcDef.category] = [];
  122. }
  123. categories[funcDef.category].push({
  124. text: funcDef.name,
  125. click: "ctrl.addFunction('" + funcDef.name + "')",
  126. });
  127. });
  128. return _.sortBy(
  129. _.map(categories, (submenu, category) => {
  130. return {
  131. text: category,
  132. submenu: _.sortBy(submenu, 'text'),
  133. };
  134. }),
  135. 'text'
  136. );
  137. }