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

message.js « pane « view « src - github.com/candy-chat/candy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e65b8d230e0a3de7f90bc099806b7e3d0f5e836e (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
/** File: message.js
 * Candy - Chats are not dead yet.
 *
 * Legal: See the LICENSE file at the top-level directory of this distribution and at https://github.com/candy-chat/candy/blob/master/LICENSE
 */
'use strict';

/* global Candy, Mustache, 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.Message
   * Message submit/show handling
   */
  self.Message = {
    /** Function: submit
     * on submit handler for message field sends the message to the server and if it's a private chat, shows the message
     * immediately because the server doesn't send back those message.
     *
     * Parameters:
     *   (Event) event - Triggered event
     *
     * Triggers:
     *   candy:view.message.before-send using {message}
     *
     * FIXME: as everywhere, `roomJid` might be slightly incorrect in this case
     *        - maybe rename this as part of a refactoring.
     */
    submit: function(event) {
      var roomJid = Candy.View.getCurrent().roomJid,
        room = Candy.View.Pane.Chat.rooms[roomJid],
        roomType = room.type,
        targetJid = room.targetJid,
        message = $(this).children('.field').val().substring(0, Candy.View.getOptions().crop.message.body),
        xhtmlMessage,
        evtData = {
          roomJid: roomJid,
          message: message,
          xhtmlMessage: xhtmlMessage
        };

      /** Event: candy:view.message.before-send
       * Before sending a message
       *
       * Parameters:
       *   (String) roomJid - room to which the message should be sent
       *   (String) message - Message text
       *   (String) xhtmlMessage - XHTML formatted message [default: undefined]
       *
       * Returns:
       *   Boolean|undefined - if you like to stop sending the message, return false.
       */
      if($(Candy).triggerHandler('candy:view.message.before-send', evtData) === false) {
        event.preventDefault();
        return;
      }

      message = evtData.message;
      xhtmlMessage = evtData.xhtmlMessage;

      Candy.Core.Action.Jabber.Room.Message(targetJid, message, roomType, xhtmlMessage);
      // Private user chat. Jabber won't notify the user who has sent the message. Just show it as the user hits the button...
      if(roomType === 'chat' && message) {
        self.Message.show(roomJid, self.Room.getUser(roomJid).getNick(), message, xhtmlMessage, undefined, Candy.Core.getUser().getJid());
      }
      // Clear input and set focus to it
      $(this).children('.field').val('').focus();
      event.preventDefault();
    },

    /** Function: show
     * Show a message in the message pane
     *
     * Parameters:
     *   (String) roomJid - room in which the message has been sent to
     *   (String) name - Name of the user which sent the message
     *   (String) message - Message
     *   (String) xhtmlMessage - XHTML formatted message [if options enableXHTML is true]
     *   (String) timestamp - [optional] Timestamp of the message, if not present, current date.
     *   (Boolean) carbon - [optional] Indication of wether or not the message was a carbon
     *
     * Triggers:
     *   candy:view.message.before-show using {roomJid, name, message}
     *   candy.view.message.before-render using {template, templateData}
     *   candy:view.message.after-show using {roomJid, name, message, element}
     */
    show: function(roomJid, name, message, xhtmlMessage, timestamp, from, carbon, stanza) {
      message = Candy.Util.Parser.all(message.substring(0, Candy.View.getOptions().crop.message.body));
      if(Candy.View.getOptions().enableXHTML === true && xhtmlMessage) {
        xhtmlMessage = Candy.Util.parseAndCropXhtml(xhtmlMessage, Candy.View.getOptions().crop.message.body);
      }

      timestamp = timestamp || new Date();

      // Assume we have an ISO-8601 date string and convert it to a Date object
      if (!timestamp.toDateString) {
        timestamp = Candy.Util.iso8601toDate(timestamp);
      }

      // Before we add the new message, check to see if we should be automatically scrolling or not.
      var messagePane = self.Room.getPane(roomJid, '.message-pane');
      var enableScroll = ((messagePane.scrollTop() + messagePane.outerHeight()) === messagePane.prop('scrollHeight')) || !$(messagePane).is(':visible');
      Candy.View.Pane.Chat.rooms[roomJid].enableScroll = enableScroll;

      var evtData = {
        'roomJid': roomJid,
        'name': name,
        'message': message,
        'xhtmlMessage': xhtmlMessage,
        'from': from,
        'stanza': stanza
      };

      /** Event: candy:view.message.before-show
       * Before showing a new message
       *
       * Parameters:
       *   (String) roomJid - Room JID
       *   (String) name - Name of the sending user
       *   (String) message - Message text
       *
       * Returns:
       *   Boolean - if you don't want to show the message, return false
       */
      if($(Candy).triggerHandler('candy:view.message.before-show', evtData) === false) {
        return;
      }

      message = evtData.message;
      xhtmlMessage = evtData.xhtmlMessage;
      if(xhtmlMessage !== undefined && xhtmlMessage.length > 0) {
        message = xhtmlMessage;
      }

      if(!message) {
        return;
      }

      var renderEvtData = {
        template: Candy.View.Template.Message.item,
        templateData: {
          name: name,
          displayName: Candy.Util.crop(name, Candy.View.getOptions().crop.message.nickname),
          message: message,
          time: Candy.Util.localizedTime(timestamp),
          timestamp: timestamp.toISOString(),
          roomjid: roomJid,
          from: from
        },
        stanza: stanza
      };

      /** Event: candy:view.message.before-render
       * Before rendering the message element
       *
       * Parameters:
       *   (String) template - Template to use
       *   (Object) templateData - Template data consists of:
       *                           - (String) name - Name of the sending user
       *                           - (String) displayName - Cropped name of the sending user
       *                           - (String) message - Message text
       *                           - (String) time - Localized time of message
       *                           - (String) timestamp - ISO formatted timestamp of message
       */
      $(Candy).triggerHandler('candy:view.message.before-render', renderEvtData);

      var html = Mustache.to_html(renderEvtData.template, renderEvtData.templateData);
      self.Room.appendToMessagePane(roomJid, html);
      var elem = self.Room.getPane(roomJid, '.message-pane').children().last();
      // click on username opens private chat
      elem.find('a.label').click(function(event) {
        event.preventDefault();
        // Check if user is online and not myself
        var room = Candy.Core.getRoom(roomJid);
        if(room && name !== self.Room.getUser(Candy.View.getCurrent().roomJid).getNick() && room.getRoster().get(roomJid + '/' + name)) {
          if(Candy.View.Pane.PrivateRoom.open(roomJid + '/' + name, name, true) === false) {
            return false;
          }
        }
      });

      if (!carbon) {
        var notifyEvtData = {
          name: name,
          displayName: Candy.Util.crop(name, Candy.View.getOptions().crop.message.nickname),
          roomJid: roomJid,
          message: message,
          time: Candy.Util.localizedTime(timestamp),
          timestamp: timestamp.toISOString()
        };
        /** Event: candy:view.message.notify
         * Notify the user (optionally) that a new message has been received
         *
         * Parameters:
         *   (Object) templateData - Template data consists of:
         *                           - (String) name - Name of the sending user
         *                           - (String) displayName - Cropped name of the sending user
         *                           - (String) roomJid - JID into which the message was sent
         *                           - (String) message - Message text
         *                           - (String) time - Localized time of message
         *                           - (String) timestamp - ISO formatted timestamp of message
         *                           - (Boolean) carbon - Indication of wether or not the message was a carbon
         */
        $(Candy).triggerHandler('candy:view.message.notify', notifyEvtData);

        // Check to see if in-core notifications are disabled
        if(!Candy.Core.getOptions().disableCoreNotifications) {
          if(Candy.View.getCurrent().roomJid !== roomJid || !self.Window.hasFocus()) {
            self.Chat.increaseUnreadMessages(roomJid);
            if(!self.Window.hasFocus()) {
              // Notify the user about a new private message OR on all messages if configured
              if(Candy.View.Pane.Chat.rooms[roomJid].type === 'chat' || Candy.View.getOptions().updateWindowOnAllMessages === true) {
                self.Chat.Toolbar.playSound();
              }
            }
          }
        }

        if(Candy.View.getCurrent().roomJid === roomJid) {
          self.Room.scrollToBottom(roomJid);
        }
      }

      evtData.element = elem;

      /** Event: candy:view.message.after-show
       * Triggered after showing a message
       *
       * Parameters:
       *   (String) roomJid - Room JID
       *   (jQuery.Element) element - User element
       *   (String) name - Name of the sending user
       *   (String) message - Message text
       */
      $(Candy).triggerHandler('candy:view.message.after-show', evtData);
    }
  };

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