sendMessage.js 3.83 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
        alert(I18N.i18nText('msgContentRequired'));
43 44
        return false;
    }
45 46
    if (!ValidationUtil.CheckMaxLengthForByte(message, SendMessage.contentMaxLength)) {
        alert(COMMON.format(I18N.i18nText('msgContentInvalidLength'), SendMessage.contentMaxLength));
47
        return false;
48
    }
49 50
    const operationId = SendMessage.getCurrentOperationId();
    if (!ValidationUtil.IsNumber(operationId)) {
51
        alert(I18N.i18nText('msgOperationRequired'));
52
        return false;
53 54 55
    }
    const sendType = SendMessage.getCurrentSendType();
    if (!ValidationUtil.IsNumber(sendType)) {
56
        alert(I18N.i18nText('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 75 76
    SendMessage.postMessage(message, operationId, sendType);
};

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

99
SendMessage.operationSelectedCallback = function (operationId, operationName) {
100 101
    $('#operationSelected').attr('data-operation-id', operationId);
    $('#operationSelected').text(operationName);
102
};
103

104
SendMessage.templateSelectedCallback = function (template) {
105
    $('#messageContent').val(template);
106
};
107

NGUYEN HOANG SON committed
108 109 110 111
/**
 * init data, action when screen onload
 */
SendMessage.init = function () {
112 113 114
    //Check if user is logged in
    COMMON.checkAuth(false);
    TEMPLATE.loadHearder('#includedHeader');
115 116 117 118 119 120 121 122 123
    const navs = [
        {
            titleLang: 'dashboard',
            href: 'dashboard.html',
        },
        {
            titleLang: 'sendMessageTitle',
        },
    ];
124
    TEMPLATE.loadMainNavsTitle('#includedMainTitle', 'sendMessageTitle', navs, null);
125 126
    TEMPLATE.loadOperationSelect('#includeOperationSelect', SendMessage.operationSelectedCallback);
    TEMPLATE.loadNotificationSelect('#includeTemplateModal', SendMessage.templateSelectedCallback);
127
    $('#messageContent').attr('maxlength', SendMessage.contentMaxLength);
128
    //load lang for elements none class lang
129
    I18N.initi18n();
130 131
    $("label[for='sendTypeGroup']").append(I18N.i18nText('labelSendTypeGroup'));
    $("label[for='sendTypeAll']").append(I18N.i18nText('labelSendTypeAll'));
132
};