dynamic_dashboard_srv.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import angular from 'angular';
  4. import _ from 'lodash';
  5. import coreModule from 'app/core/core_module';
  6. import {DashboardRow} from './row/row_model';
  7. export class DynamicDashboardSrv {
  8. iteration: number;
  9. dashboard: any;
  10. variables: any;
  11. init(dashboard, variableSrv) {
  12. this.dashboard = dashboard;
  13. this.variables = variableSrv.variables;
  14. }
  15. process(options) {
  16. if (this.dashboard.snapshot || this.variables.length === 0) {
  17. return;
  18. }
  19. this.iteration = (this.iteration || new Date().getTime()) + 1;
  20. options = options || {};
  21. var cleanUpOnly = options.cleanUpOnly;
  22. var i, j, row, panel;
  23. // cleanup scopedVars
  24. for (i = 0; i < this.dashboard.rows.length; i++) {
  25. row = this.dashboard.rows[i];
  26. for (j = 0; j < row.panels.length; j++) {
  27. delete row.panels[j].scopedVars;
  28. }
  29. }
  30. for (i = 0; i < this.dashboard.rows.length; i++) {
  31. row = this.dashboard.rows[i];
  32. // handle row repeats
  33. if (row.repeat) {
  34. if (!cleanUpOnly) {
  35. this.repeatRow(row, i);
  36. }
  37. } else if (row.repeatRowId && row.repeatIteration !== this.iteration) {
  38. // clean up old left overs
  39. this.dashboard.removeRow(row, true);
  40. i = i - 1;
  41. continue;
  42. }
  43. // repeat panels
  44. for (j = 0; j < row.panels.length; j++) {
  45. panel = row.panels[j];
  46. if (panel.repeat) {
  47. if (!cleanUpOnly) {
  48. this.repeatPanel(panel, row);
  49. }
  50. } else if (panel.repeatPanelId && panel.repeatIteration !== this.iteration) {
  51. // clean up old left overs
  52. row.panels = _.without(row.panels, panel);
  53. j = j - 1;
  54. }
  55. }
  56. }
  57. }
  58. // returns a new row clone or reuses a clone from previous iteration
  59. getRowClone(sourceRow, repeatIndex, sourceRowIndex) {
  60. if (repeatIndex === 0) {
  61. return sourceRow;
  62. }
  63. var i, panel, row, copy;
  64. var sourceRowId = sourceRowIndex + 1;
  65. // look for row to reuse
  66. for (i = 0; i < this.dashboard.rows.length; i++) {
  67. row = this.dashboard.rows[i];
  68. if (row.repeatRowId === sourceRowId && row.repeatIteration !== this.iteration) {
  69. copy = row;
  70. copy.copyPropertiesFromRowSource(sourceRow);
  71. break;
  72. }
  73. }
  74. if (!copy) {
  75. var modelCopy = angular.copy(sourceRow.getSaveModel());
  76. copy = new DashboardRow(modelCopy);
  77. this.dashboard.rows.splice(sourceRowIndex + repeatIndex, 0, copy);
  78. // set new panel ids
  79. for (i = 0; i < copy.panels.length; i++) {
  80. panel = copy.panels[i];
  81. panel.id = this.dashboard.getNextPanelId();
  82. }
  83. }
  84. copy.repeat = null;
  85. copy.repeatRowId = sourceRowId;
  86. copy.repeatIteration = this.iteration;
  87. return copy;
  88. }
  89. // returns a new row clone or reuses a clone from previous iteration
  90. repeatRow(row, rowIndex) {
  91. var variable = _.find(this.variables, {name: row.repeat});
  92. if (!variable) {
  93. return;
  94. }
  95. var selected, copy, i, panel;
  96. if (variable.current.text === 'All') {
  97. selected = variable.options.slice(1, variable.options.length);
  98. } else {
  99. selected = _.filter(variable.options, {selected: true});
  100. }
  101. _.each(selected, (option, index) => {
  102. copy = this.getRowClone(row, index, rowIndex);
  103. copy.scopedVars = {};
  104. copy.scopedVars[variable.name] = option;
  105. for (i = 0; i < copy.panels.length; i++) {
  106. panel = copy.panels[i];
  107. panel.scopedVars = {};
  108. panel.scopedVars[variable.name] = option;
  109. }
  110. });
  111. }
  112. getPanelClone(sourcePanel, row, index) {
  113. // if first clone return source
  114. if (index === 0) {
  115. return sourcePanel;
  116. }
  117. var i, tmpId, panel, clone;
  118. // first try finding an existing clone to use
  119. for (i = 0; i < row.panels.length; i++) {
  120. panel = row.panels[i];
  121. if (panel.repeatIteration !== this.iteration && panel.repeatPanelId === sourcePanel.id) {
  122. clone = panel;
  123. break;
  124. }
  125. }
  126. if (!clone) {
  127. clone = { id: this.dashboard.getNextPanelId() };
  128. row.panels.push(clone);
  129. }
  130. // save id
  131. tmpId = clone.id;
  132. // copy properties from source
  133. angular.copy(sourcePanel, clone);
  134. // restore id
  135. clone.id = tmpId;
  136. clone.repeatIteration = this.iteration;
  137. clone.repeatPanelId = sourcePanel.id;
  138. clone.repeat = null;
  139. return clone;
  140. }
  141. repeatPanel(panel, row) {
  142. var variable = _.find(this.variables, {name: panel.repeat});
  143. if (!variable) { return; }
  144. var selected;
  145. if (variable.current.text === 'All') {
  146. selected = variable.options.slice(1, variable.options.length);
  147. } else {
  148. selected = _.filter(variable.options, {selected: true});
  149. }
  150. _.each(selected, (option, index) => {
  151. var copy = this.getPanelClone(panel, row, index);
  152. copy.span = Math.max(12 / selected.length, panel.minSpan || 4);
  153. copy.scopedVars = copy.scopedVars || {};
  154. copy.scopedVars[variable.name] = option;
  155. });
  156. }
  157. }
  158. coreModule.service('dynamicDashboardSrv', DynamicDashboardSrv);