chamilo/chamilo-lms

View on GitHub
src/CoreBundle/Resources/views/Chat/chat.html.twig

Summary

Maintainability
Test Coverage
{% extends "@ChamiloCore/Layout/no_layout.html.twig" %}

{% block content %}
    {% autoescape false %}
<div class="page-chat">
    <div class="row">
        <div class="col-sm-4 col-md-5 col-lg-4">
            <ul class="row list-unstyled" id="chat-users"></ul>
        </div>
        <div class="col-sm-8 col-md-7 col-lg-8">
            <div id="chat-tabs">
                <ul class="nav nav-tabs" role="tablist">
                    {% if not restrict_to_coach %}
                        <li role="presentation" class="active">
                            <a href="#all" aria-controls="all" role="tab" data-toggle="tab">{{ 'All'|get_lang }}</a>
                        </li>
                    {% endif %}
                </ul>
                <div class="tab-content">
                    <div role="tabpanel" class="tab-pane active" id="{{ restrict_to_coach ? '' : 'all' }}">
                        <div class="course-chat chat-history" id="{{ restrict_to_coach ? '' : 'chat-history' }}"></div>
                    </div>
                </div>
            </div>
            <div class="profile row">
                <div class="col-xs-12">
                    <div class="message-form-chat">
                        <div class="tabbable">
                            <ul class="nav nav-tabs">
                                <li class="active">
                                    <a href="#tab1" data-toggle="tab">{{ 'Write'|get_lang }}</a>
                                </li>
                                <li>
                                    <a href="#tab2" id="preview" data-toggle="tab">{{ 'Preview'|get_lang }}</a>
                                </li>
                                <li>
                                    <button id="emojis" class="btn btn-link" type="button">
                                        <span class="sr-only">{{ 'Emoji'|get_lang }}</span>{{ emoji_smile }}
                                    </button>
                                </li>
                            </ul>
                            <div class="tab-content">
                                <div class="tab-pane active" id="tab1">
                                    <div class="row">
                                        <div class="col-sm-9">
                                            <span class="sr-only">{{ 'Message'|get_lang }}</span>
                                            <textarea id="chat-writer" name="message"></textarea>
                                        </div>
                                        <div class="col-sm-3">
                                            <button id="chat-send-message" type="button" {{ restrict_to_coach ? 'disabled' : '' }}
                                                    class="btn btn--primary">{{ 'Send'|get_lang }}</button>
                                        </div>
                                    </div>
                                </div>
                                <div class="tab-pane" id="tab2">
                                    <div id="html-preview" class="emoji-wysiwyg-editor-preview"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<audio id="chat-alert" class="skip">
    <source src="{{ asset('sound/notification.wav') }}" type="audio/wav"></source>
    <source src="{{ asset('chat/sound/notification.ogg') }}" type="audio/ogg"></source>
    <source src="{{ asset('chat/sound/notification.mp3') }}" type="audio/mpeg"></source>
</audio>
<script>
    $(function () {
        var ChChat = {
            _ajaxUrl: '{{ url('chat_ajax') ~ '?' ~ course_url_params }}',
            _historySize: -1,
            usersOnline: 0,
            currentFriend: 0,
            call: false,
            track: function () {
                return $
                    .get(ChChat._ajaxUrl, {
                        action: 'track',
                        size: ChChat._historySize,
                        users_online: ChChat.usersOnline,
                        friend: ChChat.currentFriend
                    })
                    .done(function (response) {
                        if (response.data.history) {
                            ChChat._historySize = response.data.oldFileSize;
                            ChChat.setHistory(response.data.history);
                        }

                        if (response.data.userList) {
                            ChChat.usersOnline = response.data.usersOnline;
                            ChChat.setConnectedUsers(response.data.userList);
                        }
                    });
            },
            setHistory: function (messageList) {
                var chatHistoryContainer = ChChat.currentFriend ? ('#chat-history-' + ChChat.currentFriend) : '#chat-history';

                $(chatHistoryContainer)
                    .html(messageList)
                    .prop('scrollTop', function () {
                        return this.scrollHeight;
                    });
                //$('#chat-alert').get(0).play();
            },
            setConnectedUsers: function (userList) {
                var html = '';

                userList.forEach(function (user) {
                    var buttonStatus = user.isConnected ? 'success' : 'muted',
                        buttonTitle = user.isConnected ? '{{ 'StartAChat'|get_lang }}' : '{{ 'LeaveAMessage'|get_lang }}';

                    html += '<li class="col-xs-12">' +
                        '   <div class="chat-user" data-name="' + user.complete_name + '" data-user="' + user.user_id + '">' +
                        '       <img src="' + user.image_url + '" alt="' + user.complete_name + '" class="img-circle user-image-chat"/>' +
                        '       <ul class="list-unstyled">' +
                        '           <li>' + user.complete_name;

                    if (user.user_id != {{ user.user_id }}) {
                        html += '           <button type="button" class="btn btn-link btn-xs" title="' + buttonTitle + '" data-name="' + user.complete_name + '" data-user="' + user.user_id + '">' +
                            '               <i class="fa fa-comments text-' + buttonStatus + '"></i><span class="sr-only">' + buttonTitle + '</span>' +
                            '           </button>';
                    }
                    html += '           </li>' +
                            {% if chamilo_settings_get('profile.hide_username_in_course_chat') == 'false' %}
                        '           <li><small>' + user.username + '</small></li>' +
                            {% else %}
                        '           <li>&nbsp;</li>' +
                            {% endif %}
                        '       </ul>' +
                        '   </div>' +
                        '</li>';
                });

                $('#chat-users').html(html);
            },
            onPreviewListener: function () {
                $.post(ChChat._ajaxUrl, {
                    action: 'preview',
                    'message': $('textarea#chat-writer').val()
                })
                .done(function (response) {
                    if (!response.status) {
                        return;
                    }

                    $('#html-preview').html(response.data.message);
                });
            },
            onSendMessageListener: function (e) {
                e.preventDefault();

                if (!$('textarea#chat-writer').val().trim().length) {
                    return;
                }

                var self = this;
                self.disabled = true;

                $.get(ChChat._ajaxUrl, {
                    action: 'write',
                    message: $('textarea#chat-writer').val(),
                    friend: ChChat.currentFriend
                })
                .done(function (response) {
                    self.disabled = false;

                    if (!response.status) {
                        return;
                    }

                    $('textarea#chat-writer').val('');
                    $(".emoji-wysiwyg-editor").html('');
                });
            },
            onResetListener: function (e) {
                if (!confirm("{{ 'ConfirmReset'|get_lang }}")) {
                    e.preventDefault();
                    return;
                }

                $.get(ChChat._ajaxUrl, {
                    action: 'reset',
                    friend: ChChat.currentFriend
                })
                .done(function (response) {
                    if (!response.status) {
                        return;
                    }

                    ChChat.setHistory(response.data);
                });
            },
            init: function () {
                ChChat.track().done(function () {
                    ChChat.init();
                });
            }
        };

        hljs.initHighlightingOnLoad();

        $('body').on('click', '#chat-reset', ChChat.onResetListener);
        $('#preview').on('click', ChChat.onPreviewListener);
        $('#emojis').on('click', function () {
            $('[data-toggle="tab"][href="#tab1"]').show().tab('show');
        });

        $('button#chat-send-message').on('click', ChChat.onSendMessageListener);
        $('#chat-users').on('click', 'div.chat-user', function (e) {
            e.preventDefault();
            var jSelf = $(this),
                userId = parseInt(jSelf.data('user')) || 0;

            if (!userId) {
                return;
            }

            var exists = false;

            $('#chat-tabs ul.nav li').each(function (i, el) {
                if ($(el).data('user') == userId) {
                    exists = true;
                }
            });

            $('button#chat-send-message').prop('disabled', false);

            if (exists) {
                $('#chat-tab-' + userId).tab('show');

                return;
            }

            $('#chat-tabs ul.nav-tabs').append('\
                <li role="presentation" data-user="' + userId + '">\
                    <a id="chat-tab-' + userId + '" href="#chat-' + userId + '" aria-controls="chat-' + userId + '" role="tab" data-toggle="tab">' + jSelf.data('name') + '</a>\
                </li>\
            ');

            $('#chat-tabs .tab-content').append('\
                <div role="tabpanel" class="tab-pane" id="chat-' + userId + '">\
                    <div class="course-chat chat-history" id="chat-history-' + userId + '"></div>\
                </div>\
            ');

            $('#chat-tab-' + userId).tab('show');
        });

        $('#chat-tabs ul.nav-tabs').on('shown.bs.tab', 'li a', function (e) {
            var jSelf = $(this);
            var userId = parseInt(jSelf.parent().data('user')) || 0;
            if (!userId) {
                ChChat.currentFriend = 0;

                return;
            }

            ChChat.currentFriend = userId;

            $(this).tab('show');
        });

        $('.emoji-wysiwyg-editor').on('keyup', function (e) {
            if (e.ctrlKey && e.keyCode === 13) {
                $('button#chat-send-message').trigger('click');
            }
        });

        ChChat.init();
    });
</script>

{% endautoescape %}
{% endblock %}