submenuCtrl.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. define([
  2. 'angular',
  3. 'app',
  4. 'lodash'
  5. ],
  6. function (angular, app, _) {
  7. 'use strict';
  8. var module = angular.module('grafana.controllers');
  9. module.controller('SubmenuCtrl', function($scope, $q, $rootScope, datasourceSrv) {
  10. var _d = {
  11. enable: true
  12. };
  13. _.defaults($scope.pulldown,_d);
  14. $scope.init = function() {
  15. $scope.panel = $scope.pulldown;
  16. $scope.row = $scope.pulldown;
  17. };
  18. $scope.filterOptionSelected = function(templateParameter, option, recursive) {
  19. templateParameter.current = option;
  20. $scope.filter.updateTemplateData();
  21. return $scope.applyFilterToOtherFilters(templateParameter)
  22. .then(function() {
  23. // only refresh in the outermost call
  24. if (!recursive) {
  25. $scope.dashboard.emit_refresh();
  26. }
  27. });
  28. };
  29. $scope.applyFilterToOtherFilters = function(updatedTemplatedParam) {
  30. var promises = _.map($scope.filter.templateParameters, function(templateParam) {
  31. if (templateParam === updatedTemplatedParam) {
  32. return;
  33. }
  34. if (templateParam.query.indexOf('[[' + updatedTemplatedParam.name + ']]') !== -1) {
  35. return $scope.applyFilter(templateParam);
  36. }
  37. });
  38. return $q.all(promises);
  39. };
  40. $scope.applyFilter = function(templateParam) {
  41. return datasourceSrv.default.metricFindQuery($scope.filter, templateParam.query)
  42. .then(function (results) {
  43. templateParam.editing = undefined;
  44. templateParam.options = _.map(results, function(node) {
  45. return { text: node.text, value: node.text };
  46. });
  47. if (templateParam.includeAll) {
  48. var allExpr = '{';
  49. _.each(templateParam.options, function(option) {
  50. allExpr += option.text + ',';
  51. });
  52. allExpr = allExpr.substring(0, allExpr.length - 1) + '}';
  53. templateParam.options.unshift({text: 'All', value: allExpr});
  54. }
  55. // if parameter has current value
  56. // if it exists in options array keep value
  57. if (templateParam.current) {
  58. var currentExists = _.findWhere(templateParam.options, { value: templateParam.current.value });
  59. if (currentExists) {
  60. return $scope.filterOptionSelected(templateParam, templateParam.current, true);
  61. }
  62. }
  63. return $scope.filterOptionSelected(templateParam, templateParam.options[0], true);
  64. });
  65. };
  66. $scope.disableAnnotation = function (annotation) {
  67. annotation.enable = !annotation.enable;
  68. $rootScope.$broadcast('refresh');
  69. };
  70. $scope.init();
  71. });
  72. });