Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ReportingPage.store.ts « ReportingPage « src « vue « CoreHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e39ecd2986898af1c20b339a58abf305090d3374 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*!
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

import { computed, reactive, readonly } from 'vue';
import ReportingPagesStoreInstance, { Page } from '../ReportingPages/ReportingPages.store';
import ReportMetadataStoreInstance from '../ReportMetadata/ReportMetadata.store';
import { sortOrderables } from '../Orderable';
import { Widget } from '../Widget/Widgets.store';

interface ReportingMenuStoreState {
  page?: Page|null;
}

function shouldBeRenderedWithFullWidth(widget: Widget) {
  // rather controller logic
  if ((widget.isContainer && widget.layout && widget.layout === 'ByDimension')
    || widget.viewDataTable === 'bydimension'
  ) {
    return true;
  }

  if (widget.isWide) {
    return true;
  }

  return widget.viewDataTable
    && (widget.viewDataTable === 'tableAllColumns'
      || widget.viewDataTable === 'sparklines'
      || widget.viewDataTable === 'graphEvolution');
}

function markWidgetsInFirstRowOfPage(widgets: Widget[]) {
  if (widgets && widgets[0]) {
    const newWidgets = [...widgets];

    if (widgets[0].group) {
      newWidgets[0] = {
        ...newWidgets[0],
        left: markWidgetsInFirstRowOfPage(widgets[0].left),
        right: markWidgetsInFirstRowOfPage(widgets[0].right),
      };
    } else {
      newWidgets[0] = { ...newWidgets[0], isFirstInPage: true };
    }

    return newWidgets;
  }

  return widgets;
}

export class ReportingPageStore {
  private privateState = reactive<ReportingMenuStoreState>({
    page: null,
  });

  private state = computed(() => readonly(this.privateState));

  readonly page = computed(() => this.state.value.page);

  readonly widgets = computed(() => {
    const page = this.page.value;
    if (!page) {
      return [];
    }

    let widgets = [];
    const reportsToIgnore = {};

    const isIgnoredReport = (widget: Widget) => widget.isReport
      && reportsToIgnore[`${widget.module}.${widget.action}`];

    const getRelatedReports = (widget) => {
      if (!widget.isReport) {
        return [];
      }

      const report = ReportMetadataStoreInstance.findReport(widget.module, widget.action);
      if (!report || !report.relatedReports) {
        return [];
      }

      return report.relatedReports;
    };

    (page.widgets || []).forEach((widget) => {
      if (isIgnoredReport(widget)) {
        return;
      }

      getRelatedReports(widget).forEach((report) => {
        reportsToIgnore[`${report.module}.${report.action}`] = true;
      });

      widgets.push(widget);
    });

    widgets = sortOrderables(widgets);

    if (widgets.length === 1) {
      // if there is only one widget, we always display it full width
      return markWidgetsInFirstRowOfPage(widgets);
    }

    let groupedWidgets = [];
    for (let i = 0; i < widgets.length; i += 1) {
      const widget = widgets[i];

      if (shouldBeRenderedWithFullWidth(widget)
        || (widgets[i + 1] && shouldBeRenderedWithFullWidth(widgets[i + 1]))
      ) {
        groupedWidgets.push({
          ...widget,
          widgets: sortOrderables(widget.widgets),
        });
      } else {
        let counter = 0;
        const left = [widget];
        const right = [];

        while (widgets[i + 1] && !shouldBeRenderedWithFullWidth(widgets[i + 1])) {
          i += 1;
          counter += 1;
          if (counter % 2 === 0) {
            left.push(widgets[i]);
          } else {
            right.push(widgets[i]);
          }
        }

        groupedWidgets.push({ group: true, left, right });
      }
    }

    groupedWidgets = markWidgetsInFirstRowOfPage(groupedWidgets);

    return groupedWidgets;
  });

  fetchPage(category: string, subcategory: string): Promise<typeof ReportingPageStore['page']['value']> {
    this.resetPage();

    return Promise.all([
      ReportingPagesStoreInstance.getAllPages(),
      ReportMetadataStoreInstance.fetchReportMetadata(),
    ]).then(() => {
      this.privateState.page = ReportingPagesStoreInstance.findPage(category, subcategory);
      return this.page.value;
    });
  }

  resetPage(): void {
    this.privateState.page = null;
  }
}

export default new ReportingPageStore();