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