]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/UIxMailEditor.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1265 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / WebServerResources / UIxMailEditor.js
1 var contactSelectorAction = 'mailer-contacts';
2
3 function onContactAdd() {
4   var selector = null;
5   var selectorURL = '?popup=YES&selectorId=mailer-contacts';
6  
7   urlstr = ApplicationBaseURL 
8     + "../Contacts/"
9     + contactSelectorAction + selectorURL;
10   var w = window.open(urlstr, "Addressbook",
11                       "width=640,height=400,resizable=1,scrollbars=0");
12   w.selector = selector;
13   w.opener = this;
14   w.focus();
15
16   return false;
17 }
18
19 function addContact(tag, fullContactName, contactId, contactName, contactEmail) {
20   if (!mailIsRecipient(contactEmail)) {
21     var neededOptionValue = 0;
22     if (tag == "cc")
23       neededOptionValue = 1;
24     else if (tag == "bcc")
25       neededOptionValue = 2;
26
27     var stop = false;
28     var counter = 0;
29     var currentRow = $('row_' + counter);
30     while (currentRow && !stop) {
31       var currentValue = $(currentRow.childNodesWithTag("td")[1]).childNodesWithTag("input")[0].value;
32       if (currentValue == neededOptionValue) {
33         stop = true;
34         insertContact($("addr_" + counter), contactName, contactEmail);
35       }
36       counter++;
37       currentRow = $('row_' + counter);
38     }
39
40     if (!stop) {
41       fancyAddRow(false, "");
42       $($("row_" + counter).childNodesWithTag("td")[0]).childNodesWithTag("select")[0].value
43         = neededOptionValue;
44       insertContact($("addr_" + counter), contactName, contactEmail);
45       onWindowResize(null);
46     }
47   }
48 }
49
50 function mailIsRecipient(mailto) {
51   var isRecipient = false;
52
53   var counter = 0;
54   var currentRow = $('row_' + counter);
55
56   var email = extractEmailAddress(mailto).toUpperCase();
57
58   while (currentRow && !isRecipient) {
59     var currentValue = $("addr_"+counter).value.toUpperCase();
60     if (currentValue.indexOf(email) > -1)
61       isRecipient = true;
62     else
63       {
64         counter++;
65         currentRow = $('row_' + counter);
66       }
67   }
68
69   return isRecipient;
70 }
71
72 function insertContact(inputNode, contactName, contactEmail) {
73   var value = '' + inputNode.value;
74
75   var newContact = contactName;
76   if (newContact.length > 0)
77     newContact += ' <' + contactEmail + '>';
78   else
79     newContact = contactEmail;
80
81   if (value.length > 0)
82     value += ", ";
83   value += newContact;
84
85   inputNode.value = value;
86 }
87
88 function toggleAttachments() {
89   var div = $("attachmentsArea");
90   if (div.style.display)
91     div.style.display = "";
92   else
93     div.style.display = "block;";
94
95   return false;
96 }
97
98 function updateInlineAttachmentList(sender, attachments) {
99   var count = 0;
100
101   var div = $("attachmentsArea");
102   if (attachments)
103     count = attachments.length;
104   if (count)
105     {
106       var text  = "";
107       for (var i = 0; i < count; i++) {
108         text = text + attachments[i];
109         text = text + '<br />';
110       }
111
112       var e = $('compose_attachments_list');
113       e.innerHTML = text;
114       if (!div.style.display)
115         div.style.display = "block;";
116     }
117   else
118     div.style.display = "";
119 }
120 /* mail editor */
121
122 function validateEditorInput(sender) {
123   var errortext = "";
124   var field;
125    
126   field = document.pageform.subject;
127   if (field.value == "")
128     errortext = errortext + labels["error_missingsubject"] + "\n";
129
130   if (!hasRecipients())
131     errortext = errortext + labels["error_missingrecipients"] + "\n";
132    
133   if (errortext.length > 0) {
134     alert(labels["error_validationfailed"] + ":\n" + errortext);
135     return false;
136   }
137
138   return true;
139 }
140
141 function clickedEditorSend(sender) {
142   if (!validateEditorInput(sender))
143     return false;
144
145   var input = currentAttachmentInput();
146   if (input)
147     input.parentNode.removeChild(input);
148
149   var toolbar = document.getElementById("toolbar");
150   if (!document.busyAnim)
151     document.busyAnim = startAnimation(toolbar);
152    
153   window.shouldPreserve = true;
154   document.pageform.action = "send";
155   document.pageform.submit();
156
157   return false;
158 }
159
160 function currentAttachmentInput() {
161   var input = null;
162
163   var inputs = $("attachmentsArea").getElementsByTagName("input");
164   var i = 0;
165   while (!input && i < inputs.length)
166     if ($(inputs[i]).hasClassName("currentAttachment"))
167       input = inputs[i];
168     else
169       i++;
170
171   return input;
172 }
173
174 function clickedEditorAttach(sender) {
175   var input = currentAttachmentInput();
176   if (!input) {
177     var area = $("attachmentsArea");
178
179     if (!area.style.display) {
180       area.setStyle({ display: "block" });
181       onWindowResize(null);
182     }
183     var inputs = area.getElementsByTagName("input");
184     var attachmentName = "attachment" + inputs.length;
185     var newAttachment = createElement("input", attachmentName,
186                                       "currentAttachment", null,
187                                       { type: "file",
188                                         name: attachmentName },
189                                       area);
190     Event.observe(newAttachment, "change",
191                   onAttachmentChange.bindAsEventListener(newAttachment));
192   }
193
194   return false;
195 }
196
197 function onAddAttachment() {
198   return clickedEditorAttach(null);
199 }
200
201 function onAttachmentChange(event) {
202   if (this.value == "")
203     this.parentNode.removeChild(this);
204   else {
205     this.addClassName("attachment");
206     this.removeClassName("currentAttachment");
207     var list = $("attachments");
208     createAttachment(this, list);
209   }
210 }
211
212 function createAttachment(node, list) {
213   var attachment = createElement("li", null, null, { node: node }, null, list);
214   createElement("img", null, null, { src: ResourcesURL + "/attachment.gif" },
215                 null, attachment);
216   Event.observe(attachment, "click", onRowClick);
217
218   var filename = node.value;
219   var separator;
220   if (navigator.appVersion.indexOf("Windows") > -1)
221     separator = "\\";
222   else
223     separator = "/";
224   var fileArray = filename.split(separator);
225   var attachmentName = document.createTextNode(fileArray[fileArray.length-1]);
226   attachment.appendChild(attachmentName);
227 }
228
229 function clickedEditorSave(sender) {
230   var input = currentAttachmentInput();
231   if (input)
232     input.parentNode.removeChild(input);
233
234   window.shouldPreserve = true;
235   document.pageform.action = "save";
236   document.pageform.submit();
237
238   if (window.opener && window.open && !window.closed)
239     window.opener.refreshFolderByType('draft');
240   return false;
241 }
242
243 function initMailEditor() {
244   var list = $("attachments");
245   $(list).attachMenu("attachmentsMenu");
246   var elements = list.childNodesWithTag("li");
247   for (var i = 0; i < elements.length; i++) {
248     Event.observe(elements[i], "click",
249                   onRowClick.bindAsEventListener(elements[i]));
250   }
251
252   var listContent = $("attachments").childNodesWithTag("li");
253   if (listContent.length > 0)
254     $("attachmentsArea").setStyle({ display: "block" });
255
256   var list = $("addressList");
257   TableKit.Resizable.init(list, {'trueResize' : true, 'keepWidth' : true});
258
259   onWindowResize(null);
260   Event.observe(window, "resize", onWindowResize);
261   Event.observe(window, "beforeunload", onMailEditorClose);
262 }
263
264 function getMenus() {
265   return { "attachmentsMenu": new Array(null, onRemoveAttachments,
266                                         onSelectAllAttachments,
267                                         "-",
268                                         onAddAttachment, null) };
269 }
270
271 function onRemoveAttachments() {
272   var list = $("attachments");
273   var nodes = list.getSelectedNodes();
274   for (var i = nodes.length-1; i > -1; i--) {
275     var input = $(nodes[i]).node;
276     if (input) {
277       input.parentNode.removeChild(input);
278       list.removeChild(nodes[i]);
279     }
280     else {
281       var filename = "";
282       var childNodes = nodes[i].childNodes;
283       for (var j = 0; j < childNodes.length; j++) {
284         if (childNodes[j].nodeType == 3)
285           filename += childNodes[j].nodeValue;
286       }
287       var url = "" + window.location;
288       var parts = url.split("/");
289       parts[parts.length-1] = "deleteAttachment?filename=" + encodeURIComponent(filename);
290       url = parts.join("/");
291       triggerAjaxRequest(url, attachmentDeleteCallback,
292                          nodes[i]);
293     }
294   }
295 }
296
297 function attachmentDeleteCallback(http) {
298   if (http.readyState == 4) {
299     if (isHttpStatus204(http.status)) {
300       var node = http.callbackData;
301       node.parentNode.removeChild(node);
302     }
303     else
304       log("attachmentDeleteCallback: an error occured: " + http.responseText);
305   }
306 }
307
308 function onSelectAllAttachments() {
309   var list = $("attachments");
310   var nodes = list.childNodesWithTag("li");
311   for (var i = 0; i < nodes.length; i++)
312     nodes[i].select();
313 }
314
315 function onWindowResize(event) {
316   var textarea = document.pageform.text;
317   var rowheight = (Element.getHeight(textarea) / textarea.rows);
318   var headerarea = $("headerArea");
319   
320   var attachmentsarea = $("attachmentsArea");
321   var attachmentswidth = 0;
322   if (attachmentsarea.style.display) {
323     attachmentswidth = attachmentsarea.getWidth();
324     // Resize of attachment list is b0rken under IE7
325 //    fromfield = $(document).getElementsByClassName('headerField',
326 //                                                 headerarea)[0];
327 //    $("attachments").setStyle({ height: (headerarea.getHeight() - fromfield.getHeight() - 10) + 'px' });
328   }
329 //  var subjectfield = $(document).getElementsByClassName('headerField',
330 //                                                      $('subjectRow'))[0];
331 //  var subjectinput = $(document).getElementsByClassName('textField',
332 //                                                      $('subjectRow'))[0];
333 //
334   // Resize subject field
335 //  subjectinput.setStyle({ width: (window.width()
336 //                                - $(subjectfield).getWidth()
337 //                                - attachmentswidth
338 //                                - 4 - 30) + 'px' });
339
340   // Resize address fields
341   var addresslist = $('addressList');
342   addresslist.setStyle({ width: ($(this).width() - attachmentswidth - 10) + 'px' });
343
344   // Set textarea position
345   textarea.setStyle({ 'top': (headerarea.getHeight() + headerarea.offsetTop) + 'px' });
346
347   var textareaoffset = textarea.offsetTop;
348
349   // Resize the textarea (message content)
350   textarea.rows = Math.round((window.height() - textareaoffset) / rowheight);
351 }
352
353 function onMailEditorClose(event) {
354   if (window.shouldPreserve)
355     window.shouldPreserve = false;
356   else {
357     if (window.opener && window.opener.open && !window.opener.closed) {
358       var url = "" + window.location;
359       var parts = url.split("/");
360       parts[parts.length-1] = "delete";
361       url = parts.join("/");
362       window.opener.deleteDraft(url);
363     }
364   }
365
366   Event.stopObserving(window, "beforeunload", onMailEditorClose);
367 }
368
369 FastInit.addOnLoad(initMailEditor);