]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/UIxMailEditor.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1209 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
139   return true;
140 }
141
142 function clickedEditorSend(sender) {
143   if (!validateEditorInput(sender))
144     return false;
145
146   var input = currentAttachmentInput();
147   if (input)
148     input.parentNode.removeChild(input);
149
150   var toolbar = document.getElementById("toolbar");
151   if (!document.busyAnim)
152     document.busyAnim = startAnimation(toolbar);
153    
154   window.shouldPreserve = true;
155   document.pageform.action = "send";
156   document.pageform.submit();
157
158   return false;
159 }
160
161 function currentAttachmentInput() {
162   var input = null;
163
164   var inputs = $("attachmentsArea").getElementsByTagName("input");
165   var i = 0;
166   while (!input && i < inputs.length)
167     if ($(inputs[i]).hasClassName("currentAttachment"))
168       input = inputs[i];
169     else
170       i++;
171
172   return input;
173 }
174
175 function clickedEditorAttach(sender) {
176   var input = currentAttachmentInput();
177   if (!input) {
178     var area = $("attachmentsArea");
179
180     if (!area.style.display) {
181       area.setStyle({ display: "block" });
182       onWindowResize(null);
183     }
184     var inputs = area.getElementsByTagName("input");
185     var attachmentName = "attachment" + inputs.length;
186     var newAttachment = createElement("input", attachmentName,
187                                       "currentAttachment", null,
188                                       { type: "file",
189                                         name: attachmentName },
190                                       area);
191     Event.observe(newAttachment, "change",
192                   onAttachmentChange.bindAsEventListener(newAttachment));
193   }
194
195   return false;
196 }
197
198 function onAddAttachment() {
199   return clickedEditorAttach(null);
200 }
201
202 function onAttachmentChange(event) {
203   if (this.value == "")
204     this.parentNode.removeChild(this);
205   else {
206     this.addClassName("attachment");
207     this.removeClassName("currentAttachment");
208     var list = $("attachments");
209     createAttachment(this, list);
210   }
211 }
212
213 function createAttachment(node, list) {
214   var attachment = createElement("li", null, null, { node: node }, null, list);
215   createElement("img", null, null, { src: ResourcesURL + "/attachment.gif" },
216                 null, attachment);
217   Event.observe(attachment, "click", onRowClick);
218
219   var filename = node.value;
220   var separator;
221   if (navigator.appVersion.indexOf("Windows") > -1)
222     separator = "\\";
223   else
224     separator = "/";
225   var fileArray = filename.split(separator);
226   var attachmentName = document.createTextNode(fileArray[fileArray.length-1]);
227   attachment.appendChild(attachmentName);
228 }
229
230 function clickedEditorSave(sender) {
231   var input = currentAttachmentInput();
232   if (input)
233     input.parentNode.removeChild(input);
234
235   window.shouldPreserve = true;
236   document.pageform.action = "save";
237   document.pageform.submit();
238
239   if (window.opener && window.open && !window.closed)
240     window.opener.refreshFolderByType('draft');
241   return false;
242 }
243
244 function initMailEditor() {
245   var list = $("attachments");
246   $(list).attachMenu("attachmentsMenu");
247   var elements = list.childNodesWithTag("li");
248   for (var i = 0; i < elements.length; i++) {
249     Event.observe(elements[i], "click",
250                   onRowClick.bindAsEventListener(elements[i]));
251   }
252
253   var listContent = $("attachments").childNodesWithTag("li");
254   if (listContent.length > 0)
255     $("attachmentsArea").setStyle({ display: "block" });
256
257   onWindowResize(null);
258   Event.observe(window, "resize", onWindowResize);
259   Event.observe(window, "beforeunload", onMailEditorClose);
260 }
261
262 function getMenus() {
263   return { "attachmentsMenu": new Array(null, onRemoveAttachments,
264                                         onSelectAllAttachments,
265                                         "-",
266                                         onAddAttachment, null) };
267 }
268
269 function onRemoveAttachments() {
270   var list = $("attachments");
271   var nodes = list.getSelectedNodes();
272   for (var i = nodes.length-1; i > -1; i--) {
273     var input = $(nodes[i]).node;
274     if (input) {
275       input.parentNode.removeChild(input);
276       list.removeChild(nodes[i]);
277     }
278     else {
279       var filename = "";
280       var childNodes = nodes[i].childNodes;
281       for (var j = 0; j < childNodes.length; j++) {
282         if (childNodes[j].nodeType == 3)
283           filename += childNodes[j].nodeValue;
284       }
285       var url = "" + window.location;
286       var parts = url.split("/");
287       parts[parts.length-1] = "deleteAttachment?filename=" + encodeURIComponent(filename);
288       url = parts.join("/");
289       triggerAjaxRequest(url, attachmentDeleteCallback,
290                          nodes[i]);
291     }
292   }
293 }
294
295 function attachmentDeleteCallback(http) {
296   if (http.readyState == 4) {
297     if (isHttpStatus204(http.status)) {
298       var node = http.callbackData;
299       node.parentNode.removeChild(node);
300     }
301     else
302       log("attachmentDeleteCallback: an error occured: " + http.responseText);
303   }
304 }
305
306 function onSelectAllAttachments() {
307   var list = $("attachments");
308   var nodes = list.childNodesWithTag("li");
309   for (var i = 0; i < nodes.length; i++)
310     nodes[i].select();
311 }
312
313 function onWindowResize(event) {
314   var textarea = document.pageform.text;
315   var rowheight = (Element.getHeight(textarea) / textarea.rows);
316   var headerarea = $("headerArea");
317
318   // Set textarea position
319   textarea.setStyle({ 'top': (headerarea.getHeight() + headerarea.offsetTop) + 'px' });
320
321   var textareaoffset = textarea.offsetTop;
322
323   // Resize the textarea (message content)
324   textarea.rows = Math.round((window.height() - textareaoffset) / rowheight);
325   
326   var attachmentsarea = $("attachmentsArea");
327   var attachmentswidth = 0;
328   if (attachmentsarea.style.display)
329     attachmentswidth = attachmentsarea.getWidth();
330   var subjectfield = $(document).getElementsByClassName('headerField',
331                                                         $('subjectRow'))[0];
332   var subjectinput = $(document).getElementsByClassName('textField',
333                                                         $('subjectRow'))[0];
334
335   // Resize subject field
336   subjectinput.setStyle({ width: (window.width()
337                                   - $(subjectfield).getWidth()
338                                   - attachmentswidth
339                                   - 4 - 30) + 'px' });
340
341   // Resize address fields
342   var addresslist = $('addressList');
343   var firstselect = document.getElementsByClassName('headerField', addresslist)[0];
344   var inputwidth = ($(this).width() - $(firstselect).getWidth()
345                     - attachmentswidth - 24 - 30);
346   var addresses = document.getElementsByClassName('textField', addresslist);
347   for (var i = 0; i < addresses.length; i++)
348     addresses[i].setStyle({ width: inputwidth + 'px' });
349 }
350
351 function onMailEditorClose(event) {
352   if (window.shouldPreserve)
353     window.shouldPreserve = false;
354   else {
355     if (window.opener && window.opener.open && !window.opener.closed) {
356       var url = "" + window.location;
357       var parts = url.split("/");
358       parts[parts.length-1] = "delete";
359       url = parts.join("/");
360       window.opener.deleteDraft(url);
361     }
362   }
363
364   Event.stopObserving(window, "beforeunload", onMailEditorClose);
365 }
366
367 addEvent(window, 'load', initMailEditor);