func_editor.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import $ from 'jquery';
  4. import rst2html from 'rst2html';
  5. /** @ngInject */
  6. export function graphiteFuncEditor($compile, templateSrv, popoverSrv) {
  7. const funcSpanTemplate = '<a ng-click="">{{func.def.name}}</a><span>(</span>';
  8. const paramTemplate =
  9. '<input type="text" style="display:none"' + ' class="input-small tight-form-func-param"></input>';
  10. const funcControlsTemplate = `
  11. <div class="tight-form-func-controls">
  12. <span class="pointer fa fa-arrow-left"></span>
  13. <span class="pointer fa fa-question-circle"></span>
  14. <span class="pointer fa fa-remove" ></span>
  15. <span class="pointer fa fa-arrow-right"></span>
  16. </div>`;
  17. return {
  18. restrict: 'A',
  19. link: function postLink($scope, elem) {
  20. const $funcLink = $(funcSpanTemplate);
  21. const $funcControls = $(funcControlsTemplate);
  22. const ctrl = $scope.ctrl;
  23. const func = $scope.func;
  24. let scheduledRelink = false;
  25. let paramCountAtLink = 0;
  26. let cancelBlur = null;
  27. function clickFuncParam(this: any, paramIndex) {
  28. /*jshint validthis:true */
  29. const $link = $(this);
  30. const $comma = $link.prev('.comma');
  31. const $input = $link.next();
  32. $input.val(func.params[paramIndex]);
  33. $comma.removeClass('query-part__last');
  34. $link.hide();
  35. $input.show();
  36. $input.focus();
  37. $input.select();
  38. const typeahead = $input.data('typeahead');
  39. if (typeahead) {
  40. $input.val('');
  41. typeahead.lookup();
  42. }
  43. }
  44. function scheduledRelinkIfNeeded() {
  45. if (paramCountAtLink === func.params.length) {
  46. return;
  47. }
  48. if (!scheduledRelink) {
  49. scheduledRelink = true;
  50. setTimeout(function() {
  51. relink();
  52. scheduledRelink = false;
  53. }, 200);
  54. }
  55. }
  56. function paramDef(index) {
  57. if (index < func.def.params.length) {
  58. return func.def.params[index];
  59. }
  60. if (_.last(func.def.params).multiple) {
  61. return _.assign({}, _.last(func.def.params), { optional: true });
  62. }
  63. return {};
  64. }
  65. function switchToLink(inputElem, paramIndex) {
  66. /*jshint validthis:true */
  67. const $input = $(inputElem);
  68. clearTimeout(cancelBlur);
  69. cancelBlur = null;
  70. const $link = $input.prev();
  71. const $comma = $link.prev('.comma');
  72. const newValue = $input.val();
  73. // remove optional empty params
  74. if (newValue !== '' || paramDef(paramIndex).optional) {
  75. func.updateParam(newValue, paramIndex);
  76. $link.html(newValue ? templateSrv.highlightVariablesAsHtml(newValue) : '&nbsp;');
  77. }
  78. scheduledRelinkIfNeeded();
  79. $scope.$apply(function() {
  80. ctrl.targetChanged();
  81. });
  82. if ($link.hasClass('query-part__last') && newValue === '') {
  83. $comma.addClass('query-part__last');
  84. } else {
  85. $link.removeClass('query-part__last');
  86. }
  87. $input.hide();
  88. $link.show();
  89. }
  90. // this = input element
  91. function inputBlur(this: any, paramIndex) {
  92. /*jshint validthis:true */
  93. const inputElem = this;
  94. // happens long before the click event on the typeahead options
  95. // need to have long delay because the blur
  96. cancelBlur = setTimeout(function() {
  97. switchToLink(inputElem, paramIndex);
  98. }, 200);
  99. }
  100. function inputKeyPress(this: any, paramIndex, e) {
  101. /*jshint validthis:true */
  102. if (e.which === 13) {
  103. $(this).blur();
  104. }
  105. }
  106. function inputKeyDown(this: any) {
  107. /*jshint validthis:true */
  108. this.style.width = (3 + this.value.length) * 8 + 'px';
  109. }
  110. function addTypeahead($input, paramIndex) {
  111. $input.attr('data-provide', 'typeahead');
  112. let options = paramDef(paramIndex).options;
  113. if (paramDef(paramIndex).type === 'int') {
  114. options = _.map(options, function(val) {
  115. return val.toString();
  116. });
  117. }
  118. $input.typeahead({
  119. source: options,
  120. minLength: 0,
  121. items: 20,
  122. updater: function(value) {
  123. $input.val(value);
  124. switchToLink($input[0], paramIndex);
  125. return value;
  126. },
  127. });
  128. const typeahead = $input.data('typeahead');
  129. typeahead.lookup = function() {
  130. this.query = this.$element.val() || '';
  131. return this.process(this.source);
  132. };
  133. }
  134. function toggleFuncControls() {
  135. const targetDiv = elem.closest('.tight-form');
  136. if (elem.hasClass('show-function-controls')) {
  137. elem.removeClass('show-function-controls');
  138. targetDiv.removeClass('has-open-function');
  139. $funcControls.hide();
  140. return;
  141. }
  142. elem.addClass('show-function-controls');
  143. targetDiv.addClass('has-open-function');
  144. $funcControls.show();
  145. }
  146. function addElementsAndCompile() {
  147. $funcControls.appendTo(elem);
  148. $funcLink.appendTo(elem);
  149. const defParams = _.clone(func.def.params);
  150. const lastParam = _.last(func.def.params);
  151. while (func.params.length >= defParams.length && lastParam && lastParam.multiple) {
  152. defParams.push(_.assign({}, lastParam, { optional: true }));
  153. }
  154. _.each(defParams, function(param, index) {
  155. if (param.optional && func.params.length < index) {
  156. return false;
  157. }
  158. let paramValue = templateSrv.highlightVariablesAsHtml(func.params[index]);
  159. const last = index >= func.params.length - 1 && param.optional && !paramValue;
  160. if (last && param.multiple) {
  161. paramValue = '+';
  162. }
  163. if (index > 0) {
  164. $('<span class="comma' + (last ? ' query-part__last' : '') + '">, </span>').appendTo(elem);
  165. }
  166. const $paramLink = $(
  167. '<a ng-click="" class="graphite-func-param-link' +
  168. (last ? ' query-part__last' : '') +
  169. '">' +
  170. (paramValue || '&nbsp;') +
  171. '</a>'
  172. );
  173. const $input = $(paramTemplate);
  174. $input.attr('placeholder', param.name);
  175. paramCountAtLink++;
  176. $paramLink.appendTo(elem);
  177. $input.appendTo(elem);
  178. $input.blur(_.partial(inputBlur, index));
  179. $input.keyup(inputKeyDown);
  180. $input.keypress(_.partial(inputKeyPress, index));
  181. $paramLink.click(_.partial(clickFuncParam, index));
  182. if (param.options) {
  183. addTypeahead($input, index);
  184. }
  185. return true;
  186. });
  187. $('<span>)</span>').appendTo(elem);
  188. $compile(elem.contents())($scope);
  189. }
  190. function ifJustAddedFocusFirstParam() {
  191. if ($scope.func.added) {
  192. $scope.func.added = false;
  193. setTimeout(function() {
  194. elem
  195. .find('.graphite-func-param-link')
  196. .first()
  197. .click();
  198. }, 10);
  199. }
  200. }
  201. function registerFuncControlsToggle() {
  202. $funcLink.click(toggleFuncControls);
  203. }
  204. function registerFuncControlsActions() {
  205. $funcControls.click(function(e) {
  206. const $target = $(e.target);
  207. if ($target.hasClass('fa-remove')) {
  208. toggleFuncControls();
  209. $scope.$apply(function() {
  210. ctrl.removeFunction($scope.func);
  211. });
  212. return;
  213. }
  214. if ($target.hasClass('fa-arrow-left')) {
  215. $scope.$apply(function() {
  216. _.move(ctrl.queryModel.functions, $scope.$index, $scope.$index - 1);
  217. ctrl.targetChanged();
  218. });
  219. return;
  220. }
  221. if ($target.hasClass('fa-arrow-right')) {
  222. $scope.$apply(function() {
  223. _.move(ctrl.queryModel.functions, $scope.$index, $scope.$index + 1);
  224. ctrl.targetChanged();
  225. });
  226. return;
  227. }
  228. if ($target.hasClass('fa-question-circle')) {
  229. const funcDef = ctrl.datasource.getFuncDef(func.def.name);
  230. if (funcDef && funcDef.description) {
  231. popoverSrv.show({
  232. element: e.target,
  233. position: 'bottom left',
  234. classNames: 'drop-popover drop-function-def',
  235. template: `
  236. <div style="overflow:auto;max-height:30rem;">
  237. <h4> ${funcDef.name} </h4>
  238. ${rst2html(funcDef.description)}
  239. </div>`,
  240. openOn: 'click',
  241. });
  242. } else {
  243. window.open(
  244. 'http://graphite.readthedocs.org/en/latest/functions.html#graphite.render.functions.' + func.def.name,
  245. '_blank'
  246. );
  247. }
  248. return;
  249. }
  250. });
  251. }
  252. function relink() {
  253. elem.children().remove();
  254. addElementsAndCompile();
  255. ifJustAddedFocusFirstParam();
  256. registerFuncControlsToggle();
  257. registerFuncControlsActions();
  258. }
  259. relink();
  260. },
  261. };
  262. }
  263. angular.module('grafana.directives').directive('graphiteFuncEditor', graphiteFuncEditor);