summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/im/content/chat-contact.js
blob: d3e9baf974564734e3d4c06fcd16c5ea37e1fb07 (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
/* 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 MozXULElement, MozElements, Status, chatHandler */

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

  /**
   * The MozChatContactRichlistitem widget displays contact information about user under
   * chat-groups, online contacts and offline contacts: i.e. icon and username.
   * On double clicking the element, it gets moved into the conversations.
   *
   * @augments {MozElements.MozRichlistitem}
   */
  class MozChatContactRichlistitem extends MozElements.MozRichlistitem {
    static get inheritedAttributes() {
      return {
        ".box-line": "selected",
        ".contactDisplayName": "value=displayname",
        ".contactDisplayNameInput": "value=displayname",
        ".contactStatusText": "value=statusTextWithDash",
      };
    }

    static get markup() {
      return `
      <vbox class="box-line"></vbox>
      <stack class="prplBuddyIcon">
        <html:img class="protoIcon" alt="" />
        <html:img class="smallStatusIcon" />
      </stack>
      <hbox flex="1" class="contact-hbox">
        <stack>
          <label crop="end"
                 class="contactDisplayName blistDisplayName">
          </label>
          <html:input type="text"
                      class="contactDisplayNameInput"
                      hidden="hidden"/>
        </stack>
        <label crop="end"
               style="flex: 100000 100000;"
               class="contactStatusText">
        </label>
        <button class="startChatBubble"
                tooltiptext="&openConversationButton.tooltip;">
        </button>
      </hbox>
      `;
    }

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

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

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

      this.addEventListener("blur", event => {
        if (!this.hasAttribute("aliasing")) {
          return;
        }

        if (Services.focus.activeWindow == document.defaultView) {
          this.finishAliasing(true);
        }
      });

      this.addEventListener("mousedown", event => {
        if (
          !this.hasAttribute("aliasing") &&
          this.canOpenConversation() &&
          event.target.classList.contains("startChatBubble")
        ) {
          this.openConversation();
          event.preventDefault();
        }
      });

      this.addEventListener("click", event => {
        if (
          !this.hasAttribute("aliasing") &&
          this.canOpenConversation() &&
          event.detail == 2
        ) {
          this.openConversation();
        }
      });

      this.parentNode.addEventListener("mousedown", event => {
        event.preventDefault();
      });

      // @implements {nsIObserver}
      this.observer = {
        QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
        observe: function (subject, topic, data) {
          if (
            topic == "contact-preferred-buddy-changed" ||
            topic == "contact-display-name-changed" ||
            topic == "contact-status-changed"
          ) {
            this.update();
          }
          if (
            topic == "contact-availability-changed" ||
            topic == "contact-display-name-changed"
          ) {
            this.group.updateContactPosition(subject);
          }
        }.bind(this),
      };

      this.appendChild(this.constructor.fragment);

      this.initializeAttributeInheritance();
    }

    get displayName() {
      return this.contact.displayName;
    }

    update() {
      this.setAttribute("displayname", this.contact.displayName);

      let statusText = this.contact.statusText;
      if (statusText) {
        statusText = " - " + statusText;
      }
      this.setAttribute("statusTextWithDash", statusText);
      let statusType = this.contact.statusType;

      let statusIcon = this.querySelector(".smallStatusIcon");
      let statusName = Status.toAttribute(statusType);
      statusIcon.setAttribute("src", ChatIcons.getStatusIconURI(statusName));
      statusIcon.setAttribute("alt", Status.toLabel(statusType));

      if (this.contact.canSendMessage) {
        this.setAttribute("cansend", "true");
      } else {
        this.removeAttribute("cansend");
      }

      let protoIcon = this.querySelector(".protoIcon");
      protoIcon.setAttribute(
        "src",
        ChatIcons.getProtocolIconURI(this.contact.preferredBuddy.protocol)
      );
      ChatIcons.setProtocolIconOpacity(protoIcon, statusName);
    }

    build(contact) {
      this.contact = contact;
      this.contact.addObserver(this.observer);
      this.update();
    }

    destroy() {
      this.contact.removeObserver(this.observer);
      delete this.contact;
      this.remove();
    }

    startAliasing() {
      if (this.hasAttribute("aliasing")) {
        return; // prevent re-entry.
      }

      this.setAttribute("aliasing", "true");
      let input = this.querySelector(".contactDisplayNameInput");
      let label = this.querySelector(".contactDisplayName");
      input.removeAttribute("hidden");
      label.setAttribute("hidden", "true");
      input.focus();

      this._inputBlurListener = function (event) {
        this.finishAliasing(true);
      }.bind(this);
      input.addEventListener("blur", this._inputBlurListener);

      // Some keys (home/end for example) can make the selected item
      // of the richlistbox change without producing a blur event on
      // our textbox. Make sure we watch richlistbox selection changes.
      this._parentSelectListener = function (event) {
        if (event.target == this.parentNode) {
          this.finishAliasing(true);
        }
      }.bind(this);
      this.parentNode.addEventListener("select", this._parentSelectListener);
    }

    finishAliasing(save) {
      // Cache the parentNode because when we change the contact alias, we
      // trigger a re-order (and a removeContact call), which sets
      // this.parentNode to undefined.
      let listbox = this.parentNode;
      let input = this.querySelector(".contactDisplayNameInput");
      let label = this.querySelector(".contactDisplayName");
      input.setAttribute("hidden", "hidden");
      label.removeAttribute("hidden");
      if (save) {
        this.contact.alias = input.value;
      }
      this.removeAttribute("aliasing");
      listbox.removeEventListener("select", this._parentSelectListener);
      input.removeEventListener("blur", this._inputBlurListener);
      delete this._parentSelectListener;
      listbox.focus();
    }

    deleteContact() {
      this.contact.remove();
    }

    canOpenConversation() {
      return this.contact.canSendMessage;
    }

    openConversation() {
      let prplConv = this.contact.createConversation();
      let uiConv = IMServices.conversations.getUIConversation(prplConv);
      chatHandler.focusConversation(uiConv);
    }

    keyPress(event) {
      switch (event.keyCode) {
        // If Enter or Return is pressed, open a new conversation
        case event.DOM_VK_RETURN:
          if (this.hasAttribute("aliasing")) {
            this.finishAliasing(true);
          } else if (this.canOpenConversation()) {
            this.openConversation();
          }
          break;

        case event.DOM_VK_F2:
          if (!this.hasAttribute("aliasing")) {
            this.startAliasing();
          }
          break;

        case event.DOM_VK_ESCAPE:
          if (this.hasAttribute("aliasing")) {
            this.finishAliasing(false);
          }
          break;
      }
    }
    disconnectedCallback() {
      if (this.contact) {
        this.contact.removeObserver(this.observer);
        delete this.contact;
      }
    }
  }

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

  customElements.define(
    "chat-contact-richlistitem",
    MozChatContactRichlistitem,
    {
      extends: "richlistitem",
    }
  );
}