]> git.openstreetmap.org Git - osqa.git/blob - forum/skins/default/media/js/osqa.ask.js
Reintegrate merge cacheimp -> trunk.
[osqa.git] / forum / skins / default / media / js / osqa.ask.js
1 var currentSideBar = 'div#title_side_bar';
2 function changeSideBar(enabled_bar) {
3     $(currentSideBar).hide();
4     currentSideBar = enabled_bar;
5     $(currentSideBar).fadeIn('slow');
6
7 }
8
9 $(function () {
10     $('div#editor_side_bar').hide();
11     $('div#tags_side_bar').hide();
12
13     $('#id_title').focus(function(){changeSideBar('div#title_side_bar')});
14     $('#editor').focus(function(){changeSideBar('div#editor_side_bar')});
15     $('#id_tags').focus(function(){changeSideBar('div#tags_side_bar')});
16 });
17
18 $(function() {
19     var $input = $('#id_title');
20     var $box = $('#ask-related-questions');
21     var template = $('#question-summary-template').html();
22     var $editor = $('#editor');
23
24     var results_cache = {};
25
26     function reload_suggestions_box(e) {
27         var relatedQuestionsDiv = $('#ask-related-questions');
28         var q = $input.val().replace(/^\s+|\s+$/g,"");
29
30         if(q.length == 0) {
31             close_suggestions_box();
32             relatedQuestionsDiv.html('');
33             return false;
34         } else if(relatedQuestionsDiv[0].style.height == 0 || relatedQuestionsDiv[0].style.height == '0px') {
35             relatedQuestionsDiv.animate({'height':'150'}, 350);
36         }
37
38         if (results_cache[q] && results_cache[q] != '') {
39             relatedQuestionsDiv.html(results_cache[q]);
40             return false;
41         }
42
43         $.post(related_questions_url, {title: q}, function(data) {
44             if (data) {
45                 var c = $input.val().replace(/^\s+|\s+$/g,"");
46
47                 if (c != q) {
48                     return;
49                 }
50
51                 if(data.length == 0) {
52                     relatedQuestionsDiv.html('<br /><br /><div align="center">No questions like this have been found.</div>');
53                     return;
54                 }
55
56                 var html = '';
57                 for (var i = 0; i < data.length; i++) {
58                     var item = template.replace(new RegExp('%URL%', 'g'), data[i].url)
59                                        .replace(new RegExp('%SCORE%', 'g'), data[i].score)
60                                        .replace(new RegExp('%TITLE%', 'g'), data[i].title)
61                                        .replace(new RegExp('%SUMMARY%', 'g'), data[i].summary);
62
63                     html += item;
64
65                 }
66
67                 results_cache[q] = html;
68
69                 relatedQuestionsDiv.html(html);
70             }
71         }, 'json');
72
73         return false;
74     }
75
76     function close_suggestions_box() {
77         $('#ask-related-questions').animate({'height':'0'},350, function() {
78             $('#ask-related-questions').html('');
79         });
80     }
81
82     $input.keyup(reload_suggestions_box);
83     $input.focus(reload_suggestions_box);
84
85     $editor.change(function() {
86         if ($editor.html().length > 10) {
87             close_suggestions_box();
88         }
89     });
90
91
92
93     // for chrome
94     $input.keydown(focus_on_question);
95     function focus_on_question(e) {
96         var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
97
98         if(e.keyCode == 9 && is_chrome) {
99             $('#editor')[0].focus();
100         }
101     }
102 });