summaryrefslogtreecommitdiffstats
path: root/web/react/stores/config_store.jsx
blob: 7ff177b35fa37ebf5eb7d0c772680374e45d0ab9 (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
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

var AppDispatcher = require('../dispatcher/app_dispatcher.jsx');
var EventEmitter = require('events').EventEmitter;
var assign = require('object-assign');

var BrowserStore = require('../stores/browser_store.jsx');

var Constants = require('../utils/constants.jsx');
var ActionTypes = Constants.ActionTypes;

var CHANGE_EVENT = 'change';

var ConfigStore = assign({}, EventEmitter.prototype, {
    emitChange: function emitChange() {
        this.emit(CHANGE_EVENT);
    },
    addChangeListener: function addChangeListener(callback) {
        this.on(CHANGE_EVENT, callback);
    },
    removeChangeListener: function removeChangeListener(callback) {
        this.removeListener(CHANGE_EVENT, callback);
    },
    getSetting: function getSetting(key, defaultValue) {
        return BrowserStore.getItem('config_' + key, defaultValue);
    },
    getSettingAsBoolean: function getSettingAsNumber(key, defaultValue) {
        var value = ConfigStore.getSetting(key, defaultValue);

        if (typeof value !== 'string') {
            return !!value;
        } else {
            return value === 'true';
        }
    },
    updateStoredSettings: function updateStoredSettings(settings) {
        for (var key in settings) {
            BrowserStore.setItem('config_' + key, settings[key]);
        }
    }
});

ConfigStore.dispatchToken = AppDispatcher.register(function registry(payload) {
    var action = payload.action;

    switch (action.type) {
        case ActionTypes.RECIEVED_CONFIG:
            ConfigStore.updateStoredSettings(action.settings);
            ConfigStore.emitChange();
            break;
        default:
    }
});

module.exports = ConfigStore;