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