variable_srv.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import $ from 'jquery';
  5. import kbn from 'app/core/utils/kbn';
  6. import coreModule from 'app/core/core_module';
  7. import appEvents from 'app/core/app_events';
  8. import {IntervalVariable} from './interval_variable';
  9. import {Variable} from './variable';
  10. export var variableConstructorMap: any = {};
  11. export class VariableSrv {
  12. dashboard: any;
  13. variables: any;
  14. variableLock: any;
  15. /** @ngInject */
  16. constructor(
  17. private $rootScope,
  18. private $q,
  19. private $location,
  20. private $injector,
  21. private templateSrv) {
  22. }
  23. init(dashboard) {
  24. this.variableLock = {};
  25. this.dashboard = dashboard;
  26. this.variables = [];
  27. dashboard.templating.list.map(this.addVariable.bind(this));
  28. this.templateSrv.init(this.variables);
  29. var queryParams = this.$location.search();
  30. for (let variable of this.variables) {
  31. this.variableLock[variable.name] = this.$q.defer();
  32. }
  33. return this.$q.all(this.variables.map(variable => {
  34. return this.processVariable(variable, queryParams);
  35. }));
  36. }
  37. processVariable(variable, queryParams) {
  38. var dependencies = [];
  39. var lock = this.variableLock[variable.name];
  40. for (let otherVariable of this.variables) {
  41. if (variable.dependsOn(otherVariable)) {
  42. dependencies.push(this.variableLock[otherVariable.name].promise);
  43. }
  44. }
  45. return this.$q.all(dependencies).then(() => {
  46. var urlValue = queryParams['var-' + variable.name];
  47. if (urlValue !== void 0) {
  48. return variable.setValueFromUrl(urlValue).then(lock.resolve);
  49. }
  50. if (variable.refresh === 1 || variable.refresh === 2) {
  51. return variable.updateOptions().then(() => {
  52. // if (_.isEmpty(variable.current) && variable.options.length) {
  53. // self.setVariableValue(variable, variable.options[0]);
  54. // }
  55. lock.resolve();
  56. });
  57. }
  58. lock.resolve();
  59. }).finally(() => {
  60. delete this.variableLock[variable.name];
  61. });
  62. }
  63. addVariable(model) {
  64. var ctor = variableConstructorMap[model.type];
  65. if (!ctor) {
  66. throw "Unable to find variable constructor for " + model.type;
  67. }
  68. var variable = this.$injector.instantiate(ctor, {model: model});
  69. this.variables.push(variable);
  70. this.dashboard.templating.list.push(model);
  71. return variable;
  72. }
  73. updateOptions(variable) {
  74. return variable.updateOptions();
  75. }
  76. variableUpdated(variable) {
  77. // if there is a variable lock ignore cascading update because we are in a boot up scenario
  78. if (this.variableLock[variable.name]) {
  79. return this.$q.when();
  80. }
  81. // cascade updates to variables that use this variable
  82. var promises = _.map(this.variables, otherVariable => {
  83. if (otherVariable === variable) {
  84. return;
  85. }
  86. if (otherVariable.dependsOn(variable)) {
  87. return this.updateOptions(otherVariable);
  88. }
  89. });
  90. return this.$q.all(promises);
  91. }
  92. selectOptionsForCurrentValue(variable) {
  93. var i, y, value, option;
  94. var selected: any = [];
  95. for (i = 0; i < variable.options.length; i++) {
  96. option = variable.options[i];
  97. option.selected = false;
  98. if (_.isArray(variable.current.value)) {
  99. for (y = 0; y < variable.current.value.length; y++) {
  100. value = variable.current.value[y];
  101. if (option.value === value) {
  102. option.selected = true;
  103. selected.push(option);
  104. }
  105. }
  106. } else if (option.value === variable.current.value) {
  107. option.selected = true;
  108. selected.push(option);
  109. }
  110. }
  111. return selected;
  112. }
  113. validateVariableSelectionState(variable) {
  114. if (!variable.current) {
  115. if (!variable.options.length) { return this.$q.when(); }
  116. return variable.setValue(variable.options[0]);
  117. }
  118. if (_.isArray(variable.current.value)) {
  119. var selected = this.selectOptionsForCurrentValue(variable);
  120. // if none pick first
  121. if (selected.length === 0) {
  122. selected = variable.options[0];
  123. } else {
  124. selected = {
  125. value: _.map(selected, function(val) {return val.value;}),
  126. text: _.map(selected, function(val) {return val.text;}).join(' + '),
  127. };
  128. }
  129. return variable.setValue(selected);
  130. } else {
  131. var currentOption = _.find(variable.options, {text: variable.current.text});
  132. if (currentOption) {
  133. return variable.setValue(currentOption);
  134. } else {
  135. if (!variable.options.length) { return Promise.resolve(); }
  136. return variable.setValue(variable.options[0]);
  137. }
  138. }
  139. }
  140. setOptionFromUrl(variable, urlValue) {
  141. var promise = this.$q.when();
  142. if (variable.refresh) {
  143. promise = variable.updateOptions();
  144. }
  145. return promise.then(() => {
  146. var option = _.find(variable.options, op => {
  147. return op.text === urlValue || op.value === urlValue;
  148. });
  149. option = option || {text: urlValue, value: urlValue};
  150. return variable.setValue(option);
  151. });
  152. }
  153. setOptionAsCurrent(variable, option) {
  154. variable.current = _.cloneDeep(option);
  155. if (_.isArray(variable.current.text)) {
  156. variable.current.text = variable.current.text.join(' + ');
  157. }
  158. this.selectOptionsForCurrentValue(variable);
  159. return this.variableUpdated(variable);
  160. }
  161. }
  162. coreModule.service('variableSrv', VariableSrv);