variable_srv.ts 7.5 KB

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