summaryrefslogtreecommitdiffstats
path: root/comm/calendar/base/content/dialogs/chooseCalendarDialog.js
blob: 47532df8ea637fb9a2a841ee79d89b48f20747f6 (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
/* 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 loadCalendars */

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

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

function loadCalendars() {
  const calendarManager = Cc["@mozilla.org/calendar/manager;1"].getService(Ci.calICalendarManager);
  let listbox = document.getElementById("calendar-list");
  let composite = cal.view.getCompositeCalendar(window.opener);
  let selectedIndex = 0;
  let calendars;

  if (window.arguments[0].calendars) {
    calendars = window.arguments[0].calendars;
  } else {
    calendars = calendarManager.getCalendars();
  }
  calendars = sortCalendarArray(calendars);

  for (let i = 0; i < calendars.length; i++) {
    let calendar = calendars[i];
    let listItem = document.createXULElement("richlistitem");

    let colorCell = document.createXULElement("box");
    try {
      colorCell.style.backgroundColor = calendar.getProperty("color") || "#a8c2e1";
    } catch (e) {}
    listItem.appendChild(colorCell);

    let nameCell = document.createXULElement("label");
    nameCell.setAttribute("value", calendar.name);
    nameCell.setAttribute("flex", "1");
    listItem.appendChild(nameCell);

    listItem.calendar = calendar;
    listbox.appendChild(listItem);

    // Select the default calendar of the opening calendar window.
    if (calendar.id == composite.defaultCalendar.id) {
      selectedIndex = i;
    }
  }
  document.getElementById("prompt").textContent = window.arguments[0].promptText;
  if (window.arguments[0].promptNotify) {
    document.getElementById("promptNotify").textContent = window.arguments[0].promptNotify;
  }

  // this button is the default action
  let dialog = document.querySelector("dialog");
  let accept = dialog.getButton("accept");
  if (window.arguments[0].labelOk) {
    accept.setAttribute("label", window.arguments[0].labelOk);
    accept.removeAttribute("hidden");
  }

  let extra1 = dialog.getButton("extra1");
  if (window.arguments[0].labelExtra1) {
    extra1.setAttribute("label", window.arguments[0].labelExtra1);
    extra1.removeAttribute("hidden");
  } else {
    extra1.setAttribute("hidden", "true");
  }

  if (calendars.length) {
    listbox.ensureIndexIsVisible(selectedIndex);
    listbox.timedSelect(listbox.getItemAtIndex(selectedIndex), 0);
  } else {
    // If there are no calendars, then disable the accept button
    accept.setAttribute("disabled", "true");
  }

  window.sizeToContent();
}

document.addEventListener("dialogaccept", () => {
  let listbox = document.getElementById("calendar-list");
  window.arguments[0].onOk(listbox.selectedItem.calendar);
});

document.addEventListener("dialogextra1", () => {
  let listbox = document.getElementById("calendar-list");
  window.arguments[0].onExtra1(listbox.selectedItem.calendar);
  window.close();
});