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

SitesStore.ts « SiteSelector « src « vue « CoreHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 655803843c16a1f6a4a26ba96a06386ca27a30cb (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
/*!
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

import { reactive, computed, readonly } from 'vue';
import AjaxHelper from '../AjaxHelper/AjaxHelper';
import MatomoUrl from '../MatomoUrl/MatomoUrl';

export interface Site {
  idsite: string;
  name: string;
}

interface SitesStoreState {
  initialSites: Site[]|null;
  isInitialized: boolean;
}

class SitesStore {
  private state = reactive<SitesStoreState>({
    initialSites: [],
    isInitialized: false,
  });

  private currentRequest: AbortablePromise;

  private limitRequest: AbortablePromise;

  public readonly initialSites = computed(() => readonly(this.state.initialSites));

  loadInitialSites(): Promise<Site[]> {
    if (this.state.isInitialized) {
      return Promise.resolve(readonly(this.state.initialSites));
    }

    return this.searchSite('%').then((sites) => {
      this.state.isInitialized = true;
      this.state.initialSites = sites;
      return readonly(sites);
    });
  }

  loadSite(idSite: number|string): void {
    if (idSite === 'all') {
      MatomoUrl.updateUrl({
        ...MatomoUrl.urlParsed.value,
        module: 'MultiSites',
        action: 'index',
        date: MatomoUrl.parsed.value.date,
        period: MatomoUrl.parsed.value.period,
      });
    } else {
      MatomoUrl.updateUrl({
        ...MatomoUrl.parsed.value,
        segment: '',
        idSite,
      });
    }
  }

  searchSite(term, onlySitesWithAdminAccess = false): Promise<Site[]> {
    if (!term) {
      return this.loadInitialSites();
    }

    if (this.currentRequest) {
      this.currentRequest.abort();
    }

    if (this.limitRequest) {
      this.limitRequest.abort();
      this.limitRequest = null;
    }

    if (!this.limitRequest) {
      this.limitRequest = AjaxHelper.fetch({ method: 'SitesManager.getNumWebsitesToDisplayPerPage' });
    }

    return this.limitRequest.then((response) => {
      const limit = response.value;

      let methodToCall = 'SitesManager.getPatternMatchSites';
      if (onlySitesWithAdminAccess) {
        methodToCall = 'SitesManager.getSitesWithAdminAccess';
      }

      this.currentRequest = AjaxHelper.fetch({
        method: methodToCall,
        limit,
        pattern: term,
      });

      return this.currentRequest;
    }).then((response) => {
      if (response) {
        return this.processWebsitesList(response);
      }

      return null;
    }).finally(() => {
      this.currentRequest = null;
    });
  }

  private processWebsitesList(response) {
    let sites = response;

    if (!sites || !sites.length) {
      return [];
    }

    sites = sites.map((s) => ({
      ...s,
      name: s.group ? `[${s.group}] ${s.name}` : s.name,
    }));

    sites.sort((lhs, rhs) => {
      if (lhs.name.toLowerCase() < rhs.name.toLowerCase()) {
        return -1;
      }
      return lhs.name.toLowerCase() > rhs.name.toLowerCase() ? 1 : 0;
    });

    return sites;
  }
}

export default new SitesStore();