diff --git a/public/app/plugins/datasource/graphite/add_graphite_func.js b/public/app/plugins/datasource/graphite/add_graphite_func.js index c66176aeb8f..1d72c2c11eb 100644 --- a/public/app/plugins/datasource/graphite/add_graphite_func.js +++ b/public/app/plugins/datasource/graphite/add_graphite_func.js @@ -1,137 +1,130 @@ -define([ - 'angular', - 'lodash', - 'jquery', - 'rst2html', - 'tether-drop', -], -function (angular, _, $, rst2html, Drop) { +define(['angular', 'lodash', 'jquery', 'rst2html', 'tether-drop'], function(angular, _, $, rst2html, Drop) { 'use strict'; - angular - .module('grafana.directives') - .directive('graphiteAddFunc', function($compile) { - var inputTemplate = ''; + angular.module('grafana.directives').directive('graphiteAddFunc', function($compile) { + var inputTemplate = + ''; - var buttonTemplate = '' + - ''; + var buttonTemplate = + '' + + ''; - return { - link: function($scope, elem) { - var ctrl = $scope.ctrl; + return { + link: function($scope, elem) { + var ctrl = $scope.ctrl; - var $input = $(inputTemplate); - var $button = $(buttonTemplate); + var $input = $(inputTemplate); + var $button = $(buttonTemplate); - $input.appendTo(elem); - $button.appendTo(elem); + $input.appendTo(elem); + $button.appendTo(elem); - ctrl.datasource.getFuncDefs().then(function(funcDefs) { - var allFunctions = _.map(funcDefs, 'name').sort(); + ctrl.datasource.getFuncDefs().then(function(funcDefs) { + var allFunctions = _.map(funcDefs, 'name').sort(); - $scope.functionMenu = createFunctionDropDownMenu(funcDefs); + $scope.functionMenu = createFunctionDropDownMenu(funcDefs); - $input.attr('data-provide', 'typeahead'); - $input.typeahead({ - source: allFunctions, - minLength: 1, - items: 10, - updater: function (value) { - var funcDef = ctrl.datasource.getFuncDef(value); - if (!funcDef) { - // try find close match - value = value.toLowerCase(); - funcDef = _.find(allFunctions, function(funcName) { - return funcName.toLowerCase().indexOf(value) === 0; - }); - - if (!funcDef) { return; } - } - - $scope.$apply(function() { - ctrl.addFunction(funcDef); + $input.attr('data-provide', 'typeahead'); + $input.typeahead({ + source: allFunctions, + minLength: 1, + items: 10, + updater: function(value) { + var funcDef = ctrl.datasource.getFuncDef(value); + if (!funcDef) { + // try find close match + value = value.toLowerCase(); + funcDef = _.find(allFunctions, function(funcName) { + return funcName.toLowerCase().indexOf(value) === 0; }); - $input.trigger('blur'); - return ''; + if (!funcDef) { + return; + } } - }); - $button.click(function() { - $button.hide(); - $input.show(); - $input.focus(); - }); + $scope.$apply(function() { + ctrl.addFunction(funcDef); + }); - $input.keyup(function() { - elem.toggleClass('open', $input.val() === ''); - }); - - $input.blur(function() { - // clicking the function dropdown menu wont - // work if you remove class at once - setTimeout(function() { - $input.val(''); - $input.hide(); - $button.show(); - elem.removeClass('open'); - }, 200); - }); - - $compile(elem.contents())($scope); + $input.trigger('blur'); + return ''; + }, }); - var drops = []; + $button.click(function() { + $button.hide(); + $input.show(); + $input.focus(); + }); - $(elem) - .on('mouseenter', 'ul.dropdown-menu li', function () { - while (drops.length > 0) { - drops.pop().remove(); + $input.keyup(function() { + elem.toggleClass('open', $input.val() === ''); + }); + + $input.blur(function() { + // clicking the function dropdown menu wont + // work if you remove class at once + setTimeout(function() { + $input.val(''); + $input.hide(); + $button.show(); + elem.removeClass('open'); + }, 200); + }); + + $compile(elem.contents())($scope); + }); + + var drop; + var cleanUpDrop = function() { + if (drop) { + drop.destroy(); + drop = null; + } + }; + + $(elem) + .on('mouseenter', 'ul.dropdown-menu li', function() { + cleanUpDrop(); + + var funcDef; + try { + funcDef = ctrl.datasource.getFuncDef($('a', this).text()); + } catch (e) { + // ignore + } + + if (funcDef && funcDef.description) { + var shortDesc = funcDef.description; + if (shortDesc.length > 500) { + shortDesc = shortDesc.substring(0, 497) + '...'; } - var funcDef; - try { - funcDef = ctrl.datasource.getFuncDef($('a', this).text()); - } catch (e) { - // ignore - } + var contentElement = document.createElement('div'); + contentElement.innerHTML = '