add_graphite_func.js 3.1 KB

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