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

PopupStateService.js « Services « js « src - github.com/marius-wieschollek/passwords-webextension.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 24113df72dfbcbc6b64a1aa075957c5101fc4d53 (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
import MessageService from "@js/Services/MessageService";
import ErrorManager   from "@js/Manager/ErrorManager";
import EventQueue     from '@js/Event/EventQueue';

export default new class PopupStateService {

    get change() {
        return this._dataEvents;
    }

    get status() {
        return this._statusEvents;
    }

    constructor() {
        this._tab = 'related';
        this._data = {};
        this._status = {};
        this._dataEvents = new EventQueue();
        this._statusEvents = new EventQueue();

        this._defaults = {
            'search.query': ''
        };
        this._payload = null;
    }

    async init() {
        let reply  = await MessageService.send('popup.status.get'),
            status = reply.getPayload();

        this._tab = status.tab;
        this._status = status.status;
        this._data = status.data;
        this._payload = status;
    }

    /**
     * @deprecated
     * @returns {*}
     */
    getPayload() {
        return this._payload;
    }

    /**
     * @returns {String}
     */
    getTab() {
        return this._tab;
    }

    /**
     * @param {String} tab
     */
    setTab(tab) {
        this._tab = tab;
        this._saveState();
    }

    /**
     * @param {String} item
     * @returns {*}
     */
    getStatus(item) {
        return this._status.hasOwnProperty(item) ? this._status[item]:null;
    }

    /**
     * @param {String} key
     * @param {*} value
     */
    setStatus(key, value) {
        this._status[key] = [value];
        this._statusEvents.emit({key, value});
    }

    /**
     * @param {(String|String[])} key
     * @param {(String|null)} [tab=null]
     * @returns {*}
     */
    get(key, tab = null) {
        if(typeof key !== 'string') {
            let data = {};
            for(let value of key) {
                data[value] = this._getValue(value, tab);
            }

            return data;
        }
        return this._getValue(key, tab);
    }

    /**
     * @param {(String|Object)} key The key of the value to set or an object with the keys and values to set
     * @param {*} [value] The value or the tab of the key is an object
     * @param {(String|null)} [tab=null]
     * @returns void
     */
    set(key, value, tab = null) {
        if(typeof key !== 'string') {
            this._setValues(key, value === undefined ? null:value);
        } else {
            this._setValue(key, value, tab);
        }

        this._saveState();
    }

    /**
     *
     * @param key
     * @param tab
     * @returns {null|*}
     * @private
     */
    _getValue(key, tab = null) {
        if(tab === null) tab = this._tab;
        if(!this._data.hasOwnProperty(tab) || !this._data[tab].hasOwnProperty(key)) {
            let defaultKey = `${tab}.${key}`;
            if(this._defaults.hasOwnProperty(defaultKey)) {
                return this._defaults[defaultKey];
            }

            return null;
        }

        return this._data[tab][key];
    }

    /**
     *
     * @param data
     * @param tab
     * @private
     */
    _setValues(data, tab = null) {
        for(let key in data) {
            if(data.hasOwnProperty(key)) this._setValue(key, data[key], tab);
        }
    }

    /**
     *
     * @param key
     * @param value
     * @param tab
     * @returns {{}}
     * @private
     */
    _setValue(key, value, tab = null) {
        if(tab === null) tab = this._tab;
        if(!this._data.hasOwnProperty(tab)) this._data[tab] = {};
        this._data[tab][key] = value;
        this._dataEvents.emit({tab, key, value});
    }

    /**
     *
     * @private
     */
    _saveState() {
        let message = {
            type   : 'popup.status.set',
            payload: {
                service: true,
                tab    : this._tab,
                data   : this._data
            }
        };

        MessageService
            .send(message)
            .catch(ErrorManager.catch);
    }
};