dynamic_dashboard_srv.ts 4.9 KB

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