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
|
/* 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/. */
/* import-globals-from ../calendar-management.js */
/* import-globals-from ../calendar-ui-utils.js */
/* import-globals-from calendar-item-editing.js */
var { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");
var { XPCOMUtils } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs");
XPCOMUtils.defineLazyModuleGetters(this, {
CalTodo: "resource:///modules/CalTodo.jsm",
});
/**
* Used by the "quick add" feature for tasks, for example in the task view or
* the uniinder-todo.
*
* NOTE: many of the following methods are called without taskEdit being the
* |this| object.
*/
var taskEdit = {
/**
* Helper function to set readonly and aria-disabled states and the value
* for a given target.
*
* @param aTarget The ID or XUL node to set the value
* @param aDisable A boolean if the target should be disabled.
* @param aValue The value that should be set on the target.
*/
setupTaskField(aTarget, aDisable, aValue) {
aTarget.value = aValue;
aTarget.readOnly = aDisable;
aTarget.ariaDisabled = aDisable;
},
/**
* Handler function to call when the quick-add input gains focus.
*
* @param aEvent The DOM focus event
*/
onFocus(aEvent) {
let edit = aEvent.target;
let calendar = getSelectedCalendar();
edit.showsInstructions = true;
if (calendar.getProperty("capabilities.tasks.supported") === false) {
taskEdit.setupTaskField(edit, true, cal.l10n.getCalString("taskEditInstructionsCapability"));
} else if (cal.acl.isCalendarWritable(calendar)) {
edit.showsInstructions = false;
taskEdit.setupTaskField(edit, false, edit.savedValue || "");
} else {
taskEdit.setupTaskField(edit, true, cal.l10n.getCalString("taskEditInstructionsReadonly"));
}
},
/**
* Handler function to call when the quick-add input loses focus.
*
* @param aEvent The DOM blur event
*/
onBlur(aEvent) {
let edit = aEvent.target;
let calendar = getSelectedCalendar();
if (!calendar) {
// this must be a first run, we don't have a calendar yet
return;
}
if (calendar.getProperty("capabilities.tasks.supported") === false) {
taskEdit.setupTaskField(edit, true, cal.l10n.getCalString("taskEditInstructionsCapability"));
} else if (cal.acl.isCalendarWritable(calendar)) {
if (!edit.showsInstructions) {
edit.savedValue = edit.value || "";
}
taskEdit.setupTaskField(edit, false, cal.l10n.getCalString("taskEditInstructions"));
} else {
taskEdit.setupTaskField(edit, true, cal.l10n.getCalString("taskEditInstructionsReadonly"));
}
edit.showsInstructions = true;
},
/**
* Handler function to call on keypress for the quick-add input.
*
* @param aEvent The DOM keypress event
*/
onKeyPress(aEvent) {
if (aEvent.key == "Enter") {
let edit = aEvent.target;
if (edit.value && edit.value.length > 0) {
let item = new CalTodo();
setDefaultItemValues(item);
item.title = edit.value;
edit.value = "";
doTransaction("add", item, item.calendar, null, null);
}
}
},
/**
* Helper function to call onBlur for all fields with class name
* "task-edit-field".
*/
callOnBlurForAllTaskFields() {
let taskEditFields = document.getElementsByClassName("task-edit-field");
for (let i = 0; i < taskEditFields.length; i++) {
taskEdit.onBlur({ target: taskEditFields[i] });
}
},
/**
* Load function to set up all quick-add inputs. The input must
* have the class "task-edit-field".
*/
onLoad(aEvent) {
cal.view.getCompositeCalendar(window).addObserver(taskEdit.compositeObserver);
taskEdit.callOnBlurForAllTaskFields();
},
/**
* Window load function to clean up all quick-add fields.
*/
onUnload() {
cal.view.getCompositeCalendar(window).removeObserver(taskEdit.compositeObserver);
},
/**
* Observer to watch for changes to the selected calendar.
*
* @see calIObserver
* @see calICompositeObserver
*/
compositeObserver: {
QueryInterface: ChromeUtils.generateQI(["calIObserver", "calICompositeObserver"]),
// calIObserver:
onStartBatch() {},
onEndBatch() {},
onLoad(aCalendar) {},
onAddItem(aItem) {},
onModifyItem(aNewItem, aOldItem) {},
onDeleteItem(aDeletedItem) {},
onError(aCalendar, aErrNo, aMessage) {},
onPropertyChanged(aCalendar, aName, aValue, aOldValue) {
if (aCalendar.id != getSelectedCalendar().id) {
// Optimization: if the given calendar isn't the selected calendar,
// then we don't need to change any readonly/disabled states.
return;
}
switch (aName) {
case "readOnly":
case "disabled": {
taskEdit.callOnBlurForAllTaskFields();
break;
}
}
},
onPropertyDeleting(aCalendar, aName) {
// Since the old value is not used directly in onPropertyChanged,
// but should not be the same as the value, set it to a different
// value.
this.onPropertyChanged(aCalendar, aName, null, null);
},
// calICompositeObserver:
onCalendarAdded(aCalendar) {},
onCalendarRemoved(aCalendar) {},
onDefaultCalendarChanged(aNewDefault) {
taskEdit.callOnBlurForAllTaskFields();
},
},
};
|