Commit e9d33750 by Torkel Ödegaard Committed by Dan Cech

fix: graphite func editor fixes, this component is messy and ugly as hell

parent 1a6bf692
define([ define(['angular', 'lodash', 'jquery', 'rst2html', 'tether-drop'], function(angular, _, $, rst2html, Drop) {
'angular',
'lodash',
'jquery',
'rst2html',
'tether-drop',
],
function (angular, _, $, rst2html, Drop) {
'use strict'; 'use strict';
angular angular.module('grafana.directives').directive('graphiteAddFunc', function($compile) {
.module('grafana.directives') var inputTemplate =
.directive('graphiteAddFunc', function($compile) { '<input type="text"' + ' class="gf-form-input"' + ' spellcheck="false" style="display:none"></input>';
var inputTemplate = '<input type="text"'+
' class="gf-form-input"' +
' spellcheck="false" style="display:none"></input>';
var buttonTemplate = '<a class="gf-form-label query-part dropdown-toggle"' + var buttonTemplate =
'<a class="gf-form-label query-part dropdown-toggle"' +
' tabindex="1" gf-dropdown="functionMenu" data-toggle="dropdown">' + ' tabindex="1" gf-dropdown="functionMenu" data-toggle="dropdown">' +
'<i class="fa fa-plus"></i></a>'; '<i class="fa fa-plus"></i></a>';
...@@ -39,7 +30,7 @@ function (angular, _, $, rst2html, Drop) { ...@@ -39,7 +30,7 @@ function (angular, _, $, rst2html, Drop) {
source: allFunctions, source: allFunctions,
minLength: 1, minLength: 1,
items: 10, items: 10,
updater: function (value) { updater: function(value) {
var funcDef = ctrl.datasource.getFuncDef(value); var funcDef = ctrl.datasource.getFuncDef(value);
if (!funcDef) { if (!funcDef) {
// try find close match // try find close match
...@@ -48,7 +39,9 @@ function (angular, _, $, rst2html, Drop) { ...@@ -48,7 +39,9 @@ function (angular, _, $, rst2html, Drop) {
return funcName.toLowerCase().indexOf(value) === 0; return funcName.toLowerCase().indexOf(value) === 0;
}); });
if (!funcDef) { return; } if (!funcDef) {
return;
}
} }
$scope.$apply(function() { $scope.$apply(function() {
...@@ -57,7 +50,7 @@ function (angular, _, $, rst2html, Drop) { ...@@ -57,7 +50,7 @@ function (angular, _, $, rst2html, Drop) {
$input.trigger('blur'); $input.trigger('blur');
return ''; return '';
} },
}); });
$button.click(function() { $button.click(function() {
...@@ -84,13 +77,17 @@ function (angular, _, $, rst2html, Drop) { ...@@ -84,13 +77,17 @@ function (angular, _, $, rst2html, Drop) {
$compile(elem.contents())($scope); $compile(elem.contents())($scope);
}); });
var drops = []; var drop;
var cleanUpDrop = function() {
if (drop) {
drop.destroy();
drop = null;
}
};
$(elem) $(elem)
.on('mouseenter', 'ul.dropdown-menu li', function () { .on('mouseenter', 'ul.dropdown-menu li', function() {
while (drops.length > 0) { cleanUpDrop();
drops.pop().remove();
}
var funcDef; var funcDef;
try { try {
...@@ -108,7 +105,7 @@ function (angular, _, $, rst2html, Drop) { ...@@ -108,7 +105,7 @@ function (angular, _, $, rst2html, Drop) {
var contentElement = document.createElement('div'); var contentElement = document.createElement('div');
contentElement.innerHTML = '<h4>' + funcDef.name + '</h4>' + rst2html(shortDesc); contentElement.innerHTML = '<h4>' + funcDef.name + '</h4>' + rst2html(shortDesc);
var drop = new Drop({ drop = new Drop({
target: this, target: this,
content: contentElement, content: contentElement,
classes: 'drop-popover', classes: 'drop-popover',
...@@ -118,18 +115,14 @@ function (angular, _, $, rst2html, Drop) { ...@@ -118,18 +115,14 @@ function (angular, _, $, rst2html, Drop) {
targetAttachment: 'bottom right', targetAttachment: 'bottom right',
}, },
}); });
drops.push(drop);
drop.open();
} }
}) })
.on('mouseout', 'ul.dropdown-menu li', function() { .on('mouseout', 'ul.dropdown-menu li', function() {
while (drops.length > 0) { cleanUpDrop();
drops.pop().remove();
}
}); });
}
$scope.$on('$destroy', cleanUpDrop);
},
}; };
}); });
...@@ -149,11 +142,14 @@ function (angular, _, $, rst2html, Drop) { ...@@ -149,11 +142,14 @@ function (angular, _, $, rst2html, Drop) {
}); });
}); });
return _.sortBy(_.map(categories, function(submenu, category) { return _.sortBy(
_.map(categories, function(submenu, category) {
return { return {
text: category, text: category,
submenu: _.sortBy(submenu, 'text') submenu: _.sortBy(submenu, 'text'),
}; };
}), 'text'); }),
'text'
);
} }
}); });
...@@ -32,6 +32,7 @@ function (angular, _, $, rst2html) { ...@@ -32,6 +32,7 @@ function (angular, _, $, rst2html) {
var func = $scope.func; var func = $scope.func;
var scheduledRelink = false; var scheduledRelink = false;
var paramCountAtLink = 0; var paramCountAtLink = 0;
var cancelBlur = null;
function clickFuncParam(paramIndex) { function clickFuncParam(paramIndex) {
/*jshint validthis:true */ /*jshint validthis:true */
...@@ -79,21 +80,23 @@ function (angular, _, $, rst2html) { ...@@ -79,21 +80,23 @@ function (angular, _, $, rst2html) {
return {}; return {};
} }
function inputBlur(paramIndex) { function switchToLink(inputElem, paramIndex) {
/*jshint validthis:true */ /*jshint validthis:true */
var $input = $(this); var $input = $(inputElem);
if ($input.data('typeahead') && $input.data('typeahead').shown) {
return; clearTimeout(cancelBlur);
} cancelBlur = null;
var $link = $input.prev(); var $link = $input.prev();
var $comma = $link.prev('.comma'); var $comma = $link.prev('.comma');
var newValue = $input.val(); var newValue = $input.val();
// remove optional empty params
if (newValue !== '' || paramDef(paramIndex).optional) { if (newValue !== '' || paramDef(paramIndex).optional) {
$link.html(templateSrv.highlightVariablesAsHtml(newValue));
func.updateParam(newValue, paramIndex); func.updateParam(newValue, paramIndex);
}
$link.html(templateSrv.highlightVariablesAsHtml(newValue));
scheduledRelinkIfNeeded(); scheduledRelinkIfNeeded();
$scope.$apply(function() { $scope.$apply(function() {
...@@ -105,15 +108,26 @@ function (angular, _, $, rst2html) { ...@@ -105,15 +108,26 @@ function (angular, _, $, rst2html) {
} else { } else {
$link.removeClass('last'); $link.removeClass('last');
} }
$input.hide(); $input.hide();
$link.show(); $link.show();
} }
// this = input element
function inputBlur(paramIndex) {
/*jshint validthis:true */
var inputElem = this;
// happens long before the click event on the typeahead options
// need to have long delay because the blur
cancelBlur = setTimeout(function() {
switchToLink(inputElem, paramIndex);
}, 200);
} }
function inputKeyPress(paramIndex, e) { function inputKeyPress(paramIndex, e) {
/*jshint validthis:true */ /*jshint validthis:true */
if(e.which === 13) { if(e.which === 13) {
inputBlur.call(this, paramIndex); $(this).blur();
} }
} }
...@@ -135,9 +149,8 @@ function (angular, _, $, rst2html) { ...@@ -135,9 +149,8 @@ function (angular, _, $, rst2html) {
minLength: 0, minLength: 0,
items: 20, items: 20,
updater: function (value) { updater: function (value) {
setTimeout(function() { $input.val(value);
inputBlur.call($input[0], paramIndex); switchToLink($input[0], paramIndex);
}, 0);
return value; return value;
} }
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment