dynamic_dashboard_srv.ts 5.0 KB

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