Welcome to mirror list, hosted at ThFree Co, Russian Federation.

room.js « pane « view « src - github.com/candy-chat/candy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5eced80366a1421d3b095199489ad30eef9a02a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/** File: room.js
 * Candy - Chats are not dead yet.
 *
 * Authors:
 *   - Patrick Stadler <patrick.stadler@gmail.com>
 *   - Michael Weibel <michael.weibel@gmail.com>
 *
 * Copyright:
 *   (c) 2011 Amiado Group AG. All rights reserved.
 *   (c) 2012-2014 Patrick Stadler & Michael Weibel. All rights reserved.
 *   (c) 2015 Adhearsion Foundation Inc <info@adhearsion.com>. All rights reserved.
 */
'use strict';

/* global Candy, Mustache, Strophe, jQuery */

/** Class: Candy.View.Pane
 * Candy view pane handles everything regarding DOM updates etc.
 *
 * Parameters:
 *   (Candy.View.Pane) self - itself
 *   (jQuery) $ - jQuery
 */
Candy.View.Pane = (function(self, $) {

  /** Class: Candy.View.Pane.Room
   * Everything which belongs to room view things belongs here.
   */
  self.Room = {
    /** Function: init
     * Initialize a new room and inserts the room html into the DOM
     *
     * Parameters:
     *   (String) roomJid - Room JID
     *   (String) roomName - Room name
     *   (String) roomType - Type: either "groupchat" or "chat" (private chat)
     *
     * Uses:
     *   - <Candy.Util.jidToId>
     *   - <Candy.View.Pane.Chat.addTab>
     *   - <getPane>
     *
     * Triggers:
     *   candy:view.room.after-add using {roomJid, type, element}
     *
     * Returns:
     *   (String) - the room id of the element created.
     */
    init: function(roomJid, roomName, roomType) {
      roomType = roomType || 'groupchat';
      roomJid = Candy.Util.unescapeJid(roomJid);

      var evtData = {
        roomJid: roomJid,
        type: roomType
      };
      /** Event: candy:view.room.before-add
       * Before initialising a room
       *
       * Parameters:
       *   (String) roomJid - Room JID
       *   (String) type - Room Type
       *
       * Returns:
       *   Boolean - if you don't want to initialise the room, return false.
       */
      if($(Candy).triggerHandler('candy:view.room.before-add', evtData) === false) {
        return false;
      }

      // First room, show sound control
      if(Candy.Util.isEmptyObject(self.Chat.rooms)) {
        self.Chat.Toolbar.show();
      }

      var roomId = Candy.Util.jidToId(roomJid);
      self.Chat.rooms[roomJid] = {id: roomId, usercount: 0, name: roomName, type: roomType, messageCount: 0, scrollPosition: -1, targetJid: roomJid};

      $('#chat-rooms').append(Mustache.to_html(Candy.View.Template.Room.pane, {
        roomId: roomId,
        roomJid: roomJid,
        roomType: roomType,
        form: {
          _messageSubmit: $.i18n._('messageSubmit')
        },
        roster: {
          _userOnline: $.i18n._('userOnline')
        }
      }, {
        roster: Candy.View.Template.Roster.pane,
        messages: Candy.View.Template.Message.pane,
        form: Candy.View.Template.Room.form
      }));
      self.Chat.addTab(roomJid, roomName, roomType);
      self.Room.getPane(roomJid, '.message-form').submit(self.Message.submit);
      self.Room.scrollToBottom(roomJid);

      evtData.element = self.Room.getPane(roomJid);

      /** Event: candy:view.room.after-add
       * After initialising a room
       *
       * Parameters:
       *   (String) roomJid - Room JID
       *   (String) type - Room Type
       *   (jQuery.Element) element - Room element
       */
      $(Candy).triggerHandler('candy:view.room.after-add', evtData);

      return roomId;
    },

    /** Function: show
     * Show a specific room and hides the other rooms (if there are any)
     *
     * Parameters:
     *   (String) roomJid - room jid to show
     *
     * Triggers:
     *   candy:view.room.after-show using {roomJid, element}
     *   candy:view.room.after-hide using {roomJid, element}
     */
    show: function(roomJid) {
      var roomId = self.Chat.rooms[roomJid].id,
        evtData;

      $('.room-pane').each(function() {
        var elem = $(this);
        evtData = {
          'roomJid': elem.attr('data-roomjid'),
          'type': elem.attr('data-roomtype'),
          'element' : elem
        };

        if(elem.attr('id') === ('chat-room-' + roomId)) {
          elem.show();
          Candy.View.getCurrent().roomJid = roomJid;
          self.Chat.setActiveTab(roomJid);
          self.Chat.Toolbar.update(roomJid);
          self.Chat.clearUnreadMessages(roomJid);
          self.Room.setFocusToForm(roomJid);
          self.Room.scrollToBottom(roomJid);

          /** Event: candy:view.room.after-show
           * After showing a room
           *
           * Parameters:
           *   (String) roomJid - Room JID
           *   (String) type - Room Type
           *   (jQuery.Element) element - Room element
           */
          $(Candy).triggerHandler('candy:view.room.after-show', evtData);

        } else {
          elem.hide();

          /** Event: candy:view.room.after-hide
           * After hiding a room
           *
           * Parameters:
           *   (String) roomJid - Room JID
           *   (String) type - Room Type
           *   (jQuery.Element) element - Room element
           */
          $(Candy).triggerHandler('candy:view.room.after-hide', evtData);
        }
      });
    },

    /** Function: setSubject
     * Called when someone changes the subject in the channel
     *
     * Triggers:
     *   candy:view.room.after-subject-change using {roomJid, element, subject}
     *
     * Parameters:
     *   (String) roomJid - Room Jid
     *   (String) subject - The new subject
     */
    setSubject: function(roomJid, subject) {
      subject = Candy.Util.Parser.linkify(Candy.Util.Parser.escape(subject));
      var timestamp = new Date();
      var html = Mustache.to_html(Candy.View.Template.Room.subject, {
        subject: subject,
        roomName: self.Chat.rooms[roomJid].name,
        _roomSubject: $.i18n._('roomSubject'),
        time: Candy.Util.localizedTime(timestamp),
        timestamp: timestamp.toISOString()
      });
      self.Room.appendToMessagePane(roomJid, html);
      self.Room.scrollToBottom(roomJid);

      /** Event: candy:view.room.after-subject-change
       * After changing the subject of a room
       *
       * Parameters:
       *   (String) roomJid - Room JID
       *   (jQuery.Element) element - Room element
       *   (String) subject - New subject
       */
      $(Candy).triggerHandler('candy:view.room.after-subject-change', {
        'roomJid': roomJid,
        'element' : self.Room.getPane(roomJid),
        'subject' : subject
      });
    },

    /** Function: close
     * Close a room and remove everything in the DOM belonging to this room.
     *
     * NOTICE: There's a rendering bug in Opera when all rooms have been closed.
     *         (Take a look in the source for a more detailed description)
     *
     * Triggers:
     *   candy:view.room.after-close using {roomJid}
     *
     * Parameters:
     *   (String) roomJid - Room to close
     */
    close: function(roomJid) {
      self.Chat.removeTab(roomJid);
      self.Window.clearUnreadMessages();

      /* TODO:
        There's a rendering bug in Opera which doesn't redraw (remove) the message form.
        Only a cosmetical issue (when all tabs are closed) but it's annoying...
        This happens when form has no focus too. Maybe it's because of CSS positioning.
      */
      self.Room.getPane(roomJid).remove();
      var openRooms = $('#chat-rooms').children();
      if(Candy.View.getCurrent().roomJid === roomJid) {
        Candy.View.getCurrent().roomJid = null;
        if(openRooms.length === 0) {
          self.Chat.allTabsClosed();
        } else {
          self.Room.show(openRooms.last().attr('data-roomjid'));
        }
      }
      delete self.Chat.rooms[roomJid];

      /** Event: candy:view.room.after-close
       * After closing a room
       *
       * Parameters:
       *   (String) roomJid - Room JID
       */
      $(Candy).triggerHandler('candy:view.room.after-close', {
        'roomJid' : roomJid
      });
    },

    /** Function: appendToMessagePane
     * Append a new message to the message pane.
     *
     * Parameters:
     *   (String) roomJid - Room JID
     *   (String) html - rendered message html
     */
    appendToMessagePane: function(roomJid, html) {
      self.Room.getPane(roomJid, '.message-pane').append(html);
      self.Chat.rooms[roomJid].messageCount++;
      self.Room.sliceMessagePane(roomJid);
    },

    /** Function: sliceMessagePane
     * Slices the message pane after the max amount of messages specified in the Candy View options (limit setting).
     *
     * This is done to hopefully prevent browsers from getting slow after a certain amount of messages in the DOM.
     *
     * The slice is only done when autoscroll is on, because otherwise someone might lose exactly the message he want to look for.
     *
     * Parameters:
     *   (String) roomJid - Room JID
     */
    sliceMessagePane: function(roomJid) {
      // Only clean if autoscroll is enabled
      if(self.Window.autoscroll) {
        var options = Candy.View.getOptions().messages;
        if(self.Chat.rooms[roomJid].messageCount > options.limit) {
          self.Room.getPane(roomJid, '.message-pane').children().slice(0, options.remove).remove();
          self.Chat.rooms[roomJid].messageCount -= options.remove;
        }
      }
    },

    /** Function: scrollToBottom
     * Scroll to bottom wrapper for <onScrollToBottom> to be able to disable it by overwriting the function.
     *
     * Parameters:
     *   (String) roomJid - Room JID
     *
     * Uses:
     *   - <onScrollToBottom>
     */
    scrollToBottom: function(roomJid) {
      self.Room.onScrollToBottom(roomJid);
    },

    /** Function: onScrollToBottom
     * Scrolls to the latest message received/sent.
     *
     * Parameters:
     *   (String) roomJid - Room JID
     */
    onScrollToBottom: function(roomJid) {
      var messagePane = self.Room.getPane(roomJid, '.message-pane-wrapper');

      if (Candy.View.Pane.Chat.rooms[roomJid].enableScroll === true) {
        messagePane.scrollTop(messagePane.prop('scrollHeight'));
      } else {
        return false;
      }
    },

    /** Function: onScrollToStoredPosition
     * When autoscroll is off, the position where the scrollbar is has to be stored for each room, because it otherwise
     * goes to the top in the message window.
     *
     * Parameters:
     *   (String) roomJid - Room JID
     */
    onScrollToStoredPosition: function(roomJid) {
      // This should only apply when entering a room...
      // ... therefore we set scrollPosition to -1 after execution.
      if(self.Chat.rooms[roomJid].scrollPosition > -1) {
        var messagePane = self.Room.getPane(roomJid, '.message-pane-wrapper');
        messagePane.scrollTop(self.Chat.rooms[roomJid].scrollPosition);
        self.Chat.rooms[roomJid].scrollPosition = -1;
      }
    },

    /** Function: setFocusToForm
     * Set focus to the message input field within the message form.
     *
     * Parameters:
     *   (String) roomJid - Room JID
     */
    setFocusToForm: function(roomJid) {
      // If we're on mobile, don't focus the input field.
      if (Candy.Util.isMobile()) { return true; }

      var pane = self.Room.getPane(roomJid, '.message-form');
      if (pane) {
        // IE8 will fail maybe, because the field isn't there yet.
        try {
          pane.children('.field')[0].focus();
        } catch(e) {
          // fail silently
        }
      }
    },

    /** Function: setUser
     * Sets or updates the current user in the specified room (called by <Candy.View.Pane.Roster.update>) and set specific informations
     * (roles and affiliations) on the room tab (chat-pane).
     *
     * Parameters:
     *   (String) roomJid - Room in which the user is set to.
     *   (Candy.Core.ChatUser) user - The user
     */
    setUser: function(roomJid, user) {
      self.Chat.rooms[roomJid].user = user;
      var roomPane = self.Room.getPane(roomJid),
        chatPane = $('#chat-pane');

      roomPane.attr('data-userjid', user.getJid());
      // Set classes based on user role / affiliation
      if(user.isModerator()) {
        if (user.getRole() === user.ROLE_MODERATOR) {
          chatPane.addClass('role-moderator');
        }
        if (user.getAffiliation() === user.AFFILIATION_OWNER) {
          chatPane.addClass('affiliation-owner');
        }
      } else {
        chatPane.removeClass('role-moderator affiliation-owner');
      }
      self.Chat.Context.init();
    },

    /** Function: getUser
     * Get the current user in the room specified with the jid
     *
     * Parameters:
     *   (String) roomJid - Room of which the user should be returned from
     *
     * Returns:
     *   (Candy.Core.ChatUser) - user
     */
    getUser: function(roomJid) {
      return self.Chat.rooms[roomJid].user;
    },

    /** Function: ignoreUser
     * Ignore specified user and add the ignore icon to the roster item of the user
     *
     * Parameters:
     *   (String) roomJid - Room in which the user should be ignored
     *   (String) userJid - User which should be ignored
     */
    ignoreUser: function(roomJid, userJid) {
      Candy.Core.Action.Jabber.Room.IgnoreUnignore(userJid);
      Candy.View.Pane.Room.addIgnoreIcon(roomJid, userJid);
    },

    /** Function: unignoreUser
     * Unignore an ignored user and remove the ignore icon of the roster item.
     *
     * Parameters:
     *   (String) roomJid - Room in which the user should be unignored
     *   (String) userJid - User which should be unignored
     */
    unignoreUser: function(roomJid, userJid) {
      Candy.Core.Action.Jabber.Room.IgnoreUnignore(userJid);
      Candy.View.Pane.Room.removeIgnoreIcon(roomJid, userJid);
    },

    /** Function: addIgnoreIcon
     * Add the ignore icon to the roster item of the specified user
     *
     * Parameters:
     *   (String) roomJid - Room in which the roster item should be updated
     *   (String) userJid - User of which the roster item should be updated
     */
    addIgnoreIcon: function(roomJid, userJid) {
      if (Candy.View.Pane.Chat.rooms[userJid]) {
        $('#user-' + Candy.View.Pane.Chat.rooms[userJid].id + '-' + Candy.Util.jidToId(userJid)).addClass('status-ignored');
      }
      if (Candy.View.Pane.Chat.rooms[Strophe.getBareJidFromJid(roomJid)]) {
        $('#user-' + Candy.View.Pane.Chat.rooms[Strophe.getBareJidFromJid(roomJid)].id + '-' + Candy.Util.jidToId(userJid)).addClass('status-ignored');
      }
    },

    /** Function: removeIgnoreIcon
     * Remove the ignore icon to the roster item of the specified user
     *
     * Parameters:
     *   (String) roomJid - Room in which the roster item should be updated
     *   (String) userJid - User of which the roster item should be updated
     */
    removeIgnoreIcon: function(roomJid, userJid) {
      if (Candy.View.Pane.Chat.rooms[userJid]) {
        $('#user-' + Candy.View.Pane.Chat.rooms[userJid].id + '-' + Candy.Util.jidToId(userJid)).removeClass('status-ignored');
      }
      if (Candy.View.Pane.Chat.rooms[Strophe.getBareJidFromJid(roomJid)]) {
        $('#user-' + Candy.View.Pane.Chat.rooms[Strophe.getBareJidFromJid(roomJid)].id + '-' + Candy.Util.jidToId(userJid)).removeClass('status-ignored');
      }
    },

    /** Function: getPane
     * Get the chat room pane or a subPane of it (if subPane is specified)
     *
     * Parameters:
     *   (String) roomJid - Room in which the pane lies
     *   (String) subPane - Sub pane of the chat room pane if needed [optional]
     */
    getPane: function(roomJid, subPane) {
      if (self.Chat.rooms[roomJid]) {
        if(subPane) {
          if(self.Chat.rooms[roomJid]['pane-' + subPane]) {
            return self.Chat.rooms[roomJid]['pane-' + subPane];
          } else {
            self.Chat.rooms[roomJid]['pane-' + subPane] = $('#chat-room-' + self.Chat.rooms[roomJid].id).find(subPane);
            return self.Chat.rooms[roomJid]['pane-' + subPane];
          }
        } else {
          return $('#chat-room-' + self.Chat.rooms[roomJid].id);
        }
      }
    },

    /** Function: changeDataUserJidIfUserIsMe
     * Changes the room's data-userjid attribute if the specified user is the current user.
     *
     * Parameters:
     *   (String) roomId - Id of the room
     *   (Candy.Core.ChatUser) user - User
     */
    changeDataUserJidIfUserIsMe: function(roomId, user) {
      if (user.getNick() === Candy.Core.getUser().getNick()) {
        var roomElement = $('#chat-room-' + roomId);
        roomElement.attr('data-userjid', Strophe.getBareJidFromJid(roomElement.attr('data-userjid')) + '/' + user.getNick());
      }
    }
  };

  return self;
}(Candy.View.Pane || {}, jQuery));