variable_srv.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 {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(() => {
  38. if (angular.toJson(previousOptions) !== angular.toJson(variable.options)) {
  39. this.$rootScope.$emit('template-variable-value-updated');
  40. }
  41. });
  42. });
  43. return this.$q.all(promises);
  44. }
  45. processVariable(variable, queryParams) {
  46. var dependencies = [];
  47. for (let otherVariable of this.variables) {
  48. if (variable.dependsOn(otherVariable)) {
  49. dependencies.push(otherVariable.initLock.promise);
  50. }
  51. }
  52. return this.$q.all(dependencies).then(() => {
  53. var urlValue = queryParams['var-' + variable.name];
  54. if (urlValue !== void 0) {
  55. return variable.setValueFromUrl(urlValue).then(variable.initLock.resolve);
  56. }
  57. if (variable.refresh === 1 || variable.refresh === 2) {
  58. return variable.updateOptions().then(variable.initLock.resolve);
  59. }
  60. variable.initLock.resolve();
  61. }).finally(() => {
  62. this.templateSrv.variableInitialized(variable);
  63. delete variable.initLock;
  64. });
  65. }
  66. createVariableFromModel(model) {
  67. var ctor = variableTypes[model.type].ctor;
  68. if (!ctor) {
  69. throw {message: "Unable to find variable constructor for " + model.type};
  70. }
  71. var variable = this.$injector.instantiate(ctor, {model: model});
  72. return variable;
  73. }
  74. addVariable(variable) {
  75. this.variables.push(variable);
  76. this.templateSrv.updateTemplateData();
  77. this.dashboard.updateSubmenuVisibility();
  78. }
  79. removeVariable(variable) {
  80. var index = _.indexOf(this.variables, variable);
  81. this.variables.splice(index, 1);
  82. this.templateSrv.updateTemplateData();
  83. this.dashboard.updateSubmenuVisibility();
  84. }
  85. updateOptions(variable) {
  86. return variable.updateOptions();
  87. }
  88. variableUpdated(variable, emitChangeEvents?) {
  89. // if there is a variable lock ignore cascading update because we are in a boot up scenario
  90. if (variable.initLock) {
  91. return this.$q.when();
  92. }
  93. // cascade updates to variables that use this variable
  94. var promises = _.map(this.variables, otherVariable => {
  95. if (otherVariable === variable) {
  96. return;
  97. }
  98. if (otherVariable.dependsOn(variable)) {
  99. return this.updateOptions(otherVariable);
  100. }
  101. });
  102. return this.$q.all(promises).then(() => {
  103. if (emitChangeEvents) {
  104. this.$rootScope.$emit('template-variable-value-updated');
  105. this.$rootScope.$broadcast('refresh');
  106. }
  107. });
  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. variable.current = {};
  133. }
  134. if (_.isArray(variable.current.value)) {
  135. var selected = this.selectOptionsForCurrentValue(variable);
  136. // if none pick first
  137. if (selected.length === 0) {
  138. selected = variable.options[0];
  139. } else {
  140. selected = {
  141. value: _.map(selected, function(val) {return val.value;}),
  142. text: _.map(selected, function(val) {return val.text;}).join(' + '),
  143. };
  144. }
  145. return variable.setValue(selected);
  146. } else {
  147. var currentOption = _.find(variable.options, {text: variable.current.text});
  148. if (currentOption) {
  149. return variable.setValue(currentOption);
  150. } else {
  151. if (!variable.options.length) { return Promise.resolve(); }
  152. return variable.setValue(variable.options[0]);
  153. }
  154. }
  155. }
  156. setOptionFromUrl(variable, urlValue) {
  157. var promise = this.$q.when();
  158. if (variable.refresh) {
  159. promise = variable.updateOptions();
  160. }
  161. return promise.then(() => {
  162. var option = _.find(variable.options, op => {
  163. return op.text === urlValue || op.value === urlValue;
  164. });
  165. option = option || {text: urlValue, value: urlValue};
  166. return variable.setValue(option);
  167. });
  168. }
  169. setOptionAsCurrent(variable, option) {
  170. variable.current = _.cloneDeep(option);
  171. if (_.isArray(variable.current.text)) {
  172. variable.current.text = variable.current.text.join(' + ');
  173. }
  174. this.selectOptionsForCurrentValue(variable);
  175. return this.variableUpdated(variable);
  176. }
  177. updateUrlParamsWithCurrentVariables() {
  178. // update url
  179. var params = this.$location.search();
  180. // remove variable params
  181. _.each(params, function(value, key) {
  182. if (key.indexOf('var-') === 0) {
  183. delete params[key];
  184. }
  185. });
  186. // add new values
  187. this.templateSrv.fillVariableValuesForUrl(params);
  188. // update url
  189. this.$location.search(params);
  190. }
  191. setAdhocFilter(options) {
  192. var variable = _.find(this.variables, {type: 'adhoc', datasource: options.datasource});
  193. if (!variable) {
  194. variable = this.createVariableFromModel({name: 'Filters', type: 'adhoc', datasource: options.datasource});
  195. this.addVariable(variable);
  196. }
  197. let filters = variable.filters;
  198. let filter = _.find(filters, {key: options.key, value: options.value});
  199. if (!filter) {
  200. filter = {key: options.key, value: options.value};
  201. filters.push(filter);
  202. }
  203. filter.operator = options.operator;
  204. this.variableUpdated(variable, true);
  205. }
  206. }
  207. coreModule.service('variableSrv', VariableSrv);