Prevents the RSS/iCal popup to be displayed when the current url points to a resouce.
[platal.git] / htdocs / javascript / xorg.js
1 /***************************************************************************
2 * Copyright (C) 2003-2009 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_IE = $.browser.msie;
22
23 // {{{ function getNow()
24
25 function getNow() {
26 dt = new Date();
27 dy = dt.getDay();
28 mh = dt.getMonth();
29 wd = dt.getDate();
30 yr = dt.getYear();
31 if (yr<1000) yr += 1900;
32 hr = dt.getHours();
33 mi = dt.getMinutes();
34
35 time = (mi < 10) ? hr +':0'+mi : hr+':'+mi;
36 days = ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'];
37 months = ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet',
38 'août', 'septembre', 'octobre', 'novembre', 'décembre']
39
40 return days[dy]+' '+wd+' '+months[mh]+' '+yr+'<br />'+time;
41 }
42
43 // }}}
44 // {{{ Search Engine
45
46 function canAddSearchEngine()
47 {
48 if (((typeof window.sidebar == "object") && $.isFunction(window.sidebar.addSearchEngine))
49 || ((typeof window.external == "object") && $.isFunction(window.external.AddSearchProvider))) {
50 return true;
51 }
52 return false;
53 }
54
55 function addSearchEngine()
56 {
57 var searchURI = "http://www.polytechnique.org/xorg.opensearch.xml";
58 if ((typeof window.sidebar == "object") && $.isFunction(window.sidebar.addSearchEngine)) {
59 window.sidebar.addSearchEngine(
60 searchURI,
61 "http://www.polytechnique.org/images/xorg.png",
62 "Annuaire Polytechnique.org",
63 "Academic");
64 } else {
65 try {
66 window.external.AddSearchProvider(searchURI);
67 } catch(e) {
68 alert("Impossible d'installer la barre de recherche");
69 }
70 }
71 }
72
73 // }}}
74 // {{{ dynpost()
75
76 function dynpost(action, values)
77 {
78 var form = document.createElement('form');
79 form.action = action;
80 form.method = 'post';
81
82 $('body').get(0).appendChild(form);
83
84 for (var k in values) {
85 var input = document.createElement('input');
86 input.type = 'hidden';
87 input.name = k;
88 input.value = values[k];
89 form.appendChild(input);
90 }
91
92 form.submit();
93 }
94
95
96 function dynpostkv(action, k, v)
97 {
98 var dict = {};
99 dict[k] = v;
100 dynpost(action, dict);
101 }
102
103 // }}}
104 // {{{ function RegExp.escape()
105
106 RegExp.escape = function(text) {
107 if (!arguments.callee.sRE) {
108 var specials = [
109 '/', '.', '*', '+', '?', '|',
110 '(', ')', '[', ']', '{', '}',
111 '\\', '^' , '$'
112 ];
113 arguments.callee.sRE = new RegExp(
114 '(\\' + specials.join('|\\') + ')', 'g'
115 );
116 }
117 return text.replace(arguments.callee.sRE, '\\$1');
118 }
119
120 // }}}
121
122 /***************************************************************************
123 * POPUP THINGS
124 */
125
126 // {{{ function popWin()
127
128 function popWin(theNode, w, h) {
129 window.open(theNode.href, '_blank',
130 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+w+',height='+h);
131 return false;
132 }
133
134 // }}}
135 // {{{ function goodiesPopup()
136
137 var __goodies_active = true;
138
139 var __goodies_ical = {
140 default_title: 'Calendrier iCal',
141 sites: [
142 {'url_prefix': '',
143 'img': 'images/icons/calendar_view_day.gif',
144 'title': 'Calendrier iCal'},
145 {'url_prefix': 'http://www.google.com/calendar/render?cid=',
146 'img': 'images/goodies/add-google-calendar.gif',
147 'title': 'Ajouter à Google Calendar'},
148 {'url_prefix': 'https://www.google.com/calendar/hosted/polytechnique.org/render?cid=',
149 'img': 'images/goodies/add-google-calendar.gif',
150 'title': 'Ajouter à Google Apps / Calendar'}
151 ]
152 };
153
154 var __goodies_rss = {
155 default_title: 'Fils RSS',
156 sites: [
157 {'url_prefix': '',
158 'img': 'images/icons/feed.gif',
159 'title': 'Fil rss'},
160 {'url_prefix': 'http://fusion.google.com/add?feedurl=',
161 'img': 'images/goodies/add-google.gif',
162 'alt': 'Add to Google',
163 'title': 'Ajouter à iGoogle/Google Reader'},
164 {'url_prefix': 'http://www.netvibes.com/subscribe.php?url=',
165 'img': 'images/goodies/add-netvibes.gif',
166 'title': 'Ajouter à Netvibes'},
167 {'url_prefix': 'http://add.my.yahoo.com/content?.intl=fr&url=',
168 'img': 'images/goodies/add-yahoo.gif',
169 'alt': 'Add to My Yahoo!',
170 'title': 'Ajouter à My Yahoo!'},
171 {'url_prefix': 'http://www.newsgator.com/ngs/subscriber/subext.aspx?url=',
172 'img': 'images/goodies/add-newsgator.gif',
173 'alt': 'Subscribe in NewsGator Online',
174 'title': 'Ajouter à Newsgator'}
175 ]
176 };
177
178 function disableGoodiesPopups() {
179 __goodies_active = false;
180 }
181
182 function goodiesPopup(node, goodies) {
183 var text = '<div style="text-align: center; line-height: 2.2">';
184 for (var site in goodies.sites) {
185 var entry = goodies.sites[site];
186 var s_alt = entry["alt"] ? entry["alt"] : "";
187 var s_img = entry["img"];
188 var s_title = entry["title"] ? entry["title"] : "";
189 var s_url = entry["url_prefix"].length > 0 ? entry["url_prefix"] + escape(this.href) : this.href;
190
191 text += '<a href="' + s_url + '"><img src="' + s_img + '" title="' + s_title + '" alt="' + s_alt + '"></a><br />';
192 }
193 text += '<a href="https://www.polytechnique.org/Xorg/Goodies">Plus de bonus</a> ...</div>';
194
195 var title = node.title ? node.title : goodies.default_title;
196
197 $(node)
198 .mouseover(
199 function() {
200 if (__goodies_active) {
201 return overlib(text, CAPTION, title, CLOSETEXT, 'Fermer', DELAY, 800, STICKY, WIDTH, 150);
202 }
203 }
204 )
205 .mouseout(nd);
206 }
207
208 // }}}
209 // {{{ function auto_links()
210
211 function auto_links() {
212 url = document.URL;
213 fqdn = url.replace(/^https?:\/\/([^\/]*)\/.*$/,'$1');
214 light = (url.indexOf('display=light') > url.indexOf('?'));
215 resource_page = (url.indexOf('rss') > -1 || url.indexOf('ical') > -1);
216
217 $("a,link").each(
218 function(i) {
219 node = $(this);
220 href = this.href;
221 if(!href || node.hasClass('xdx')
222 || href.indexOf('mailto:') > -1 || href.indexOf('javascript:') > -1) {
223 return;
224 }
225 if ((href.indexOf(fqdn) < 0 && this.className.indexOf('popup') < 0) || node.hasClass('popup')) {
226 node.click(function () { window.open(this.href); return false; });
227 }
228 if (href.indexOf(fqdn) > -1 && light) {
229 href = href.replace(/([^\#\?]*)\??([^\#]*)(\#.*)?/, "$1?display=light&$2$3");
230 this.href = href;
231 }
232 rss = href.indexOf('rss') > -1;
233 ical = href.indexOf('ical') > -1;
234 if (rss || ical) {
235 if (href.indexOf('http') < 0) {
236 href = 'http://' + fqdn + '/' + href;
237 }
238 }
239 if (this.nodeName.toLowerCase() == 'a' && !resource_page) {
240 if (rss && href.indexOf('prefs/rss') < 0 && (href.indexOf('xml') > -1 || href.indexOf('hash'))) {
241 goodiesPopup(this, __goodies_rss);
242 } else if (ical) {
243 goodiesPopup(this, __goodies_ical);
244 }
245 }
246 if(matches = (/^popup_([0-9]*)x([0-9]*)$/).exec(this.className)) {
247 var w = matches[1], h = matches[2];
248 node.click(function() { return popWin(this, w, h); });
249 }
250 }
251 );
252 $('.popup2').click(function() { return popWin(this, 840, 600); });
253 $('.popup3').click(function() { return popWin(this, 640, 800); });
254 }
255
256
257 // }}}
258
259
260 /***************************************************************************
261 * Password check
262 */
263
264 // {{{ function checkPassword
265
266 function getType(char) {
267 if (char >= 'a' && char <= 'z') {
268 return 1;
269 } else if (char >= 'A' && char <= 'Z') {
270 return 2;
271 } else if (char >= '0' && char <= '9') {
272 return 3;
273 } else {
274 return 4;
275 }
276 }
277
278 function checkPassword(box, okLabel) {
279 var prev = 0;
280 var prop = 0;
281 var pass = box.value;
282 var types = Array(0, 0, 0, 0, 0);
283 var firstType = true;
284 for (i = 0 ; i < pass.length ; ++i) {
285 type = getType(pass.charAt(i));
286 if (prev != 0 && prev != type) {
287 prop += 5;
288 }
289 prop += i;
290 if (types[type] == 0 && !firstType) {
291 prop += 15;
292 } else {
293 firstType = false;
294 }
295 types[type]++;
296 prev = type;
297 }
298 if (pass.length < 6) {
299 prop *= 0.75;
300 }
301 if (prop > 100) {
302 prop = 100;
303 } else if (prop < 0) {
304 prop = 0;
305 }
306 if (prop >= 60) {
307 color = "#4f4";
308 bgcolor = "#050";
309 ok = true;
310 } else if (prop >= 40) {
311 color = "#ff4";
312 bgcolor = "#750";
313 ok = true;
314 } else {
315 color = "#f20";
316 bgcolor = "#700";
317 ok = false;
318 }
319 $("#passwords_measure")
320 .stop()
321 .animate({ width: prop + "%",
322 backgroundColor: color
323 }, 750)
324 .parent().stop()
325 .animate({ backgroundColor: bgcolor }, 750);
326 var submitButton = $(":submit[@name='" + passwordprompt_submit + "']");
327 if (ok && pass.length >= 6) {
328 submitButton.attr("value", okLabel);
329 submitButton.removeAttr("disabled");
330 } else {
331 submitButton.attr("value", "Mot de passe trop faible");
332 submitButton.attr("disabled", "disabled");
333 }
334 }
335
336 // }}}
337
338
339 /***************************************************************************
340 * The real OnLoad
341 */
342
343 $(document).ready(auto_links);
344
345 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: