Change behavior of quick question box:
[shapado:shapado.git] / public / javascripts / modules / new_question.js
1
2 $(document).ready(function() {
3   $("label#rqlabel").hide();
4
5   $(".text_field#question_title").focus( function() {
6     highlightEffect($("#sidebar .help"))
7   });
8
9   $("#related_questions").hide();
10
11   $("#ask_question").searcher({url : "/questions/related_questions.js",
12                               target : $("#related_questions"),
13                               fields : $("form#ask_question input[type=text][name*=question]"),
14                               behaviour : "focusout",
15                               timeout : 2500,
16                               extraParams : { 'format' : 'js', 'per_page' : 5 },
17                               before_query: function(target) {
18                                 target.show();
19                               },
20                               success: function(data) {
21                                 if(!data.html) {
22                                   $("#related_questions").hide();
23                                   // TODO: show a message
24                                 }
25                                 $("label#rqlabel").show();
26                               }
27   });
28
29   $("#ask_question").bind("keypress", function(e) {
30     if (e.keyCode == 13) {
31        return false;
32      }
33   });
34
35   var fields = $("#attachments #fields");
36   var template = fields.find(".template");
37   template.find("input").attr("name", "question[attachments[id]]");
38   template.hide();
39
40   $("#attachments #fields .attachment_field .remove_attachment").live("click", function(e) {
41     $(this).parent().remove();
42     return false;
43   });
44
45   $(".remove_attachment_link").live("click", function(e) {
46     var url = $(this).attr("href");
47     var remove = confirm("are you sure?"); //TODO; i18n
48     if (remove) {
49       $.ajax({url: url, dataType: 'json', context: $(this), success: function(data, textStatus, XMLHttpRequest){
50         $(this).parent().remove();
51       }});
52     }
53     return false;
54   });
55
56   var count = -1;
57   $("#attachments .add_attachment").live("click", function(e) {
58     var template = fields.find(".template");
59     var new_field = template.clone();
60     new_field.removeClass("template");
61     count++;
62     var new_name = new_field.find("input").attr("name").replace(/(id)/, count);
63     new_field.find("input").attr("name",new_name)
64
65     new_field.show();
66
67     fields.append(new_field);
68
69     return false;
70   });
71
72
73 });