variable_srv.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.map(this.createVariableFromModel.bind(this));
  19. this.templateSrv.init(this.variables);
  20. // register event to sync back to persisted model
  21. this.dashboard.events.on('prepare-save-model', this.syncToDashboardModel.bind(this));
  22. // init variables
  23. for (let variable of this.variables) {
  24. variable.initLock = this.$q.defer();
  25. }
  26. var queryParams = this.$location.search();
  27. return this.$q.all(this.variables.map(variable => {
  28. return this.processVariable(variable, queryParams);
  29. })).then(() => {
  30. this.templateSrv.updateTemplateData();
  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. for (let otherVariable of this.variables) {
  51. if (variable.dependsOn(otherVariable)) {
  52. dependencies.push(otherVariable.initLock.promise);
  53. }
  54. }
  55. return this.$q.all(dependencies).then(() => {
  56. var urlValue = queryParams['var-' + variable.name];
  57. if (urlValue !== void 0) {
  58. return variable.setValueFromUrl(urlValue).then(variable.initLock.resolve);
  59. }
  60. if (variable.refresh === 1 || variable.refresh === 2) {
  61. return variable.updateOptions().then(variable.initLock.resolve);
  62. }
  63. variable.initLock.resolve();
  64. }).finally(() => {
  65. this.templateSrv.variableInitialized(variable);
  66. delete variable.initLock;
  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 (variable.initLock) {
  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. variable.current = {};
  130. }
  131. if (_.isArray(variable.current.value)) {
  132. var selected = this.selectOptionsForCurrentValue(variable);
  133. // if none pick first
  134. if (selected.length === 0) {
  135. selected = variable.options[0];
  136. } else {
  137. selected = {
  138. value: _.map(selected, function(val) {return val.value;}),
  139. text: _.map(selected, function(val) {return val.text;}).join(' + '),
  140. };
  141. }
  142. return variable.setValue(selected);
  143. } else {
  144. var currentOption = _.find(variable.options, {text: variable.current.text});
  145. if (currentOption) {
  146. return variable.setValue(currentOption);
  147. } else {
  148. if (!variable.options.length) { return Promise.resolve(); }
  149. return variable.setValue(variable.options[0]);
  150. }
  151. }
  152. }
  153. setOptionFromUrl(variable, urlValue) {
  154. var promise = this.$q.when();
  155. if (variable.refresh) {
  156. promise = variable.updateOptions();
  157. }
  158. return promise.then(() => {
  159. var option = _.find(variable.options, op => {
  160. return op.text === urlValue || op.value === urlValue;
  161. });
  162. option = option || {text: urlValue, value: urlValue};
  163. return variable.setValue(option);
  164. });
  165. }
  166. setOptionAsCurrent(variable, option) {
  167. variable.current = _.cloneDeep(option);
  168. if (_.isArray(variable.current.text)) {
  169. variable.current.text = variable.current.text.join(' + ');
  170. }
  171. this.selectOptionsForCurrentValue(variable);
  172. return this.variableUpdated(variable);
  173. }
  174. updateUrlParamsWithCurrentVariables() {
  175. // update url
  176. var params = this.$location.search();
  177. // remove variable params
  178. _.each(params, function(value, key) {
  179. if (key.indexOf('var-') === 0) {
  180. delete params[key];
  181. }
  182. });
  183. // add new values
  184. this.templateSrv.fillVariableValuesForUrl(params);
  185. // update url
  186. this.$location.search(params);
  187. }
  188. }
  189. coreModule.service('variableSrv', VariableSrv);