funcEditor.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'jquery',
  5. ],
  6. function (angular, _, $) {
  7. 'use strict';
  8. angular
  9. .module('grafana.directives')
  10. .directive('influxdbFuncEditor', function($compile) {
  11. var funcSpanTemplate = '<a gf-dropdown="functionMenu" class="dropdown-toggle" ' +
  12. 'data-toggle="dropdown">{{field.func}}</a><span>(</span>';
  13. var paramTemplate = '<input type="text" style="display:none"' +
  14. ' class="input-mini tight-form-func-param"></input>';
  15. return {
  16. restrict: 'A',
  17. scope: {
  18. field: "=",
  19. },
  20. link: function postLink($scope, elem) {
  21. var $funcLink = $(funcSpanTemplate);
  22. function clickFuncParam() {
  23. /*jshint validthis:true */
  24. var $link = $(this);
  25. var $input = $link.next();
  26. $input.val($scope.field.name);
  27. $input.css('width', ($link.width() + 16) + 'px');
  28. $link.hide();
  29. $input.show();
  30. $input.focus();
  31. $input.select();
  32. var typeahead = $input.data('typeahead');
  33. if (typeahead) {
  34. $input.val('');
  35. typeahead.lookup();
  36. }
  37. }
  38. function inputBlur() {
  39. /*jshint validthis:true */
  40. var $input = $(this);
  41. var $link = $input.prev();
  42. if ($input.val() !== '') {
  43. $link.text($input.val());
  44. $scope.field.name = $input.val();
  45. $scope.$apply($scope.get_data);
  46. }
  47. $input.hide();
  48. $link.show();
  49. }
  50. function inputKeyPress(e) {
  51. /*jshint validthis:true */
  52. if(e.which === 13) {
  53. inputBlur.call(this);
  54. }
  55. }
  56. function inputKeyDown() {
  57. /*jshint validthis:true */
  58. this.style.width = (3 + this.value.length) * 8 + 'px';
  59. }
  60. function addTypeahead($input) {
  61. $input.attr('data-provide', 'typeahead');
  62. $input.typeahead({
  63. source: function () {
  64. return $scope.getFields.apply(null, arguments);
  65. },
  66. minLength: 0,
  67. items: 20,
  68. updater: function (value) {
  69. setTimeout(function() {
  70. inputBlur.call($input[0]);
  71. }, 0);
  72. return value;
  73. }
  74. });
  75. var typeahead = $input.data('typeahead');
  76. typeahead.lookup = function () {
  77. var items;
  78. this.query = this.$element.val() || '';
  79. items = this.source(this.query, $.proxy(this.process, this));
  80. return items ? this.process(items) : items;
  81. };
  82. }
  83. function addElementsAndCompile() {
  84. $funcLink.appendTo(elem);
  85. var $paramLink = $('<a ng-click="" class="graphite-func-param-link">value</a>');
  86. var $input = $(paramTemplate);
  87. $paramLink.appendTo(elem);
  88. $input.appendTo(elem);
  89. $input.blur(inputBlur);
  90. $input.keyup(inputKeyDown);
  91. $input.keypress(inputKeyPress);
  92. $paramLink.click(clickFuncParam);
  93. addTypeahead($input);
  94. $('<span>)</span>').appendTo(elem);
  95. $compile(elem.contents())($scope);
  96. }
  97. addElementsAndCompile();
  98. }
  99. };
  100. });
  101. });