variable_srv.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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
  26. .all(
  27. this.variables.map(variable => {
  28. return this.processVariable(variable, queryParams);
  29. })
  30. )
  31. .then(() => {
  32. this.templateSrv.updateTemplateData();
  33. });
  34. }
  35. onDashboardRefresh() {
  36. var promises = this.variables.filter(variable => variable.refresh === 2).map(variable => {
  37. var previousOptions = variable.options.slice();
  38. return variable.updateOptions().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
  54. .all(dependencies)
  55. .then(() => {
  56. var urlValue = queryParams['var-' + variable.name];
  57. if (urlValue !== void 0) {
  58. return variable.setValueFromUrl(urlValue).then(variable.initLock.resolve);
  59. }
  60. if (variable.refresh === 1 || variable.refresh === 2) {
  61. return variable.updateOptions().then(variable.initLock.resolve);
  62. }
  63. variable.initLock.resolve();
  64. })
  65. .finally(() => {
  66. this.templateSrv.variableInitialized(variable);
  67. delete variable.initLock;
  68. });
  69. }
  70. createVariableFromModel(model) {
  71. var ctor = variableTypes[model.type].ctor;
  72. if (!ctor) {
  73. throw {
  74. message: 'Unable to find variable constructor for ' + model.type,
  75. };
  76. }
  77. var variable = this.$injector.instantiate(ctor, { model: model });
  78. return variable;
  79. }
  80. addVariable(variable) {
  81. this.variables.push(variable);
  82. this.templateSrv.updateTemplateData();
  83. this.dashboard.updateSubmenuVisibility();
  84. }
  85. removeVariable(variable) {
  86. var index = _.indexOf(this.variables, variable);
  87. this.variables.splice(index, 1);
  88. this.templateSrv.updateTemplateData();
  89. this.dashboard.updateSubmenuVisibility();
  90. }
  91. updateOptions(variable) {
  92. return variable.updateOptions();
  93. }
  94. variableUpdated(variable, emitChangeEvents?) {
  95. // if there is a variable lock ignore cascading update because we are in a boot up scenario
  96. if (variable.initLock) {
  97. return this.$q.when();
  98. }
  99. // cascade updates to variables that use this variable
  100. var promises = _.map(this.variables, otherVariable => {
  101. if (otherVariable === variable) {
  102. return;
  103. }
  104. if (otherVariable.dependsOn(variable)) {
  105. return this.updateOptions(otherVariable);
  106. }
  107. });
  108. return this.$q.all(promises).then(() => {
  109. if (emitChangeEvents) {
  110. this.$rootScope.$emit('template-variable-value-updated');
  111. this.$rootScope.$broadcast('refresh');
  112. }
  113. });
  114. }
  115. selectOptionsForCurrentValue(variable) {
  116. var i, y, value, option;
  117. var selected: any = [];
  118. for (i = 0; i < variable.options.length; i++) {
  119. option = variable.options[i];
  120. option.selected = false;
  121. if (_.isArray(variable.current.value)) {
  122. for (y = 0; y < variable.current.value.length; y++) {
  123. value = variable.current.value[y];
  124. if (option.value === value) {
  125. option.selected = true;
  126. selected.push(option);
  127. }
  128. }
  129. } else if (option.value === variable.current.value) {
  130. option.selected = true;
  131. selected.push(option);
  132. }
  133. }
  134. return selected;
  135. }
  136. validateVariableSelectionState(variable) {
  137. if (!variable.current) {
  138. variable.current = {};
  139. }
  140. if (_.isArray(variable.current.value)) {
  141. var selected = this.selectOptionsForCurrentValue(variable);
  142. // if none pick first
  143. if (selected.length === 0) {
  144. selected = variable.options[0];
  145. } else {
  146. selected = {
  147. value: _.map(selected, function(val) {
  148. return val.value;
  149. }),
  150. text: _.map(selected, function(val) {
  151. return val.text;
  152. }).join(' + '),
  153. };
  154. }
  155. return variable.setValue(selected);
  156. } else {
  157. var currentOption = _.find(variable.options, {
  158. text: variable.current.text,
  159. });
  160. if (currentOption) {
  161. return variable.setValue(currentOption);
  162. } else {
  163. if (!variable.options.length) {
  164. return Promise.resolve();
  165. }
  166. return variable.setValue(variable.options[0]);
  167. }
  168. }
  169. }
  170. setOptionFromUrl(variable, urlValue) {
  171. var promise = this.$q.when();
  172. if (variable.refresh) {
  173. promise = variable.updateOptions();
  174. }
  175. return promise.then(() => {
  176. var option = _.find(variable.options, op => {
  177. return op.text === urlValue || op.value === urlValue;
  178. });
  179. option = option || { text: urlValue, value: urlValue };
  180. return variable.setValue(option);
  181. });
  182. }
  183. setOptionAsCurrent(variable, option) {
  184. variable.current = _.cloneDeep(option);
  185. if (_.isArray(variable.current.text)) {
  186. variable.current.text = variable.current.text.join(' + ');
  187. }
  188. this.selectOptionsForCurrentValue(variable);
  189. return this.variableUpdated(variable);
  190. }
  191. updateUrlParamsWithCurrentVariables() {
  192. // update url
  193. var params = this.$location.search();
  194. // remove variable params
  195. _.each(params, function(value, key) {
  196. if (key.indexOf('var-') === 0) {
  197. delete params[key];
  198. }
  199. });
  200. // add new values
  201. this.templateSrv.fillVariableValuesForUrl(params);
  202. // update url
  203. this.$location.search(params);
  204. }
  205. setAdhocFilter(options) {
  206. var variable = _.find(this.variables, {
  207. type: 'adhoc',
  208. datasource: options.datasource,
  209. });
  210. if (!variable) {
  211. variable = this.createVariableFromModel({
  212. name: 'Filters',
  213. type: 'adhoc',
  214. datasource: options.datasource,
  215. });
  216. this.addVariable(variable);
  217. }
  218. let filters = variable.filters;
  219. let filter = _.find(filters, { key: options.key, value: options.value });
  220. if (!filter) {
  221. filter = { key: options.key, value: options.value };
  222. filters.push(filter);
  223. }
  224. filter.operator = options.operator;
  225. this.variableUpdated(variable, true);
  226. }
  227. }
  228. coreModule.service('variableSrv', VariableSrv);