]> git.openstreetmap.org Git - osqa.git/blob - forum/skins/default/media/js/osqa.ask.js
Applies patches by Justing Grant. Makes several improvements in the db exporter.
[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
23     var results_cache = {};
24
25     function reload_suggestions_box(e) {
26         var relatedQuestionsDiv = $('#ask-related-questions');
27         var q = $input.val().replace(/^\s+|\s+$/g,"");
28
29         if(q.length == 0) {
30             close_suggestions_box();
31             relatedQuestionsDiv.html('');
32             return false;
33         } else if(relatedQuestionsDiv[0].style.height == 0 || relatedQuestionsDiv[0].style.height == '0px') {
34             relatedQuestionsDiv.animate({'height':'150'}, 350);
35         }
36
37         if (results_cache[q] && results_cache[q] != '') {
38             relatedQuestionsDiv.html(results_cache[q]);
39             return false;
40         }
41
42         $.post(related_questions_url, {title: q}, function(data) {
43             if (data) {
44                 var c = $input.val().replace(/^\s+|\s+$/g,"");
45
46                 if (c != q) {
47                     return;
48                 }
49
50                 if(data.length == 0) {
51                     relatedQuestionsDiv.html('<br /><br /><div align="center">No questions like this have been found.</div>');
52                     return;
53                 }
54
55                 var html = '';
56                 for (var i = 0; i < data.length; i++) {
57                     var item = template.replace(new RegExp('%URL%', 'g'), data[i].url)
58                                        .replace(new RegExp('%SCORE%', 'g'), data[i].score)
59                                        .replace(new RegExp('%TITLE%', 'g'), data[i].title)
60                                        .replace(new RegExp('%SUMMARY%', 'g'), data[i].summary);
61
62                     html += item;
63
64                 }
65
66                 results_cache[q] = html;
67
68                 relatedQuestionsDiv.html(html);
69             }
70         }, 'json');
71
72         return false;
73     }
74
75     function close_suggestions_box() {
76         $('#ask-related-questions').animate({'height':'0'},350, function() {
77             $('#ask-related-questions').html('');
78         });
79     }
80
81     $input.keyup(reload_suggestions_box);
82     $input.focus(reload_suggestions_box);
83     $input.blur(close_suggestions_box);
84
85     // for chrome
86     $input.keydown(focus_on_question);
87     function focus_on_question(e) {
88         var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
89
90         if(e.keyCode == 9 && is_chrome) {
91             $('#editor')[0].focus();
92         }
93     }
94 });