Remove Address::updateGeocoding unused parameter
[platal.git] / htdocs / javascript / xorg.js
index dfda329..2ce2212 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -132,21 +132,22 @@ function addSearchEngine()
             var s_img;
             var s_title;
             var s_url;
+            var href = this.attr('href');
 
             for (site in goodies[type].sites) {
                 entry = goodies[type].sites[site];
                 s_alt   = entry.alt || "";
                 s_img   = entry.img;
                 s_title = entry.title || "";
-                s_url   = entry.url_prefix.length > 0 ? entry.url_prefix + escape(this.href) : this.href;
+                s_url   = entry.url_prefix.length > 0 ? entry.url_prefix + escape(href) : href;
 
                 text += '<a href="' + s_url + '"><img src="' + s_img + '" title="' + s_title + '" alt="' + s_alt + '"></a><br />';
-                }
+            }
             text += '<a href="https://www.polytechnique.org/Xorg/Goodies">Plus de bonus</a> ...</div>';
 
             return this.overlib({
                 text: text,
-                caption: this.attr('title') || goodies.default_title,
+                caption: this.attr('title') || goodies[type].default_title,
                 close_text: 'Fermer',
                 delay: 800,
                 sticky: true,
@@ -178,7 +179,7 @@ function auto_links() {
         }
         if ((!href.contains(fqdn) && !this.className.contains('popup')) || node.hasClass('popup')) {
             node.click(function () {
-                window.open(href);
+                window.open($.plURL(this.href));
                 return false;
             });
         }
@@ -565,7 +566,7 @@ function checkPassword(box, okLabel) {
     }
 }
 
-function hashResponse(password1, password2, hasConfirmation) {
+function hashResponse(password1, password2, hasConfirmation, doAuth) {
     var pw1 = $('[name=' + password1 + ']').val();
     var pw2;
 
@@ -589,9 +590,15 @@ function hashResponse(password1, password2, hasConfirmation) {
         return false;
     }
 
-    alert("Le mot de passe que tu as rentré va être chiffré avant de nous parvenir par Internet ! Ainsi il ne circulera pas en clair.");
+    alert("Le mot de passe va être chiffré avant de nous parvenir par Internet ! Ainsi il ne circulera pas en clair.");
     $('[name=' + password1 + ']').val('');
     $('[name=pwhash]').val(hash_encrypt(pw1));
+
+    if (doAuth) {
+        $('[name=password]').val(pw1);
+        doChallengeResponse();
+    }
+
     return true;
 }
 
@@ -655,7 +662,6 @@ function doChallengeResponse() {
     }
     document.forms.loginsub.username.value = document.forms.login.username.value;
     document.forms.loginsub.remember.value = document.forms.login.remember.checked;
-    document.forms.loginsub.domain.value = document.forms.login.domain.value;
     document.forms.login.password.value = "";
     document.forms.loginsub.submit();
 }
@@ -684,7 +690,7 @@ function sendTestEmail(token, hruid)
     } else {
         msg += " sur ton addresse.";
     }
-    $('#mail_sent').successMessage($url + '?token=' + token, msg);
+    $('#mail_sent').successMessage(url + '?token=' + token, msg);
     return false;
 }
 
@@ -810,7 +816,7 @@ function sendTestEmail(token, hruid)
 
         popWin: function(w, h) {
             return this.click(function() {
-                window.open(this.href, '_blank',
+                window.open($.plURL(this.href), '_blank',
                             'toolbar=0,location=0,directories=0,status=0,'
                            +'menubar=0,scrollbars=1,resizable=1,'
                            +'width='+w+',height='+h);
@@ -832,20 +838,23 @@ function previewWiki(idFrom, idTo, withTitle, idShow)
 }
 
 // }}}
-// {{{ send test email
-
-function sendTestEmail(token, hruid)
-{
-    var url = 'emails/test';
-    var msg = "Un email a été envoyé avec succès";
-    if (hruid != null) {
-        url += '/' + hruid;
-        msg += " sur l'adresse de " + hruid + ".";
-    } else {
-        msg += " sur ton addresse.";
+// {{{ updatepromofields
+
+function updatepromofields(egal1, egal2, promo2) {
+    var comparator = egal1.val();
+
+    if (comparator == '=') {
+        egal2.attr('disabled', 'disabled');
+        promo2.attr('disabled', 'disabled');
+    } else if (comparator == '<=' || comparator == '>=') {
+        egal2.removeAttr('disabled');
+        promo2.removeAttr('disabled');
+        if (comparator == '<=') {
+            egal2.val('>=');
+        } else {
+            egal2.val('<=');
+        }
     }
-    $('#mail_sent').successMessage($url + '?token=' + token, msg);
-    return false;
 }
 
 // }}}
@@ -871,7 +880,7 @@ function sendTestEmail(token, hruid)
                 '<div class="identity">' +
                     '<div class="photo"><img src="photo/${hrpid}" alt="${directory_name}" /></div>' +
                     '<div class="nom">' +
-                        '{{if is_female}}&bull;{{/if}}<a href="profile/${hrpid}">${directory_name}</a>' +
+                        '{{if is_female}}&bull;{{/if}}<a>${directory_name}</a>' +
                     '</div>' +
                     '<div class="edu">${promo}</div>' +
                 '</div>' +
@@ -880,20 +889,11 @@ function sendTestEmail(token, hruid)
             '</div>');
 
 
-    function buildPopup(input, linkBindFunction)
+    function buildPopup(input, destination, linkBindFunction)
     {
-        var pos    = findPos(input.get(0));
-        var $popup = $('<div>').hide()
-            .addClass('contact-list')
-            .css({
-                position: 'absolute',
-                width: '300px',
-                top: input.css('bottom'),
-                left: pos.x - 300 + input.width(),
-                clear: 'both',
-                'text-align': 'left'
-            });
+        var $popup = destination;
         var selected = null;
+        var hovered  = 0;
 
         function updateSelection()
         {
@@ -911,27 +911,44 @@ function sendTestEmail(token, hruid)
 
         function formatProfile(i, profile) {
             var data = $.tmpl('quickMinifiche', profile)
+                .css('cursor', 'pointer')
                 .hover(function() {
                     selected = i;
                     updateSelection();
+                    hovered++;
                 }, function() {
                     if (selected === i) {
                         selected = null;
                         updateSelection();
                     }
+                    hovered--;
                 }).mouseup(function() {
-                    if (!($(this).find('a').is(':hover'))) {
-                        $(this).find('a').click();
+                    var sel = $(this).find('a');
+                    if (!sel.attr('hovered')) {
+                        sel.click();
                     }
                 });
+            data.find('a').hover(function() { $(this).attr('hovered', true) },
+                                 function() { $(this).attr('hovered', false) });
             return data;
         }
 
-        input.after($popup);
+        if (!$popup) {
+            $popup = $('<div>').hide()
+            .addClass('contact-list')
+            .css({
+                position: 'absolute',
+                width: '300px',
+                top: input.css('bottom'),
+                clear: 'both',
+                'text-align': 'left'
+            });
+            input.after($popup);
+        }
 
         return {
             hide: function(ignoreIfHover) {
-                if (ignoreIfHover && $popup.is(':hover')) {
+                if (ignoreIfHover && hovered !== 0) {
                     return true;
                 }
                 selected = null;
@@ -941,7 +958,8 @@ function sendTestEmail(token, hruid)
             },
 
             show: function() {
-                $popup.show();
+                var pos = findPos(input.get(0));
+                $popup.css('left', pos.x - 300 + input.width()).show();
                 return true;
             },
 
@@ -983,13 +1001,18 @@ function sendTestEmail(token, hruid)
                 return true;
             },
 
-            updateContent: function(profiles) {
+            updateContent: function(profiles, extra) {
                 var profile;
+                var $this;
                 $popup.empty();
                 for (var i = 0, len = profiles.length; i < len; i++) {
-                    profile = formatProfile(i, profiles[i]);
-                    profile.find('a').each(linkBindFunction);
-                    profile.appendTo($popup);
+                    (function(elt) {
+                        var profile = formatProfile(i, elt);
+                        profile.find('a').each(function() {
+                            linkBindFunction.call(this, elt, $this, extra);
+                        });
+                        profile.appendTo($popup);
+                    }(profiles[i]));
                 }
                 if (len === 1) {
                     selected = 0;
@@ -1008,79 +1031,110 @@ function sendTestEmail(token, hruid)
     }
 
     $.fn.extend({
-        quickSearch: function(args) {
-            var query = null;
-            var previous = null;
-            var $this = this;
+        quickSearch: function(options) {
+            return this.each(function() {
+            var $this  = $(this);
+            var $input = this;
             var $popup;
-            var token;
-            var url   = 'search';
-            var pending = false;
+            var previous = null;
+            var pending  = false;
             var disabled = false;
             var updatePopup;
-            args  = args || { };
-            token = args.token || $.xsrf_token;
-            if (token) {
-                url += '?token=' + token;
-            }
 
-            $popup = buildPopup(this, function() {
-                $(this).popWin(840, 600)
-                    .click(function() {
-                        $popup.hide();
-                    });
-            });
+            options = options || { };
+            options = $.extend({
+                destination:       null,
+                minChars:          3,
+                shortChars:        5,
+                shortTimeout:      300,
+                longTimeout:       100,
+                queryParams:       {
+                    offset: 0,
+                    count:  10,
+                    allow_special: true
+                },
+                loadingClassLeft:  'ac_loading',
+                loadingClassRight: 'ac_loading_left',
+                selectAction: function(profile, popup, extra) {
+                    var type = extra.link_type || 'profile';
+                    switch (type) {
+                      case 'profile':
+                        $(this).attr('href', 'profile/' + profile.hrpid)
+                        .popWin(840, 600)
+                        .click(function() { $popup.hide(); return false; });
+                        break;
+                      case 'admin':
+                        $(this).attr('href', 'admin/user/' + profile.hrpid)
+                        .click(function() { window.open($.plURL(this.href)); return false });
+                        break;
+                    }
+                }
+            }, options);
+            options.loadingClass = $this.css('text-align') === 'right' ? options.loadingClassRight
+                                                                       : options.loadingClassLeft;
+            $this.attr('autocomplete', 'off');
+            $popup = buildPopup($this, options.destination, options.selectAction);
 
             function markPending() {
                 pending = true;
             }
 
-            function doUpdatePopup()
+            function performUpdate(quick)
             {
-                var quick = $(this).val();
-                if (query !== null) {
-                    query.abort();
-                }
-                if (disabled || quick.length < 3) {
-                    previous = quick;
-                    $popup.hide();
-                    return true;
-                }
-                if (previous === quick) {
-                    $popup.show();
+                if (updatePopup === markPending) {
                     return true;
                 }
-                query = $.xapi(url, $.extend({ 'quick': quick }, args), function(data) {
-                    query = null;
-                    if (data.profile_count > 10 || data.profile_count < 0) {
-                        $popup.hide();
-                        return;
+                updatePopup = markPending;
+                $this.addClass(options.loadingClass);
+                $.xapi('search', $.extend({ 'quick': quick }, options.queryParams), function(data) {
+                    if (data.profile_count > options.queryParams.count || data.profile_count < 0) {
+                        return $popup.hide();
                     }
-                    $popup.updateContent(data.profiles);
+                    $popup.updateContent(data.profiles, data);
                     previous = quick;
                 }, function(data, text) {
                     if (text !== 'abort') {
                         disabled = true;
                     }
-                });
-                updatePopup = markPending;
-                setTimeout(function() {
+                }).complete(function() {
+                    $this.removeClass(options.loadingClass);
                     updatePopup = doUpdatePopup;
                     if (pending) {
-                        updatePopup.call($this.get(0));
+                        updatePopup();
                     }
-                    pending = false;
-                }, 500);
+                });
                 return true;
             }
 
+            function doUpdatePopup(dontDelay)
+            {
+                var quick = $this.val();
+                if ($.isFunction(quick.trim)) {
+                    quick = quick.trim();
+                }
+                pending = false;
+                if (disabled || quick.length < options.minChars) {
+                    previous = quick;
+                    return $popup.hide();
+                } else if (!dontDelay) {
+                    var timeout = quick.length < options.shortChars ? options.shortTimeout : options.longTimeout;
+                    setTimeout(function() {
+                        updatePopup(true);
+                    }, timeout);
+                    return true;
+                } else if (previous === quick) {
+                    return $popup.show();
+                }
+                return performUpdate(quick);
+            }
+
             updatePopup = doUpdatePopup;
 
-            return this.keyup(function(e) {
+            return $this.keyup(function(e) {
                 if (e.keyCode !== 27 /* escape */ && e.keyCode !== 13 /* enter */
                     && e.keyCode !== 9 /* tab */ && e.keyCode !== 38 /* up */
                     && e.keyCode !== 40 /* down */) {
-                    return updatePopup.call(this);
+                    return updatePopup();
                 }
                 return true;
             })
@@ -1111,7 +1165,7 @@ function sendTestEmail(token, hruid)
             .blur(function() {
                 return $popup.hide(true);
             })
-            .focus(updatePopup);
+            .focus(updatePopup);});
         }
     });
 }(jQuery));
@@ -1129,10 +1183,10 @@ function sendTestEmail(token, hruid)
             if (typeof text === 'string') {
                 args.push(text);
                 if (width) {
-                    args.push(width);
+                    args.push(WIDTH, width);
                 }
                 if (height) {
-                    args.push(height);
+                    args.push(HEIGHT, height);
                 }
             } else {
                 for (key in text) {
@@ -1201,4 +1255,4 @@ $(function() {
     });
 });
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: