dynamic_dashboard_srv.ts 5.0 KB

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