Passed
Push — master ( 0d32dd...265c4a )
by Marcel
02:32
created

js/userGuidance.js   A

Complexity

Total Complexity 37
Complexity/F 2.06

Size

Lines of Code 273
Function Count 18

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 171
c 0
b 0
f 0
dl 0
loc 273
rs 9.44
wmc 37
mnd 19
bc 19
fnc 18
bpm 1.0555
cpm 2.0555
noi 4
1
/**
2
 * Analytics
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the LICENSE.md file.
6
 *
7
 * @author Marcel Scherello <[email protected]>
8
 * @copyright 2021 Marcel Scherello
9
 */
10
/** global: OCA */
11
/** global: OCP */
12
/** global: OC */
13
'use strict';
14
15
if (!OCA.Analytics) {
16
    /**
17
     * @namespace
18
     */
19
    OCA.Analytics = {};
20
}
21
/**
22
 * @namespace OCA.Analytics.Wizard
23
 */
24
OCA.Analytics.Wizard = {
25
    sildeArray: [],
26
    currentSlide: 0,
27
    previousSlide: 0,
28
29
    showFirstStart: function () {
30
        OCA.Analytics.Wizard.sildeArray = [
31
            ['', ''],
32
            ['wizard-start', ''],
33
            ['wizard-charts', ''],
34
            ['wizard-filter', ''],
35
            ['wizard-datasource', ''],
36
            ['wizard-dataload', ''],
37
            ['wizardFinal', OCA.Analytics.Wizard.wizardFinal]
38
        ];
39
        OCA.Analytics.Wizard.show();
40
    },
41
42
    show: function () {
43
        OCA.Analytics.Wizard.currentSlide = 0;
44
        let wizard = document.importNode(document.getElementById('wizardDialog').content, true);
45
        document.getElementById('content').appendChild(wizard);
46
        document.getElementById('wizardNext').addEventListener('click', OCA.Analytics.Wizard.next);
47
        document.getElementById('wizardPrevious').addEventListener('click', OCA.Analytics.Wizard.previous);
48
        document.getElementById('wizardClose').addEventListener('click', OCA.Analytics.Wizard.close);
49
50
        // load all pages in the background
51
        for (let i = 1; i < OCA.Analytics.Wizard.sildeArray.length; i++) {
52
            let newpage = document.importNode(document.getElementById(OCA.Analytics.Wizard.sildeArray[i][0]).content, true);
53
            newpage.firstElementChild.id = OCA.Analytics.Wizard.sildeArray[i][0] + 'Page';
54
            document.getElementById('pageBody').appendChild(newpage);
55
56
        }
57
58
        // create the dots
59
        for (let i = 1; i < OCA.Analytics.Wizard.sildeArray.length; i++) {
60
            let dot = document.createElement('div');
61
            dot.id = 'wizardDot' + i;
62
            dot.classList.add('dot');
63
            if (i===1) dot.classList.add('active');
64
            document.getElementById('wizardFooter').appendChild(dot);
65
        }
66
67
        OCA.Analytics.Wizard.next();
68
    },
69
70
    next: function () {
71
        if (OCA.Analytics.Wizard.currentSlide < OCA.Analytics.Wizard.sildeArray.length - 1) {
72
            OCA.Analytics.Wizard.previousSlide = OCA.Analytics.Wizard.currentSlide;
73
            OCA.Analytics.Wizard.currentSlide++;
74
            OCA.Analytics.Wizard.showPage();
75
        }
76
    },
77
78
    previous: function () {
79
        if (OCA.Analytics.Wizard.currentSlide !== 1) {
80
            OCA.Analytics.Wizard.previousSlide = OCA.Analytics.Wizard.currentSlide;
81
            OCA.Analytics.Wizard.currentSlide--;
82
            OCA.Analytics.Wizard.showPage();
83
        }
84
    },
85
86
    close: function () {
87
        document.getElementById('analyticsWizard').remove();
88
        if (!OCA.Analytics.isAdvanced) document.getElementById('overviewButton').click();
89
    },
90
91
    demo: function () {
92
        let thresholds = '{"report":{"name":"Demo: Thresholds","type":2,"link":"{}","visualization":"table","chart":"","dimension1":"Type","dimension2":"Threshold","dimension3":"Value","subheader":"Thresholds can be used to highlight data. Using the notification threshold, push messages are being sent","chartoptions":"","dataoptions":"","filteroptions":"{}","value":"Value"},"dataload":[{"id":105,"user_id":"admin","dataset":103,"datasource":3,"name":"test github","option":"{\\"user\\":\\"a\\",\\"repository\\":\\"a\\",\\"limit\\":\\"1\\",\\"timestamp\\":\\"true\\",\\"delete\\":\\"false\\"}","schedule":""}],"threshold":[{"id":94,"dimension1":"Threshold","dimension2":null,"value":"1.00","option":"=","severity":4},{"id":95,"dimension1":"Threshold","dimension2":null,"value":"2.00","option":"=","severity":3},{"id":97,"dimension1":"Threshold","dimension2":null,"value":"4.00","option":"=","severity":1},{"id":98,"dimension1":"Threshold","dimension2":null,"value":"3.00","option":">=","severity":2}],"favorite":"","data":[["Normal value","none","3.00"],["Threshold","green","1.00"],["Threshold","none","0.00"],["Threshold","notification","4.00"],["Threshold","red","3.00"],["Threshold","yellow","2.00"]],"favorite":"true"}';
93
        OCA.Analytics.Sidebar.Report.import(null, thresholds);
94
95
        let population = '{"report":{"name":"Demo: Population Data","type":4,"link":"{\\"link\\":\\"https:\\/\\/raw.githubusercontent.com\\/Rello\\/analytics\\/master\\/sample_data\\/sample1.csv\\",\\"columns\\":\\"\\",\\"offset\\":\\"\\"}","visualization":"ct","chart":"line","dimension1":"","dimension2":"","dimension3":null,"subheader":"Realtime CSV Data from GitHub via the \\"Extrernal URL\\" datasource","chartoptions":"","dataoptions":"","filteroptions":null,"value":""},"dataload":[],"threshold":[],"favorite":"true"}';
96
        OCA.Analytics.Sidebar.Report.import(null, population);
97
98
        let github = '{"report":{"name":"Demo: Analytics Downloads","type":3,"link":"{\\"user\\":\\"rello\\",\\"repository\\":\\"analytics\\",\\"limit\\":\\"10\\",\\"timestamp\\":\\"false\\"}","visualization":"ct","chart":"column","dimension1":"Object","dimension2":"Date","dimension3":"Value","subheader":"Realtime download statistics form Github","chartoptions":"","dataoptions":"","filteroptions":null,"value":"Value"},"dataload":[{"id":59,"user_id":"admin","dataset":155,"datasource":6,"name":"New","option":"{}","schedule":null}],"threshold":[{"id":33,"dimension1":"*","dimension2":null,"value":"5000.00","option":">","severity":4}],"favorite":"true"}';
99
        OCA.Analytics.Sidebar.Report.import(null, github);
100
101
        let finance = '{"report":{"name":"Demo: Finance","type":2,"link":"{}","visualization":"ct","chart":"column","dimension1":"Segment","dimension2":"Year","dimension3":null,"subheader":"Sales and revenue of the last years","chartoptions":"","dataoptions":"[{\\"yAxisID\\":\\"primary\\",\\"type\\":\\"bar\\"},{\\"yAxisID\\":\\"primary\\",\\"type\\":\\"bar\\"},{\\"yAxisID\\":\\"primary\\",\\"type\\":\\"bar\\"},{\\"yAxisID\\":\\"primary\\",\\"type\\":\\"line\\"}]","filteroptions":"{\\"filter\\":{\\"dimension2\\":{\\"option\\":\\"GT\\",\\"value\\":\\"2011\\"}}}","value":"\u20ac"},"dataload":[],"threshold":[],"favorite":"","data":[["Channel Partners","2011","30216.00"],["Channel Partners","2012","18540.00"],["Channel Partners","2013","30216.00"],["Channel Partners","2014","4404.00"],["Channel Partners","2015","10944.00"],["Channel Partners","2016","25932.00"],["Channel Partners","2017","18540.00"],["Channel Partners","2018","34056.00"],["Channel Partners","2019","23436.00"],["Channel Partners","2020","25692.00"],["Enterprise","2011","33318.00"],["Enterprise","2012","43125.00"],["Enterprise","2013","25500.00"],["Enterprise","2014","52625.00"],["Enterprise","2015","43125.00"],["Enterprise","2016","52743.00"],["Enterprise","2017","32370.00"],["Enterprise","2018","26420.00"],["Enterprise","2019","52950.00"],["Enterprise","2020","37980.00"],["Government","2011","15022.00"],["Government","2012","5840.00"],["Government","2013","35200.00"],["Government","2014","6181.00"],["Government","2015","8001.00"],["Government","2016","60350.00"],["Government","2017","36340.00"],["Government","2018","59550.00"],["Government","2019","10451.00"],["Government","2020","32100.00"],["Total Sales","2011","78556.00"],["Total Sales","2012","67505.00"],["Total Sales","2013","90916.00"],["Total Sales","2014","63210.00"],["Total Sales","2015","62070.00"],["Total Sales","2016","139025.00"],["Total Sales","2017","87250.00"],["Total Sales","2018","120026.00"],["Total Sales","2019","86837.00"],["Total Sales","2020","95772.00"]]}';
102
        OCA.Analytics.Sidebar.Report.import(null, finance);
103
    },
104
105
    showPage: function () {
106
        let nextSlide = OCA.Analytics.Wizard.sildeArray[OCA.Analytics.Wizard.currentSlide][0];
107
        let prevSlide = OCA.Analytics.Wizard.sildeArray[OCA.Analytics.Wizard.previousSlide][0];
108
        let callback = OCA.Analytics.Wizard.sildeArray[OCA.Analytics.Wizard.currentSlide][1];
109
        let prev = document.getElementById('wizardPrevious');
110
        let next = document.getElementById('wizardNext');
111
112
        if (prevSlide !== '') document.getElementById(prevSlide + 'Page').style.display = 'none';
113
        document.getElementById(nextSlide + 'Page').style.display = '';
114
115
        document.querySelector('.dot.active').classList.remove('active');
116
        document.getElementById('wizardDot' + OCA.Analytics.Wizard.currentSlide).classList.add('active');
117
118
        if (typeof callback === 'function') {
119
            callback();
120
        }
121
122
        OCA.Analytics.Wizard.currentSlide === 1 ? prev.hidden = true : prev.hidden = false;
123
        OCA.Analytics.Wizard.currentSlide === OCA.Analytics.Wizard.sildeArray.length - 1 ? next.hidden = true : next.hidden = false;
124
    },
125
126
    wizardFinal: function () {
127
        document.getElementById('wizardEnd').addEventListener('click', OCA.Analytics.Wizard.close);
128
        document.getElementById('wizardDemo').addEventListener('click', OCA.Analytics.Wizard.demo);
129
        $.ajax({
130
            type: 'POST',
131
            url: OC.generateUrl('apps/analytics/wizard'),
132
        })
133
    }
134
}
135
136
/**
137
 * @namespace OCA.Analytics.WhatsNew
138
 */
139
OCA.Analytics.WhatsNew = {
140
141
    whatsnew: function (options) {
142
        options = options || {}
143
        $.ajax({
144
            type: 'GET',
145
            url: OC.generateUrl('apps/analytics/whatsnew'),
146
            data: {'format': 'json'},
147
            success: options.success || function (data, statusText, xhr) {
148
                OCA.Analytics.WhatsNew.show(data, statusText, xhr);
149
            },
150
        });
151
    },
152
153
    dismiss: function (version) {
154
        $.ajax({
155
            type: 'POST',
156
            url: OC.generateUrl('apps/analytics/whatsnew'),
157
            data: {version: encodeURIComponent(version)}
158
        })
159
        $('.whatsNewPopover').remove();
160
    },
161
162
    show: function (data, statusText, xhr) {
163
        if (xhr.status !== 200) {
164
            return
165
        }
166
167
        let item, menuItem, text, icon
168
169
        const div = document.createElement('div')
170
        div.classList.add('popovermenu', 'open', 'whatsNewPopover', 'menu-left')
171
172
        const list = document.createElement('ul')
173
174
        // header
175
        item = document.createElement('li')
176
        menuItem = document.createElement('span')
177
        menuItem.className = 'menuitem'
178
179
        text = document.createElement('span')
180
        text.innerText = t('core', 'New in') + ' ' + data['product']
181
        text.className = 'caption'
182
        menuItem.appendChild(text)
183
184
        icon = document.createElement('span')
185
        icon.className = 'icon-close'
186
        icon.onclick = function () {
187
            OCA.Analytics.WhatsNew.dismiss(data['version'])
188
        }
189
        menuItem.appendChild(icon)
190
191
        item.appendChild(menuItem)
192
        list.appendChild(item)
193
194
        // Highlights
195
        for (const i in data['whatsNew']['regular']) {
196
            const whatsNewTextItem = data['whatsNew']['regular'][i]
197
            item = document.createElement('li')
198
199
            menuItem = document.createElement('span')
200
            menuItem.className = 'menuitem'
201
202
            icon = document.createElement('span')
203
            icon.className = 'icon-checkmark'
204
            menuItem.appendChild(icon)
205
206
            text = document.createElement('p')
207
            text.innerHTML = _.escape(whatsNewTextItem)
208
            menuItem.appendChild(text)
209
210
            item.appendChild(menuItem)
211
            list.appendChild(item)
212
        }
213
214
        // Changelog URL
215
        if (!_.isUndefined(data['changelogURL'])) {
216
            item = document.createElement('li')
217
218
            menuItem = document.createElement('a')
219
            menuItem.href = data['changelogURL']
220
            menuItem.rel = 'noreferrer noopener'
221
            menuItem.target = '_blank'
222
223
            icon = document.createElement('span')
224
            icon.className = 'icon-link'
225
            menuItem.appendChild(icon)
226
227
            text = document.createElement('span')
228
            text.innerText = t('core', 'View changelog')
229
            menuItem.appendChild(text)
230
231
            item.appendChild(menuItem)
232
            list.appendChild(item)
233
        }
234
235
        div.appendChild(list)
236
        document.body.appendChild(div)
237
    },
238
}
239
/**
240
 * @namespace OCA.Analytics.Notification
241
 */
242
OCA.Analytics.Notification = {
243
    dialog: function (title, text, type) {
244
        OC.dialogs.message(
245
            text,
246
            title,
247
            type,
248
            OC.dialogs.OK_BUTTON,
249
            function () {
250
            },
251
            true,
252
            true
253
        );
254
    },
255
256
    confirm: function (title, text, callback) {
257
        OC.dialogs.confirmHtml(
258
            text,
259
            title,
260
            function (e) {
261
                if (e === true) {
262
                    if (typeof callback === 'function') {
263
                        callback();
264
                    }
265
                }
266
            },
267
            true
268
        );
269
    },
270
271
    notification: function (type, message) {
272
        if (parseInt(OC.config.versionstring.substr(0, 2)) >= 17) {
273
            if (type === 'success') {
274
                OCP.Toast.success(message)
275
            } else if (type === 'error') {
276
                OCP.Toast.error(message)
277
            } else {
278
                OCP.Toast.info(message)
279
            }
280
        } else {
281
            OC.Notification.showTemporary(message);
282
        }
283
    },
284
285
}
286