summaryrefslogtreecommitdiffstats
path: root/comm/calendar/base/content/dialogs/calendar-summary-dialog.js
blob: 0f703758141dcf769383c009693ac1e9f937c16c (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
/* 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/. */

/* exported reply */

/* global MozElements */

/* import-globals-from calendar-dialog-utils.js */

var { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");
var { XPCOMUtils } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs");

ChromeUtils.defineESModuleGetters(this, {
  SelectionUtils: "resource://gre/modules/SelectionUtils.sys.mjs",
});

XPCOMUtils.defineLazyGetter(this, "gStatusNotification", () => {
  return new MozElements.NotificationBox(async element => {
    let box = document.getElementById("status-notifications");
    // Fix window size after the notification animation is done.
    box.addEventListener(
      "transitionend",
      () => {
        window.sizeToContent();
      },
      { once: true }
    );
    box.append(element);
  });
});

window.addEventListener("load", onLoad);
window.addEventListener("unload", onUnload);

/**
 * Sets up the summary dialog, setting all needed fields on the dialog from the
 * item received in the window arguments.
 */
async function onLoad() {
  let args = window.arguments[0];
  let item = args.calendarEvent;
  item = item.clone(); // use an own copy of the passed item
  window.calendarItem = item;
  window.isInvitation = args.isInvitation;
  let dialog = document.querySelector("dialog");

  document.title = item.title;

  // set the dialog-id to enable the right CSS to be used.
  if (item.isEvent()) {
    setDialogId(dialog, "calendar-event-summary-dialog");
  } else if (item.isTodo()) {
    setDialogId(dialog, "calendar-task-summary-dialog");
  }

  // Start setting up the item summary custom element.
  let itemSummary = document.getElementById("calendar-item-summary");
  itemSummary.item = item;

  window.readOnly = itemSummary.readOnly;
  let calendar = itemSummary.calendar;

  if (!window.readOnly) {
    let attendee = cal.itip.getInvitedAttendee(item, calendar);
    if (attendee) {
      // if this is an unresponded invitation, preset our default alarm values:
      if (!item.getAlarms().length && attendee.participationStatus == "NEEDS-ACTION") {
        cal.alarms.setDefaultValues(item);
      }

      window.attendee = attendee.clone();
      // Since we don't have API to update an attendee in place, remove
      // and add again. Also, this is needed if the attendee doesn't exist
      // (i.e REPLY on a mailing list)
      item.removeAttendee(attendee);
      item.addAttendee(window.attendee);

      window.responseMode = "USER";
    }
  }

  // Finish setting up the item summary custom element.
  itemSummary.updateItemDetails();

  updateToolbar();
  updateDialogButtons(item);

  if (typeof window.ToolbarIconColor !== "undefined") {
    window.ToolbarIconColor.init();
  }

  await document.l10n.translateRoots();
  window.sizeToContent();
  window.focus();
  opener.setCursor("auto");
}

function onUnload() {
  if (typeof window.ToolbarIconColor !== "undefined") {
    window.ToolbarIconColor.uninit();
  }
}

/**
 * Updates the user's participation status (PARTSTAT from see RFC5545), and
 * send a notification if requested. Then close the dialog.
 *
 * @param {string} aResponseMode - a literal of one of the response modes defined
 *                                 in calIItipItem (like 'NONE')
 * @param {string} aPartStat - participation status; a PARTSTAT value
 */
function reply(aResponseMode, aPartStat) {
  // Set participation status.
  if (window.attendee) {
    let aclEntry = window.calendarItem.calendar.aclEntry;
    if (aclEntry) {
      let userAddresses = aclEntry.getUserAddresses();
      if (
        userAddresses.length > 0 &&
        !cal.email.attendeeMatchesAddresses(window.attendee, userAddresses)
      ) {
        window.attendee.setProperty("SENT-BY", "mailto:" + userAddresses[0]);
      }
    }
    window.attendee.participationStatus = aPartStat;
    updateToolbar();
  }

  // Send notification and close window.
  saveAndClose(aResponseMode);
}

/**
 * Stores the event in the calendar, sends a notification if requested and
 * closes the dialog.
 *
 * @param {string} aResponseMode - a literal of one of the response modes defined
 *                                 in calIItipItem (like 'NONE')
 */
function saveAndClose(aResponseMode) {
  window.responseMode = aResponseMode;
  document.querySelector("dialog").acceptDialog();
}

function updateToolbar() {
  if (window.readOnly || window.isInvitation !== true) {
    document.getElementById("summary-toolbox").hidden = true;
    return;
  }

  let replyButtons = document.getElementsByAttribute("type", "menu-button");
  for (let element of replyButtons) {
    element.removeAttribute("hidden");
    if (window.attendee) {
      // we disable the control which represents the current partstat
      let status = window.attendee.participationStatus || "NEEDS-ACTION";
      if (element.getAttribute("value") == status) {
        element.setAttribute("disabled", "true");
      } else {
        element.removeAttribute("disabled");
      }
    }
  }

  if (window.attendee) {
    // we display a notification about the users partstat
    let partStat = window.attendee.participationStatus || "NEEDS-ACTION";
    let type = window.calendarItem.isEvent() ? "event" : "task";

    let msgStr = {
      ACCEPTED: type + "Accepted",
      COMPLETED: "taskCompleted",
      DECLINED: type + "Declined",
      DELEGATED: type + "Delegated",
      TENTATIVE: type + "Tentative",
    };
    // this needs to be noted differently to get accepted the '-' in the key
    msgStr["NEEDS-ACTION"] = type + "NeedsAction";
    msgStr["IN-PROGRESS"] = "taskInProgress";

    let msg = cal.l10n.getString("calendar-event-dialog", msgStr[partStat]);

    gStatusNotification.appendNotification(
      "statusNotification",
      {
        label: msg,
        priority: gStatusNotification.PRIORITY_INFO_MEDIUM,
      },
      null
    );
  } else {
    gStatusNotification.removeAllNotifications();
  }
}

/**
 * Copy the text content of the given link node to the clipboard.
 *
 * @param {string} labelNode - The label node inside an html:a element.
 */
function locationCopyLink(labelNode) {
  let clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper);
  clipboard.copyString(labelNode.parentNode.getAttribute("href"));
}

/**
 * This configures the dialog buttons depending on the writable status
 * of the item and whether it recurs or not:
 * 1) The calendar is read-only - The buttons stay hidden.
 * 2) The item is an invitation - The buttons stay hidden.
 * 3) The item is recurring     - Show an edit menu with occurrence options.
 * 4) Otherwise                 - Show the single edit button.
 *
 * @param {calIItemBase} item
 */
function updateDialogButtons(item) {
  let editButton = document.getElementById("calendar-summary-dialog-edit-button");
  let isRecurring = item.parentItem !== item;
  if (window.readOnly === true) {
    // This enables pressing the "enter" key to close the dialog.
    editButton.focus();
  } else if (window.isInvitation === true) {
    document.addEventListener("dialogaccept", onInvitationDialogAccept);
  } else if (isRecurring) {
    // Show the edit button menu for repeating events.
    let menuButton = document.getElementById("calendar-summary-dialog-edit-menu-button");
    menuButton.hidden = false;

    // Pressing the "enter" key will display the occurrence menu.
    document.getElementById("calendar-summary-dialog-edit-menu-button").focus();
    document.addEventListener("dialogaccept", evt => {
      evt.preventDefault();
    });
  } else {
    // Show the single edit button for non-repeating events.
    document.addEventListener("dialogaccept", () => {
      useEditDialog(item);
    });
    editButton.hidden = false;
  }
  // Show the custom dialog footer when the event is editable.
  if (window.readOnly !== true && window.isInvitation !== true) {
    let footer = document.getElementById("calendar-summary-dialog-custom-button-footer");
    footer.hidden = false;
  }
}

/**
 * Saves any changed information to the item.
 */
function onInvitationDialogAccept() {
  // let's make sure we have a response mode defined
  let resp = window.responseMode || "USER";
  let respMode = { responseMode: Ci.calIItipItem[resp] };

  let args = window.arguments[0];
  let oldItem = args.calendarEvent;
  let newItem = window.calendarItem;
  let calendar = newItem.calendar;
  saveReminder(newItem, calendar, document.querySelector(".item-alarm"));
  adaptScheduleAgent(newItem);
  args.onOk(newItem, calendar, oldItem, null, respMode);
  window.calendarItem = newItem;
}

/**
 * Invokes the editing dialog for the current item occurrence.
 */
function onEditThisOccurrence() {
  useEditDialog(window.calendarItem);
}

/**
 * Invokes the editing dialog for all occurrences of the current item.
 */
function onEditAllOccurrences() {
  useEditDialog(window.calendarItem.parentItem);
}

/**
 * Switch to the "modify" mode dialog so the user can make changes to the event.
 *
 * @param {calIItemBase} item
 */
function useEditDialog(item) {
  window.addEventListener("unload", () => {
    window.opener.modifyEventWithDialog(item, false);
  });
  window.close();
}

/**
 * Initializes the context menu used for the attendees area.
 *
 * @param {Event} event
 */
function onAttendeeContextMenu(event) {
  let copyMenu = document.getElementById("attendee-popup-copy-menu");
  let item = window.arguments[0].calendarEvent;

  let attId =
    event.target.getAttribute("attendeeid") || event.target.parentNode.getAttribute("attendeeid");
  let attendee = item.getAttendees().find(att => att.id == attId);

  if (!attendee) {
    copyMenu.hidden = true;
    return;
  }

  let id = attendee.toString();
  let idMenuItem = document.getElementById("attendee-popup-copy-menu-id");
  idMenuItem.setAttribute("label", id);
  idMenuItem.hidden = false;

  let name = attendee.commonName;
  let nameMenuItem = document.getElementById("attendee-popup-copy-menu-common-name");
  if (name && name != id) {
    nameMenuItem.setAttribute("label", name);
    nameMenuItem.hidden = false;
  } else {
    nameMenuItem.hidden = true;
  }

  copyMenu.hidden = false;
}

/**
 * Initializes the context menu used for the event description area in the
 * event summary.
 *
 * @param {Event} event
 */
function openDescriptionContextMenu(event) {
  const popup = document.getElementById("description-popup");
  const link = event.target.closest("a") ? event.target.closest("a").getAttribute("href") : null;
  const linkText = event.target.closest("a") ? event.target.closest("a").text : null;
  const copyLinkTextMenuItem = document.getElementById("description-context-menu-copy-link-text");
  const copyLinkLocationMenuItem = document.getElementById(
    "description-context-menu-copy-link-location"
  );
  const selectionCollapsed = SelectionUtils.getSelectionDetails(window).docSelectionIsCollapsed;

  // Hide copy command if there is no text selected.
  popup.querySelector('[command="cmd_copy"]').hidden = selectionCollapsed;

  copyLinkLocationMenuItem.hidden = !link;
  copyLinkTextMenuItem.hidden = !link;
  popup.querySelector("#calendar-summary-description-context-menuseparator").hidden =
    selectionCollapsed && !link;
  copyLinkTextMenuItem.setAttribute("text", linkText);

  popup.openPopupAtScreen(event.screenX, event.screenY, true, event);
  event.preventDefault();
}

/**
 * Copies the link text in a calender event description
 * @param {Event} event
 */
async function copyLinkTextToClipboard(event) {
  return navigator.clipboard.writeText(event.target.getAttribute("text"));
}

/**
 * Copies the label value of a menuitem to the clipboard.
 */
async function copyLabelToClipboard(event) {
  return navigator.clipboard.writeText(event.target.getAttribute("label"));
}

/**
 * Brings up the compose window to send an e-mail to all attendees.
 */
function sendMailToAttendees() {
  let item = window.arguments[0].calendarEvent;
  let toList = cal.email.createRecipientList(item.getAttendees());
  let emailSubject = cal.l10n.getString("calendar-event-dialog", "emailSubjectReply", [item.title]);
  let identity = item.calendar.getProperty("imip.identity");
  cal.email.sendTo(toList, emailSubject, null, identity);
}