add_graphite_func.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'jquery',
  5. './gfunc',
  6. ],
  7. function (angular, _, $, gfunc) {
  8. 'use strict';
  9. angular
  10. .module('grafana.directives')
  11. .directive('graphiteAddFunc', function($compile) {
  12. var inputTemplate = '<input type="text"'+
  13. ' class="gf-form-input"' +
  14. ' spellcheck="false" style="display:none"></input>';
  15. var buttonTemplate = '<a class="gf-form-label query-part dropdown-toggle"' +
  16. ' tabindex="1" gf-dropdown="functionMenu" data-toggle="dropdown">' +
  17. '<i class="fa fa-plus"></i></a>';
  18. return {
  19. link: function($scope, elem) {
  20. var ctrl = $scope.ctrl;
  21. var graphiteVersion = ctrl.datasource.graphiteVersion;
  22. var categories = gfunc.getCategories(graphiteVersion);
  23. var allFunctions = getAllFunctionNames(categories);
  24. $scope.functionMenu = createFunctionDropDownMenu(categories);
  25. var $input = $(inputTemplate);
  26. var $button = $(buttonTemplate);
  27. $input.appendTo(elem);
  28. $button.appendTo(elem);
  29. $input.attr('data-provide', 'typeahead');
  30. $input.typeahead({
  31. source: allFunctions,
  32. minLength: 1,
  33. items: 10,
  34. updater: function (value) {
  35. var funcDef = gfunc.getFuncDef(value);
  36. if (!funcDef) {
  37. // try find close match
  38. value = value.toLowerCase();
  39. funcDef = _.find(allFunctions, function(funcName) {
  40. return funcName.toLowerCase().indexOf(value) === 0;
  41. });
  42. if (!funcDef) { return; }
  43. }
  44. $scope.$apply(function() {
  45. ctrl.addFunction(funcDef);
  46. });
  47. $input.trigger('blur');
  48. return '';
  49. }
  50. });
  51. $button.click(function() {
  52. $button.hide();
  53. $input.show();
  54. $input.focus();
  55. });
  56. $input.keyup(function() {
  57. elem.toggleClass('open', $input.val() === '');
  58. });
  59. $input.blur(function() {
  60. // clicking the function dropdown menu wont
  61. // work if you remove class at once
  62. setTimeout(function() {
  63. $input.val('');
  64. $input.hide();
  65. $button.show();
  66. elem.removeClass('open');
  67. }, 200);
  68. });
  69. $compile(elem.contents())($scope);
  70. }
  71. };
  72. });
  73. function getAllFunctionNames(categories) {
  74. return _.reduce(categories, function(list, category) {
  75. _.each(category, function(func) {
  76. list.push(func.name);
  77. });
  78. return list;
  79. }, []);
  80. }
  81. function createFunctionDropDownMenu(categories) {
  82. return _.map(categories, function(list, category) {
  83. var submenu = _.map(list, function(value) {
  84. return {
  85. text: value.name,
  86. click: "ctrl.addFunction('" + value.name + "')",
  87. };
  88. });
  89. return {
  90. text: category,
  91. submenu: submenu
  92. };
  93. });
  94. }
  95. });