Utklippstavle

Legg inn en helt ny kode

Valgmuligheter

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

 
 
var log = typeof console !== "undefined" && console.log ?
    function () {
        console.log.apply(console, arguments);
    }
    :
    function () {
 
    };
 
if(!window.jQuery && typeof unsafeWindow != "undefined" && unsafeWindow.jQuery) {
    var $ = jQuery = unsafeWindow.jQuery;   
}
 
$.fn.selectFocus = function () {
  if(this[0]) {
    this[0].focus();
    this[0].select();
  }
};
$.fn.parentContainer = function () {
    return this.closest("div.show");
};
 
var $searchField = $("#searchfield");
if (!$searchField.length) {
    $searchField = $('<input type="text" name="searchfield" id="searchfield" />');
    $searchField.appendTo("div.sort");
 
}
 
var $label = $("#searchlabel");
if (!$label.length) {
    $label = $('<label for="searchfield"></label>');
    $label.insertAfter($searchField);
}
 
function updateLabel($matches) {
    if($matches.length) {
        $label.html($matches.length + " treff");
    } else {
        $label.html("Ingen treff");
    }
}
 
 
$(document).keydown(function (ev) {
 
  var key = String.fromCharCode(ev.keyCode);
 
  if (key == "F"  && (ev.ctrlkey || ev.metaKey)) {
    ev.preventDefault();
 
    log("ctrl+f trykt!");
    $searchField.selectFocus();
 
 
  } 
});
 
var $shows = $("#page-content div.show a[href^=/shows/]");
$searchField.keyup( function highlightShow() {
  var val = this.value;
  //if(!val || val.length < 2) return false;
 
  val = val.toLowerCase();
 
  //filter out <a>-elements without the val in its href
  if (val) {
    var $matches = $shows.filter(function() {
 
 
      // find the last part of the href value and decode it to make "kyle%20xy" -> "kyle xy"
      var sub = decodeURIComponent(this.href).toLowerCase();
      sub = sub.substr(sub.lastIndexOf("/")+1);
      //console.log("\t checking " + sub);
 
      //check if "kyle xy" has val (the search input) in it
      var alt = $(this).attr("alt") || "";
      return ( sub.indexOf( val ) + 1 )  ||
              (alt.indexOf( val ) + 1 );  
    });
  } else {
    log("empty input length");
    $shows.parentContainer().slideDown();
    $label.html("");
    return;
  }
 
  if($matches.length) {
    log("showing some matches..!");
    $matches.parentContainer().slideDown();
    $shows.not($matches).parentContainer().slideUp();
 
  } else {
    log("no matches, show every show")
    $shows.parentContainer().slideDown();
  }
 
  updateLabel($matches);
 
 
});
 
$(document).bind("updateshowlist", function () {
    log("updating show list contents")
    $shows = $("#page-content div.show a[href^=/shows/]"); 
})
 

Tilbake til toppen ^

Emneord (tags):