variable_srv.ts 5.7 KB

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