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

ReportingMenu.vue « ReportingMenu « src « vue « CoreHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 47751afb319f48a4c2e9bb36f9537d50a173a49b (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<!--
  Matomo - free/libre analytics platform
  @link https://matomo.org
  @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
-->

<template>
  <div class="reportingMenu">
    <ul
      class="navbar hide-on-med-and-down"
      role="menu"
      :aria-label="translate('CoreHome_MainNavigation')"
    >
      <li
        class="menuTab"
        role="menuitem"
        v-for="category in menu"
        :class="{ 'active': category.id === activeCategory }"
        :key="category.id"
      >
        <a
          class="item"
          tabindex="5"
          href=""
          @click.prevent="loadCategory(category)"
        >
          <span
            :class="`menu-icon ${category.icon ? category.icon : 'icon-arrow-right'}`"
          />{{ category.name }}
          <span class="hidden">
            {{ translate('CoreHome_Menu') }}
          </span>
        </a>
        <ul role="menu">
          <li
            role="menuitem"
            :class="{
              'active': (subcategory.id === displayedSubcategory
                || (subcategory.isGroup && activeSubsubcategory === displayedSubcategory)
              ) && category.id === displayedCategory,
            }"
            v-for="subcategory in category.subcategories"
            :key="subcategory.id"
          >
            <MenuDropdown
              v-if="subcategory.isGroup"
              :show-search="true"
              :menu-title="htmlEntities(subcategory.name)"
            >
              <a
                class="item"
                tabindex="5"
                :class="{
                  active: subcat.id === activeSubsubcategory
                    && subcategory.id === displayedSubcategory
                    && category.id === displayedCategory,
                }"
                :href="`#?${makeUrl(category, subcat)}`"
                @click="loadSubcategory(category, subcat, $event)"
                v-for="subcat in subcategory.subcategories"
                :title="subcat.tooltip"
                :key="subcat.id"
              >
                {{ subcat.name }}
              </a>
            </MenuDropdown>
            <a
              v-if="!subcategory.isGroup"
              :href="`#?${makeUrl(category, subcategory)}`"
              class="item"
              @click="loadSubcategory(category, subcategory, $event)"
            >
              {{ subcategory.name }}
            </a>
            <a
              class="item-help-icon"
              tabindex="5"
              href="javascript:"
              v-if="subcategory.help"
              @click="showHelp(category, subcategory, $event)"
              :class="{active: helpShownCategory
                && helpShownCategory.subcategory === subcategory.id
                && helpShownCategory.category === category.id
                && subcategory.help}"
            >
              <span class="icon-help" />
            </a>
          </li>
        </ul>
      </li>
    </ul>
    <ul
      id="mobile-left-menu"
      class="sidenav hide-on-large-only"
    >
      <li
        class="no-padding"
        v-for="category in menu"
        :key="category.id"
      >
        <ul
          class="collapsible collapsible-accordion"
          v-side-nav="{ activator: sideNavActivator }"
        >
          <li>
            <a class="collapsible-header">
              <i :class="category.icon ? category.icon : 'icon-arrow-bottom'" />{{ category.name }}
            </a>
            <div class="collapsible-body">
              <ul>
                <li v-for="subcategory in category.subcategories" :key="subcategory.id">
                  <span v-if="subcategory.isGroup">
                    <a
                      @click="loadSubcategory(category, subcat)"
                      :href="`#?${makeUrl(category, subcat)}`"
                      v-for="subcat in subcategory.subcategories"
                      :key="subcat.id"
                    >
                      {{ subcat.name }}
                    </a>
                  </span>
                  <span v-if="!subcategory.isGroup">
                    <a
                      @click="loadSubcategory(category, subcategory)"
                      :href="`#?${makeUrl(category, subcategory)}`"
                    >
                      {{ subcategory.name }}
                    </a>
                  </span>
                </li>
              </ul>
            </div>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script lang="ts">
import { defineComponent, watch } from 'vue';
import MenuDropdown from '../MenuDropdown/MenuDropdown.vue';
import SideNav from '../SideNav/SideNav';
import { NotificationsStore } from '../Notification';
import MatomoUrl from '../MatomoUrl/MatomoUrl';
import ReportingMenuStoreInstance from './ReportingMenu.store';
import Matomo from '../Matomo/Matomo';
import translate from '../translate';
import WidgetsStoreInstance from '../Widget/Widgets.store';
import Category from './Category';
import Subcategory from './Subcategory';

const REPORTING_HELP_NOTIFICATION_ID = 'reportingmenu-help';

export default defineComponent({
  components: {
    MenuDropdown,
  },
  directives: {
    SideNav,
  },
  props: {},
  data() {
    return {
      showSubcategoryHelpOnLoad: null,
      initialLoad: true,
      helpShownCategory: null,
    };
  },
  computed: {
    sideNavActivator() {
      return document.querySelector('nav .activateLeftMenu');
    },
    menu() {
      return ReportingMenuStoreInstance.menu.value;
    },
    activeCategory() {
      return ReportingMenuStoreInstance.activeCategory.value;
    },
    activeSubcategory() {
      return ReportingMenuStoreInstance.activeSubcategory.value;
    },
    activeSubsubcategory() {
      return ReportingMenuStoreInstance.activeSubsubcategory.value;
    },
    displayedCategory() {
      return MatomoUrl.parsed.value.category;
    },
    displayedSubcategory() {
      return MatomoUrl.parsed.value.subcategory;
    },
  },
  created() {
    ReportingMenuStoreInstance.fetchMenuItems().then((menu) => {
      if (!MatomoUrl.parsed.value.subcategory) {
        const categoryToLoad = menu[0];
        const subcategoryToLoad = categoryToLoad.subcategories[0];

        // load first, initial page if no subcategory is present
        ReportingMenuStoreInstance.enterSubcategory(categoryToLoad, subcategoryToLoad);
        this.propagateUrlChange(categoryToLoad, subcategoryToLoad);
      }
    });

    watch(() => MatomoUrl.parsed.value, (query) => {
      const found = ReportingMenuStoreInstance.findSubcategory(query.category, query.subcategory);
      ReportingMenuStoreInstance.enterSubcategory(
        found.category,
        found.subcategory,
        found.subsubcategory,
      );
    });

    Matomo.on('piwikPageChange', () => {
      if (!this.initialLoad) {
        window.globalAjaxQueue.abort();
      }

      this.helpShownCategory = null;

      if (this.showSubcategoryHelpOnLoad) {
        this.showHelp(
          this.showSubcategoryHelpOnLoad.category,
          this.showSubcategoryHelpOnLoad.subcategory,
        );
        this.showSubcategoryHelpOnLoad = null;
      }

      window.$('#loadingError').hide();

      this.initialLoad = false;
    });

    Matomo.on('updateReportingMenu', () => {
      ReportingMenuStoreInstance.reloadMenuItems().then(() => {
        const { category, subcategory } = MatomoUrl.parsed.value;

        // we need to make sure to select same categories again
        if (category && subcategory) {
          const found = ReportingMenuStoreInstance.findSubcategory(category, subcategory);
          if (found) {
            ReportingMenuStoreInstance.enterSubcategory(
              found.category,
              found.subcategory,
              found.subsubcategory,
            );
          }
        }
      });

      WidgetsStoreInstance.reloadAvailableWidgets();
    });
  },
  methods: {
    propagateUrlChange(category: Category, subcategory: Subcategory) {
      const queryParams = MatomoUrl.parsed.value;
      if (queryParams.category === category.id && queryParams.subcategory === subcategory.id) {
        // we need to manually trigger change as URL would not change and therefore page would not
        // be reloaded
        this.loadSubcategory(category, subcategory);
      } else {
        MatomoUrl.updateHash({
          ...MatomoUrl.hashParsed.value,
          category: category.id,
          subcategory: subcategory.id,
        });
      }
    },
    loadCategory(category: Category) {
      NotificationsStore.remove(REPORTING_HELP_NOTIFICATION_ID);

      const isActive = ReportingMenuStoreInstance.toggleCategory(category);
      if (isActive && category.subcategories && category.subcategories.length === 1) {
        this.helpShownCategory = null;

        const subcategory = category.subcategories[0];
        this.propagateUrlChange(category, subcategory);
      }
    },
    loadSubcategory(category: Category, subcategory: Subcategory, event: MouseEvent) {
      if (event
        && (event.shiftKey || event.ctrlKey || event.metaKey)
      ) {
        return;
      }

      NotificationsStore.remove(REPORTING_HELP_NOTIFICATION_ID);

      if (subcategory && subcategory.id === this.activeSubcategory) {
        this.helpShownCategory = null;

        // this menu item is already active, a location change success would not be triggered,
        // instead trigger an event (after the URL changes)
        setTimeout(() => {
          Matomo.postEvent('loadPage', category.id, subcategory.id);
        });
      }
    },
    makeUrl(category: Category, subcategory: Subcategory) {
      const {
        idSite,
        period,
        date,
        segment,
        comparePeriods,
        compareDates,
        compareSegments,
      } = MatomoUrl.parsed.value;

      return MatomoUrl.stringify({
        idSite,
        period,
        date,
        segment,
        comparePeriods,
        compareDates,
        compareSegments,
        category: category.id,
        subcategory: subcategory.id,
      });
    },
    htmlEntities(v: string) {
      return Matomo.helper.htmlEntities(v);
    },
    showHelp(category: Category, subcategory: Subcategory, event?: Event) {
      const parsedUrl = MatomoUrl.parsed.value;
      const currentCategory = parsedUrl.category;
      const currentSubcategory = parsedUrl.subcategory;

      if ((currentCategory !== category.id
        || currentSubcategory !== subcategory.id)
        && event
      ) {
        this.showSubcategoryHelpOnLoad = { category, subcategory };
        MatomoUrl.updateHash({
          ...MatomoUrl.hashParsed.value,
          category: category.id,
          subcategory: subcategory.id,
        });
        return;
      }

      if (this.helpShownCategory
        && category.id === this.helpShownCategory.category
        && subcategory.id === this.helpShownCategory.subcategory
      ) {
        NotificationsStore.remove(REPORTING_HELP_NOTIFICATION_ID);
        this.helpShownCategory = null;
        return;
      }

      const prefixText = translate('CoreHome_ReportingCategoryHelpPrefix',
        category.name, subcategory.name);
      const prefix = `<strong>${prefixText}</strong><br/>`;

      NotificationsStore.show({
        context: 'info',
        id: REPORTING_HELP_NOTIFICATION_ID,
        type: 'help',
        noclear: true,
        class: 'help-notification',
        message: prefix + subcategory.help,
        placeat: '#notificationContainer',
        prepend: true,
      });

      this.helpShownCategory = {
        category: category.id,
        subcategory: subcategory.id,
      };
    },
  },
});
</script>