summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/content/editContactPanel.js
blob: 40e1061167f3acedfb1981e6278bbe953a1b9da3 (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
/* 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/. */

var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

var editContactInlineUI = {
  _overlayLoaded: false,
  _overlayLoading: false,
  _cardDetails: null,
  _writeable: true,
  _blockedCommands: ["cmd_close"],

  _blockCommands() {
    for (var i = 0; i < this._blockedCommands; ++i) {
      var elt = document.getElementById(this._blockedCommands[i]);
      // make sure not to permanetly disable this item
      if (elt.hasAttribute("wasDisabled")) {
        continue;
      }

      if (elt.getAttribute("disabled") == "true") {
        elt.setAttribute("wasDisabled", "true");
      } else {
        elt.setAttribute("wasDisabled", "false");
        elt.setAttribute("disabled", "true");
      }
    }
  },

  _restoreCommandsState() {
    for (var i = 0; i < this._blockedCommands; ++i) {
      var elt = document.getElementById(this._blockedCommands[i]);
      if (elt.getAttribute("wasDisabled") != "true") {
        elt.removeAttribute("disabled");
      }
      elt.removeAttribute("wasDisabled");
    }
    document.getElementById("editContactAddressBookList").disabled = false;
    document.getElementById("contactMoveDisabledText").hidden = true;
  },

  onPopupHidden(aEvent) {
    if (aEvent.target == this.panel) {
      this._restoreCommandsState();
    }
  },

  onPopupShown(aEvent) {
    if (aEvent.target == this.panel) {
      document.getElementById("editContactName").focus();
    }
  },

  onKeyPress(aEvent, aHandleOnlyReadOnly) {
    // Escape should just close this panel
    if (aEvent.keyCode == KeyEvent.DOM_VK_ESCAPE) {
      this.panel.hidePopup();
      return;
    }

    // Return does the default button (done)
    if (aEvent.keyCode == KeyEvent.DOM_VK_RETURN) {
      if (!aEvent.target.hasAttribute("oncommand")) {
        this.saveChanges();
      }
      return;
    }

    // Only handle the read-only cases here.
    if (aHandleOnlyReadOnly && this._writeable && !aEvent.target.readOnly) {
      return;
    }

    // Any other character and we prevent the default, this stops us doing
    // things in the main message window.
    if (aEvent.charCode) {
      aEvent.preventDefault();
    }
  },

  get panel() {
    // The panel is initially stored in a template for performance reasons.
    // Load it into the DOM now.
    delete this.panel;
    let template = document.getElementById("editContactPanelTemplate");
    template.replaceWith(template.content);
    let element = document.getElementById("editContactPanel");
    return (this.panel = element);
  },

  showEditContactPanel(aCardDetails, aAnchorElement) {
    this._cardDetails = aCardDetails;
    let position = "after_start";
    this._doShowEditContactPanel(aAnchorElement, position);
  },

  _doShowEditContactPanel(aAnchorElement, aPosition) {
    this._blockCommands(); // un-done in the popuphiding handler.
    var bundle = Services.strings.createBundle(
      "chrome://messenger/locale/editContactOverlay.properties"
    );

    // Is this address book writeable?
    this._writeable = !this._cardDetails.book.readOnly;
    var type = this._writeable ? "edit" : "view";

    // Force the panel to be created from the template, if necessary.
    this.panel;

    // Update the labels accordingly.
    document.getElementById("editContactPanelTitle").textContent =
      bundle.GetStringFromName(type + "Title");
    document.getElementById("editContactPanelEditDetailsButton").label =
      bundle.GetStringFromName(type + "DetailsLabel");
    document.getElementById("editContactPanelEditDetailsButton").accessKey =
      bundle.GetStringFromName(type + "DetailsAccessKey");

    // We don't need a delete button for a read only card.
    document.getElementById("editContactPanelDeleteContactButton").hidden =
      !this._writeable;

    var nameElement = document.getElementById("editContactName");

    // Set these to read only if we can't write to the directory.
    if (this._writeable) {
      nameElement.removeAttribute("readonly");
      nameElement.class = "editContactTextbox";
    } else {
      nameElement.setAttribute("readonly", "readonly");
      nameElement.class = "plain";
    }

    // Fill in the card details
    nameElement.value = this._cardDetails.card.displayName;
    document.getElementById("editContactEmail").value =
      aAnchorElement.getAttribute("emailAddress") ||
      aAnchorElement.emailAddress;

    document.getElementById("editContactAddressBookList").value =
      this._cardDetails.book.URI;

    // Is this card contained within mailing lists?
    let inMailList = false;
    if (this._cardDetails.book.supportsMailingLists) {
      // We only have to look in one book here, because cards currently have
      // to be in the address book they belong to.
      for (let list of this._cardDetails.book.childNodes) {
        if (!list.isMailList) {
          continue;
        }

        for (let card of list.childCards) {
          if (card.primaryEmail == this._cardDetails.card.primaryEmail) {
            inMailList = true;
            break;
          }
        }
        if (inMailList) {
          break;
        }
      }
    }

    if (!this._writeable || inMailList) {
      document.getElementById("editContactAddressBookList").disabled = true;
    }

    if (inMailList) {
      document.getElementById("contactMoveDisabledText").hidden = false;
    }

    this.panel.openPopup(aAnchorElement, aPosition, -1, -1);
  },

  editDetails() {
    this.saveChanges();
    top.toAddressBook({ action: "edit", card: this._cardDetails.card });
  },

  deleteContact() {
    if (this._cardDetails.book.readOnly) {
      // Double check we can delete this.
      return;
    }

    // Hide before the dialog or the panel takes the first click.
    this.panel.hidePopup();

    var bundle = Services.strings.createBundle(
      "chrome://messenger/locale/editContactOverlay.properties"
    );
    if (
      !Services.prompt.confirm(
        window,
        bundle.GetStringFromName("deleteContactTitle"),
        bundle.GetStringFromName("deleteContactMessage")
      )
    ) {
      // XXX Would be nice to bring the popup back up here.
      return;
    }

    MailServices.ab
      .getDirectory(this._cardDetails.book.URI)
      .deleteCards([this._cardDetails.card]);
  },

  saveChanges() {
    // If we're a popup dialog, just hide the popup and return
    if (!this._writeable) {
      this.panel.hidePopup();
      return;
    }

    let originalBook = this._cardDetails.book;

    let abURI = document.getElementById("editContactAddressBookList").value;
    if (abURI != originalBook.URI) {
      this._cardDetails.book = MailServices.ab.getDirectory(abURI);
    }

    // We can assume the email address stays the same, so just update the name
    var newName = document.getElementById("editContactName").value;
    if (newName != this._cardDetails.card.displayName) {
      this._cardDetails.card.displayName = newName;
      this._cardDetails.card.setProperty("PreferDisplayName", true);
    }

    // Save the card
    if (this._cardDetails.book.hasCard(this._cardDetails.card)) {
      // Address book wasn't changed.
      this._cardDetails.book.modifyCard(this._cardDetails.card);
    } else {
      // We changed address books for the card.

      // Add it to the chosen address book...
      this._cardDetails.book.addCard(this._cardDetails.card);

      // ...and delete it from the old place.
      originalBook.deleteCards([this._cardDetails.card]);
    }

    this.panel.hidePopup();
  },
};