]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/UIxMailEditor.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1147 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    document.pageform.action = "send";
148    document.pageform.submit();
149
150    return false;
151 }
152
153 function clickedEditorAttach(sender) {
154   var area = $("attachmentsArea");
155
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   document.pageform.action = "save";
214   document.pageform.submit();
215
216   return false;
217 }
218
219 function clickedEditorDelete(sender) {
220   document.pageform.action = "delete";
221   document.pageform.submit();
222   window.close();
223
224   return false;
225 }
226
227 function initMailEditor() {
228   var list = $("attachments");
229   $(list).attachMenu("attachmentsMenu");
230   var elements = list.childNodesWithTag("li");
231   for (var i = 0; i < elements.length; i++) {
232     Event.observe(elements[i], "click",
233                   onRowClick.bindAsEventListener(elements[i]));
234   }
235   onWindowResize(null);
236   Event.observe(window, "resize", onWindowResize);
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 function onWindowResize(event) {
268   var textarea = document.pageform.text;
269   var windowheight = (typeof self.innerHeight == "number" ? self.innerHeight : document.body.clientHeight);
270   var textareaoffset = textarea.offsetTop;
271   var rowheight = (Element.getHeight(textarea) / textarea.rows);
272
273   textarea.rows = Math.round((windowheight - textareaoffset) / rowheight);
274   log ("onWindowResize new number of rows = " + textarea.rows);
275 }
276
277 addEvent(window, 'load', initMailEditor);