summaryrefslogtreecommitdiffstats
path: root/comm/calendar/base/content/preferences/general.js
blob: 8991193e055d25bca5bb09a3442b9a20f57d19b2 (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
/* 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 gCalendarGeneralPane */

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

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

Preferences.addAll([
  { id: "calendar.date.format", type: "int" },
  { id: "calendar.event.defaultlength", type: "int" },
  { id: "calendar.timezone.local", type: "string" },
  { id: "calendar.timezone.useSystemTimezone", type: "bool" },
  { id: "calendar.task.defaultstart", type: "string" },
  { id: "calendar.task.defaultstartoffset", type: "int" },
  { id: "calendar.task.defaultstartoffsetunits", type: "string" },
  { id: "calendar.task.defaultdue", type: "string" },
  { id: "calendar.task.defaultdueoffset", type: "int" },
  { id: "calendar.task.defaultdueoffsetunits", type: "string" },
  { id: "calendar.agenda.days", type: "int" },
  { id: "calendar.item.editInTab", type: "bool" },
  { id: "calendar.item.promptDelete", type: "bool" },
]);

/**
 * Global Object to hold methods for the general pref pane
 */
var gCalendarGeneralPane = {
  /**
   * Initialize the general pref pane. Sets up dialog controls to match the
   * values set in prefs.
   */
  init() {
    this.onChangedUseSystemTimezonePref();

    let formatter = cal.dtz.formatter;
    let dateFormattedLong = formatter.formatDateLong(cal.dtz.now());
    let dateFormattedShort = formatter.formatDateShort(cal.dtz.now());

    // menu items include examples of current date formats.
    document.l10n.setAttributes(
      document.getElementById("dateformat-long-menuitem"),
      "dateformat-long",
      { date: dateFormattedLong }
    );

    document.l10n.setAttributes(
      document.getElementById("dateformat-short-menuitem"),
      "dateformat-short",
      { date: dateFormattedShort }
    );

    // deselect and reselect to update visible item title
    updateUnitLabelPlural("defaultlength", "defaultlengthunit", "minutes");
    this.updateDefaultTodoDates();

    let tzMenuList = document.getElementById("calendar-timezone-menulist");
    let tzMenuPopup = document.getElementById("calendar-timezone-menupopup");

    let tzids = {};
    let displayNames = [];
    // don't rely on what order the timezone-service gives you
    for (let timezoneId of cal.timezoneService.timezoneIds) {
      let timezone = cal.timezoneService.getTimezone(timezoneId);
      if (timezone && !timezone.isFloating && !timezone.isUTC) {
        let displayName = timezone.displayName;
        displayNames.push(displayName);
        tzids[displayName] = timezone.tzid;
      }
    }
    // the display names need to be sorted
    displayNames.sort((a, b) => a.localeCompare(b));
    for (let displayName of displayNames) {
      addMenuItem(tzMenuPopup, displayName, tzids[displayName]);
    }

    let prefValue = Preferences.get("calendar.timezone.local").value;
    if (!prefValue) {
      prefValue = cal.dtz.defaultTimezone.tzid;
    }
    tzMenuList.value = prefValue;

    // Set the agenda length menulist.
    this.initializeTodaypaneMenu();
  },

  updateDefaultTodoDates() {
    let defaultDue = document.getElementById("default_task_due").value;
    let defaultStart = document.getElementById("default_task_start").value;
    let offsetValues = ["offsetcurrent", "offsetnexthour"];

    document.getElementById("default_task_due_offset").style.visibility = offsetValues.includes(
      defaultDue
    )
      ? ""
      : "hidden";
    document.getElementById("default_task_start_offset").style.visibility = offsetValues.includes(
      defaultStart
    )
      ? ""
      : "hidden";

    updateMenuLabelsPlural("default_task_start_offset_text", "default_task_start_offset_units");
    updateMenuLabelsPlural("default_task_due_offset_text", "default_task_due_offset_units");
  },

  initializeTodaypaneMenu() {
    // Assign the labels for the menuitem
    let menulist = document.getElementById("agenda-days-menulist");
    let items = menulist.getElementsByTagName("menuitem");
    for (let menuItem of items) {
      let menuitemValue = Number(menuItem.value);
      if (menuitemValue > 7) {
        menuItem.label = unitPluralForm(menuitemValue / 7, "weeks");
      } else {
        menuItem.label = unitPluralForm(menuitemValue, "days");
      }
    }

    let pref = Preferences.get("calendar.agenda.days");
    let value = pref.value;

    // Check if the preference has been edited with a wrong value.
    if (value > 0 && value <= 28) {
      if (value % 7 != 0) {
        let intValue = Math.floor(value / 7) * 7;
        value = intValue == 0 ? value : intValue;
        pref.value = value;
      }
    } else {
      pref.value = 14;
    }
  },

  onChangedUseSystemTimezonePref() {
    let useSystemTimezonePref = Preferences.get("calendar.timezone.useSystemTimezone");

    document.getElementById("calendar-timezone-menulist").disabled = useSystemTimezonePref.value;
  },
};

Preferences.get("calendar.timezone.useSystemTimezone").on(
  "change",
  gCalendarGeneralPane.onChangedUseSystemTimezonePref
);