func_editor.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import _ from 'lodash';
  2. import $ from 'jquery';
  3. import coreModule from 'app/core/core_module';
  4. /** @ngInject */
  5. export function graphiteFuncEditor($compile, templateSrv, popoverSrv) {
  6. const funcSpanTemplate = `
  7. <function-editor
  8. func="func"
  9. onRemove="ctrl.handleRemoveFunction"
  10. onMoveLeft="ctrl.handleMoveLeft"
  11. onMoveRight="ctrl.handleMoveRight"
  12. /><span>(</span>
  13. `;
  14. const paramTemplate =
  15. '<input type="text" style="display:none"' + ' class="input-small tight-form-func-param"></input>';
  16. return {
  17. restrict: 'A',
  18. link: function postLink($scope, elem) {
  19. const $funcLink = $(funcSpanTemplate);
  20. const ctrl = $scope.ctrl;
  21. const func = $scope.func;
  22. let scheduledRelink = false;
  23. let paramCountAtLink = 0;
  24. let cancelBlur = null;
  25. ctrl.handleRemoveFunction = func => {
  26. ctrl.removeFunction(func);
  27. };
  28. ctrl.handleMoveLeft = func => {
  29. ctrl.moveFunction(func, -1);
  30. };
  31. ctrl.handleMoveRight = func => {
  32. ctrl.moveFunction(func, 1);
  33. };
  34. function clickFuncParam(this: any, paramIndex) {
  35. /*jshint validthis:true */
  36. const $link = $(this);
  37. const $comma = $link.prev('.comma');
  38. const $input = $link.next();
  39. $input.val(func.params[paramIndex]);
  40. $comma.removeClass('query-part__last');
  41. $link.hide();
  42. $input.show();
  43. $input.focus();
  44. $input.select();
  45. const typeahead = $input.data('typeahead');
  46. if (typeahead) {
  47. $input.val('');
  48. typeahead.lookup();
  49. }
  50. }
  51. function scheduledRelinkIfNeeded() {
  52. if (paramCountAtLink === func.params.length) {
  53. return;
  54. }
  55. if (!scheduledRelink) {
  56. scheduledRelink = true;
  57. setTimeout(() => {
  58. relink();
  59. scheduledRelink = false;
  60. }, 200);
  61. }
  62. }
  63. function paramDef(index) {
  64. if (index < func.def.params.length) {
  65. return func.def.params[index];
  66. }
  67. if (_.last(func.def.params).multiple) {
  68. return _.assign({}, _.last(func.def.params), { optional: true });
  69. }
  70. return {};
  71. }
  72. function switchToLink(inputElem, paramIndex) {
  73. /*jshint validthis:true */
  74. const $input = $(inputElem);
  75. clearTimeout(cancelBlur);
  76. cancelBlur = null;
  77. const $link = $input.prev();
  78. const $comma = $link.prev('.comma');
  79. const newValue = $input.val();
  80. // remove optional empty params
  81. if (newValue !== '' || paramDef(paramIndex).optional) {
  82. func.updateParam(newValue, paramIndex);
  83. $link.html(newValue ? templateSrv.highlightVariablesAsHtml(newValue) : '&nbsp;');
  84. }
  85. scheduledRelinkIfNeeded();
  86. $scope.$apply(() => {
  87. ctrl.targetChanged();
  88. });
  89. if ($link.hasClass('query-part__last') && newValue === '') {
  90. $comma.addClass('query-part__last');
  91. } else {
  92. $link.removeClass('query-part__last');
  93. }
  94. $input.hide();
  95. $link.show();
  96. }
  97. // this = input element
  98. function inputBlur(this: any, paramIndex) {
  99. /*jshint validthis:true */
  100. const inputElem = this;
  101. // happens long before the click event on the typeahead options
  102. // need to have long delay because the blur
  103. cancelBlur = setTimeout(() => {
  104. switchToLink(inputElem, paramIndex);
  105. }, 200);
  106. }
  107. function inputKeyPress(this: any, paramIndex, e) {
  108. /*jshint validthis:true */
  109. if (e.which === 13) {
  110. $(this).blur();
  111. }
  112. }
  113. function inputKeyDown(this: any) {
  114. /*jshint validthis:true */
  115. this.style.width = (3 + this.value.length) * 8 + 'px';
  116. }
  117. function addTypeahead($input, paramIndex) {
  118. $input.attr('data-provide', 'typeahead');
  119. let options = paramDef(paramIndex).options;
  120. if (paramDef(paramIndex).type === 'int') {
  121. options = _.map(options, val => {
  122. return val.toString();
  123. });
  124. }
  125. $input.typeahead({
  126. source: options,
  127. minLength: 0,
  128. items: 20,
  129. updater: value => {
  130. $input.val(value);
  131. switchToLink($input[0], paramIndex);
  132. return value;
  133. },
  134. });
  135. const typeahead = $input.data('typeahead');
  136. typeahead.lookup = function() {
  137. this.query = this.$element.val() || '';
  138. return this.process(this.source);
  139. };
  140. }
  141. function addElementsAndCompile() {
  142. $funcLink.appendTo(elem);
  143. const defParams = _.clone(func.def.params);
  144. const lastParam = _.last(func.def.params);
  145. while (func.params.length >= defParams.length && lastParam && lastParam.multiple) {
  146. defParams.push(_.assign({}, lastParam, { optional: true }));
  147. }
  148. _.each(defParams, (param, index) => {
  149. if (param.optional && func.params.length < index) {
  150. return false;
  151. }
  152. let paramValue = templateSrv.highlightVariablesAsHtml(func.params[index]);
  153. const hasValue = paramValue !== null && paramValue !== undefined;
  154. const last = index >= func.params.length - 1 && param.optional && !hasValue;
  155. if (last && param.multiple) {
  156. paramValue = '+';
  157. }
  158. if (index > 0) {
  159. $('<span class="comma' + (last ? ' query-part__last' : '') + '">, </span>').appendTo(elem);
  160. }
  161. const $paramLink = $(
  162. '<a ng-click="" class="graphite-func-param-link' +
  163. (last ? ' query-part__last' : '') +
  164. '">' +
  165. (hasValue ? paramValue : '&nbsp;') +
  166. '</a>'
  167. );
  168. const $input = $(paramTemplate);
  169. $input.attr('placeholder', param.name);
  170. paramCountAtLink++;
  171. $paramLink.appendTo(elem);
  172. $input.appendTo(elem);
  173. $input.blur(_.partial(inputBlur, index));
  174. $input.keyup(inputKeyDown);
  175. $input.keypress(_.partial(inputKeyPress, index));
  176. $paramLink.click(_.partial(clickFuncParam, index));
  177. if (param.options) {
  178. addTypeahead($input, index);
  179. }
  180. return true;
  181. });
  182. $('<span>)</span>').appendTo(elem);
  183. $compile(elem.contents())($scope);
  184. }
  185. function ifJustAddedFocusFirstParam() {
  186. if ($scope.func.added) {
  187. $scope.func.added = false;
  188. setTimeout(() => {
  189. elem
  190. .find('.graphite-func-param-link')
  191. .first()
  192. .click();
  193. }, 10);
  194. }
  195. }
  196. function relink() {
  197. elem.children().remove();
  198. addElementsAndCompile();
  199. ifJustAddedFocusFirstParam();
  200. }
  201. relink();
  202. },
  203. };
  204. }
  205. coreModule.directive('graphiteFuncEditor', graphiteFuncEditor);