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