summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/im/content/chat-imconv.js
blob: 759a3ce78a4039c9833a2ba0199bbd390a9e8b83 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

/* global MozElements, MozXULElement, gChatTab, chatHandler */

// Wrap in a block to prevent leaking to window scope.
{
  const { Status } = ChromeUtils.importESModule(
    "resource:///modules/imStatusUtils.sys.mjs"
  );
  const { AppConstants } = ChromeUtils.importESModule(
    "resource://gre/modules/AppConstants.sys.mjs"
  );
  const { ChatIcons } = ChromeUtils.importESModule(
    "resource:///modules/chatIcons.sys.mjs"
  );

  /**
   * The MozChatConvRichlistitem widget displays opened conversation information from the
   * contacts: i.e name and icon. It gets displayed under conversation expansion
   * twisty in the contactlist richlistbox.
   *
   * @augments {MozElements.MozRichlistitem}
   */
  class MozChatConvRichlistitem extends MozElements.MozRichlistitem {
    static get inheritedAttributes() {
      return {
        ".box-line": "selected",
        ".convDisplayName": "value=displayname,status",
        ".convUnreadTargetedCount": "value=unreadTargetedCount",
        ".convUnreadCount": "value=unreadCount",
        ".convUnreadTargetedCountLabel": "value=unreadTargetedCount",
      };
    }

    static get markup() {
      return `
      <vbox class="box-line"></vbox>
      <button class="closeConversationButton close-icon"
              tooltiptext="&closeConversationButton.tooltip;"></button>
      <stack class="prplBuddyIcon">
        <html:img class="protoIcon" alt="" />
        <html:img class="smallStatusIcon" />
      </stack>
      <hbox flex="1" class="conv-hbox">
        <label crop="end" class="convDisplayName blistDisplayName">
        </label>
        <label class="convUnreadCount" crop="end"></label>
        <box class="convUnreadTargetedCount">
          <label class="convUnreadTargetedCountLabel" crop="end"></label>
        </box>
        <spacer style="flex: 1000000 1000000;"></spacer>
      </hbox>
      `;
    }

    static get entities() {
      return ["chrome://messenger/locale/chat.dtd"];
    }

    connectedCallback() {
      if (this.delayConnectedCallback() || this.hasChildNodes()) {
        return;
      }

      this.setAttribute("is", "chat-imconv-richlistitem");

      this.addEventListener(
        "mousedown",
        event => {
          if (event.target.classList.contains("closeConversationButton")) {
            this.closeConversation();
            event.stopPropagation();
            event.preventDefault();
          }
        },
        true
      );

      this.appendChild(this.constructor.fragment);

      this.convView = null;

      this.directedUnreadCount = 0;

      new MutationObserver(mutations => {
        if (!this.convView || !this.convView.loaded) {
          return;
        }
        if (this.hasAttribute("selected")) {
          this.convView.switchingToPanel();
        } else {
          this.convView.switchingAwayFromPanel(true);
        }
      }).observe(this, { attributes: true, attributeFilter: ["selected"] });

      // @implements {nsIObserver}
      this.observer = {
        QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
        observe: function (subject, topic, data) {
          if (
            topic == "target-prpl-conversation-changed" ||
            topic == "unread-message-count-changed" ||
            topic == "update-conv-title" ||
            topic == "update-buddy-status" ||
            topic == "update-buddy-status" ||
            topic == "update-conv-chatleft" ||
            topic == "update-conv-chatjoining" ||
            topic == "chat-update-topic"
          ) {
            this.update();
          }
          if (topic == "update-conv-title") {
            this.group.updateContactPosition(
              this.conv,
              "chat-imconv-richlistitem"
            );
          }
        }.bind(this),
      };

      if (this.hasAttribute("is-search-result")) {
        let icon = this.querySelector(".protoIcon");
        icon.classList.add("searchProtoIcon");
        icon.setAttribute("src", "chrome://global/skin/icons/search-glass.svg");
        let statusIcon = this.querySelector(".smallStatusIcon");
        statusIcon.hidden = true;
        this.setAttribute("unreadCount", "0");
        this.setAttribute("unreadTargetedCount", "0");
      }

      this.initializeAttributeInheritance();
    }

    get displayName() {
      return this.conv.title;
    }

    /**
     * This getter exists to provide compatibility with the imgroup sortComparator.
     */
    get contact() {
      return this.conv;
    }

    set selected(val) {
      if (val) {
        this.setAttribute("selected", "true");
      } else {
        this.removeAttribute("selected");
      }
    }

    get selected() {
      return (
        gChatTab &&
        gChatTab.tabNode.selected &&
        this.getAttribute("selected") == "true"
      );
    }

    /**
     * Set the conversation this item should represent. Updates appearance and
     * adds observers to keep it up to date.
     *
     * @param {imIConversation} conv - Conversation this item represents.
     */
    build(conv) {
      this.conv = conv;
      this.conv.addObserver(this.observer);
      this.update();
    }

    update() {
      this.setAttribute("displayname", this.displayName);
      if (this.selected && document.hasFocus()) {
        if (this.convView && this.convView.loaded) {
          this.conv.markAsRead();
          this.directedUnreadCount = 0;
          chatHandler.updateTitle();
          chatHandler.updateChatButtonState();
        }
        this.setAttribute("unreadCount", "0");
        this.setAttribute("unreadTargetedCount", "0");
        this.removeAttribute("unread");
        this.removeAttribute("attention");
      } else {
        let unreadCount =
          this.conv.unreadIncomingMessageCount +
          this.conv.unreadOTRNotificationCount;
        let directedMessages = unreadCount;
        if (unreadCount) {
          this.setAttribute("unread", "true");
          if (this.conv.isChat) {
            directedMessages = this.conv.unreadTargetedMessageCount;
            if (directedMessages) {
              this.setAttribute("attention", "true");
            }
          }
          unreadCount -= directedMessages;
          if (directedMessages > this.directedUnreadCount) {
            this.directedUnreadCount = directedMessages;
          }
        }
        if (unreadCount) {
          unreadCount = "(" + unreadCount + ")";
        }
        this.setAttribute("unreadCount", unreadCount);
        if (
          Services.prefs.getBoolPref(
            "messenger.options.getAttentionOnNewMessages"
          ) &&
          directedMessages > parseInt(this.getAttribute("unreadTargetedCount"))
        ) {
          window.getAttention();
        }
        this.setAttribute("unreadTargetedCount", directedMessages);
        chatHandler.updateTitle();
      }

      let statusIcon = this.querySelector(".smallStatusIcon");
      let statusName;
      statusIcon.hidden = false;
      if (this.conv.isChat) {
        if (this.conv.joining) {
          statusName = "joining";
        } else if (!this.conv.account.connected || this.conv.left) {
          statusName = "left";
        }
        if (statusName) {
          statusIcon.setAttribute(
            "src",
            ChatIcons.getStatusIconURI(statusName)
          );
          // Set alt using messenger/chat.ftl.
          document.l10n.setAttributes(
            statusIcon,
            `chat-${statusName}-chat-icon2`
          );
        } else {
          statusIcon.removeAttribute("src");
          statusIcon.removeAttribute("data-l10n-id");
          statusIcon.removeAttribute("alt");
          statusIcon.hidden = true;
          // Treat protoIcon as if connected.
          statusName = "connected";
        }
      } else {
        let statusType = Ci.imIStatusInfo.STATUS_UNKNOWN;
        let buddy = this.conv.buddy;
        if (buddy && buddy.account.connected) {
          statusType = buddy.statusType;
        }
        statusName = Status.toAttribute(statusType);
        statusIcon.setAttribute("src", ChatIcons.getStatusIconURI(statusName));
        statusIcon.removeAttribute("data-l10n-id");
        statusIcon.setAttribute("alt", Status.toLabel(statusType));
      }

      if (!this.hasAttribute("is-search-result")) {
        let protoIcon = this.querySelector(".protoIcon");
        protoIcon.setAttribute(
          "src",
          ChatIcons.getProtocolIconURI(this.conv.account.protocol)
        );
        ChatIcons.setProtocolIconOpacity(protoIcon, statusName);
      }
    }

    destroy() {
      if (this.conv) {
        this.conv.removeObserver(this.observer);
      }
      if (this.convView) {
        this.convView.destroy();
        this.convView.remove();
      }

      // If the conversation we are destroying was selected, we should
      // select something else, but the 'select' event handler of
      // the listbox will choke while updating the Chat tab title if
      // there are conversation nodes associated with a conversation
      // that no longer exists from the chat core's point of view, so
      // we do the actual selection change only after this conversation
      // item is fully destroyed and removed from the list.
      let newSelectedItem;
      let list = this.parentNode;
      if (list.selectedItem == this) {
        newSelectedItem = this.previousElementSibling;
      }

      if (this.log) {
        this.hidden = true;
        delete this.log;
      } else {
        this.remove();
        delete this.conv;
      }
      if (newSelectedItem) {
        list.selectedItem = newSelectedItem;
      }
    }

    closeConversation() {
      if (this.conv) {
        this.conv.close();
      } else {
        this.destroy();
      }
    }

    keyPress(event) {
      // If Enter or Return is pressed, focus the input box.
      if (event.keyCode == event.DOM_VK_RETURN) {
        this.convView.focus();
        return;
      }

      let accelKeyPressed =
        AppConstants.platform == "macosx" ? event.metaKey : event.ctrlKey;
      // If a character was typed or the accel+v copy shortcut was used,
      // focus the input box and resend the key event.
      if (
        event.charCode != 0 &&
        !event.altKey &&
        ((accelKeyPressed && event.charCode == "v".charCodeAt(0)) ||
          (!event.ctrlKey && !event.metaKey))
      ) {
        this.convView.focus();

        let clonedEvent = new KeyboardEvent("keypress", event);
        this.convView.editor.dispatchEvent(clonedEvent);
        event.preventDefault();
      }
    }

    /**
     * Replace the conversation that this item represents.
     *
     * @param {imIConversation} conv - Updated conversation this should
     *   represent.
     */
    changeConversation(conv) {
      this.conv?.removeObserver(this.observer);
      this.build(conv);
    }

    disconnectedCallback() {
      if (this.conv) {
        this.conv.removeObserver(this.observer);
        delete this.conv;
      }
    }
  }

  MozXULElement.implementCustomInterface(MozChatConvRichlistitem, [
    Ci.nsIDOMXULSelectControlItemElement,
  ]);

  customElements.define("chat-imconv-richlistitem", MozChatConvRichlistitem, {
    extends: "richlistitem",
  });
}