diff options
author | Tomasz Zielinski <tomasz.zielinski@pyconsultant.eu> | 2012-01-11 11:33:04 +0100 |
---|---|---|
committer | Tomasz Zielinski <tomasz.zielinski@pyconsultant.eu> | 2012-01-11 15:18:13 +0100 |
commit | 62ca192452a2e7b0992f7a8bd729a1b97e8c9605 (patch) | |
tree | d1943c61251e85ab6f00bc079a054c2085c222aa | |
parent | c993c66c8388866bdf472d6a026ea83b2668b60e (diff) | |
download | askbot-62ca192452a2e7b0992f7a8bd729a1b97e8c9605.tar.gz askbot-62ca192452a2e7b0992f7a8bd729a1b97e8c9605.tar.bz2 askbot-62ca192452a2e7b0992f7a8bd729a1b97e8c9605.zip |
Optimized live search for the ask page
-rw-r--r-- | askbot/skins/common/media/js/live_search_new_thread.js | 82 | ||||
-rw-r--r-- | askbot/skins/default/templates/ask.html | 4 |
2 files changed, 84 insertions, 2 deletions
diff --git a/askbot/skins/common/media/js/live_search_new_thread.js b/askbot/skins/common/media/js/live_search_new_thread.js new file mode 100644 index 00000000..53c820fa --- /dev/null +++ b/askbot/skins/common/media/js/live_search_new_thread.js @@ -0,0 +1,82 @@ + +var liveSearchNewThreadInit = function() { + var query = $('input#id_title.questionTitleInput'); + var prev_text = $.trim(query.val()); + var search_url = askbot['urls']['api_get_questions']; + var running = false; + var q_list_sel = 'question-list'; //id of question listing div + + running = false; + var ask_page_eval_handle; + query.keyup(function(e){ + if (running === false){ + clearTimeout(ask_page_eval_handle); + ask_page_eval_handle = setTimeout(eval_query, 400); + } + }); + + var eval_query = function(){ + cur_text = $.trim(query.val()); + if (cur_text !== prev_text && running === false){ + if (cur_text.length >= minSearchWordLength){ + send_query(cur_text); + } else if (cur_text.length === 0){ + /* restart query */ + $('#' + q_list_sel).css('height',0).children().remove(); + running = false; + prev_text = ''; + } + } + }; + + var render_result = function(data, text_status, xhr){ + var container = $('#' + q_list_sel); + container.fadeOut(200, function() { + container.children().remove(); + $.each(data, function(idx, question){ + var url = question['url']; + var title = question['title']; + var answer_count = question['answer_count']; + var list_item = $('<h2></h2>'); + var count_element = $('<span class="item-count"></span>'); + count_element.html(answer_count); + list_item.append(count_element); + var link = $('<a></a>'); + link.attr('href', url); + list_item.append(link); + title_element = $('<span class="title"></span>'); + title_element.html(title); + link.append(title) + container.append(list_item); + }); + container.show();//show just to measure + var unit_height = container.children(':first').outerHeight(); + container.hide();//now hide + if (data.length > 5){ + container.css('overflow-y', 'scroll'); + container.css('height', unit_height*5 + 'px'); + } else { + container.css('height', data.length*unit_height + 'px'); + container.css('overflow-y', 'hidden'); + } + container.fadeIn(); + }); + }; + + var send_query = function(query_text){ + prev_text = query_text; + running = true; + $.ajax({ + url: search_url, + dataType: 'json', + success: render_result, + complete: function() { + running = false; + eval_query(); + }, + data: {query: query_text}, + cache: false + }); + } + +}; diff --git a/askbot/skins/default/templates/ask.html b/askbot/skins/default/templates/ask.html index 3849ef3b..a36f1d0c 100644 --- a/askbot/skins/default/templates/ask.html +++ b/askbot/skins/default/templates/ask.html @@ -22,7 +22,7 @@ var sortMethod = undefined;//need for live_search var minSearchWordLength = {{settings.MIN_SEARCH_WORD_LENGTH}}; </script> - <script type='text/javascript' src='{{"/js/live_search.js"|media}}'></script> + <script type='text/javascript' src='{{"/js/live_search_new_thread.js"|media}}'></script> {% include "meta/editor_data.html" %} {% if mandatory_tags %} {% include "meta/mandatory_tags_js.html" %} @@ -35,7 +35,7 @@ var codeFriendlyMarkdown = false; {% endif %} $().ready(function(){ - liveSearch().init('ask_page'); + liveSearchNewThreadInit(); //set current module button style $('#editor').TextAreaResizer(); |