variable_srv.ts 6.5 KB

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