]> git.openstreetmap.org Git - osqa.git/blob - forum/skins/default/media/js/osqa.ask.js
Improves the answer position guessing.
[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 $(function () {
9     $('div#editor_side_bar').hide();
10     $('div#tags_side_bar').hide();
11
12     $('#id_title').focus(function(){changeSideBar('div#title_side_bar')});
13     $('#editor').focus(function(){changeSideBar('div#editor_side_bar')});
14     $('#id_tags').focus(function(){changeSideBar('div#tags_side_bar')});
15 });
16
17 $(function() {
18     var $input = $('#id_title');
19     var $box = $('#ask-related-questions');
20     var template = $('#question-summary-template').html();
21
22     var results_cache = {};
23
24     function reload_suggestions_box(e) {
25         var q = $input.val().replace(/^\s+|\s+$/g,"");
26
27         if (q.length == 0) {
28             $('#ask-related-questions').html('');
29             return false;
30         }
31
32         if (results_cache[q] && results_cache[q] != '') {
33             $('#ask-related-questions').html(results_cache[q]);
34             return false;
35         }
36
37         $.post(related_questions_url, {title: q}, function(data) {
38             if (data) {
39                 var c = $input.val().replace(/^\s+|\s+$/g,"");
40
41                 if (c != q) {
42                     return;
43                 }
44
45                 var html = '';
46                 for (var i = 0; i < data.length; i++) {
47                     var item = template.replace(new RegExp('%URL%', 'g'), data[i].url)
48                                        .replace(new RegExp('%SCORE%', 'g'), data[i].score)
49                                        .replace(new RegExp('%TITLE%', 'g'), data[i].title)
50                                        .replace(new RegExp('%SUMMARY%', 'g'), data[i].summary);
51
52                     html += item;
53
54                 }
55
56                 results_cache[q] = html;
57
58                 $('#ask-related-questions').html(html);
59             }
60         }, 'json');
61
62         return false;
63     }
64
65     $input.keyup(reload_suggestions_box);
66 });