summaryrefslogtreecommitdiffstats
path: root/src/js/cloud-ui.js
blob: 228f1140b65197e6aa341ba9a5847a508d12ee5d (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
/*******************************************************************************

    uBlock Origin - a comprehensive, efficient content blocker
    Copyright (C) 2015-2018 Raymond Hill

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see {http://www.gnu.org/licenses/}.

    Home: https://github.com/gorhill/uBlock
*/

/* global faIconsInit */

'use strict';

import { i18n, i18n$ } from './i18n.js';
import { dom, qs$ } from './dom.js';
import { faIconsInit } from './fa-icons.js';

/******************************************************************************/

(( ) => {

/******************************************************************************/

self.cloud = {
    options: {},
    datakey: '',
    data: undefined,
    onPush: null,
    onPull: null,
};

/******************************************************************************/

const widget = qs$('#cloudWidget');
if ( widget === null ) { return; }

self.cloud.datakey = dom.attr(widget, 'data-cloud-entry') || '';
if ( self.cloud.datakey === '' ) { return; }

/******************************************************************************/

const fetchStorageUsed = async function() {
    let elem = qs$(widget, '#cloudCapacity');
    if ( dom.cl.has(elem, 'hide') ) { return; }
    const result = await vAPI.messaging.send('cloudWidget', {
        what: 'cloudUsed',
        datakey: self.cloud.datakey,
    });
    if ( result instanceof Object === false ) {
        dom.cl.add(elem, 'hide');
        return;
    }
    const units = ' ' + i18n$('genericBytes');
    elem.title = result.max.toLocaleString() + units;
    const total = (result.total / result.max * 100).toFixed(1);
    elem = elem.firstElementChild;
    elem.style.width = `${total}%`;
    elem.title = result.total.toLocaleString() + units;
    const used = (result.used / result.total * 100).toFixed(1);
    elem = elem.firstElementChild;
    elem.style.width = `${used}%`;
    elem.title = result.used.toLocaleString() + units;
};

/******************************************************************************/

const fetchCloudData = async function() {
    const info = qs$(widget, '#cloudInfo');

    const entry = await vAPI.messaging.send('cloudWidget', {
        what: 'cloudPull',
        datakey: self.cloud.datakey,
    });

    const hasData = entry instanceof Object;
    if ( hasData === false ) {
        dom.attr('#cloudPull', 'disabled', '');
        dom.attr('#cloudPullAndMerge', 'disabled', '');
        info.textContent = '...\n...';
        return entry;
    }

    self.cloud.data = entry.data;

    dom.attr('#cloudPull', 'disabled', null);
    dom.attr('#cloudPullAndMerge', 'disabled', null);

    const timeOptions = {
        weekday: 'short',
        year: 'numeric',
        month: 'short',
        day: 'numeric',
        hour: 'numeric',
        minute: 'numeric',
        second: 'numeric',
        timeZoneName: 'short'
    };

    const time = new Date(entry.tstamp);
    info.textContent =
        entry.source + '\n' +
        time.toLocaleString('fullwide', timeOptions);
};

/******************************************************************************/

const pushData = async function() {
    if ( typeof self.cloud.onPush !== 'function' ) { return; }

    const error = await vAPI.messaging.send('cloudWidget', {
        what: 'cloudPush',
        datakey: self.cloud.datakey,
        data: self.cloud.onPush(),
    });
    const failed = typeof error === 'string';
    dom.cl.toggle('#cloudPush', 'error', failed);
    dom.text('#cloudError', failed ? error : '');
    if ( failed ) { return; }
    fetchCloudData();
    fetchStorageUsed();
};

/******************************************************************************/

const pullData = function() {
    if ( typeof self.cloud.onPull === 'function' ) {
        self.cloud.onPull(self.cloud.data, false);
    }
    dom.cl.remove('#cloudPush', 'error');
    dom.text('#cloudError', '');
};

/******************************************************************************/

const pullAndMergeData = function() {
    if ( typeof self.cloud.onPull === 'function' ) {
        self.cloud.onPull(self.cloud.data, true);
    }
};

/******************************************************************************/

const openOptions = function() {
    const input = qs$('#cloudDeviceName');
    input.value = self.cloud.options.deviceName;
    dom.attr(input, 'placeholder', self.cloud.options.defaultDeviceName);
    dom.cl.add('#cloudOptions', 'show');
};

/******************************************************************************/

const closeOptions = function(ev) {
    const root = qs$('#cloudOptions');
    if ( ev.target !== root ) { return; }
    dom.cl.remove(root, 'show');
};

/******************************************************************************/

const submitOptions = async function() {
    dom.cl.remove('#cloudOptions', 'show');

    const options = await vAPI.messaging.send('cloudWidget', {
        what: 'cloudSetOptions',
        options: {
            deviceName: qs$('#cloudDeviceName').value
        },
    });
    if ( options instanceof Object ) {
        self.cloud.options = options;
    }
};

/******************************************************************************/

const onInitialize = function(options) {
    if ( options instanceof Object === false ) { return; }
    if ( options.enabled !== true ) { return; }
    self.cloud.options = options;

    const xhr = new XMLHttpRequest();
    xhr.open('GET', 'cloud-ui.html', true);
    xhr.overrideMimeType('text/html;charset=utf-8');
    xhr.responseType = 'text';
    xhr.onload = function() {
        this.onload = null;
        const parser = new DOMParser(),
            parsed = parser.parseFromString(this.responseText, 'text/html'),
            fromParent = parsed.body;
        while ( fromParent.firstElementChild !== null ) {
            widget.appendChild(
                document.adoptNode(fromParent.firstElementChild)
            );
        }

        faIconsInit(widget);

        i18n.render(widget);
        dom.cl.remove(widget, 'hide');

        dom.on('#cloudPush', 'click', ( ) => { pushData(); });
        dom.on('#cloudPull', 'click', pullData);
        dom.on('#cloudPullAndMerge', 'click', pullAndMergeData);
        dom.on('#cloudCog', 'click', openOptions);
        dom.on('#cloudOptions', 'click', closeOptions);
        dom.on('#cloudOptionsSubmit', 'click', ( ) => { submitOptions(); });

        fetchCloudData().then(result => {
            if ( typeof result !== 'string' ) { return; }
            dom.cl.add('#cloudPush', 'error');
            dom.text('#cloudError', result);
        });
        fetchStorageUsed();
    };
    xhr.send();
};

vAPI.messaging.send('cloudWidget', {
    what: 'cloudGetOptions',
}).then(options => {
    onInitialize(options);
});

/******************************************************************************/

})();