]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/UIxMailEditor.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1127 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
34            && !stop) {
35       var currentValue = currentRow.childNodesWithTag("span")[1].childNodesWithTag("input")[0].value;
36       if (currentValue == neededOptionValue) {
37         stop = true;
38         insertContact($("addr_" + counter), contactName, contactEmail);
39       }
40       counter++;
41       currentRow = $('row_' + counter);
42     }
43
44     if (!stop) {
45       fancyAddRow(false, "");
46       $("row_" + counter).childNodesWithTag("span")[0].childNodesWithTag("select")[0].value
47         = neededOptionValue;
48       insertContact($("addr_" + counter), contactName, contactEmail);
49     }
50   }
51 }
52
53 function mailIsRecipient(mailto) {
54   var isRecipient = false;
55
56   var counter = 0;
57   var currentRow = $('row_' + counter);
58
59   var email = extractEmailAddress(mailto).toUpperCase();
60
61   while (currentRow && !isRecipient) {
62     var currentValue = $("addr_"+counter).value.toUpperCase();
63     if (currentValue.indexOf(email) > -1)
64       isRecipient = true;
65     else
66       {
67         counter++;
68         currentRow = $('row_' + counter);
69       }
70   }
71
72   return isRecipient;
73 }
74
75 function insertContact(inputNode, contactName, contactEmail) {
76   var value = '' + inputNode.value;
77
78   var newContact = contactName;
79   if (newContact.length > 0)
80     newContact += ' <' + contactEmail + '>';
81   else
82     newContact = contactEmail;
83
84   if (value.length > 0)
85     value += ", ";
86   value += newContact;
87
88   inputNode.value = value;
89 }
90
91 function toggleAttachments() {
92   var div = $("attachmentsArea");
93   if (div.style.display)
94     div.style.display = "";
95   else
96     div.style.display = "block;";
97
98   return false;
99 }
100
101 function updateInlineAttachmentList(sender, attachments) {
102   var count = 0;
103
104   var div = $("attachmentsArea");
105   if (attachments)
106     count = attachments.length;
107   if (count)
108     {
109       var text  = "";
110       for (var i = 0; i < count; i++) {
111         text = text + attachments[i];
112         text = text + '<br />';
113       }
114
115       var e = $('compose_attachments_list');
116       e.innerHTML = text;
117       if (!div.style.display)
118         div.style.display = "block;";
119     }
120   else
121     div.style.display = "";
122 }
123 /* mail editor */
124
125 function validateEditorInput(sender) {
126    var errortext = "";
127    var field;
128    
129    field = document.pageform.subject;
130    if (field.value == "")
131       errortext = errortext + labels.error_missingsubject + "\n";
132
133    if (!UIxRecipientSelectorHasRecipients())
134       errortext = errortext + labels.error_missingrecipients + "\n";
135    
136    if (errortext.length > 0) {
137       alert(labels.error_validationfailed.decodeEntities() + ":\n"
138             + errortext.decodeEntities());
139       return false;
140    }
141    return true;
142 }
143
144 function clickedEditorSend(sender) {
145    if (!validateEditorInput(sender))
146       return false;
147
148    document.pageform.action = "send";
149    document.pageform.submit();
150
151    window.alert("cocou");
152
153    return false;
154 }
155
156 function clickedEditorAttach(sender) {
157   var area = $("attachmentsArea");
158
159   area.setStyle({ display: "block" });
160   
161   var inputs = area.getElementsByTagName("input");
162   var attachmentName = "attachment" + inputs.length;
163   var newAttachment = createElement("input", attachmentName,
164                                     "currentAttachment", null,
165                                     { type: "file",
166                                       name: attachmentName },
167                                     area);
168   Event.observe(newAttachment, "change",
169                 onAttachmentChange.bindAsEventListener(newAttachment));
170
171   return false;
172 }
173
174 function onAddAttachment() {
175   var area = $("attachmentsArea");
176   var inputs = area.getElementsByTagName("input");
177   var attachmentName = "attachment" + inputs.length;
178   var newAttachment = createElement("input", attachmentName,
179                                     "currentAttachment", null,
180                                     { type: "file",
181                                       name: attachmentName },
182                                     area);
183   Event.observe(newAttachment, "change",
184                 onAttachmentChange.bindAsEventListener(newAttachment));
185 }
186
187 function onAttachmentChange(event) {
188   if (this.value == "")
189     this.parentNode.removeChild(this);
190   else {
191     this.addClassName("attachment");
192     this.removeClassName("currentAttachment");
193     var list = $("attachments");
194     createAttachment(this, list);
195   }
196 }
197
198 function createAttachment(node, list) {
199   var attachment = createElement("li", null, null, { node: node }, null, list);
200   createElement("img", null, null, { src: ResourcesURL + "/attachment.gif" },
201                 null, attachment);
202   Event.observe(attachment, "click", onRowClick);
203
204   var filename = node.value;
205   var separator;
206   if (navigator.appVersion.indexOf("Windows") > -1)
207     separator = "\\";
208   else
209     separator = "/";
210   var fileArray = filename.split(separator);
211   var attachmentName = document.createTextNode(fileArray[fileArray.length-1]);
212   attachment.appendChild(attachmentName);
213 }
214
215 function clickedEditorSave(sender) {
216    document.pageform.action = "save";
217    document.pageform.submit();
218    refreshOpener();
219
220   return false;
221 }
222
223 function clickedEditorDelete(sender) {
224    document.pageform.action = "delete";
225    document.pageform.submit();
226    refreshOpener();
227    window.close();
228
229   return false;
230 }
231
232 function initMailEditor() {
233   var list = $("attachments");
234   $(list).attachMenu("attachmentsMenu");
235   var elements = list.childNodesWithTag("li");
236   for (var i = 0; i < elements.length; i++)
237     Event.observe(elements[i], "click",
238                   onRowClick.bindAsEventListener(elements[i]));
239 }
240
241 function getMenus() {
242   return { "attachmentsMenu": new Array(null, onRemoveAttachments,
243                                         onSelectAllAttachments,
244                                         "-",
245                                         onAddAttachment, null) };
246 }
247
248 function onRemoveAttachments() {
249   var list = $("attachments");
250   var nodes = list.getSelectedNodes();
251   for (var i = nodes.length-1; i > -1; i--) {
252     var input = $(nodes[i]).node;
253     if (input) {
254       input.parentNode.removeChild(input);
255       list.removeChild(nodes[i]);
256     }
257     else
258       window.alert("Server attachments not handled");
259   }
260 }
261
262 function onSelectAllAttachments() {
263   var list = $("attachments");
264   var nodes = list.childNodesWithTag("li");
265   for (var i = 0; i < nodes.length; i++)
266     nodes[i].select();
267 }
268
269 window.addEventListener("load", initMailEditor, false);