Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

145 lignes
4.1 KiB

  1. (function($){
  2. /*
  3. Text field auto-completion plugin for jQuery.
  4. Based on http://www.dyve.net/jquery/?autocomplete by Dylan Verheul.
  5. */
  6. $.suggest = function(input, url, paramName, minChars, delay) {
  7. var input = $(input).addClass("suggest").attr("autocomplete", "off");
  8. var timeout = null;
  9. var prev = "";
  10. var selectedIndex = -1;
  11. var results = null;
  12. input.keydown(function(e) {
  13. switch(e.keyCode) {
  14. case 27: // escape
  15. hide();
  16. break;
  17. case 38: // up
  18. case 40: // down
  19. e.preventDefault();
  20. if (results) {
  21. var items = $("li", results);
  22. if (!items) return;
  23. var index = selectedIndex + (e.keyCode == 38 ? -1 : 1);
  24. if (index >= 0 && index < items.length) {
  25. move(index);
  26. }
  27. } else {
  28. show();
  29. }
  30. break;
  31. case 9: // tab
  32. case 13: // return
  33. case 39: // right
  34. if (results) {
  35. var li = $("li.selected", results);
  36. if (li.length) {
  37. select(li);
  38. e.preventDefault();
  39. }
  40. }
  41. break;
  42. default:
  43. if (timeout) clearTimeout(timeout);
  44. timeout = setTimeout(show, delay);
  45. break;
  46. }
  47. });
  48. input.blur(function() {
  49. if (timeout) clearTimeout(timeout);
  50. timeout = setTimeout(hide, 200);
  51. });
  52. function hide() {
  53. if (timeout) clearTimeout(timeout);
  54. input.removeClass("loading");
  55. if (results) {
  56. results.fadeOut("fast").remove();
  57. results = null;
  58. }
  59. $("iframe.iefix").remove();
  60. selectedIndex = -1;
  61. }
  62. function move(index) {
  63. if (!results) return;
  64. items = $("li", results);
  65. items.removeClass("selected");
  66. $(items[index]).addClass("selected");
  67. selectedIndex = index;
  68. }
  69. function select(li) {
  70. if (!li) li = $("<li>");
  71. else li = $(li);
  72. var val = $.trim(li.text());
  73. prev = val;
  74. input.val(val);
  75. hide();
  76. selectedIndex = -1;
  77. }
  78. function show() {
  79. var val = input.val();
  80. if (val == prev) return;
  81. prev = val;
  82. if (val.length < minChars) { hide(); return; }
  83. input.addClass("loading");
  84. var params = {};
  85. params[paramName] = val;
  86. $.get(url, params, function(data) {
  87. if (!data) { hide(); return; }
  88. if (!results) {
  89. var offset = input.offset();
  90. results = $("<div>").addClass("suggestions").css({
  91. position: "absolute",
  92. minWidth: input.get(0).offsetWidth + "px",
  93. top: (offset.top + input.get(0).offsetHeight) + "px",
  94. left: offset.left + "px",
  95. zIndex: 2
  96. }).appendTo("body");
  97. if ($.browser.msie) {
  98. var iframe = $("<iframe style='display:none;position:absolute;" +
  99. "filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);'" +
  100. " class='iefix' src='javascript:false;' frameborder='0'" +
  101. " scrolling='no'></iframe>").insertAfter(results);
  102. setTimeout(function() {
  103. var offset = getOffset(results);
  104. iframe.css({
  105. top: offset.top + "px",
  106. right: (offset.left + results.get(0).offsetWidth) + "px",
  107. bottom: (offset.top + results.get(0).offsetHeight) + "px",
  108. left: offset.left + "px",
  109. zIndex: 1
  110. });
  111. iframe.show();
  112. }, 10);
  113. }
  114. }
  115. results.html(data).fadeTo("fast", 0.92);
  116. items = $("li", results);
  117. items
  118. .hover(function() { move(items.index(this)) },
  119. function() { $(this).removeClass("selected") })
  120. .click(function() { select(this); input.get(0).focus() });
  121. move(0);
  122. });
  123. }
  124. }
  125. $.fn.suggest = function(url, paramName, minChars, delay) {
  126. url = url || window.location.pathname;
  127. paramName = paramName || 'q';
  128. minChars = minChars || 1;
  129. delay = delay || 400;
  130. return this.each(function() {
  131. new $.suggest(this, url, paramName, minChars, delay);
  132. });
  133. }
  134. })(jQuery);