25a3ef512cf970c63f53093fac74840cc981684e
[platal.git] / htdocs / javascript / xorg.js
1 /***************************************************************************
2 * Copyright (C) 2003-2010 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 var days = ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'];
25 var months = ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet',
26 'août', 'septembre', 'octobre', 'novembre', 'décembre']
27
28 function getNow() {
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();
34 if (yr<1000) yr += 1900;
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 }
44
45 $(".date-heure").html(days[dy] + ' ' + wd + ' ' + months[mh] + ' ' + yr + '<br />'
46 + hr + ':' + mi + ':' + se);
47 }
48
49 // }}}
50 // {{{ Search Engine
51
52 function canAddSearchEngine()
53 {
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;
59 }
60
61 function addSearchEngine()
62 {
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 }
76 }
77 }
78
79 // }}}
80 // {{{ dynpost()
81
82 function dynpost(action, values)
83 {
84 var form = document.createElement('form');
85 form.action = action;
86 form.method = 'post';
87
88 $('body').get(0).appendChild(form);
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
101
102 function dynpostkv(action, k, v)
103 {
104 var dict = {};
105 dict[k] = v;
106 dynpost(action, dict);
107 }
108
109 // }}}
110 // {{{ function RegExp.escape()
111
112 RegExp.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 // }}}
127
128 /***************************************************************************
129 * POPUP THINGS
130 */
131
132 // {{{ function goodiesPopup()
133
134 var __goodies_active = true;
135
136 var __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
151 var __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!',
167 'title': 'Ajouter à My Yahoo!'}
168 ]
169 };
170
171 function disableGoodiesPopups() {
172 __goodies_active = false;
173 }
174
175 function goodiesPopup(node, goodies) {
176 var text = '<div style="text-align: center; line-height: 2.2">';
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;
183
184 text += '<a href="' + s_url + '"><img src="' + s_img + '" title="' + s_title + '" alt="' + s_alt + '"></a><br />';
185 }
186 text += '<a href="https://www.polytechnique.org/Xorg/Goodies">Plus de bonus</a> ...</div>';
187
188 var title = node.title ? node.title : goodies.default_title;
189
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);
199 }
200
201 // }}}
202 // {{{ function auto_links()
203
204 function auto_links() {
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);
209
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;
232 }
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);
239 }
240 }
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);
248 }
249
250
251 // }}}
252
253 /***************************************************************************
254 * Password check
255 */
256
257 // {{{ function checkPassword
258
259 function getType(c) {
260 if (c >= 'a' && c <= 'z') {
261 return 1;
262 } else if (c >= 'A' && c <= 'Z') {
263 return 2;
264 } else if (c >= '0' && c <= '9') {
265 return 3;
266 } else {
267 return 4;
268 }
269 }
270
271 function differentTypes(password) {
272 var prev = 0;
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
284 function passwordStrength(password) {
285 var prop = 0;
286 var prev = 0;
287 var firstType = true;
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));
292 if (prev != 0 && prev != type) {
293 prop += 5;
294 firstType = false;
295 }
296 prop += i;
297 if (types[type] == 0 && !firstType) {
298 prop += 15;
299 }
300 types[type]++;
301 prev = type;
302 }
303 if (password.length < 6) {
304 prop *= 0.75;
305 }
306 if (firstType) {
307 prop *= 0.75;
308 }
309 if (prop > 100) {
310 prop = 100;
311 } else if (prop < 0) {
312 prop = 0;
313 }
314
315 return prop;
316 }
317
318 function checkPassword(box, okLabel) {
319 var password = box.value;
320 var prop = passwordStrength(password);
321
322 if (prop >= 60) {
323 color = "#4f4";
324 bgcolor = "#050";
325 ok = true;
326 } else if (prop >= 35) {
327 color = "#ff4";
328 bgcolor = "#750";
329 ok = true;
330 } else {
331 color = "#f20";
332 bgcolor = "#700";
333 ok = false;
334 }
335 $("#passwords_measure")
336 .stop()
337 .animate({ width: prop + "%",
338 backgroundColor: color
339 }, 750)
340 .parent().stop()
341 .animate({ backgroundColor: bgcolor }, 750);
342 var submitButton = $(":submit[name='" + passwordprompt_submit + "']");
343 if (ok && password.length >= 6 && differentTypes(password)) {
344 submitButton.attr("value", okLabel);
345 submitButton.removeAttr("disabled");
346 } else {
347 submitButton.attr("value", "Mot de passe trop faible");
348 submitButton.attr("disabled", "disabled");
349 }
350 }
351
352 // }}}
353 // {{{ jQuery object extension
354
355 (function($) {
356 /* Add new functions to jQuery namesapce */
357 $.extend({
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
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;
370 }
371 if (onError != null && !$.isFunction(onError)) {
372 type = type || onError;
373 onError = null;
374 }
375
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
411 xgetText: function(source, data, onSuccess, onError) {
412 return $.xget(source, data, onSuccess, onError, 'text');
413 },
414
415 xpost: function(source, data, onSuccess, onError, type) {
416 return $.xajax(source, 'POST', data, onSuccess, onError, type);
417 }
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;
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 });
480 }
481 });
482 })(jQuery);
483
484 // }}}
485 // {{{ preview wiki
486
487 function 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
498 function 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 // }}}
513
514
515 /***************************************************************************
516 * The real OnLoad
517 */
518
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 });
541
542 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: