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