summaryrefslogtreecommitdiffstats
path: root/comm/calendar/base/content/preferences/editCategory.js
blob: c3479b88c042341d261cd6734bb3a53b73cfe7f2 (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
/* 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 editCategoryLoad, categoryNameChanged, clickColor, delay */

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

window.addEventListener("DOMContentLoaded", editCategoryLoad);

// Global variable, set to true if the user has picked a custom color.
var customColorSelected = false;

/**
 * Load Handler, called when the edit category dialog is loaded
 */
function editCategoryLoad() {
  let winArg = window.arguments[0];
  let color = winArg.color || cal.view.hashColor(winArg.category);
  let hasColor = !!winArg.color;
  document.getElementById("categoryName").value = winArg.category;
  document.getElementById("categoryColor").value = color;
  document.getElementById("useColor").checked = hasColor;
  customColorSelected = hasColor;
  document.title = winArg.title;

  toggleColor();
}

/**
 * Handler function to be called when the category dialog is accepted and
 * the opener should further process the selected name and color
 */
document.addEventListener("dialogaccept", () => {
  let color = document.getElementById("useColor").checked
    ? document.getElementById("categoryColor").value
    : null;

  let categoryName = document.getElementById("categoryName").value;
  window.opener.gCategoriesPane.saveCategory(categoryName, color);
});

/**
 * Handler function to be called when the category name changed
 */
function categoryNameChanged() {
  let newValue = document.getElementById("categoryName").value;

  // The user removed the category name, assign the color automatically again.
  if (newValue == "") {
    customColorSelected = false;
  }

  if (!customColorSelected && document.getElementById("useColor").checked) {
    // Color is wanted, choose the color based on the category name's hash.
    document.getElementById("categoryColor").value = cal.view.hashColor(newValue);
  }
}

/**
 * Handler function to be called when the color picker's color has been changed.
 */
function colorPickerChanged() {
  document.getElementById("useColor").checked = true;
  customColorSelected = true;
}

/**
 * Handler called when the use color checkbox is toggled.
 */
function toggleColor() {
  let useColor = document.getElementById("useColor").checked;
  let categoryColor = document.getElementById("categoryColor");

  if (useColor) {
    categoryColor.removeAttribute("disabled");
    if (toggleColor.lastColor) {
      categoryColor.value = toggleColor.lastColor;
    }
  } else {
    categoryColor.setAttribute("disabled", "disabled");
    toggleColor.lastColor = categoryColor.value;
    categoryColor.value = "";
  }
}

/**
 * Click handler for the color picker. Turns the button back into a colorpicker
 * when clicked.
 */
function clickColor() {
  let categoryColor = document.getElementById("categoryColor");
  if (categoryColor.hasAttribute("disabled")) {
    colorPickerChanged();
    toggleColor();
    categoryColor.click();
  }
}

/**
 * Call the function after the given timeout, resetting the timer if delay is
 * called again with the same function.
 *
 * @param timeout   The timeout interval.
 * @param func      The function to call after the timeout.
 */
function delay(timeout, func) {
  if (func.timer) {
    clearTimeout(func.timer);
  }
  func.timer = setTimeout(func, timeout);
}