diff --git a/src/app/controllers/graphiteTarget.js b/src/app/controllers/graphiteTarget.js index 6c0f4aca09d..07ae8bec5dd 100644 --- a/src/app/controllers/graphiteTarget.js +++ b/src/app/controllers/graphiteTarget.js @@ -13,7 +13,6 @@ function (angular, _, config, gfunc, Parser) { module.controller('GraphiteTargetCtrl', function($scope, $http, filterSrv) { $scope.init = function() { - $scope.funcCategories = gfunc.getCategories(); parseTarget(); }; diff --git a/src/app/directives/addGraphiteFunc.js b/src/app/directives/addGraphiteFunc.js new file mode 100644 index 00000000000..6ea24a18cfe --- /dev/null +++ b/src/app/directives/addGraphiteFunc.js @@ -0,0 +1,101 @@ +define([ + 'angular', + 'app', + 'underscore', + 'jquery', + '../services/graphite/gfunc', +], +function (angular, app, _, $, gfunc) { + 'use strict'; + + + angular + .module('kibana.directives') + .directive('graphiteAddFunc', function($compile) { + var inputTemplate = ''; + + var buttonTemplate = ''; + + return { + link: function($scope, elem) { + var categories = gfunc.getCategories(); + var allFunctions = getAllFunctionNames(categories); + + $scope.functionMenu = createFunctionDropDownMenu(categories); + + var $input = $(inputTemplate); + var $button = $(buttonTemplate); + $input.appendTo(elem); + $button.appendTo(elem); + + $input.attr('data-provide', 'typeahead'); + $input.typeahead({ + source: allFunctions, + minLength: 1, + items: 10, + updater: function (value) { + var funcDef = gfunc.getFuncDef(value); + + $scope.$apply(function() { + $scope.addFunction(funcDef); + }); + + $input.trigger('blur'); + return ''; + } + }); + + $button.click(function() { + $button.hide(); + $input.show(); + $input.focus(); + }); + + $input.keyup(function() { + elem.toggleClass('open', $input.val() === ''); + }); + + $input.blur(function() { + $input.hide(); + $input.val(''); + $button.show(); + $button.focus(); + // clicking the function dropdown menu wont + // work if you remove class at once + setTimeout(function() { + elem.removeClass('open'); + }, 200); + }); + + $compile(elem.contents())($scope); + } + }; + }); + + function getAllFunctionNames(categories) { + return _.reduce(categories, function(list, category) { + _.each(category, function(func) { + list.push(func.name); + }); + return list; + }, []); + } + + function createFunctionDropDownMenu(categories) { + return _.map(categories, function(list, category) { + return { + text: category, + submenu: _.map(list, function(value) { + return { + text: value.name, + click: "addFunction('" + value.name + "')", + }; + }) + }; + }); + } +}); \ No newline at end of file diff --git a/src/app/directives/all.js b/src/app/directives/all.js index 14b918c570d..f9befcf40d1 100644 --- a/src/app/directives/all.js +++ b/src/app/directives/all.js @@ -12,5 +12,6 @@ define([ './spectrumPicker', './grafanaGraph', './bootstrap-tagsinput', - './bodyClass' + './bodyClass', + './addGraphiteFunc' ], function () {}); \ No newline at end of file diff --git a/src/app/directives/arrayJoin.js b/src/app/directives/arrayJoin.js index 2b614c40335..db85fe3719e 100644 --- a/src/app/directives/arrayJoin.js +++ b/src/app/directives/arrayJoin.js @@ -31,4 +31,4 @@ function (angular, app, _) { } }; }); -}); \ No newline at end of file +}); diff --git a/src/app/directives/bootstrap-tagsinput.js b/src/app/directives/bootstrap-tagsinput.js index 356ed60b195..5012fa03212 100644 --- a/src/app/directives/bootstrap-tagsinput.js +++ b/src/app/directives/bootstrap-tagsinput.js @@ -101,8 +101,8 @@ function (angular, $) { } var li = '