variable_srv.ts 6.5 KB

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