variable_srv.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. }
  15. init(dashboard) {
  16. this.variableLock = {};
  17. this.dashboard = dashboard;
  18. // create working class models representing variables
  19. this.variables = dashboard.templating.list.map(this.createVariableFromModel.bind(this));
  20. this.templateSrv.init(this.variables);
  21. // register event to sync back to persisted model
  22. this.dashboard.events.on('prepare-save-model', this.syncToDashboardModel.bind(this));
  23. // init variables
  24. for (let variable of this.variables) {
  25. this.variableLock[variable.name] = this.$q.defer();
  26. }
  27. var queryParams = this.$location.search();
  28. return this.$q.all(this.variables.map(variable => {
  29. return this.processVariable(variable, queryParams);
  30. }));
  31. }
  32. onDashboardRefresh() {
  33. var promises = this.variables
  34. .filter(variable => variable.refresh === 2)
  35. .map(variable => {
  36. var previousOptions = variable.options.slice();
  37. return variable.updateOptions()
  38. .then(this.variableUpdated.bind(this, variable))
  39. .then(() => {
  40. if (angular.toJson(previousOptions) !== angular.toJson(variable.options)) {
  41. this.$rootScope.$emit('template-variable-value-updated');
  42. }
  43. });
  44. });
  45. return this.$q.all(promises);
  46. }
  47. processVariable(variable, queryParams) {
  48. var dependencies = [];
  49. var lock = this.variableLock[variable.name];
  50. for (let otherVariable of this.variables) {
  51. if (variable.dependsOn(otherVariable)) {
  52. dependencies.push(this.variableLock[otherVariable.name].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(lock.resolve);
  59. }
  60. if (variable.refresh === 1 || variable.refresh === 2) {
  61. return variable.updateOptions().then(lock.resolve);
  62. }
  63. lock.resolve();
  64. }).finally(() => {
  65. delete this.variableLock[variable.name];
  66. });
  67. }
  68. createVariableFromModel(model) {
  69. var ctor = variableTypes[model.type].ctor;
  70. if (!ctor) {
  71. throw "Unable to find variable constructor for " + model.type;
  72. }
  73. var variable = this.$injector.instantiate(ctor, {model: model});
  74. return variable;
  75. }
  76. addVariable(model) {
  77. var variable = this.createVariableFromModel(model);
  78. this.variables.push(this.createVariableFromModel(variable));
  79. return variable;
  80. }
  81. syncToDashboardModel() {
  82. this.dashboard.templating.list = this.variables.map(variable => {
  83. return variable.getModel();
  84. });
  85. }
  86. updateOptions(variable) {
  87. return variable.updateOptions();
  88. }
  89. variableUpdated(variable) {
  90. // if there is a variable lock ignore cascading update because we are in a boot up scenario
  91. if (this.variableLock[variable.name]) {
  92. return this.$q.when();
  93. }
  94. // cascade updates to variables that use this variable
  95. var promises = _.map(this.variables, otherVariable => {
  96. if (otherVariable === variable) {
  97. return;
  98. }
  99. if (otherVariable.dependsOn(variable)) {
  100. return this.updateOptions(otherVariable);
  101. }
  102. });
  103. return this.$q.all(promises);
  104. }
  105. selectOptionsForCurrentValue(variable) {
  106. var i, y, value, option;
  107. var selected: any = [];
  108. for (i = 0; i < variable.options.length; i++) {
  109. option = variable.options[i];
  110. option.selected = false;
  111. if (_.isArray(variable.current.value)) {
  112. for (y = 0; y < variable.current.value.length; y++) {
  113. value = variable.current.value[y];
  114. if (option.value === value) {
  115. option.selected = true;
  116. selected.push(option);
  117. }
  118. }
  119. } else if (option.value === variable.current.value) {
  120. option.selected = true;
  121. selected.push(option);
  122. }
  123. }
  124. return selected;
  125. }
  126. validateVariableSelectionState(variable) {
  127. if (!variable.current) {
  128. if (!variable.options.length) { return this.$q.when(); }
  129. return variable.setValue(variable.options[0]);
  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. }
  175. coreModule.service('variableSrv', VariableSrv);