Utklippstavle

Legg inn en helt ny kode

Valgmuligheter

dabear sendte inn denne javascript-koden 07.03.2010 kl. 16:39.

  1.  
  2.  
  3. var log = typeof console !== "undefined" && console.log ?
  4. function () {
  5. console.log.apply(console, arguments);
  6. }
  7. :
  8. function () {
  9.  
  10. };
  11.  
  12. if(!window.jQuery && typeof unsafeWindow != "undefined" && unsafeWindow.jQuery) {
  13. var $ = jQuery = unsafeWindow.jQuery;
  14. }
  15.  
  16. $.fn.selectFocus = function () {
  17. if(this[0]) {
  18. this[0].focus();
  19. this[0].select();
  20. }
  21. };
  22. $.fn.parentContainer = function () {
  23. return this.closest("div.show");
  24. };
  25.  
  26. var $searchField = $("#searchfield");
  27. if (!$searchField.length) {
  28. $searchField = $('<input type="text" name="searchfield" id="searchfield" />');
  29. $searchField.appendTo("div.sort");
  30.  
  31. }
  32.  
  33. var $label = $("#searchlabel");
  34. if (!$label.length) {
  35. $label = $('<label for="searchfield"></label>');
  36. $label.insertAfter($searchField);
  37. }
  38.  
  39. function updateLabel($matches) {
  40. if($matches.length) {
  41. $label.html($matches.length + " treff");
  42. } else {
  43. $label.html("Ingen treff");
  44. }
  45. }
  46.  
  47.  
  48. $(document).keydown(function (ev) {
  49.  
  50. var key = String.fromCharCode(ev.keyCode);
  51.  
  52. if (key == "F" && (ev.ctrlkey || ev.metaKey)) {
  53. ev.preventDefault();
  54.  
  55. log("ctrl+f trykt!");
  56. $searchField.selectFocus();
  57.  
  58.  
  59. }
  60. });
  61.  
  62. var $shows = $("#page-content div.show a[href^=/shows/]");
  63. $searchField.keyup( function highlightShow() {
  64. var val = this.value;
  65. //if(!val || val.length < 2) return false;
  66.  
  67. val = val.toLowerCase();
  68.  
  69. //filter out <a>-elements without the val in its href
  70. if (val) {
  71. var $matches = $shows.filter(function() {
  72.  
  73.  
  74. // find the last part of the href value and decode it to make "kyle%20xy" -> "kyle xy"
  75. var sub = decodeURIComponent(this.href).toLowerCase();
  76. sub = sub.substr(sub.lastIndexOf("/")+1);
  77. //console.log("\t checking " + sub);
  78.  
  79. //check if "kyle xy" has val (the search input) in it
  80. var alt = $(this).attr("alt") || "";
  81. return ( sub.indexOf( val ) + 1 ) ||
  82. (alt.indexOf( val ) + 1 );
  83. });
  84. } else {
  85. log("empty input length");
  86. $shows.parentContainer().slideDown();
  87. $label.html("");
  88. return;
  89. }
  90.  
  91. if($matches.length) {
  92. log("showing some matches..!");
  93. $matches.parentContainer().slideDown();
  94. $shows.not($matches).parentContainer().slideUp();
  95.  
  96. } else {
  97. log("no matches, show every show")
  98. $shows.parentContainer().slideDown();
  99. }
  100.  
  101. updateLabel($matches);
  102.  
  103.  
  104. });
  105.  
  106. $(document).bind("updateshowlist", function(){
  107. $shows = $("#page-content div.show a[href^=/shows/]");
  108. })
  109.  

Tilbake til toppen ^

Emneord (tags):