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

/* globals MozXULElement, MozElements */ // From calendar-invitations-dialog.xhtml.

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

// Wrap in a block to prevent leaking to window scope.
{
  class MozCalendarInvitationsRichlistitem extends MozElements.MozRichlistitem {
    constructor() {
      super();

      this.mCalendarItem = null;
      this.mInitialParticipationStatus = null;
      this.mParticipationStatus = null;
      this.calInvitationsProps = Services.strings.createBundle(
        "chrome://calendar/locale/calendar-invitations-dialog.properties"
      );
    }

    getString(propName) {
      return this.calInvitationsProps.GetStringFromName(propName);
    }

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

      this.setAttribute("is", "calendar-invitations-richlistitem");
      this.classList.add("calendar-invitations-richlistitem");

      this.appendChild(
        MozXULElement.parseXULToFragment(
          `
          <hbox align="start" flex="1">
            <!-- Note: The wrapper div is only here because the XUL box does not
               - properly crop img elements with CSS object-fit and
               - object-position. Should be removed when converting the parent
               - element to HTML. -->
            <html:div>
              <html:img class="calendar-invitations-richlistitem-icon"
                        src="chrome://calendar/skin/shared/calendar-invitations-dialog-list-images.png" />
            </html:div>
            <vbox flex="1">
              <label class="calendar-invitations-richlistitem-title" crop="end"/>
              <label class="calendar-invitations-richlistitem-date" crop="end"/>
              <label class="calendar-invitations-richlistitem-recurrence" crop="end"/>
              <label class="calendar-invitations-richlistitem-location" crop="end"/>
              <label class="calendar-invitations-richlistitem-organizer" crop="end"/>
              <label class="calendar-invitations-richlistitem-attendee" crop="end"/>
              <label class="calendar-invitations-richlistitem-spacer" value="" hidden="true"/>
            </vbox>
            <vbox>
              <button group="${this.getAttribute("itemId")}"
                      type="radio"
                      class="calendar-invitations-richlistitem-accept-button
                      calendar-invitations-richlistitem-button"
                      label="&calendar.invitations.list.accept.button.label;"
                      oncommand="accept();"/>
              <button group="${this.getAttribute("itemId")}"
                      type="radio"
                      class="calendar-invitations-richlistitem-decline-button
                      calendar-invitations-richlistitem-button"
                      label="&calendar.invitations.list.decline.button.label;"
                      oncommand="decline();"/>
            </vbox>
          </hbox>
          `,
          ["chrome://calendar/locale/calendar-invitations-dialog.dtd"]
        )
      );
    }

    set calendarItem(val) {
      this.setCalendarItem(val);
    }

    get calendarItem() {
      return this.mCalendarItem;
    }

    set initialParticipationStatus(val) {
      this.mInitialParticipationStatus = val;
    }

    get initialParticipationStatus() {
      return this.mInitialParticipationStatus;
    }

    set participationStatus(val) {
      this.mParticipationStatus = val;
      let icon = this.querySelector(".calendar-invitations-richlistitem-icon");
      // Status attribute changes the image region in CSS.
      icon.setAttribute("status", val);
      document.l10n.setAttributes(
        icon,
        `calendar-invitation-current-participation-status-icon-${val.toLowerCase()}`
      );
    }

    get participationStatus() {
      return this.mParticipationStatus;
    }

    setCalendarItem(item) {
      this.mCalendarItem = item;
      this.mInitialParticipationStatus = this.getCalendarItemParticipationStatus(item);
      this.participationStatus = this.mInitialParticipationStatus;

      let titleLabel = this.querySelector(".calendar-invitations-richlistitem-title");
      titleLabel.setAttribute("value", item.title);

      let dateLabel = this.querySelector(".calendar-invitations-richlistitem-date");
      let dateString = cal.dtz.formatter.formatItemInterval(item);
      if (item.startDate.isDate) {
        dateString += ", " + this.getString("allday-event");
      }
      dateLabel.setAttribute("value", dateString);

      let recurrenceLabel = this.querySelector(".calendar-invitations-richlistitem-recurrence");
      if (item.recurrenceInfo) {
        recurrenceLabel.setAttribute("value", this.getString("recurrent-event"));
      } else {
        recurrenceLabel.setAttribute("hidden", "true");
        let spacer = this.querySelector(".calendar-invitations-richlistitem-spacer");
        spacer.removeAttribute("hidden");
      }

      let locationLabel = this.querySelector(".calendar-invitations-richlistitem-location");
      let locationProperty = item.getProperty("LOCATION") || this.getString("none");
      let locationString = this.calInvitationsProps.formatStringFromName("location", [
        locationProperty,
      ]);

      locationLabel.setAttribute("value", locationString);

      let organizerLabel = this.querySelector(".calendar-invitations-richlistitem-organizer");
      let org = item.organizer;
      let organizerProperty = "";
      if (org) {
        if (org.commonName && org.commonName.length > 0) {
          organizerProperty = org.commonName;
        } else if (org.id) {
          organizerProperty = org.id.replace(/^mailto:/i, "");
        }
      }
      let organizerString = this.calInvitationsProps.formatStringFromName("organizer", [
        organizerProperty,
      ]);
      organizerLabel.setAttribute("value", organizerString);

      let attendeeLabel = this.querySelector(".calendar-invitations-richlistitem-attendee");
      let att = cal.itip.getInvitedAttendee(item);
      let attendeeProperty = "";
      if (att) {
        if (att.commonName && att.commonName.length > 0) {
          attendeeProperty = att.commonName;
        } else if (att.id) {
          attendeeProperty = att.id.replace(/^mailto:/i, "");
        }
      }
      let attendeeString = this.calInvitationsProps.formatStringFromName("attendee", [
        attendeeProperty,
      ]);
      attendeeLabel.setAttribute("value", attendeeString);
      Array.from(this.querySelectorAll("button")).map(button =>
        button.setAttribute("group", item.hashId)
      );
    }

    getCalendarItemParticipationStatus(item) {
      let att = cal.itip.getInvitedAttendee(item);
      return att ? att.participationStatus : null;
    }

    setCalendarItemParticipationStatus(item, status) {
      if (item.calendar?.supportsScheduling) {
        let att = item.calendar.getSchedulingSupport().getInvitedAttendee(item);
        if (att) {
          let att_ = att.clone();
          att_.participationStatus = status;

          // Update attendee
          item.removeAttendee(att);
          item.addAttendee(att_);
          return true;
        }
      }
      return false;
    }

    accept() {
      this.participationStatus = "ACCEPTED";
    }

    decline() {
      this.participationStatus = "DECLINED";
    }
  }
  customElements.define("calendar-invitations-richlistitem", MozCalendarInvitationsRichlistitem, {
    extends: "richlistitem",
  });
}

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

/**
 * Sets up the invitations dialog from the window arguments, retrieves the
 * invitations from the invitations manager.
 */
async function onLoad() {
  let title = document.title;
  let updatingBox = document.getElementById("updating-box");
  updatingBox.removeAttribute("hidden");
  opener.setCursor("auto");

  let { invitationsManager } = window.arguments[0];
  let items = await cal.iterate.mapStream(invitationsManager.getInvitations(), chunk => {
    document.title = title + " (" + chunk.length + ")";
    let updatingBox = document.getElementById("updating-box");
    updatingBox.setAttribute("hidden", "true");
    let richListBox = document.getElementById("invitations-listbox");
    for (let item of chunk) {
      let newNode = document.createXULElement("richlistitem", {
        is: "calendar-invitations-richlistitem",
      });
      richListBox.appendChild(newNode);
      newNode.calendarItem = item;
    }
  });

  invitationsManager.toggleInvitationsPanel(items);
  updatingBox.setAttribute("hidden", "true");

  let richListBox = document.getElementById("invitations-listbox");
  if (richListBox.getRowCount() > 0) {
    richListBox.selectedIndex = 0;
  } else {
    let noInvitationsBox = document.getElementById("noinvitations-box");
    noInvitationsBox.removeAttribute("hidden");
  }
}

/**
 * Cleans up the invitations dialog, cancels pending requests.
 */
async function onUnload() {
  let args = window.arguments[0];
  return args.invitationsManager.cancelPendingRequests();
}

/**
 * Handler function to be called when the accept button is pressed.
 */
document.addEventListener("dialogaccept", async () => {
  let args = window.arguments[0];
  fillJobQueue(args.queue);
  await args.invitationsManager.processJobQueue(args.queue);
  args.finishedCallBack();
});

/**
 * Handler function to be called when the cancel button is pressed.
 */
document.addEventListener("dialogcancel", () => {
  let args = window.arguments[0];
  args.finishedCallBack();
});

/**
 * Fills the job queue from the invitations-listbox's items. The job queue
 * contains objects for all items that have a modified participation status.
 *
 * @param queue     The queue to fill.
 */
function fillJobQueue(queue) {
  let richListBox = document.getElementById("invitations-listbox");
  let rowCount = richListBox.getRowCount();
  for (let i = 0; i < rowCount; i++) {
    let richListItem = richListBox.getItemAtIndex(i);
    let newStatus = richListItem.participationStatus;
    let oldStatus = richListItem.initialParticipationStatus;
    if (newStatus != oldStatus) {
      let actionString = "modify";
      let oldCalendarItem = richListItem.calendarItem;
      let newCalendarItem = oldCalendarItem.clone();

      // set default alarm on unresponded items that have not been declined:
      if (
        !newCalendarItem.getAlarms().length &&
        oldStatus == "NEEDS-ACTION" &&
        newStatus != "DECLINED"
      ) {
        cal.alarms.setDefaultValues(newCalendarItem);
      }

      richListItem.setCalendarItemParticipationStatus(newCalendarItem, newStatus);
      let job = {
        action: actionString,
        oldItem: oldCalendarItem,
        newItem: newCalendarItem,
      };
      queue.push(job);
    }
  }
}