summaryrefslogtreecommitdiff
path: root/public/javascripts/admin_search.js
blob: 28f87a0087c59249c606e989b59767fd12ed5c13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
admin_search = {
  
  initialize : function() {
    $("#search_widget").hide();

    $(document).bind("keydown", 'Alt+f', function(){
      admin_search.display_toggle();
      return false;
    });
  },
  
  display_toggle : function() {
    if ($('#search_widget').css("display") != "none") {
      $('#search_widget').fadeOut();
    }
    else {
      $('#search_widget').fadeIn();
      $('#search_term').attr("value", "");
      $('#search_term').focus();
    }
    
    $("#search_term").bind("keyup", function() {
      if ($(this).attr("value")) {
        $.ajax({
          type: "GET",
          url: "/admin/search",
          data: "search_term=" + $(this).attr("value"),
          dataType: "json",
          success : function(results) {
            admin_search.show_results(results);
          }
        });
      }
      else {
        $('#search_results').slideUp();
        $('#search_results').empty(); 
      }
    });
  },
    
  show_results : function(results) {
     $('#search_results').empty();
     for (result in results) {
       $('#search_results').append("<p><a href='"+ results[result].edit_path + "'>" + results[result].title + "</a></p>");
     }
     $('#search_results').slideDown();
  }
}