]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/UIxFreeBusyUserSelector.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1009 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / WebServerResources / UIxFreeBusyUserSelector.js
1 var resultsDiv;
2 var searchField;
3 var running = false;
4 var address;
5 var delay = 500;
6 var requestField;
7 var awaitingFreeBusyRequests = new Array();
8 var freeBusySelectorId;
9
10 function onContactKeydown(event) {
11   if (event.keyCode == 9) {
12     event.preventDefault();
13     if (this.confirmedValue)
14       this.value = this.confirmedValue;
15     var row = this.parentNode.parentNode.nextSibling;
16     while (!(row instanceof HTMLTableRowElement))
17       row = row.nextSibling;
18     this.blur();
19     var input = row.cells[0].childNodesWithTag("input")[0];
20     if (input.readOnly)
21       newAttendee(null);
22     else {
23       input.focus();
24       input.select();
25       input.focussed = true;
26     }
27   }
28   else if (!running) {
29     if (event.keyCode == 8
30         || event.keyCode == 32
31         || event.keyCode > 47) {
32       running = true;
33       requestField = this;
34       setTimeout("triggerRequest()", delay);
35     }
36     else if (this.confirmedValue) {
37       if (event.keyCode == 13) {
38         this.setSelectionRange(this.value.length, this.value.length);
39       }
40     }
41   }
42 }
43
44 function triggerRequest() {
45   if (document.contactLookupAjaxRequest) {
46     document.contactLookupAjaxRequest.aborted = yes;
47     document.contactLookupAjaxRequest.abort();
48   }
49   var urlstr = ( UserFolderURL + "Contacts/contactSearch?search="
50                  + requestField.value );
51   document.contactLookupAjaxRequest = triggerAjaxRequest(urlstr,
52                                                          updateResults,
53                                                          requestField);
54 }
55
56 function updateResults(http) {
57   if (http.readyState == 4) {
58     if (http.status == 200) {
59       var searchField = http.callbackData;
60       var start = searchField.value.length;
61       var text = http.responseText.split(":");
62       if (text[0].length > 0)
63         searchField.uid = text[0];
64       else
65         searchField.uid = null;
66       searchField.hasfreebusy = false;
67       if (text[1].substring(0, searchField.value.length).toUpperCase()
68           == searchField.value.toUpperCase())
69         searchField.value = text[1];
70       else {
71         searchField.value += ' >> ' + text[1];
72       }
73       searchField.confirmedValue = text[1];
74       if (searchField.focussed) {
75         var end = searchField.value.length;
76         searchField.setSelectionRange(start, end);
77       }
78       else
79         searchField.value = text[1];
80     }
81     running = false;
82     document.contactLookupAjaxRequest = null;
83   }
84 }
85
86 function resetFreeBusyZone()
87 {
88   var table = $("attendeesView").childNodesWithTag("div")[0].childNodesWithTag("table")[0];
89   var row = table.tHead.rows[2];
90   for (var i = 1; i < row.cells.length; i++) {
91     var nodes = row.cells[i].childNodesWithTag("span");
92     for (var j = 0; j < nodes.length; j++)
93       nodes[j].removeClassName("busy");
94   }
95 }
96
97 function redisplayFreeBusyZone()
98 {
99   var table = $("attendeesView").childNodesWithTag("div")[0].childNodesWithTag("table")[0];
100   var row = table.tHead.rows[2];
101   var stDay = this.timeWidgets['start']['date'].valueAsDate();
102   var etDay = this.timeWidgets['end']['date'].valueAsDate();
103   var days = stDay.daysUpTo(etDay);
104   var addDays = days.length - 1;
105   var stHour = parseInt(this.timeWidgets['start']['hour'].value);
106   var stMinute = parseInt(this.timeWidgets['start']['minute'].value) / 15;
107   var etHour = parseInt(this.timeWidgets['end']['hour'].value);
108   var etMinute = parseInt(this.timeWidgets['end']['minute'].value) / 15;
109   if (stHour < 8) {
110     stHour = 8;
111     stMinute = 0;
112   }
113   if (stHour > 19) {
114     stHour = 19
115     stMinute = 0;
116   }
117   if (etHour < 8) {
118     etHour = 8;
119     etMinute = 0;
120   }
121   if (etHour > 19) {
122     etHour = 19;
123     etMinute = 0;
124   }
125   if (stHour > etHour) {
126     var swap = etHour;
127     etHour = stHour;
128     stHour = swap;
129     swap = etMinute;
130     etMinute = stMinute;
131     stMinute = etMinute;
132   } else {
133     if (stMinute > etMinute) {
134       var swap = etMinute;
135       etMinute = stMinute;
136       stMinute = swap;
137     }
138   }
139
140   var deltaCells = (etHour - stHour) + (11 * addDays);
141   var deltaSpans = (deltaCells * 4 ) + (etMinute - stMinute);
142   var currentCellNbr = stHour - 7;
143   var currentCell = row.cells[currentCellNbr];
144   var currentSpanNbr = stMinute;
145   var spans = currentCell.childNodesWithTag("span");
146   resetFreeBusyZone();
147   while (deltaSpans > 0) {
148     var currentSpan = spans[currentSpanNbr];
149     currentSpan.addClassName("busy");
150     currentSpanNbr++;
151     if (currentSpanNbr > 3) {
152       currentSpanNbr = 0;
153       currentCellNbr++;
154       currentCell = row.cells[currentCellNbr];
155       spans = currentCell.childNodesWithTag("span");
156     }
157     deltaSpans--;
158   }
159 }
160
161 function newAttendee(event)
162 {
163   var table = $("attendeesView").childNodesWithTag("div")[0].childNodesWithTag("table")[0];
164   var tbody = table.tBodies[0];
165   var model = tbody.rows[tbody.rows.length - 1];
166   var newAttendeeRow = tbody.rows[tbody.rows.length - 2]
167   var newRow = model.cloneNode(true);
168   var input = newRow.cells[0].childNodesWithTag("input")[0];
169   input.setAttribute("autocomplete", "off");
170   newRow.setAttribute("class", "");
171   tbody.insertBefore(newRow, newAttendeeRow);
172   input.serial = "pouet";
173   input.addEventListener("blur", checkAttendee, false);
174   input.addEventListener("keydown", onContactKeydown, false);
175   input.focus();
176   input.focussed = true;
177 }
178
179 function checkAttendee()
180 {
181   this.focussed = false;
182   var th = this.parentNode.parentNode;
183   var tbody = th.parentNode;
184   if (this.value.trim().length == 0)
185     tbody.removeChild(th);
186   else if (!this.hasfreebusy) {
187     if (this.confirmedValue)
188       this.value = this.confirmedValue;
189     displayFreeBusyForNode(this);
190     this.hasfreebusy = true;
191   }
192   resetAttendeesValue();
193 }
194
195 function displayFreeBusyForNode(node)
196 {
197   var nodes = node.parentNode.parentNode.cells;
198   if (node.uid) {
199     for (var i = 1; i < nodes.length; i++) {
200       nodes[i].removeClassName("noFreeBusy");
201       nodes[i].innerHTML = ('<span class="freeBusyZoneElement"></span>'
202                             + '<span class="freeBusyZoneElement"></span>'
203                             + '<span class="freeBusyZoneElement"></span>'
204                             + '<span class="freeBusyZoneElement"></span>');
205     }
206     if (document.contactFreeBusyAjaxRequest) {
207       document.contactFreeBusyAjaxRequest.aborted = true;
208       document.contactFreeBusyAjaxRequest.abort();
209     }
210     var sd = startDayAsShortString();
211     var ed = endDayAsShortString();
212     var urlstr = ( UserFolderURL + "../" + node.uid + "/freebusy.ifb/ajaxRead?"
213                    + "sday=" + sd + "&eday=" + ed + "&additional=2" );
214     document.contactFreeBusyAjaxRequest
215       = triggerAjaxRequest(urlstr,
216                            updateFreeBusyData,
217                            node);
218   } else {
219     for (var i = 1; i < nodes.length; i++) {
220       nodes[i].addClassName("noFreeBusy");
221       nodes[i].innerHTML = '';
222     }
223   }
224 }
225
226 function setSlot(tds, nbr, status) {
227   var tdnbr = Math.floor(nbr / 4);
228   var spannbr = nbr - (tdnbr * 4);
229   var days = 0;
230   if (tdnbr > 24) {
231     days = Math.floor(tdnbr / 24);
232     tdnbr -= (days * 24);
233   }
234   if (tdnbr > 7 && tdnbr < 19) {
235     var i = (days * 11 + tdnbr - 7);
236     var td = tds[i];
237     var spans = td.childNodesWithTag("span");
238     if (status == '2')
239       spans[spannbr].addClassName("maybe-busy");
240     else
241       spans[spannbr].addClassName("busy");
242   }
243 }
244
245 function updateFreeBusyData(http)
246 {
247   if (http.readyState == 4) {
248     if (http.status == 200) {
249       var node = http.callbackData;
250       var slots = http.responseText.split(",");
251       var tds = node.parentNode.parentNode.cells;
252       for (var i = 0; i < slots.length; i++) {
253         if (slots[i] != '0')
254           setSlot(tds, i, slots[i]);
255       }
256     }
257     document.contactFreeBusyAjaxRequest = null;
258     if (awaitingFreeBusyRequests.length > 0)
259       displayFreeBusyForNode(awaitingFreeBusyRequests.shift());
260   }
261 }
262
263 function resetAttendeesValue()
264 {
265   var table = $("attendeesView").childNodesWithTag("div")[0].childNodesWithTag("table")[0];
266   var inputs = table.getElementsByTagName("input");
267   var uids = new Array();
268   for (var i = 0; i < inputs.length - 2; i++) {
269     var currentInput = inputs[i];
270     var uid = currentInput.getAttribute("uid");
271     if (uid) {
272       currentInput.uid = uid;
273       currentInput.setAttribute("uid", null);
274     }
275     uids.push(currentInput.uid);
276     currentInput.setAttribute("autocomplete", "off");
277     currentInput.addEventListener("keydown", onContactKeydown, false);
278     currentInput.addEventListener("blur", checkAttendee, false);
279   }
280   var input = $(freeBusySelectorId);
281   input.value = uids.join(",");
282   inputs[inputs.length - 2].setAttribute("autocomplete", "off");
283   inputs[inputs.length - 2].addEventListener("click", newAttendee, false);
284 }
285
286 function initializeFreeBusyUserSelector()
287 {
288   resetAttendeesValue();
289   resetAllFreeBusys();
290   disableAnchor($('FBStartTimeReplica_date').parentNode.childNodesWithTag('a')[0]);
291   disableAnchor($('FBEndTimeReplica_date').parentNode.childNodesWithTag('a')[0]);
292 }
293
294 function resetAllFreeBusys()
295 {
296   var table = $("attendeesView").childNodesWithTag("div")[0].childNodesWithTag("table")[0];
297   var inputs = table.getElementsByTagName("input");
298
299   for (var i = 0; i < inputs.length - 2; i++) {
300     var currentInput = inputs[i];
301     currentInput.hasfreebusy = false;
302 //     log ("input: " + currentInput.uid);
303     awaitingFreeBusyRequests.push(currentInput);
304   }
305   if (awaitingFreeBusyRequests.length > 0)
306     displayFreeBusyForNode(awaitingFreeBusyRequests.shift());
307 }
308
309 if (this.initTimeWidgets)
310   this.oldInitTimeWidgets = this.initTimeWidgets;
311
312 this.initTimeWidgets = function(widgets) {
313   if (this.oldInitTimeWidgets)
314     this.oldInitTimeWidgets(widgets);
315
316   this.timeWidgets = widgets;
317
318   widgets['start']['hour'].addEventListener("change", onTimeWidgetChange, false);
319   widgets['start']['minute'].addEventListener("change", onTimeWidgetChange, false);
320   widgets['end']['hour'].addEventListener("change", onTimeWidgetChange, false);
321   widgets['end']['minute'].addEventListener("change", onTimeWidgetChange, false);
322   widgets['start']['date'].addEventListener("change", onTimeDateWidgetChange, false);
323   widgets['end']['date'].addEventListener("change", onTimeDateWidgetChange, false);
324
325   widgets['start']['date'].assignReplica($("FBStartTimeReplica_date"));
326   widgets['end']['date'].assignReplica($("FBEndTimeReplica_date"));
327
328   var form = $("FBStartTimeReplica_date").form;
329   widgets['end']['hour'].assignReplica(form["FBEndTimeReplica_time_hour"]);
330   widgets['end']['minute'].assignReplica(form["FBEndTimeReplica_time_minute"]);
331   widgets['start']['hour'].assignReplica(form["FBStartTimeReplica_time_hour"]);
332   widgets['start']['minute'].assignReplica(form["FBStartTimeReplica_time_minute"]);
333 }
334
335 function onTimeDateWidgetChange(event) {
336   if (document.timeWidgetsFreeBusyAjaxRequest) {
337     document.timeWidgetsFreeBusyAjaxRequest.aborted = true;
338     document.timeWidgetsFreeBusyAjaxRequest.abort();
339   }
340
341   var date1 = window.timeWidgets['start']['date'].valueAsShortDateString();
342   var date2 = window.timeWidgets['end']['date'].valueAsShortDateString();
343   var attendees = $(freeBusySelectorId).value;
344   var urlstr = ( "../freeBusyTable?sday=" + date1 + "&eday=" + date2
345                  + "&attendees=" + attendees );
346   document.timeWidgetsFreeBusyAjaxRequest
347     = triggerAjaxRequest(urlstr, timeWidgetsFreeBusyCallback);
348 }
349
350 function timeWidgetsFreeBusyCallback(http)
351 {
352   if (http.readyState == 4) {
353     if (http.status == 200) {
354       var div = $("parentOf" + freeBusySelectorId.capitalize());
355       div.innerHTML = http.responseText;
356       resetAttendeesValue();
357       resetAllFreeBusys();
358       redisplayFreeBusyZone();
359     }
360     document.timeWidgetsFreeBusyAjaxRequest = null;
361   }
362 }
363
364 function onTimeWidgetChange()
365 {
366   setTimeout("redisplayFreeBusyZone();", 1000);
367 }
368
369 function onFreeBusyLoadHandler() {
370   initializeFreeBusyUserSelector();
371 }
372
373 window.addEventListener("load", onFreeBusyLoadHandler, false);