Happy New Year!
[platal.git] / htdocs / javascript / xorg.js
CommitLineData
0337d704 1/***************************************************************************
12262f13 2 * Copyright (C) 2003-2011 Polytechnique.org *
0337d704 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
350ba5ac 21var is_IE = $.browser.msie;
0337d704 22
23// {{{ function getNow()
bb56dfe6
FB
24var days = ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'];
25var months = ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet',
26 'août', 'septembre', 'octobre', 'novembre', 'décembre']
0337d704 27
0337d704 28function getNow() {
bb56dfe6
FB
29 var dt = new Date();
30 var dy = dt.getDay();
31 var mh = dt.getMonth();
32 var wd = dt.getDate();
33 var yr = dt.getYear();
0337d704 34 if (yr<1000) yr += 1900;
bb56dfe6
FB
35 var hr = dt.getHours();
36 var mi = dt.getMinutes();
37 if (mi < 10) {
38 mi = '0' + mi;
39 }
40 var se = dt.getSeconds();
41 if (se < 10) {
42 se = '0' + se;
43 }
9f449375 44
bb56dfe6
FB
45 $(".date-heure").html(days[dy] + ' ' + wd + ' ' + months[mh] + ' ' + yr + '<br />'
46 + hr + ':' + mi + ':' + se);
9f449375 47}
48
49// }}}
a27c6df8 50// {{{ Search Engine
51
52function canAddSearchEngine()
53{
350ba5ac
FB
54 if (((typeof window.sidebar == "object") && $.isFunction(window.sidebar.addSearchEngine))
55 || ((typeof window.external == "object") && $.isFunction(window.external.AddSearchProvider))) {
56 return true;
57 }
58 return false;
a27c6df8 59}
39fd8f50 60
b09e5dea 61function addSearchEngine()
62{
350ba5ac
FB
63 var searchURI = "http://www.polytechnique.org/xorg.opensearch.xml";
64 if ((typeof window.sidebar == "object") && $.isFunction(window.sidebar.addSearchEngine)) {
65 window.sidebar.addSearchEngine(
66 searchURI,
67 "http://www.polytechnique.org/images/xorg.png",
68 "Annuaire Polytechnique.org",
69 "Academic");
70 } else {
71 try {
72 window.external.AddSearchProvider(searchURI);
73 } catch(e) {
74 alert("Impossible d'installer la barre de recherche");
75 }
b09e5dea 76 }
39fd8f50 77}
78
39fd8f50 79// }}}
7e6495d4 80// {{{ dynpost()
81
82function dynpost(action, values)
83{
7e6495d4 84 var form = document.createElement('form');
85 form.action = action;
86 form.method = 'post';
87
7cf438aa 88 $('body').get(0).appendChild(form);
7e6495d4 89
90 for (var k in values) {
91 var input = document.createElement('input');
92 input.type = 'hidden';
93 input.name = k;
94 input.value = values[k];
95 form.appendChild(input);
96 }
97
98 form.submit();
99}
100
e83d83e8 101
d6610b77 102function dynpostkv(action, k, v)
103{
bee33d93 104 var dict = {};
105 dict[k] = v;
106 dynpost(action, dict);
d6610b77 107}
108
7e6495d4 109// }}}
af2ff9ef 110// {{{ function RegExp.escape()
111
112RegExp.escape = function(text) {
113 if (!arguments.callee.sRE) {
114 var specials = [
115 '/', '.', '*', '+', '?', '|',
116 '(', ')', '[', ']', '{', '}',
117 '\\', '^' , '$'
118 ];
119 arguments.callee.sRE = new RegExp(
120 '(\\' + specials.join('|\\') + ')', 'g'
121 );
122 }
123 return text.replace(arguments.callee.sRE, '\\$1');
124}
125
126// }}}
0337d704 127
128/***************************************************************************
129 * POPUP THINGS
130 */
131
769dc7d5
VZ
132// {{{ function goodiesPopup()
133
350ba5ac
FB
134var __goodies_active = true;
135
136var __goodies_ical = {
137 default_title: 'Calendrier iCal',
138 sites: [
139 {'url_prefix': '',
140 'img': 'images/icons/calendar_view_day.gif',
141 'title': 'Calendrier iCal'},
142 {'url_prefix': 'http://www.google.com/calendar/render?cid=',
143 'img': 'images/goodies/add-google-calendar.gif',
144 'title': 'Ajouter à Google Calendar'},
145 {'url_prefix': 'https://www.google.com/calendar/hosted/polytechnique.org/render?cid=',
146 'img': 'images/goodies/add-google-calendar.gif',
147 'title': 'Ajouter à Google Apps / Calendar'}
148 ]
149};
150
151var __goodies_rss = {
152 default_title: 'Fils RSS',
153 sites: [
154 {'url_prefix': '',
155 'img': 'images/icons/feed.gif',
156 'title': 'Fil rss'},
157 {'url_prefix': 'http://fusion.google.com/add?feedurl=',
158 'img': 'images/goodies/add-google.gif',
159 'alt': 'Add to Google',
160 'title': 'Ajouter à iGoogle/Google Reader'},
161 {'url_prefix': 'http://www.netvibes.com/subscribe.php?url=',
162 'img': 'images/goodies/add-netvibes.gif',
163 'title': 'Ajouter à Netvibes'},
164 {'url_prefix': 'http://add.my.yahoo.com/content?.intl=fr&url=',
165 'img': 'images/goodies/add-yahoo.gif',
166 'alt': 'Add to My Yahoo!',
cb143c01 167 'title': 'Ajouter à My Yahoo!'}
350ba5ac
FB
168 ]
169};
769dc7d5 170
f14eed3f
VZ
171function disableGoodiesPopups() {
172 __goodies_active = false;
173}
174
350ba5ac 175function goodiesPopup(node, goodies) {
769dc7d5 176 var text = '<div style="text-align: center; line-height: 2.2">';
350ba5ac
FB
177 for (var site in goodies.sites) {
178 var entry = goodies.sites[site];
179 var s_alt = entry["alt"] ? entry["alt"] : "";
180 var s_img = entry["img"];
181 var s_title = entry["title"] ? entry["title"] : "";
182 var s_url = entry["url_prefix"].length > 0 ? entry["url_prefix"] + escape(this.href) : this.href;
769dc7d5
VZ
183
184 text += '<a href="' + s_url + '"><img src="' + s_img + '" title="' + s_title + '" alt="' + s_alt + '"></a><br />';
185 }
350ba5ac 186 text += '<a href="https://www.polytechnique.org/Xorg/Goodies">Plus de bonus</a> ...</div>';
769dc7d5 187
350ba5ac 188 var title = node.title ? node.title : goodies.default_title;
769dc7d5 189
350ba5ac
FB
190 $(node)
191 .mouseover(
192 function() {
193 if (__goodies_active) {
194 return overlib(text, CAPTION, title, CLOSETEXT, 'Fermer', DELAY, 800, STICKY, WIDTH, 150);
195 }
196 }
197 )
198 .mouseout(nd);
769dc7d5
VZ
199}
200
201// }}}
0337d704 202// {{{ function auto_links()
203
0337d704 204function auto_links() {
bb56dfe6
FB
205 var url = document.URL;
206 var fqdn = url.replace(/^https?:\/\/([^\/]*)\/.*$/,'$1');
207 var light = (url.indexOf('display=light') > url.indexOf('?'));
208 var resource_page = (url.indexOf('rss') > -1 || url.indexOf('ical') > -1);
e83d83e8 209
063410c8
FB
210 $("a,link").each(function(i) {
211 var node = $(this);
212 var href = this.href;
213 if(!href || node.hasClass('xdx')
214 || href.indexOf('mailto:') > -1 || href.indexOf('javascript:') > -1) {
215 return;
216 }
217 if ((href.indexOf(fqdn) < 0 && this.className.indexOf('popup') < 0) || node.hasClass('popup')) {
218 node.click(function () {
219 window.open(href);
220 return false;
221 });
222 }
223 if (href.indexOf(fqdn) > -1 && light) {
224 href = href.replace(/([^\#\?]*)\??([^\#]*)(\#.*)?/, "$1?display=light&$2$3");
225 this.href = href;
226 }
227 var rss = href.indexOf('rss') > -1;
228 var ical = href.indexOf('ical') > -1;
229 if (rss || ical) {
230 if (href.indexOf('http') < 0) {
231 href = 'http://' + fqdn + '/' + href;
e83d83e8 232 }
063410c8
FB
233 }
234 if (this.nodeName.toLowerCase() == 'a' && !resource_page) {
235 if (rss && href.indexOf('prefs/rss') < 0 && (href.indexOf('xml') > -1 || href.indexOf('hash'))) {
236 goodiesPopup(this, __goodies_rss);
237 } else if (ical) {
238 goodiesPopup(this, __goodies_ical);
769dc7d5 239 }
3def38d5 240 }
063410c8
FB
241 if(matches = (/^popup_([0-9]*)x([0-9]*)$/).exec(this.className)) {
242 var w = matches[1], h = matches[2];
243 node.popWin(w, h);
244 }
245 });
246 $('.popup2').popWin(840, 600);
247 $('.popup3').popWin(640, 800);
0337d704 248}
249
e83d83e8 250
0337d704 251// }}}
252
7143ad2b
FB
253/***************************************************************************
254 * Password check
255 */
256
257// {{{ function checkPassword
258
0ba43ee4
VZ
259function getType(c) {
260 if (c >= 'a' && c <= 'z') {
7143ad2b 261 return 1;
0ba43ee4 262 } else if (c >= 'A' && c <= 'Z') {
7143ad2b 263 return 2;
0ba43ee4 264 } else if (c >= '0' && c <= '9') {
7143ad2b
FB
265 return 3;
266 } else {
267 return 4;
268 }
269}
270
4baa7323 271function differentTypes(password) {
7143ad2b 272 var prev = 0;
4baa7323
SJ
273
274 for (i = 0 ; i < password.length ; ++i) {
275 var type = getType(password.charAt(i));
276 if (prev != 0 && prev != type) {
277 return true;
278 }
279 prev = type;
280 }
281 return false;
282}
283
284function passwordStrength(password) {
7143ad2b 285 var prop = 0;
4baa7323 286 var prev = 0;
eaa3f284 287 var firstType = true;
4baa7323
SJ
288 var types = Array(0, 0, 0, 0, 0);
289
290 for (i = 0 ; i < password.length ; ++i) {
291 var type = getType(password.charAt(i));
7143ad2b
FB
292 if (prev != 0 && prev != type) {
293 prop += 5;
4baa7323 294 firstType = false;
7143ad2b 295 }
eaa3f284
FB
296 prop += i;
297 if (types[type] == 0 && !firstType) {
298 prop += 15;
7143ad2b
FB
299 }
300 types[type]++;
301 prev = type;
302 }
4baa7323
SJ
303 if (password.length < 6) {
304 prop *= 0.75;
305 }
306 if (firstType) {
aca1582e 307 prop *= 0.75;
eaa3f284 308 }
7143ad2b
FB
309 if (prop > 100) {
310 prop = 100;
311 } else if (prop < 0) {
312 prop = 0;
313 }
4baa7323
SJ
314
315 return prop;
316}
317
318function checkPassword(box, okLabel) {
319 var password = box.value;
320 var prop = passwordStrength(password);
321
de99248c 322 if (prop >= 60) {
00681970
FB
323 color = "#4f4";
324 bgcolor = "#050";
de99248c 325 ok = true;
23f6d4cc 326 } else if (prop >= 35) {
de99248c 327 color = "#ff4";
00681970 328 bgcolor = "#750";
b19839fd 329 ok = true;
de99248c
FB
330 } else {
331 color = "#f20";
00681970 332 bgcolor = "#700";
de99248c
FB
333 ok = false;
334 }
335 $("#passwords_measure")
00681970
FB
336 .stop()
337 .animate({ width: prop + "%",
338 backgroundColor: color
339 }, 750)
340 .parent().stop()
341 .animate({ backgroundColor: bgcolor }, 750);
ed130f47 342 var submitButton = $(":submit[name='" + passwordprompt_submit + "']");
4baa7323 343 if (ok && password.length >= 6 && differentTypes(password)) {
7b7d0970 344 submitButton.attr("value", okLabel);
de99248c 345 submitButton.removeAttr("disabled");
7143ad2b 346 } else {
7b7d0970 347 submitButton.attr("value", "Mot de passe trop faible");
de99248c 348 submitButton.attr("disabled", "disabled");
7143ad2b
FB
349 }
350}
351
352// }}}
6a686adb
FB
353// {{{ jQuery object extension
354
355(function($) {
102f3fc2
FB
356 /* Add new functions to jQuery namesapce */
357 $.extend({
063410c8
FB
358 /* The goal of the following functions is to provide an AJAX API that
359 * take a different callback in case of HTTP success code (2XX) and in
360 * other cases.
361 */
362
102f3fc2
FB
363 xajax: function(source, method, data, onSuccess, onError, type) {
364 /* Shift argument */
365 if ($.isFunction(data)) {
366 type = type || onError;
367 onError = onSuccess;
368 onSuccess = data;
369 data = null;
6a686adb 370 }
102f3fc2
FB
371 if (onError != null && !$.isFunction(onError)) {
372 type = type || onError;
373 onError = null;
6a686adb 374 }
6a686adb 375
102f3fc2
FB
376 function ajaxHandler(data, textStatus, xhr) {
377 if (textStatus == 'success') {
378 if (onSuccess) {
379 onSuccess(data, textStatus, xhr);
380 }
381 } else if (textStatus == 'error') {
382 if (onError) {
383 onError(data, textStatus, xhr);
384 } else {
385 alert("Une error s'est produite lors du traitement de la requête.\n"
386 + "Ta session a peut-être expiré");
387 }
388 }
389 }
390 return $.ajax({
391 url: source,
392 type: method,
393 success: ajaxHandler,
394 data : data,
395 dataType: type
396 });
397 },
398
399 xget: function(source, data, onSuccess, onError, type) {
400 return $.xajax(source, 'GET', data, onSuccess, onError, type);
401 },
402
403 xgetJSON: function(source, data, onSuccess, onError) {
404 return $.xget(source, data, onSuccess, onError, 'json');
405 },
406
407 xgetScript: function(source, onSuccess, onError) {
408 return $.xget(source, null, onSuccess, onError, 'script');
409 },
410
063410c8
FB
411 xgetText: function(source, data, onSuccess, onError) {
412 return $.xget(source, data, onSuccess, onError, 'text');
413 },
414
102f3fc2
FB
415 xpost: function(source, data, onSuccess, onError, type) {
416 return $.xajax(source, 'POST', data, onSuccess, onError, type);
6a686adb 417 }
102f3fc2
FB
418 });
419
420 /* Add new functions to jQuery objects */
421 $.fn.extend({
422 tmpMessage: function(message, success) {
423 if (success) {
424 this.html("<img src='images/icons/wand.gif' alt='' /> " + message)
425 .css('color', 'green');
426 } else {
427 this.html("<img src='images/icons/error.gif' alt='' /> " + message)
428 .css('color', 'red');
429 }
430 return this.css('fontWeight', 'bold')
431 .show()
432 .delay(1000)
433 .fadeOut(500);
434 },
435
436 updateHtml: function(source, callback) {
437 var elements = this;
438 function handler(data) {
439 elements.html(data);
440 if (callback) {
441 callback(data);
442 }
443 }
444 $.xget(source, handler, 'text');
445 return this;
446 },
447
448 successMessage: function(source, message) {
449 var elements = this;
450 $.xget(source, function() {
451 elements.tmpMessage(message, true);
452 });
453 return this;
454 },
455
456 wiki: function(text, withTitle) {
457 if (text == '') {
458 return this.html('');
459 }
460 var url = 'wiki_preview';
461 if (!withTitle) {
462 url += '/notitile';
463 }
464 var $this = this;
465 $.post(url, { text: text },
466 function (data) {
467 $this.html(data);
468 }, 'text');
469 return this;
063410c8
FB
470 },
471
472 popWin: function(w, h) {
473 return this.click(function() {
474 window.open(this.href, '_blank',
475 'toolbar=0,location=0,directories=0,status=0,'
476 +'menubar=0,scrollbars=1,resizable=1,'
477 +'width='+w+',height='+h);
478 return false;
479 });
6a686adb 480 }
102f3fc2 481 });
6a686adb
FB
482})(jQuery);
483
484// }}}
485// {{{ preview wiki
486
487function previewWiki(idFrom, idTo, withTitle, idShow)
488{
489 $('#' + idTo).wiki($('#' + idFrom).val(), withTitle);
490 if (idShow != null) {
491 $('#' + idShow).show();
492 }
493}
494
495// }}}
496// {{{ send test email
497
498function sendTestEmail(token, hruid)
499{
500 var url = 'emails/test';
501 var msg = "Un email a été envoyé avec succès";
502 if (hruid != null) {
503 url += '/' + hruid;
504 msg += " sur l'adresse de " + hruid + ".";
505 } else {
506 msg += " sur ton addresse.";
507 }
508 $('#mail_sent').successMessage($url + '?token=' + token, msg);
509 return false;
510}
511
512// }}}
7143ad2b
FB
513
514
0337d704 515/***************************************************************************
516 * The real OnLoad
517 */
518
bb56dfe6
FB
519$(document).ready(function() {
520 auto_links();
521 getNow();
522 setInterval(getNow, 1000);
523 $("#quick")
524 .focus(function() {
525 if ($(this).val() === 'Recherche dans l\'annuaire') {
526 $(this).val('');
527 }
528 $("#quick_button").show();
529 })
530 .blur(function() {
531 $("#quick_button").hide();
532 });
533 $("#quick_button").click(function() {
534 if ($("#quick").val() === 'Recherche dans l\'annuaire'
535 || $("#quick").val() === '') {
536 return false;
537 }
538 return true;
539 });
540});
0337d704 541
a14159bf 542// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: