Project

General

Profile

1
var $chatbox_wrapper = $('#chatbox_wrapper');
2

    
3
$(function() {
4
    $body.addClass('sidebar_secondary_active sidebar_secondary_persisten');
5

    
6
    altair_chat_alt.chat_list();
7
    altair_chat_alt.chatboxes();
8
});
9

    
10
altair_chat_alt = {
11
    chat_list: function() {
12
        var source = $("#chatbox_template").html();
13
        // show chatbox
14
        $('#chatboxes').on('click','> li', function() {
15
            var $this = $(this),
16
                isActive = $this.hasClass('chatbox_active');
17

    
18
            if(!isActive) {
19
                $this.addClass('chatbox_active');
20
                var context = {
21
                    username: $(this).attr('data-user'),
22
                    conversation: [
23
                        {
24
                            avatarUrl: 'assets/img/avatars/'+$(this).attr('data-user-avatar')+'_tn.png',
25
                            messages: [
26
                                {
27
                                    time: 1473940165000,
28
                                    text: 'lorem ipsum dolor sit amet...'
29
                                },
30
                                {
31
                                    time: 1473940165000,
32
                                    text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Enim, est, quasi. Accusamus adipisci consequuntur exercitationem inventore itaque'
33
                                },
34
                                {
35
                                    time: 1473940165000,
36
                                    text: 'Enim, est, quasi. Accusamus adipisci consequuntur exercitationem inventore itaque'
37
                                }
38
                            ]
39
                        },
40
                        {
41
                            own: true,
42
                            messages: [
43
                                {
44
                                    time: 1473940165000,
45
                                    text: 'Accusamus adipisci consequuntur exercitationem inventore itaque'
46
                                },
47
                                {
48
                                    time: 1473940165000,
49
                                    text: 'consequuntur exercitationem inventore itaque'
50
                                },
51
                                {
52
                                    time: 1473940165000,
53
                                    text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.'
54
                                }
55
                            ]
56
                        }
57
                    ]
58
                },
59
                template = Handlebars.compile(source);
60

    
61
                Handlebars.registerPartial("conversation", $("#chatbox_conversation").html());
62
                Handlebars.registerPartial("messages", $("#chatbox_messages").html());
63

    
64
                var html = template(context);
65
                $chatbox_wrapper.prepend(html);
66

    
67
                var $chatbox =  $chatbox_wrapper.children('.chatbox:first-child'),
68
                    $chatbox_content = $chatbox.find('.chatbox_content');
69

    
70
                $chatbox_content.scrollTop($chatbox_content[0].scrollHeight);
71

    
72
                //$chatbox_content.height($chatbox.closest('.chatbox').height() - $chatbox.find('.chatbox_header').height() - $chatbox.find('.chatbox_footer').height() );
73

    
74
                // message input autosize
75
                var $messageInput = $chatbox_wrapper.find('.message_input');
76
                autosize($messageInput);
77

    
78
                $messageInput.on('autosize:resized', function(){
79
                    $chatbox.css({
80
                        'padding-bottom': $messageInput.outerHeight()
81
                    })
82
                });
83

    
84
                if (window.matchMedia("(max-width: 767px)").matches) {
85
                    altair_secondary_sidebar.hide_sidebar();
86
                }
87

    
88
            }
89
        });
90
    },
91
    chatboxes: function () {
92

    
93
        // disable page scroll when scrolling chatbox content
94
        $chatbox_wrapper.on( 'mousewheel DOMMouseScroll', '.chatbox_content', function (e) {
95
            var e0 = e.originalEvent;
96
            var delta = e0.wheelDelta || -e0.detail;
97
            this.scrollTop += ( delta < 0 ? 1 : -1 ) * 30;
98
            e.preventDefault();
99
        });
100

    
101
        // make chatbox active
102
        $chatbox_wrapper.on('click','.chatbox',function(e) {
103
            e.preventDefault();
104
            if($(e.target).closest('.chatbox_close').length) {
105
                return;
106
            }
107
            $chatbox_wrapper.find('.cb_active').not($(this)).removeClass('cb_active');
108
            var $chatbox = $(this);
109
            if(!$chatbox.hasClass('cb_active')) {
110
                $chatbox.addClass('cb_active');
111
                if(!$(e.target).closest('.actions_dropdown').length) {
112
                    $chatbox.find('.message_input').focus();
113
                }
114
            }
115
        });
116
        // make chatbox inactive
117
        $document.on('click',function(e) {
118
            if(!$(e.target).closest('.chatbox').length) {
119
                $chatbox_wrapper.find('.cb_active').removeClass('cb_active');
120
            }
121
        });
122

    
123
        // close chatbox
124
        $chatbox_wrapper.on('click','.chatbox_close',function(e) {
125
            e.preventDefault();
126

    
127
            var $chatbox = $(this).closest('.chatbox').addClass('removing'),
128
                user = $chatbox.attr('data-user');
129
            setTimeout(function() {
130
                $('#chatboxes').children('li[data-user="'+user+'"]').removeClass('chatbox_active');
131
                $chatbox.remove();
132
            },280)
133
        });
134

    
135
        // send message
136
        $chatbox_wrapper.on('keyup','.message_input',function(e) {
137
            var $this = $(this);
138
            e.preventDefault();
139
            var code = e.keyCode || e.which;
140
            if(code == 13 && $this.val() != '') {
141
                var $chatbox = $(this).closest('.chatbox'),
142
                    $chatbox_content = $chatbox.find('.chatbox_content'),
143
                    conversation_template = Handlebars.compile($("#chatbox_conversation").html());
144
                    messages_template = Handlebars.compile($("#chatbox_messages").html());
145

    
146
                var context = {
147
                    conversation: [
148
                        {
149
                            own: true,
150
                            messages: [
151
                                {
152
                                    time: new Date().getTime(),
153
                                    text: $this.val()
154
                                }
155
                            ]
156
                        }
157
                    ]
158
                };
159

    
160
                var conversation = conversation_template(context),
161
                    messages = messages_template(context.conversation[0]);
162

    
163
                if(!$chatbox_content.children('.chatbox_message:last-child').hasClass('own')) {
164
                    $chatbox_content.append(conversation);
165
                } else {
166
                    $chatbox_content.children('.chatbox_message:last-child').children('ul').append(messages);
167
                }
168

    
169
                $(this).val('');
170

    
171
                $chatbox_content.scrollTop($chatbox_content[0].scrollHeight);
172

    
173
            }
174
        });
175

    
176
    }
177
};
(39-39/114)