sendMessage.js 4.31 KB
Newer Older
NGUYEN HOANG SON committed
1
/**
NGO THI HONG committed
2
 * Send Message js in sendMessage.html
NGUYEN HOANG SON committed
3
 *
4
 * @since cms:1.4.3.2&1.4.3.3 web:1.0
NGUYEN HOANG SON committed
5 6 7
 */
var SendMessage = {};

Kang Donghun committed
8
SendMessage.baseApiUrl = COMMON.format(ClientData.conf_checkApiUrl(), ClientData.userInfo_accountPath()) + CONSTANT.URL.CMS.API.SEND_PUSH_MESSAGE;
9
SendMessage.contentMaxLength = 207;
NGUYEN HOANG SON committed
10 11

/**
12 13
 * Get input content
 * @returns string
NGUYEN HOANG SON committed
14
 */
15
SendMessage.getCurrentMessageContent = function () {
16
    return $('#messageContent').val();
17
};
18

19 20 21 22
/**
 * Get operation id selected
 * @returns string
 */
23
SendMessage.getCurrentOperationId = function () {
24
    return $('#operationSelected').attr('data-operation-id');
25
};
26

27 28 29 30
/**
 * Get send type selected
 * @returns string
 */
31
SendMessage.getCurrentSendType = function () {
32
    return $('input[name="sendType"]:checked').val();
33
};
34

35 36 37 38
/**
 * Check data required when send message request to cms
 * @returns boolean
 */
39
SendMessage.checkValidation = function () {
40 41
    const message = SendMessage.getCurrentMessageContent();
    if (!ValidationUtil.CheckRequiredForText(message)) {
42
        COMMON.showAlert('msgContentRequired');
43 44
        return false;
    }
45
    if (!ValidationUtil.CheckMaxLengthForByte(message, SendMessage.contentMaxLength)) {
46
        COMMON.showAlert(null, {message: COMMON.format(I18N.i18nText('msgContentInvalidLength'), SendMessage.contentMaxLength)});
47
        return false;
48
    }
49 50
    const operationId = SendMessage.getCurrentOperationId();
    if (!ValidationUtil.IsNumber(operationId)) {
51
        COMMON.showAlert('msgOperationRequired');
52
        return false;
53 54 55
    }
    const sendType = SendMessage.getCurrentSendType();
    if (!ValidationUtil.IsNumber(sendType)) {
56
        COMMON.showAlert('msgSendTypeRequired');
57
        return false;
58 59 60 61
    }
    return true;
};

NGUYEN HOANG SON committed
62
/**
63
 * handle click event of send button
NGUYEN HOANG SON committed
64
 */
65
SendMessage.onClickSend = function () {
66 67 68 69 70 71
    if (!SendMessage.checkValidation()) {
        return;
    }
    const message = SendMessage.getCurrentMessageContent();
    const operationId = SendMessage.getCurrentOperationId();
    const sendType = SendMessage.getCurrentSendType();
72 73 74
    COMMON.showConfirm('msgSendPushMessageConfirm', function() {
        SendMessage.postMessage(message, operationId, sendType);
    });
75 76 77 78
};

/**
 * post message data to cms
79 80
 * @param {string} message
 * @param {long} operationId
81 82
 * @param {int} sendType - 0: Group, 1: All
 */
83
SendMessage.postMessage = function (message, operationId, sendType) {
84
    COMMON.showLoading();
NGUYEN HOANG SON committed
85 86
    let param = {
        sid: COMMON.getSid(),
87 88
        message: message,
        operationId: operationId,
89
        sendType: sendType,
NGUYEN HOANG SON committed
90
    };
91 92 93 94
    COMMON.cmsAjax(
        SendMessage.baseApiUrl,
        param,
        false,
95 96
        function (json) {
            COMMON.closeLoading();
97
            COMMON.showAlert('msgSendPushMessageSuccess');
98
        },
99 100
        function () {
            console.log('SendMessage.postMessage error');
101
            COMMON.closeLoading();
102 103
        },
    );
NGUYEN HOANG SON committed
104 105
};

Takumi Imai committed
106 107 108 109 110
/**
 * operation selected callback
 * @param {Number} operationId
 * @param {*} operationName
 */
111
SendMessage.operationSelectedCallback = function (operationId, operationName) {
112 113
    $('#operationSelected').attr('data-operation-id', operationId);
    $('#operationSelected').text(operationName);
114
};
115

Takumi Imai committed
116 117 118 119
/**
 * template selected callback
 * @param {*} template
 */
120
SendMessage.templateSelectedCallback = function (template) {
121
    $('#messageContent').val(template);
122
};
123

NGUYEN HOANG SON committed
124 125 126 127
/**
 * init data, action when screen onload
 */
SendMessage.init = function () {
128 129
    //Check if user is logged in
    COMMON.checkAuth(false);
Kang Donghun committed
130
    TEMPLATE.loadHeader('#includedHeader');
131
    TEMPLATE.loadConfirmModal('#includeConfirmModal');
132 133 134 135 136 137
    const navs = [
        {
            titleLang: 'dashboard',
            href: 'dashboard.html',
        },
        {
138
            titleLang: 'sendMessage',
139 140 141
        },
    ];
    TEMPLATE.loadMainNavsTitle('#includedMainTitle', 'sendMessage', navs, null);
142 143
    TEMPLATE.loadOperationSelect('#includeOperationSelect', SendMessage.operationSelectedCallback);
    TEMPLATE.loadNotificationSelect('#includeTemplateModal', SendMessage.templateSelectedCallback);
144
    $('#messageContent').attr('maxlength', SendMessage.contentMaxLength);
145
    //load lang for elements none class lang
146
    I18N.initi18n();
147 148
    $("label[for='sendTypeGroup']").append(I18N.i18nText('labelSendTypeGroup'));
    $("label[for='sendTypeAll']").append(I18N.i18nText('labelSendTypeAll'));
149 150

    COMMON.closeLoading();
151
};