variable_srv.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 = dashboard.templating.list.map(this.createVariableFromModel.bind(this));
  19. this.templateSrv.init(this.variables);
  20. // init variables
  21. for (let variable of this.variables) {
  22. variable.initLock = this.$q.defer();
  23. }
  24. var queryParams = this.$location.search();
  25. return this.$q.all(this.variables.map(variable => {
  26. return this.processVariable(variable, queryParams);
  27. })).then(() => {
  28. this.templateSrv.updateTemplateData();
  29. });
  30. }
  31. onDashboardRefresh() {
  32. var promises = this.variables
  33. .filter(variable => variable.refresh === 2)
  34. .map(variable => {
  35. var previousOptions = variable.options.slice();
  36. return variable.updateOptions()
  37. .then(this.variableUpdated.bind(this, variable))
  38. .then(() => {
  39. if (angular.toJson(previousOptions) !== angular.toJson(variable.options)) {
  40. this.$rootScope.$emit('template-variable-value-updated');
  41. }
  42. });
  43. });
  44. return this.$q.all(promises);
  45. }
  46. processVariable(variable, queryParams) {
  47. var dependencies = [];
  48. for (let otherVariable of this.variables) {
  49. if (variable.dependsOn(otherVariable)) {
  50. dependencies.push(otherVariable.initLock.promise);
  51. }
  52. }
  53. return this.$q.all(dependencies).then(() => {
  54. var urlValue = queryParams['var-' + variable.name];
  55. if (urlValue !== void 0) {
  56. return variable.setValueFromUrl(urlValue).then(variable.initLock.resolve);
  57. }
  58. if (variable.refresh === 1 || variable.refresh === 2) {
  59. return variable.updateOptions().then(variable.initLock.resolve);
  60. }
  61. variable.initLock.resolve();
  62. }).finally(() => {
  63. this.templateSrv.variableInitialized(variable);
  64. delete variable.initLock;
  65. });
  66. }
  67. createVariableFromModel(model) {
  68. var ctor = variableTypes[model.type].ctor;
  69. if (!ctor) {
  70. throw "Unable to find variable constructor for " + model.type;
  71. }
  72. var variable = this.$injector.instantiate(ctor, {model: model});
  73. return variable;
  74. }
  75. addVariable(variable) {
  76. this.variables.push(variable);
  77. this.templateSrv.updateTemplateData();
  78. this.dashboard.updateSubmenuVisibility();
  79. }
  80. removeVariable(variable) {
  81. var index = _.indexOf(this.variables, variable);
  82. this.variables.splice(index, 1);
  83. this.templateSrv.updateTemplateData();
  84. this.dashboard.updateSubmenuVisibility();
  85. }
  86. updateOptions(variable) {
  87. return variable.updateOptions();
  88. }
  89. variableUpdated(variable, emitChangeEvents?) {
  90. // if there is a variable lock ignore cascading update because we are in a boot up scenario
  91. if (variable.initLock) {
  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).then(() => {
  104. if (emitChangeEvents) {
  105. this.$rootScope.$emit('template-variable-value-updated');
  106. this.$rootScope.$broadcast('refresh');
  107. }
  108. });
  109. }
  110. selectOptionsForCurrentValue(variable) {
  111. var i, y, value, option;
  112. var selected: any = [];
  113. for (i = 0; i < variable.options.length; i++) {
  114. option = variable.options[i];
  115. option.selected = false;
  116. if (_.isArray(variable.current.value)) {
  117. for (y = 0; y < variable.current.value.length; y++) {
  118. value = variable.current.value[y];
  119. if (option.value === value) {
  120. option.selected = true;
  121. selected.push(option);
  122. }
  123. }
  124. } else if (option.value === variable.current.value) {
  125. option.selected = true;
  126. selected.push(option);
  127. }
  128. }
  129. return selected;
  130. }
  131. validateVariableSelectionState(variable) {
  132. if (!variable.current) {
  133. variable.current = {};
  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. updateUrlParamsWithCurrentVariables() {
  179. // update url
  180. var params = this.$location.search();
  181. // remove variable params
  182. _.each(params, function(value, key) {
  183. if (key.indexOf('var-') === 0) {
  184. delete params[key];
  185. }
  186. });
  187. // add new values
  188. this.templateSrv.fillVariableValuesForUrl(params);
  189. // update url
  190. this.$location.search(params);
  191. }
  192. setAdhocFilter(options) {
  193. var variable = _.find(this.variables, {type: 'adhoc', datasource: options.datasource});
  194. if (!variable) {
  195. variable = this.createVariableFromModel({name: 'Filters', type: 'adhoc', datasource: options.datasource});
  196. this.addVariable(variable);
  197. }
  198. let filters = variable.filters;
  199. let filter = _.find(filters, {key: options.key, value: options.value});
  200. if (!filter) {
  201. filter = {key: options.key, value: options.value};
  202. filters.push(filter);
  203. }
  204. filter.operator = options.operator;
  205. this.variableUpdated(variable, true);
  206. }
  207. }
  208. coreModule.service('variableSrv', VariableSrv);