]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/Mailer/mailer.js
added mouseover-row highlighting
[scalable-opengroupware.org] / SOGo / UI / Mailer / mailer.js
1 /*
2   Copyright (C) 2005 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 /* JavaScript for SOGo Mailer */
22
23 /*
24   DOM ids available in mail list view:
25     row_$msgid
26     div_$msgid
27     readdiv_$msgid
28     unreaddiv_$msgid
29
30   Window Properties:
31     width, height
32     bool: resizable, scrollbars, toolbar, location, directories, status,
33           menubar, copyhistory
34 */
35
36 /* mail list */
37
38 function openMessageWindow(sender, msguid, url) {
39   return window.open(url, "SOGo_msg_" + msguid,
40            "width=640,height=480,resizable=1,scrollbars=1,toolbar=0," +
41            "location=0,directories=0,status=0,menubar=0,copyhistory=0")
42 }
43
44 function clickedUid(sender, msguid) {
45   resetSelection(window);
46   openMessageWindow(sender, msguid, msguid + "/tb.view");
47   return true;
48 }
49 function doubleClickedUid(sender, msguid) {
50   alert("DOUBLE Clicked " + msguid);
51
52   return false;
53 }
54
55 function toggleMailSelect(sender) {
56   var row;
57   row = document.getElementById(sender.name);
58   row.className = sender.checked ? "tableview_selected" : "tableview";
59 }
60 function collectSelectedRows() {
61   var pageform = document.forms['pageform'];
62   var rows = new Array();
63
64   for (key in pageform) {
65     if (key.indexOf("row_") != 0)
66       continue;
67
68     if (!pageform[key].checked)
69       continue;
70     
71     rows[rows.length] = key.substring(4, key.length);
72   }
73   return rows;
74 }
75
76 function clearSearch(sender) {
77   var searchField = window.document.getElementById("searchtext");
78   if (searchField) searchField.value="";
79   return true;
80 }
81
82 /* compose support */
83
84 function clickedCompose(sender) {
85   var urlstr;
86   
87   urlstr = "compose";
88   window.open(urlstr, "SOGo_compose",
89               "width=680,height=480,resizable=1,scrollbars=1,toolbar=0," +
90               "location=0,directories=0,status=0,menubar=0,copyhistory=0");
91   return false; /* stop following the link */
92 }
93
94 /* mail editor */
95
96 function validateEditorInput(sender) {
97   var errortext = "";
98   var field;
99   
100   field = document.pageform.subject;
101   if (field.value == "")
102     errortext = errortext + "Missing Subject\n";
103   
104   if (errortext.length > 0) {
105     alert("validation failed:\n" + errortext);
106     return false;
107   }
108   return true;
109 }
110
111 function clickedEditorSend(sender) {
112   if (!validateEditorInput(sender))
113     return false;
114
115   // TODO: validate whether we have a recipient! (#1220)
116   
117   document.pageform.action="send";
118   document.pageform.submit();
119   // if everything is ok, close the window
120   return true;
121 }
122
123 function clickedEditorAttach(sender) {
124   var urlstr;
125   
126   urlstr = "viewAttachments";
127   window.open(urlstr, "SOGo_attach",
128               "width=320,height=320,resizable=1,scrollbars=1,toolbar=0," +
129               "location=0,directories=0,status=0,menubar=0,copyhistory=0");
130   return false; /* stop following the link */
131 }
132
133 function clickedEditorSave(sender) {
134   document.pageform.action="save";
135   document.pageform.submit();
136   refreshOpener();
137   return true;
138 }
139
140 function clickedEditorDelete(sender) {
141   document.pageform.action="delete";
142   document.pageform.submit();
143   refreshOpener();
144   window.close();
145   return true;
146 }
147
148 /* addressbook helpers */
149
150 function openAnais(sender) {
151   var urlstr;
152
153   urlstr = "anais";
154   var w = window.open(urlstr, "Anais",
155                       "width=350,height=600,left=10,top=10,toolbar=no," +
156                       "dependent=yes,menubar=no,location=no,resizable=yes," +
157                       "scrollbars=yes,directories=no,status=no");
158   w.focus();
159 }
160
161 function openAddressbook(sender) {
162   var urlstr;
163   
164   urlstr = "addressbook";
165   var w = window.open(urlstr, "Addressbook",
166                       "width=600,height=400,left=10,top=10,toolbar=no," +
167                       "dependent=yes,menubar=no,location=no,resizable=yes," +
168                       "scrollbars=yes,directories=no,status=no");
169   w.focus();
170 }
171
172 /* filters */
173
174 function clickedFilter(sender, scriptname) {
175   var urlstr;
176   
177   urlstr = scriptname + "/edit";
178   window.open(urlstr, "SOGo_filter_" + scriptname,
179               "width=640,height=480,resizable=1,scrollbars=1,toolbar=0," +
180               "location=0,directories=0,status=0,menubar=0,copyhistory=0")
181   return true;
182 }
183
184 function clickedNewFilter(sender) {
185   var urlstr;
186   
187   urlstr = "create";
188   window.open(urlstr, "SOGo_filter",
189               "width=680,height=480,resizable=1,scrollbars=1,toolbar=0," +
190               "location=0,directories=0,status=0,menubar=0,copyhistory=0");
191   return false; /* stop following the link */
192 }
193
194 /* generic stuff */
195
196 function createHTTPClient() {
197   // http://developer.apple.com/internet/webcontent/xmlhttpreq.html
198   if (typeof XMLHttpRequest != "undefined")
199     return new XMLHttpRequest();
200   
201   try { return new ActiveXObject("Msxml2.XMLHTTP"); } 
202   catch (e) { }
203   try { return new ActiveXObject("Microsoft.XMLHTTP"); } 
204   catch (e) { }
205   return null;
206 }
207
208 function resetSelection(win) {
209   var t = "";
210   if (win && win.getSelection) {
211     t = win.getSelection().toString();
212     win.getSelection().removeAllRanges();
213   }
214   return t;
215 }
216
217 function refreshOpener() {
218   if (window.opener && !window.opener.closed) {
219     window.opener.location.reload();
220   }
221 }
222
223 function getQueryParaArray(s) {
224   if (s.charAt(0) == "?") s = s.substr(1, s.length - 1);
225   return s.split("&");
226 }
227 function getQueryParaValue(s, name) {
228   var t;
229   
230   t = getQueryParaArray(s);
231   for (var i = 0; i < t.length; i++) {
232     var s = t[i];
233     
234     if (s.indexOf(name) != 0)
235       continue;
236     
237     s = s.substr(name.length, s.length - name.length);
238     return decodeURIComponent(s);
239   }
240   return None;
241 }
242
243 function triggerOpenerCallback() {
244   /* this code has some issue if the folder has no proper trailing slash! */
245   if (window.opener && !window.opener.closed) {
246     var t, cburl;
247     
248     t = getQueryParaValue(window.location.search, "openerurl=");
249     cburl = window.opener.location.href;
250     if (cburl[cburl.length - 1] != "/") {
251       cburl = cburl.substr(0, cburl.lastIndexOf("/") + 1);
252     }
253     cburl = cburl + t;
254     window.opener.location.href = cburl;
255   }
256 }
257
258 /* mail list DOM changes */
259
260 function markMailInWindow(win, msguid, markread) {
261   var msgDiv;
262   
263   msgDiv = win.document.getElementById("div_" + msguid);
264   if (msgDiv) {
265     if (markread) {
266       msgDiv.className = "mailer_readmailsubject";
267     
268       msgDiv = win.document.getElementById("unreaddiv_" + msguid);
269       if (msgDiv) msgDiv.style.display = "none";
270       msgDiv = win.document.getElementById("readdiv_" + msguid);
271       if (msgDiv) msgDiv.style.display = "block";
272     }
273     else {
274       msgDiv.className = "mailer_unreadmailsubject";
275     
276       msgDiv = win.document.getElementById("readdiv_" + msguid);
277       if (msgDiv) msgDiv.style.display = "none";
278       msgDiv = win.document.getElementById("unreaddiv_" + msguid);
279       if (msgDiv) msgDiv.style.display = "block";
280     }
281     return true;
282   }
283   else
284     return false;
285 }
286 function markMailReadInWindow(win, msguid) {
287   return markMailInWindow(win, msguid, true);
288 }
289
290 /* main window */
291
292 function reopenToRemoveLocationBar() {
293   // we cannot really use this, see below at the close comment
294   if (window.locationbar && window.locationbar.visible) {
295     newwin = window.open(window.location.href, "SOGo",
296                          "width=800,height=600,resizable=1,scrollbars=1," +
297                          "toolbar=0,location=0,directories=0,status=0," + 
298                          "menubar=0,copyhistory=0");
299     if (newwin) {
300       window.close(); // this does only work for windows opened by scripts!
301       newwin.focus();
302       return true;
303     }
304     return false;
305   }
306   return true;
307 }
308
309 /* mail list reply */
310
311 function openMessageWindowsForSelection(sender, action) {
312   var rows  = collectSelectedRows();
313   var idset = "";
314   
315   for (var i = 0; i < rows.length; i++)
316     win = openMessageWindow(sender, rows[i], rows[i] + "/" + action);
317 }
318
319 function mailListMarkMessage(sender, action, msguid, markread) {
320   var url;
321   var http = createHTTPClient();
322
323   url = action + "?uid=" + msguid;
324
325   if (http) {
326     // TODO: add parameter to signal that we are only interested in OK
327     http.open("GET", url + "&jsonly=1", false);
328     http.send(null);
329     if (http.status != 200) {
330       // TODO: refresh page?
331       alert("Message Mark Failed: " + http.statusText);
332       window.opener.location.reload();
333     }
334     else {
335       markMailInWindow(window, msguid, markread);
336     }
337   }
338   else {
339     window.opener.location.href = url;
340   }
341 }
342
343 /* maillist row highlight */
344
345 var oldMaillistHighlight = None; // to remember deleted/selected style
346
347 function ml_highlight(sender) {
348   oldMaillistHighlight = sender.className;
349   sender.className = "tableview_highlight";
350 }
351 function ml_lowlight(sender) {
352   if (oldMaillistHighlight) {
353     sender.className = oldMaillistHighlight;
354     oldMaillistHighlight = None;
355   }
356   else
357     sender.className = "tableview";
358 }