]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/UIxMailEditor.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1141 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    return false;
152 }
153
154 function clickedEditorAttach(sender) {
155   var area = $("attachmentsArea");
156
157   area.setStyle({ display: "block" });
158   
159   var inputs = area.getElementsByTagName("input");
160   var attachmentName = "attachment" + inputs.length;
161   var newAttachment = createElement("input", attachmentName,
162                                     "currentAttachment", null,
163                                     { type: "file",
164                                       name: attachmentName },
165                                     area);
166   Event.observe(newAttachment, "change",
167                 onAttachmentChange.bindAsEventListener(newAttachment));
168
169   return false;
170 }
171
172 function onAddAttachment() {
173   var area = $("attachmentsArea");
174   var inputs = area.getElementsByTagName("input");
175   var attachmentName = "attachment" + inputs.length;
176   var newAttachment = createElement("input", attachmentName,
177                                     "currentAttachment", null,
178                                     { type: "file",
179                                       name: attachmentName },
180                                     area);
181   Event.observe(newAttachment, "change",
182                 onAttachmentChange.bindAsEventListener(newAttachment));
183 }
184
185 function onAttachmentChange(event) {
186   if (this.value == "")
187     this.parentNode.removeChild(this);
188   else {
189     this.addClassName("attachment");
190     this.removeClassName("currentAttachment");
191     var list = $("attachments");
192     createAttachment(this, list);
193   }
194 }
195
196 function createAttachment(node, list) {
197   var attachment = createElement("li", null, null, { node: node }, null, list);
198   createElement("img", null, null, { src: ResourcesURL + "/attachment.gif" },
199                 null, attachment);
200   Event.observe(attachment, "click", onRowClick);
201
202   var filename = node.value;
203   var separator;
204   if (navigator.appVersion.indexOf("Windows") > -1)
205     separator = "\\";
206   else
207     separator = "/";
208   var fileArray = filename.split(separator);
209   var attachmentName = document.createTextNode(fileArray[fileArray.length-1]);
210   attachment.appendChild(attachmentName);
211 }
212
213 function clickedEditorSave(sender) {
214    document.pageform.action = "save";
215    document.pageform.submit();
216    refreshOpener();
217
218   return false;
219 }
220
221 function clickedEditorDelete(sender) {
222    document.pageform.action = "delete";
223    document.pageform.submit();
224    refreshOpener();
225    window.close();
226
227   return false;
228 }
229
230 function initMailEditor() {
231   var list = $("attachments");
232   $(list).attachMenu("attachmentsMenu");
233   var elements = list.childNodesWithTag("li");
234   for (var i = 0; i < elements.length; i++)
235     Event.observe(elements[i], "click",
236                   onRowClick.bindAsEventListener(elements[i]));
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       window.alert("Server attachments not handled");
257   }
258 }
259
260 function onSelectAllAttachments() {
261   var list = $("attachments");
262   var nodes = list.childNodesWithTag("li");
263   for (var i = 0; i < nodes.length; i++)
264     nodes[i].select();
265 }
266
267 window.addEventListener("load", initMailEditor, false);