dashboardSetting.js 5.31 KB
Newer Older
1 2 3
/**
 * Dashboard Setting js in dashboard.html
 *
4
 * @since cms:1.4.3.2&1.4.3.3 web:1.0
5
 */
6
var DashboardSetting = {};
7

8
DashboardSetting.changeSettingCallback = function () {};
9

NGUYEN HOANG SON committed
10 11 12
/**
 * Html element array, map with json key from setting data API
 */
13 14 15
DashboardSetting.elementItems = {
    newReport: { id: 'settingNewReport', name: 'chk-new', enabled: true },
    continousWork: { id: 'settingContinousWork', name: 'chk-proccess', enabled: true },
16
    warningReport: { id: 'settingWarningReport', name: 'chk-alert', enabled: true },
17 18
    dashboardHome: { id: 'settingDashboardHome', name: 'chk-home', enabled: false },
};
19

Kang Donghun committed
20
DashboardSetting.baseApiUrl = COMMON.format(ClientData.conf_checkApiUrl(), ClientData.userInfo_accountPath()) + CONSTANT.URL.CMS.API.DASHBOARD_SETTING;
21

NGUYEN HOANG SON committed
22
/**
23
 * default setting
NGUYEN HOANG SON committed
24
 */
25
DashboardSetting.defaultSetting = {
26 27
    newReport: 1,
    continousWork: 1,
28
    warningReport: 1,
29
    dashboardHome: 0,
30
};
31

32 33 34 35
/** dummy setting json reponse */
DashboardSetting.dummySettingJson = {
    dashboardSettingList: [
        {
36 37 38
            id: 1,
            settingName: 'New Report',
            settingValue: 1,
39 40
        },
        {
41 42 43
            id: 2,
            settingName: 'Continous Work',
            settingValue: 1,
44 45
        },
        {
46 47 48
            id: 3,
            settingName: 'Warning Report',
            settingValue: 1,
49 50
        },
        {
51 52 53 54
            id: 4,
            settingName: 'Dashboard Home',
            settingValue: 0,
        },
55
    ],
56
};
57

NGUYEN HOANG SON committed
58 59
/**
 * get setting data from cms
60
 * @param {function} callback - callback with settings
NGUYEN HOANG SON committed
61
 */
62 63 64 65
DashboardSetting.getSettingData = function (callback) {
    let param = {
        sid: COMMON.getSid(),
    };
66
    const url = DashboardSetting.baseApiUrl;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    COMMON.cmsAjax(
        url,
        param,
        false,
        function (json) {
            let settings = {};
            //map id with key when save setting
            const mapKeys = {
                1: 'newReport',
                2: 'continousWork',
                3: 'warningReport',
                4: 'dashboardHome',
            };
            if (json && json.dashboardSettingList) {
                for (const item of json.dashboardSettingList) {
                    if (mapKeys.hasOwnProperty(item.id)) {
                        const settingKey = mapKeys[item.id];
                        settings[settingKey] = item.settingValue;
                    }
86 87
                }
            }
88 89 90 91 92 93 94 95
            if (callback) {
                callback(settings);
            }
        },
        function () {
            console.log('DashboardSetting.getSettingData error');
        },
    );
96 97
};

NGUYEN HOANG SON committed
98 99
/**
 * apply settings to screen
100
 * @param {JSON} settings
NGUYEN HOANG SON committed
101
 */
102
DashboardSetting.applySettings = function (settings) {
103 104 105
    for (const key in settings) {
        const enabled = settings[key];
        if (DashboardSetting.elementItems.hasOwnProperty(key)) {
106
            //apply enabled, disabled setting
107
            let elementItem = DashboardSetting.elementItems[key];
108 109 110 111 112 113 114 115
            elementItem.enabled = enabled;
            let element = $('#' + elementItem.id);
            if (element) {
                if (enabled) {
                    element.addClass('checked');
                } else {
                    element.removeClass('checked');
                }
116 117
            }
        }
118
    }
119 120
};

NGUYEN HOANG SON committed
121 122 123
/**
 * post request to save setting to cms
 */
124
DashboardSetting.saveSetting = function () {
125 126
    let param = {};
    param.sid = COMMON.getSid();
127 128
    for (const key in DashboardSetting.elementItems) {
        const element = DashboardSetting.elementItems[key];
129
        param[key] = element.enabled ? 1 : 0;
130
    }
Kang Donghun committed
131
    const url = COMMON.format(ClientData.conf_checkApiUrl(), ClientData.userInfo_accountPath()) + CONSTANT.URL.CMS.API.SAVE_DASHBOARD_SETTING;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    COMMON.cmsAjax(
        url,
        param,
        false,
        function (json) {
            DashboardSetting.closeModal();
            if (DashboardSetting.changeSettingCallback && typeof DashboardSetting.changeSettingCallback === 'function') {
                DashboardSetting.changeSettingCallback();
            }
        },
        function () {
            console.log('DashboardSetting.saveSetting error');
            DashboardSetting.closeModal();
        },
    );
147 148
};

NGUYEN HOANG SON committed
149 150 151
/**
 * handle on/off setting
 */
152 153 154
DashboardSetting.bindToggleClick = function () {
    $('.toggle').on('click', function () {
        $(this).toggleClass('checked');
155
        const key = $(this).attr('data-key');
156
        let enabled = false;
157 158 159
        if ($(this).hasClass('checked')) {
            enabled = true;
        }
160
        if (DashboardSetting.elementItems.hasOwnProperty(key)) {
161
            let elementItem = DashboardSetting.elementItems[key];
162 163
            elementItem.enabled = enabled;
        }
164 165 166
    });
};

NGUYEN HOANG SON committed
167 168
/**
 * init data, action when screen onload
169
 * @param {function} changeCallback - callback when settings changed
NGUYEN HOANG SON committed
170
 */
171
DashboardSetting.init = function (changeCallback) {
172
    DashboardSetting.getSettingData(function (settings) {
173
        DashboardSetting.applySettings(settings);
174
    });
175
    DashboardSetting.bindToggleClick();
176
    DashboardSetting.changeSettingCallback = changeCallback;
177 178
};

NGUYEN HOANG SON committed
179 180 181
/**
 * handle click event of save button
 */
182
DashboardSetting.onClickSave = function () {
183 184 185
    DashboardSetting.saveSetting();
};

NGUYEN HOANG SON committed
186 187 188
/**
 * close setting dialog
 */
189 190
DashboardSetting.closeModal = function () {
    $('#dashboard-setting-modal .close').click();
191
};