variable_srv.ts 6.2 KB

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