-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathedit_form.js
202 lines (182 loc) · 6 KB
/
edit_form.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* EventCalendar. $Revision: 255 $
* Copyright (C) 2005 2006, Alex Tingle.
* This file is licensed under the GNU GPL. See LICENSE file for details.
*/
/** Register an onload function. */
function WindowOnload(f)
{
var prev=window.onload;
window.onload=function(){ if(prev)prev(); f(); }
}
/***
*** Set elsewhere: Ec3EditForm.event_cat_id
*** Ec3EditForm.start_of_week
***/
// namespace
function Ec3EditForm()
{
var fmt="%Y-%m-%d %H:%M";
WindowOnload( function()
{
var ELEMENT_NODE=1;
var TEXT_NODE=3;
Ec3EditForm.fieldset_se=document.getElementById('ec3_schedule_editor');
Ec3EditForm.rows=document.getElementById('ec3_rows');
Ec3EditForm.catinput=document.getElementById(Ec3EditForm.event_cat_id);
if(Ec3EditForm.fieldset_se && Ec3EditForm.rows && Ec3EditForm.catinput)
{
// Perform some sleight of hand....
// What I WANT to do, is set Ec3EditForm.catinput to read only, however
// that's not possible for a checkbox. Disabled is good, but disabled
// checkboxes are never submitted with the form. So, we HIDE the real
// checkbox, and make a new dummy one that is shown but disabled.
Ec3EditForm.dummyinput=Ec3EditForm.catinput.cloneNode(true);
Ec3EditForm.dummyinput.id='ec3_dummy';
Ec3EditForm.dummyinput.name='ec3_dummy';
Ec3EditForm.dummyinput.disabled=true;
Ec3EditForm.catinput.parentNode.insertBefore(
Ec3EditForm.dummyinput, Ec3EditForm.catinput
);
Ec3EditForm.catinput.style.display='none';
update_category();
add_row_listeners(Ec3EditForm.fieldset_se);
}
} );
function add_row_listeners(e)
{
var buttons=e.getElementsByTagName('button');
for(var i=0; i<buttons.length; i++)
if(buttons[i].id.indexOf('trigger_ec3_')==0)
{
input_id=buttons[i].id.substr(8);
input=document.getElementById(input_id);
if(!input)
continue;
cal=Calendar.setup({
inputField:input,
ifFormat:fmt,
showsTime:true,
button:buttons[i].id,
step:1,
firstDay:Ec3EditForm.start_of_week,
singleClick:false,
onUpdate:cal_changed
});
try{
input.setAttribute('ec3_date',
Date.parseDate(input.value,fmt).getTime());
} catch(e) {
input.setAttribute('ec3_date',0);
}
firetree_addEvent(input,'change',on_changed);
}
}
function update_category()
{
// Set both the real AND dummpy checkboxes to match Ec3EditForm.rows.
if(Ec3EditForm.rows.value>0)
{
Ec3EditForm.catinput.checked=true;
Ec3EditForm.dummyinput.checked=true;
}
else
{
Ec3EditForm.catinput.checked=false;
Ec3EditForm.dummyinput.checked=false;
}
}
Ec3EditForm.add_row=function()
{
// Get the second-from-last TR
var trs=Ec3EditForm.fieldset_se.getElementsByTagName('tr');
var tr=trs[trs.length-2];
// Make a new row, based on it & add it into the table.
var new_tr=tr.cloneNode(1);
var selects=new_tr.getElementsByTagName('select');
for(var i=0; i<selects.length; i++)
{
selects[i].name+=trs.length-3;
selects[i].id=selects[i].name;
}
var inputs=new_tr.getElementsByTagName('input');
for(var i=0; i<inputs.length; i++)
{
inputs[i].name+=trs.length-3;
inputs[i].id=inputs[i].name;
}
var buttons=new_tr.getElementsByTagName('button');
for(var i=0; i<buttons.length; i++)
{
buttons[i].id+=trs.length-3;
}
new_tr.style.display=trs[0].style.display;
tr.parentNode.insertBefore(new_tr,tr);
Ec3EditForm.rows.value++;
update_category();
add_row_listeners(new_tr);
}
Ec3EditForm.del_row=function(element)
{
tr=element.parentNode.parentNode.parentNode;
var inputs=tr.getElementsByTagName('input');
for(var i=0; i<inputs.length; i++)
if(0 == inputs[i].name.indexOf('ec3_action_'))
{
inputs[i].value='delete';
break;
}
tr.style.display='none';
Ec3EditForm.rows.value--;
update_category();
}
function on_changed(e)
{
var input;
if(e.currentTarget)
input=e.currentTarget; // Mozilla/Safari/w3c
else if(window.event)
input=window.event.srcElement; // IE
else
return;
input_changed(input);
}
function input_changed(input)
{
if(input.id.indexOf('ec3_start_')>=0)
{
// Change the end time to preserve the event duration.
var start=input;
var end=document.getElementById(start.id.replace('_start_','_end_'));
var start_date0=parseInt(start.getAttribute('ec3_date'));
var end_date0=parseInt(end.getAttribute('ec3_date'));
var start_date1=Date.parseDate(start.value,fmt).getTime();
start.setAttribute('ec3_date',start_date1);
var delta=start_date1 - start_date0;
var end_date1=end_date0+delta;
end.setAttribute('ec3_date',end_date1);
end.value=(new Date(end_date1)).print(fmt);
}
else if(input.id.indexOf('ec3_end_')>=0)
{
// Make sure that the start time is before the end time.
var start=document.getElementById(input.id.replace('_end_','_start_'));
var end=input;
var start_date0=parseInt(start.getAttribute('ec3_date'));
var end_date0=parseInt(end.getAttribute('ec3_date'));
var end_date1=Date.parseDate(end.value,fmt).getTime();
end.setAttribute('ec3_date',end_date1);
if(start_date0>end_date1)
{
var start_date1=end_date1;
start.setAttribute('ec3_date',start_date1);
start.value=(new Date(start_date1)).print(fmt);
}
}
}
function cal_changed(cal)
{
input_changed(cal.params.inputField);
}
} // end namespace Ec3EditForm
// Export public functions from namespace.
Ec3EditForm();