func_editor.js 3.6 KB

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