Enables deactivation of RSS/iCal popups (to be used in Xorg/Goodies).
[platal.git] / htdocs / javascript / xorg.js
1 /***************************************************************************
2 * Copyright (C) 2003-2008 Polytechnique.org *
3 * http://opensource.polytechnique.org/ *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., *
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 ***************************************************************************/
20
21 var is_netscape = (navigator.appName.substring(0,8) == "Netscape");
22 var is_IE = (navigator.appName.substring(0,9) == "Microsoft");
23
24 // {{{ function getNow()
25
26 function getNow() {
27 dt = new Date();
28 dy = dt.getDay();
29 mh = dt.getMonth();
30 wd = dt.getDate();
31 yr = dt.getYear();
32 if (yr<1000) yr += 1900;
33 hr = dt.getHours();
34 mi = dt.getMinutes();
35
36 time = (mi < 10) ? hr +':0'+mi : hr+':'+mi;
37 days = ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'];
38 months = ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet',
39 'août', 'septembre', 'octobre', 'novembre', 'décembre']
40
41 return days[dy]+' '+wd+' '+months[mh]+' '+yr+'<br />'+time;
42 }
43
44 // }}}
45 // {{{ Search Engine
46
47 function canAddSearchEngine()
48 {
49 if (((typeof window.sidebar == "object") && (typeof window.sidebar.addSearchEngine == "function"))
50 || ((typeof window.sidebar == "object") && (typeof window.sidebar.addSearchEngine == "function"))) {
51 return true;
52 }
53 return false;
54 }
55
56 function addSearchEngine()
57 {
58 var searchURI = "http://www.polytechnique.org/xorg.opensearch.xml";
59 if ((typeof window.sidebar == "object") && (typeof window.sidebar.addSearchEngine == "function")) {
60 window.sidebar.addSearchEngine(
61 searchURI,
62 "http://www.polytechnique.org/images/xorg.png",
63 "Annuaire Polytechnique.org",
64 "Academic");
65 } else {
66 try {
67 window.external.AddSearchProvider(searchURI);
68 } catch(e) {
69 alert("Impossible d'installer la barre de recherche");
70 }
71 }
72 }
73
74 // }}}
75 // {{{ Events
76
77 function eventClosure(obj, methodName) {
78 return (function(e) {
79 e = e || window.event;
80 return obj[methodName](e);
81 });
82 }
83
84 function attachEvent(obj, evt, f, useCapture) {
85 if (!useCapture) useCapture = false;
86
87 if (obj.addEventListener) {
88 obj.addEventListener(evt, f, useCapture);
89 return true;
90 } else if (obj.attachEvent) {
91 return obj.attachEvent("on"+evt, f);
92 }
93 return false;
94 }
95
96 // }}}
97 // {{{ dynpost()
98
99 function dynpost(action, values)
100 {
101 var body = document.getElementsByTagName('body')[0];
102
103 var form = document.createElement('form');
104 form.action = action;
105 form.method = 'post';
106
107 body.appendChild(form);
108
109 for (var k in values) {
110 var input = document.createElement('input');
111 input.type = 'hidden';
112 input.name = k;
113 input.value = values[k];
114 form.appendChild(input);
115 }
116
117 form.submit();
118 }
119
120 function dynpostkv(action, k, v)
121 {
122 var dict = {};
123 dict[k] = v;
124 dynpost(action, dict);
125 }
126
127 // }}}
128 // {{{ function RegExp.escape()
129
130 RegExp.escape = function(text) {
131 if (!arguments.callee.sRE) {
132 var specials = [
133 '/', '.', '*', '+', '?', '|',
134 '(', ')', '[', ']', '{', '}',
135 '\\', '^' , '$'
136 ];
137 arguments.callee.sRE = new RegExp(
138 '(\\' + specials.join('|\\') + ')', 'g'
139 );
140 }
141 return text.replace(arguments.callee.sRE, '\\$1');
142 }
143
144 // }}}
145
146 /***************************************************************************
147 * POPUP THINGS
148 */
149
150 // {{{ function popWin()
151
152 function popWin(theNode,w,h) {
153 window.open(theNode.href, '_blank',
154 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+w+',height='+h);
155 }
156
157 // }}}
158 // {{{ function goodiesPopup()
159
160 function goodiesPopup(node) {
161 if (node.href.indexOf('ical') > -1) {
162 __goodies_popup(node, __goodies_ical_sites, 'Calendrier iCal');
163 } else if (node.href.indexOf('rss') > -1 && (node.href.indexOf('xml') > -1 || node.href.indexOf('hash'))) {
164 __goodies_popup(node, __goodies_rss_sites, 'Fil rss');
165 }
166 }
167
168 function disableGoodiesPopups() {
169 __goodies_active = false;
170 }
171
172 var __goodies_active = true;
173 var __goodies_ical_sites = [
174 {'url_prefix': '',
175 'img': 'images/icons/calendar_view_day.gif',
176 'title': 'Calendrier iCal'},
177 {'url_prefix': 'http://www.google.com/calendar/render?cid=',
178 'img': 'images/goodies/add-google-calendar.gif',
179 'title': 'Ajouter à Google Calendar'}
180 ];
181 var __goodies_rss_sites = [
182 {'url_prefix': '',
183 'img': 'images/icons/feed.gif',
184 'title': 'Fil rss'},
185 {'url_prefix': 'http://fusion.google.com/add?feedurl=',
186 'img': 'images/goodies/add-google.gif',
187 'alt': 'Add to Google',
188 'title': 'Ajouter à iGoogle/Google Reader'},
189 {'url_prefix': 'http://www.netvibes.com/subscribe.php?url=',
190 'img': 'images/goodies/add-netvibes.gif',
191 'title': 'Ajouter à Netvibes'},
192 {'url_prefix': 'http://add.my.yahoo.com/content?.intl=fr&url=',
193 'img': 'images/goodies/add-yahoo.gif',
194 'alt': 'Add to My Yahoo!',
195 'title': 'Ajouter à My Yahoo!'}
196 ];
197
198 function __goodies_popupText(url, sites) {
199 var text = '<div style="text-align: center; line-height: 2.2">';
200 for (var site in sites) {
201 var s_alt = (sites[site]["alt"] ? sites[site]["alt"] : "");
202 var s_img = sites[site]["img"];
203 var s_title = (sites[site]["title"] ? sites[site]["title"] : "");
204 var s_url = (sites[site]["url_prefix"].length > 0 ? sites[site]["url_prefix"] + escape(url) : url);
205
206 text += '<a href="' + s_url + '"><img src="' + s_img + '" title="' + s_title + '" alt="' + s_alt + '"></a><br />';
207 }
208 text += '<a href="https://www.polytechnique.org/Xorg/Goodies">Plus de bonus</a> ...</div>'
209 return text;
210 }
211
212 function __goodies_popup(node, sites, default_title) {
213 var mouseover_cb = function() {
214 if (__goodies_active) {
215 var rss_text = __goodies_popupText(node.href, sites);
216 var rss_title = (node.title ? node.title : default_title);
217 return overlib(rss_text, CAPTION, rss_title, CLOSETEXT, 'Fermer', DELAY, 800, STICKY, WIDTH, 150);
218 }
219 }
220 var mouseout_cb = function() {
221 nd();
222 }
223
224 node.onmouseover = mouseover_cb;
225 node.onmouseout = mouseout_cb;
226 }
227
228 // }}}
229 // {{{ function auto_links()
230
231 function auto_links() {
232 auto_links_nodes(document.getElementsByTagName('a'));
233 auto_links_nodes(document.getElementsByTagName('link'));
234 }
235
236 function auto_links_nodes(nodes) {
237 url = document.URL;
238 fqdn = url.replace(/^https?:\/\/([^\/]*)\/.*$/,'$1');
239 light = (url.indexOf('display=light') > url.indexOf('?'));
240 for(var i=0; i < nodes.length; i++) {
241 node = nodes[i];
242 if(!node.href || node.className == 'xdx'
243 || node.href.indexOf('mailto:') > -1 || node.href.indexOf('javascript:') > -1)
244 continue;
245 if (node.href.indexOf(fqdn) < 0 || node.className == 'popup') {
246 node.onclick = function () { window.open(this.href); return false; };
247 }
248 if (node.href.indexOf(fqdn) > -1 && light) {
249 node.href = node.href.replace(/([^\#\?]*)\??([^\#]*)(\#.*)?/,
250 "$1?display=light&$2$3");
251 }
252 if (node.href.indexOf('rss') > -1 || node.href.indexOf('ical') > -1) {
253 node.href = node.href.replace(/https/, 'http');
254 if (node.href.indexOf('http') < 0) {
255 node.href = 'http://' + fqdn + '/' + node.href;
256 }
257 if (node.nodeName.toLowerCase() == 'a') {
258 goodiesPopup(node);
259 }
260 }
261 if(node.className == 'popup2') {
262 node.onclick = function () { popWin(this,840,600); return false; };
263 }
264 if(node.className == 'popup3') {
265 node.onclick = function () { popWin(this, 640, 800); return false; };
266 }
267 if(matches = (/^popup_([0-9]*)x([0-9]*)$/).exec(node.className)) {
268 var w = matches[1], h = matches[2];
269 node.onclick = function () { popWin(this,w,h); return false; };
270 }
271 }
272 }
273
274 // }}}
275
276 /***************************************************************************
277 * The real OnLoad
278 */
279
280 // {{{ function pa_onload
281
282 if (!attachEvent(window, 'load', auto_links)) {
283 window.onload = auto_links;
284 }
285
286 // }}}
287
288 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: