variable_srv.ts 6.2 KB

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