X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=htdocs%2Fjavascript%2Fxorg.js;h=20221e2849705afb82ce6b3d3b7f4f3065ffc6d7;hb=241ba4945741a0c1512b24ac2893f7415886e850;hp=0a191a017f73bacb4274a2d3902fb5bd34fed1a1;hpb=0b2c582e3daf62d24b2e61742b37b2530f842926;p=platal.git diff --git a/htdocs/javascript/xorg.js b/htdocs/javascript/xorg.js index 0a191a0..20221e2 100644 --- a/htdocs/javascript/xorg.js +++ b/htdocs/javascript/xorg.js @@ -854,6 +854,7 @@ function sendTestEmail(token, hruid) * Quick search */ +/* quick search {{{ */ (function($) { function findPos(obj) { var curleft = obj.offsetLeft || 0; @@ -870,7 +871,7 @@ function sendTestEmail(token, hruid) '
' + '
${directory_name}
' + '
' + - '{{if is_female}}•{{/if}}${directory_name}' + + '{{if is_female}}•{{/if}}${directory_name}' + '
' + '
${promo}
' + '
' + @@ -878,139 +879,254 @@ function sendTestEmail(token, hruid) '
' + ''); - $.fn.extend({ - quickSearch: function(args) { - var query = null; - var previous = null; - var $this = this; - var $popup; - var token = (args && args.token) || $.xsrf_token; - var url = 'search'; - var pending = false; - var pos = findPos(this.get(0)); - var disabled = false; - var updatePopup; - var selected = null; - if (token) { - url += '?token=' + token; + + function buildPopup(input, destination, linkBindFunction) + { + var pos = findPos(input.get(0)); + var $popup = destination; + var selected = null; + var hovered = 0; + + function updateSelection() + { + var sel = $popup.children('.contact').addClass('grayed'); + if (selected !== null) { + while (selected < 0) { + selected += sel.length; + } + if (selected >= sel.length) { + selected -= sel.length; + } + sel.eq(selected).removeClass('grayed'); } - $popup = $('
').hide() - .addClass('contact-list') - .css({ - position: 'absolute', - width: '300px', - top: $this.css('bottom'), - left: pos.x - 300 + $this.width(), - clear: 'both', - 'text-align': 'left' - }); - $this.after($popup); + } - function formatProfile(i, profile) { - var data = $.tmpl('quickMinifiche', profile) - .hover(function() { - console.log("hover", i); - selected = i; + 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(); - }, function() { - if (selected === i) { - console.log("unhover", i); - selected = null; - updateSelection(); - } - }).mouseup(function() { - $(this).find('a').click(); - }); - data.find('a').popWin(840, 600).click(function() { - hidePopup(); + } + hovered--; + }).mouseup(function() { + var sel = $(this).find('a'); + if (!sel.attr('hovered')) { + sel.click(); + } }); - return data; + data.find('a').hover(function() { $(this).attr('hovered', true) }, + function() { $(this).attr('hovered', false) }); + return data; + } + + if (!$popup) { + $popup = $('
').hide() + .addClass('contact-list') + .css({ + position: 'absolute', + width: '300px', + top: input.css('bottom'), + left: pos.x - 300 + input.width(), + clear: 'both', + 'text-align': 'left' + }); + input.after($popup); + } + + return { + hide: function(ignoreIfHover) { + if (ignoreIfHover && hovered !== 0) { + return true; + } + selected = null; + updateSelection(); + $popup.hide(); + return true; + }, + + show: function() { + $popup.show(); + return true; + }, + + selected: function() { + return selected !== null; + }, + + unselect: function() { + selected = null; + updateSelection(); + }, + + selectNext: function() { + if (selected === null) { + selected = 0; + } else { + selected++; + } + updateSelection(); + return true; + }, + + selectPrev: function() { + if (selected === null) { + selected = -1; + } else { + selected--; + } + updateSelection(); + return true; + }, + + activeCurrent: function() { + var sel = $popup.children('.contact'); + if (selected !== null) { + sel.eq(selected).find('a').click(); + return false; + } + return true; + }, + + updateContent: function(profiles, extra) { + var profile; + var $this; + $popup.empty(); + for (var i = 0, len = profiles.length; i < len; i++) { + (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; + } else { + selected = null; + } + updateSelection(); + if (len > 0) { + this.show(); + } else { + this.hide(); + } + return true; } + }; + } + + $.fn.extend({ + quickSearch: function(options) { + return this.each(function() { + var $this = $(this); + var $input = this; + var $popup; + var previous = null; + var pending = false; + var disabled = false; + var updatePopup; + + 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($(this).attr('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; - $popup.empty(); - if (data.profile_count > 10 || data.profile_count < 0) { - $popup.hide(); - return; - } - for (var i = 0, len = data.profiles.length; i < len; i++) { - formatProfile(i, data.profiles[i]).appendTo($popup); + 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, data); previous = quick; - selected = len == 1 ? 0 : null; - updateSelection(); - $popup.show(); - }, function() { disabled = true; }); - updatePopup = markPending; - setTimeout(function() { + }, function(data, text) { + if (text !== 'abort') { + disabled = true; + } + }).complete(function() { + $this.removeClass(options.loadingClass); updatePopup = doUpdatePopup; if (pending) { - updatePopup.call($this.get(0)); + updatePopup(); } - pending = false; - }, 500); - return true; - } - - function hidePopup() - { - selected = null; - updateSelection(); - $popup.hide(); + }); return true; } - function updateSelection() + function doUpdatePopup(dontDelay) { - var sel = $popup.children('.contact').addClass('grayed'); - if (selected !== null) { - while (selected < 0) { - selected += sel.length; - } - if (selected >= sel.length) { - selected -= sel.length; - } - sel.eq(selected).removeClass('grayed'); + var quick = $this.val(); + if ($.isFunction(quick.trim)) { + quick = quick.trim(); } - } - - function activeCurrent() - { - var sel = $popup.children('.contact'); - if (selected !== null) { - sel.eq(selected).find('a').click(); - return false; + 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 true; + 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; }) @@ -1018,29 +1134,30 @@ function sendTestEmail(token, hruid) switch (e.keyCode) { case 9: /* Tab */ case 40: /* Down */ - selected += 1; - updateSelection(); + $popup.selectNext(); return false; case 38: - selected -= 1; - updateSelection(); + $popup.selectPrev(); return false; case 13: /* Return */ - return activeCurrent(); + return $popup.activeCurrent(); case 27: /* Escape */ - return hidePopup(); + if ($popup.selected()) { + $popup.unselect(); + } else { + $popup.hide(); + } + return true; } return true; }) .blur(function() { - if (!$popup.is(':hover')) { - return hidePopup(); - } + return $popup.hide(true); }) - .focus(updatePopup); + .focus(updatePopup);}); } }); }(jQuery)); @@ -1058,10 +1175,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) { @@ -1100,7 +1217,7 @@ function sendTestEmail(token, hruid) } }); }(jQuery)); - +/* }}} */ /*************************************************************************** * The real OnLoad