]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/WebServerResources/generic.js
added query parameter functions
[scalable-opengroupware.org] / SOGo / UI / WebServerResources / generic.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 /* some generic JavaScript code for SOGo */
22
23 /* generic stuff */
24
25 function ml_stripActionInURL(url) {
26   if (url[url.length - 1] != '/') {
27     var i;
28     
29     i = url.lastIndexOf("/");
30     if (i != -1) url = url.substring(0, i);
31   }
32   if (url[url.length - 1] != '/') // ensure trailing slash
33     url = url + "/";
34   return url;
35 }
36
37 function escapeHTML(s) {
38         s = s.replace(/&/g, "&");
39         s = s.replace(/</g, "&lt;");
40         s = s.replace(/>/g, "&gt;");
41         s = s.replace(/"/g, "&quot;");
42         return s;
43 }
44 function unescapeHTML(s) {
45         s = s.replace(/&lt;/g, "<");
46         s = s.replace(/&gt;/g, ">");
47         s = s.replace(/&quot;/g, '"');
48         s = s.replace(/&amp;/g, "&");
49         return s;
50 }
51
52 function createHTTPClient() {
53   // http://developer.apple.com/internet/webcontent/xmlhttpreq.html
54   if (typeof XMLHttpRequest != "undefined")
55     return new XMLHttpRequest();
56   
57   try { return new ActiveXObject("Msxml2.XMLHTTP"); } 
58   catch (e) { }
59   try { return new ActiveXObject("Microsoft.XMLHTTP"); } 
60   catch (e) { }
61   return null;
62 }
63
64 function resetSelection(win) {
65   var t = "";
66   if (win && win.getSelection) {
67     t = win.getSelection().toString();
68     win.getSelection().removeAllRanges();
69   }
70   return t;
71 }
72
73 function refreshOpener() {
74   if (window.opener && !window.opener.closed) {
75     window.opener.location.reload();
76   }
77 }
78
79 /* query string */
80
81 function parseQueryString() {
82   var queryArray, queryDict
83   var key, value, s, idx;
84   queryDict.length = 0;
85   
86   queryDict  = new Array();
87   queryArray = location.search.substr(1).split('&');
88   for (var i in queryArray) {
89     if (!queryArray[i]) continue ;
90     s   = queryArray[i];
91     idx = s.indexOf("=");
92     if (idx == -1) {
93       key   = s;
94       value = "";
95     }
96     else {
97       key   = s.substr(0, idx);
98       value = unescape(s.substr(idx + 1));
99     }
100     
101     if (typeof queryDict[key] == 'undefined')
102       queryDict.length++;
103     
104     queryDict[key] = value;
105   }
106   return queryDict;
107 }
108
109 function generateQueryString(queryDict) {
110   var s = "":
111   for (var key in queryDict) {
112     if (s.length == 0)
113       s = "?";
114     else
115       s = s + "&";
116     s = s + key + "=" + escape(queryDict[key]);
117   }
118   return s;
119 }
120
121 function getQueryParaArray(s) {
122   if (s.charAt(0) == "?") s = s.substr(1, s.length - 1);
123   return s.split("&");
124 }
125 function getQueryParaValue(s, name) {
126   var t;
127   
128   t = getQueryParaArray(s);
129   for (var i = 0; i < t.length; i++) {
130     var s = t[i];
131     
132     if (s.indexOf(name) != 0)
133       continue;
134     
135     s = s.substr(name.length, s.length - name.length);
136     return decodeURIComponent(s);
137   }
138   return null;
139 }
140
141 /* opener callback */
142
143 function triggerOpenerCallback() {
144   /* this code has some issue if the folder has no proper trailing slash! */
145   if (window.opener && !window.opener.closed) {
146     var t, cburl;
147     
148     t = getQueryParaValue(window.location.search, "openerurl=");
149     cburl = window.opener.location.href;
150     if (cburl[cburl.length - 1] != "/") {
151       cburl = cburl.substr(0, cburl.lastIndexOf("/") + 1);
152     }
153     cburl = cburl + t;
154     window.opener.location.href = cburl;
155   }
156 }