]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/UIxMailEditor.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1149 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 //   log (urlstr);
13   var w = window.open(urlstr, "Addressbook",
14                       "width=640,height=400,resizable=1,scrollbars=0");
15   w.selector = selector;
16   w.opener = this;
17   w.focus();
18
19   return false;
20  }
21
22 function addContact(tag, fullContactName, contactId, contactName, contactEmail) {
23   if (!mailIsRecipient(contactEmail)) {
24     var neededOptionValue = 0;
25     if (tag == "cc")
26       neededOptionValue = 1;
27     else if (tag == "bcc")
28       neededOptionValue = 2;
29
30     var stop = false;
31     var counter = 0;
32     var currentRow = $('row_' + counter);
33     while (currentRow && !stop) {
34       var currentValue = $(currentRow.childNodesWithTag("span")[1]).childNodesWithTag("input")[0].value;
35       if (currentValue == neededOptionValue) {
36         stop = true;
37         insertContact($("addr_" + counter), contactName, contactEmail);
38       }
39       counter++;
40       currentRow = $('row_' + counter);
41     }
42
43     if (!stop) {
44       fancyAddRow(false, "");
45       $("row_" + counter).childNodesWithTag("span")[0].childNodesWithTag("select")[0].value
46         = neededOptionValue;
47       insertContact($("addr_" + counter), contactName, contactEmail);
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 (!UIxRecipientSelectorHasRecipients())
133       errortext = errortext + labels.error_missingrecipients + "\n";
134    
135    if (errortext.length > 0) {
136       alert(labels.error_validationfailed.decodeEntities() + ":\n"
137             + errortext.decodeEntities());
138       return false;
139    }
140    return true;
141 }
142
143 function clickedEditorSend(sender) {
144    if (!validateEditorInput(sender))
145       return false;
146
147    window.shouldPreserve = true;
148    document.pageform.action = "send";
149    document.pageform.submit();
150
151    return false;
152 }
153
154 function clickedEditorAttach(sender) {
155   var area = $("attachmentsArea");
156   area.setStyle({ display: "block" });
157   
158   var inputs = area.getElementsByTagName("input");
159   var attachmentName = "attachment" + inputs.length;
160   var newAttachment = createElement("input", attachmentName,
161                                     "currentAttachment", null,
162                                     { type: "file",
163                                       name: attachmentName },
164                                     area);
165   Event.observe(newAttachment, "change",
166                 onAttachmentChange.bindAsEventListener(newAttachment));
167
168   return false;
169 }
170
171 function onAddAttachment() {
172   var area = $("attachmentsArea");
173   var inputs = area.getElementsByTagName("input");
174   var attachmentName = "attachment" + inputs.length;
175   var newAttachment = createElement("input", attachmentName,
176                                     "currentAttachment", null,
177                                     { type: "file",
178                                       name: attachmentName },
179                                     area);
180   Event.observe(newAttachment, "change",
181                 onAttachmentChange.bindAsEventListener(newAttachment));
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   return false;
218 }
219
220 function initMailEditor() {
221   var list = $("attachments");
222   $(list).attachMenu("attachmentsMenu");
223   var elements = list.childNodesWithTag("li");
224   for (var i = 0; i < elements.length; i++) {
225     Event.observe(elements[i], "click",
226                   onRowClick.bindAsEventListener(elements[i]));
227   }
228
229   var listContent = $("attachments").childNodesWithTag("li");
230   if (listContent.length > 0)
231     $("attachmentsArea").setStyle({ display: "block" });
232
233   onWindowResize(null);
234   Event.observe(window, "resize", onWindowResize);
235   Event.observe(window, "beforeunload", onMailEditorClose);
236 }
237
238 function getMenus() {
239   return { "attachmentsMenu": new Array(null, onRemoveAttachments,
240                                         onSelectAllAttachments,
241                                         "-",
242                                         onAddAttachment, null) };
243 }
244
245 function onRemoveAttachments() {
246   var list = $("attachments");
247   var nodes = list.getSelectedNodes();
248   for (var i = nodes.length-1; i > -1; i--) {
249     var input = $(nodes[i]).node;
250     if (input) {
251       input.parentNode.removeChild(input);
252       list.removeChild(nodes[i]);
253     }
254     else {
255       var filename = "";
256       var childNodes = nodes[i].childNodes;
257       for (var j = 0; j < childNodes.length; j++) {
258         if (childNodes[j].nodeType == 3)
259           filename += childNodes[j].nodeValue;
260       }
261       var url = "" + window.location;
262       var parts = url.split("/");
263       parts[parts.length-1] = "deleteAttachment?filename=" + encodeURIComponent(filename);
264       url = parts.join("/");
265       triggerAjaxRequest(url, attachmentDeleteCallback,
266                          nodes[i]);
267     }
268   }
269 }
270
271 function attachmentDeleteCallback(http) {
272   if (http.readyState == 4) {
273     if (http.status == 204) {
274       var node = http.callbackData;
275       node.parentNode.removeChild(node);
276     }
277     else
278       log("attachmentDeleteCallback: an error occured: " + http.responseText);
279   }
280 }
281
282 function onSelectAllAttachments() {
283   var list = $("attachments");
284   var nodes = list.childNodesWithTag("li");
285   for (var i = 0; i < nodes.length; i++)
286     nodes[i].select();
287 }
288
289 function onWindowResize(event) {
290   var textarea = document.pageform.text;
291   var windowheight = (typeof self.innerHeight == "number" ? self.innerHeight : document.body.clientHeight);
292   var textareaoffset = textarea.offsetTop;
293   var rowheight = (Element.getHeight(textarea) / textarea.rows);
294
295   textarea.rows = Math.round((windowheight - textareaoffset) / rowheight);
296   log ("onWindowResize new number of rows = " + textarea.rows);
297 }
298
299 function onMailEditorClose(event) {
300   if (window.shouldPreserve)
301     window.shouldPreserve = false;
302   else {
303     var url = "" + window.location;
304     var parts = url.split("/");
305     parts[parts.length-1] = "delete";
306     url = parts.join("/");
307
308     http = createHTTPClient();
309     http.open("POST", url, false /* not async */);
310     http.send("");
311   }
312 }
313
314 addEvent(window, 'load', initMailEditor);