summaryrefslogtreecommitdiff
path: root/public/javascripts/admin_search.js
blob: c8d269fb8036e3ca8b3bc6b5eee0be1064715df4 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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();
  }
};

menu_items = {
  
  initialize_search : function() {
    $("#menu_search_term").bind("keyup", function() {
      if ($(this).attr("value")) {
        $.ajax({
          type: "GET",
          url: "/admin/menu_search",
          data: "search_term=" + $(this).attr("value"),
          dataType: "json",
          success : function(results) {
            menu_items.show_results(results);
          }
        });
      }
      else {
        $('#search_results').slideUp();
        $('#search_results').empty(); 
      }
    });
  },
  
  show_results : function(results) {
    $("#search_results").empty();
    for (result in results) {
      var link = $(("<a href='#'>"+ results[result].title + "</a>"));
      $(link).bind("click", menu_items.link_closure(results[result]));
      
      
      // Sometimes I don't get jquery; wrap() didn't work *sigh*
      // Guess I'll need a book someday or another framework
      var wrapper = $("<div></div>");
      $(wrapper).append(link)
      
      $("#search_results").append(wrapper);
      
    }
  },
  
  link_closure : function(node) {
    var barf = function(){
      $("#menu_item_node_id").val(node.node_id);
      $("#menu_item_path").val("/" + node.unique_name);
      $("#menu_item_title").val(node.title);
      return false;
    }
    
    return barf;
  } 
};