summaryrefslogtreecommitdiffstats
path: root/comm/calendar/base/content/preferences/views.js
blob: 7c1a7384a1c1913c771bf39ac8a35a01a10a4ed8 (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
/* 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 gViewsPane */

/* globals Preferences */

Preferences.addAll([
  { id: "calendar.week.start", type: "int" },
  { id: "calendar.view-minimonth.showWeekNumber", type: "bool" },
  { id: "calendar.week.d0sundaysoff", type: "bool", inverted: "true" },
  { id: "calendar.week.d1mondaysoff", type: "bool", inverted: "true" },
  { id: "calendar.week.d2tuesdaysoff", type: "bool", inverted: "true" },
  { id: "calendar.week.d3wednesdaysoff", type: "bool", inverted: "true" },
  { id: "calendar.week.d4thursdaysoff", type: "bool", inverted: "true" },
  { id: "calendar.week.d5fridaysoff", type: "bool", inverted: "true" },
  { id: "calendar.week.d6saturdaysoff", type: "bool", inverted: "true" },
  { id: "calendar.view.daystarthour", type: "int" },
  { id: "calendar.view.dayendhour", type: "int" },
  { id: "calendar.view.visiblehours", type: "int" },
  { id: "calendar.weeks.inview", type: "int" },
  { id: "calendar.previousweeks.inview", type: "int" },
  { id: "calendar.view.showLocation", type: "bool" },
]);

/**
 * Global Object to hold methods for the views pref pane
 */
var gViewsPane = {
  /**
   * Initialize the views pref pane. Sets up dialog controls to match the
   * values set in prefs.
   */
  init() {
    this.updateViewEndMenu(Preferences.get("calendar.view.daystarthour").value);
    this.updateViewStartMenu(Preferences.get("calendar.view.dayendhour").value);
    this.updateViewWorkDayCheckboxes(Preferences.get("calendar.week.start").value);
    this.initializeViewStartEndMenus();
  },

  /**
   * Initialize the strings for the  "day starts at" and "day ends at"
   * menulists. This is needed to respect locales that use AM/PM.
   */
  initializeViewStartEndMenus() {
    const { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");

    let formatter = cal.dtz.formatter;
    let calTime = cal.createDateTime();
    calTime.minute = 0;

    // 1 to 23 instead of 0 to 24 to keep midnight & noon as the localized strings
    for (let theHour = 1; theHour <= 23; theHour++) {
      calTime.hour = theHour;
      let time = formatter.formatTime(calTime);

      let labelIdStart = "timeStart" + theHour;
      let labelIdEnd = "timeEnd" + theHour;
      // This if block to keep Noon as the localized string, instead of as a number.
      if (theHour != 12) {
        document.getElementById(labelIdStart).setAttribute("label", time);
        document.getElementById(labelIdEnd).setAttribute("label", time);
      }
    }
  },

  /**
   * Updates the view end menu to only display hours after the selected view
   * start.
   *
   * @param aStartValue       The value selected for view start.
   */
  updateViewEndMenu(aStartValue) {
    let endMenuKids = document.getElementById("dayendhourpopup").children;
    for (let i = 0; i < endMenuKids.length; i++) {
      if (Number(endMenuKids[i].value) <= Number(aStartValue)) {
        endMenuKids[i].setAttribute("hidden", true);
      } else {
        endMenuKids[i].removeAttribute("hidden");
      }
    }
  },

  /**
   * Updates the view start menu to only display hours before the selected view
   * end.
   *
   * @param aEndValue         The value selected for view end.
   */
  updateViewStartMenu(aEndValue) {
    let startMenuKids = document.getElementById("daystarthourpopup").children;
    for (let i = 0; i < startMenuKids.length; i++) {
      if (Number(startMenuKids[i].value) >= Number(aEndValue)) {
        startMenuKids[i].setAttribute("hidden", true);
      } else {
        startMenuKids[i].removeAttribute("hidden");
      }
    }
  },

  /**
   * Update the workday checkboxes based on the start of the week.
   *
   * @Param weekStart         The (0-based) index of the weekday the week
   *                            should start at.
   */
  updateViewWorkDayCheckboxes(weekStart) {
    weekStart = Number(weekStart);
    for (let i = weekStart; i < weekStart + 7; i++) {
      let checkbox = document.getElementById("dayoff" + (i % 7));
      checkbox.parentNode.appendChild(checkbox);
    }
  },
};