Search live plugin: restore target's html when query's value is lower than minLength
[shapado:shapado.git] / public / javascripts / app / initializers / search.js
1 (function($){
2   $.fn.searcher = function(options) {
3     var defaults = { timeout: 500,
4       threshold: 100,
5       minLength: 1,
6       extraParams: {},
7       url: "",
8       target: $("body"),
9       behaviour : "live",
10       success: function(data) {},
11       before_query: function() {}
12     }
13
14     var options =  $.extend(defaults, options);
15
16     return this.each(function() {
17       var timer = null;
18       var last = "";
19       var settings = options;
20       var self = $(this);
21       var extraParams = [];
22       var cache = settings.target.html();
23
24       if(typeof settings.fields == "undefined") {
25         settings.fields = $(this).find("input[type=text],textarea");
26       }
27
28        //HACK?
29       for(var property in settings.extraParams) {
30         extraParams.push({ name : property, value : settings.extraParams[property]});
31       }
32
33       var query = function() {
34         settings.before_query.call(self, settings.target);
35
36         $.ajax({
37           url: settings.url,
38           dataType: "json",
39           type: "GET",
40           data: $.merge(settings.fields.serializeArray(), extraParams),
41           success: function(data) {
42             settings.target.empty();
43             settings.target.append(data.html);
44             settings.success(data);
45           }
46         });
47       }
48
49       $.each(settings.fields, function(){
50         if(this.value) {
51           query();
52           return false;
53         }
54       });
55
56       var live = function() {
57         $.each(settings.fields, function(){
58           var timer = null;
59           $(this).keyup(function() {
60             if(!($(this).val().length <= settings.minLength)){
61               if(this.value != last) {
62                 if (timer){
63                   clearTimeout(timer);
64                 }
65                 last = this.value;
66                 timer = setTimeout(query, settings.timeout);
67               }
68             } else {
69               settings.target.empty();
70               settings.target.append(cache);
71             }
72           });
73         });
74       }
75
76       var focusout = function() {
77         $.each(settings.fields, function(){
78           $(this).blur(function() {
79             if (!($(this).val().length <= settings.minLength)) {
80               if(this.value != last) {
81                 query();
82               }
83             } else {
84               settings.target.empty();
85               settings.target.append(cache);
86             }
87           });
88         });
89       }
90
91       switch(settings.behaviour) {
92         case "live":
93           live();
94           break;
95         case "focusout":
96           focusout();
97           live();
98           break;
99       }
100     });
101   }
102 })(jQuery);