dynamic_dashboard_srv.ts 5.1 KB

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