diff options
Diffstat (limited to 'askbot/skins/default/media/js/live_search.js')
-rw-r--r-- | askbot/skins/default/media/js/live_search.js | 327 |
1 files changed, 250 insertions, 77 deletions
diff --git a/askbot/skins/default/media/js/live_search.js b/askbot/skins/default/media/js/live_search.js index cdd165d3..b94ecf07 100644 --- a/askbot/skins/default/media/js/live_search.js +++ b/askbot/skins/default/media/js/live_search.js @@ -1,13 +1,18 @@ var prevSortMethod = sortMethod; -$(document).ready(function(){ - var query = $('input#keywords'); - var prev_text = $.trim(query.val()); +var liveSearch = function(){ + var query = undefined; + var prev_text = undefined; var running = false; - var q_list_sel = 'listA';//id of question listing div + var q_list_sel = 'question-list';//id of question listing div + var search_url = undefined; + var restart_query = function(){}; + var process_query = function(){}; + var render_result = function(){}; + var refresh_x_button = function(){ if ($.trim(query.val()).length > 0){ - if (query.attr('class') == 'searchInput'){ + if (query.attr('class') === 'searchInput'){ query.attr('class', 'searchInputCancelable'); x_button = $('<input class="cancelSearchBtn" type="button" name="reset_query"/>'); //x_button.click(reset_query); @@ -15,70 +20,81 @@ $(document).ready(function(){ x_button.click( function(){ query.val(''); - if (sortMethod == 'relevance-desc'){ + if (sortMethod === 'relevance-desc'){ sortMethod = prevSortMethod; } + refresh_x_button(); reset_query(sortMethod); } ); query.after(x_button); } - } - else { + } else { $('input[name=reset_query]').remove(); query.attr('class', 'searchInput'); } }; + var reset_sort_method = function(){ + if (sortMethod === 'relevance-desc'){ + sortMethod = prevSortMethod; + if (sortMethod === 'relevance-desc'){ + sortMethod = 'activity-desc'; + } + } else { + sortMethod = 'activity-desc'; + prevSortMethod = 'activity-desc'; + } + }; + var eval_query = function(){ cur_text = $.trim(query.val()); - if (cur_text != prev_text && running === false){ + if (cur_text !== prev_text && running === false){ if (cur_text.length >= minSearchWordLength){ - if (prev_text.length === 0 && showSortByRelevance){ - if (sortMethod == 'activity-desc'){ - prevSortMethod = sortMethod; - sortMethod = 'relevance-desc'; - } - } - send_query(cur_text, sortMethod); - running = true; - } - else if (cur_text.length === 0){ - if (sortMethod == 'relevance-desc'){ - sortMethod = prevSortMethod; - } - reset_query(sortMethod); + process_query(); running = true; + } else if (cur_text.length === 0){ + restart_query(); } } - } + }; - var listen = function(){ + var ask_page_search_listen = function(){ running = false; - refresh_x_button(); - query.keydown(function(e){ - refresh_x_button(); + var ask_page_eval_handle; + query.keyup(function(e){ if (running === false){ - setTimeout(eval_query, 50); + clearTimeout(ask_page_eval_handle); + ask_page_eval_handle = setTimeout(eval_query, 400); } }); - query.keyup(function(){ + }; + + var main_page_search_listen = function(){ + running = false; + refresh_x_button(); + var main_page_eval_handle; + query.keyup(function(e){ refresh_x_button(); + if (running === false){ + clearTimeout(main_page_eval_handle); + main_page_eval_handle = setTimeout(eval_query, 400); + } }); - } + }; - var render_counter = function(count, word, counter_class){ - var output = '<div class="votes">' + - '<span class="item-count ' + counter_class + '">' + + var render_counter = function(count, word, counter_class, counter_subclass){ + var output = '<div class="' + counter_class + ' ' + counter_subclass + '">' + + '<span class="item-count">' + count; - if (counter_class == 'accepted'){ - output += '✓' + if (counter_class === 'accepted'){ + output += '✓'; } output += '</span>' + '<div>' + word + '</div>' + '</div>'; return output; - } + }; var render_title = function(result){ return '<h2>' + @@ -94,15 +110,21 @@ $(document).ready(function(){ var render_user_link = function(result){ if (result['u_id'] !== false){ - var u_slug = result['u_name'].toLowerCase().replace(/ +/g, '-'); - return '<a ' + - 'href="' + - askbot['urls']['user_url_template'] - .replace('{{user_id}}', result['u_id']) - .replace('{{slug}}', u_slug) + - '">' + - result['u_name'] + - '</a> '; + if (result['u_is_anonymous'] === true){ + return '<span class="anonymous">' + + askbot['messages']['name_of_anonymous_user'] + + '</span>'; + } else { + var u_slug = result['u_name'].toLowerCase().replace(/ +/g, '-'); + return '<a ' + + 'href="' + + askbot['urls']['user_url_template'] + .replace('{{user_id}}', result['u_id']) + .replace('{{slug}}', u_slug) + + '">' + + result['u_name'] + + '</a> '; + } } else { return ''; @@ -137,6 +159,21 @@ $(document).ready(function(){ return html; }; + var render_user_flag = function(result){ + var country_code = result['u_country_code']; + if (country_code) { + return '<img class="flag" src="'+ + mediaUrl( + 'media/images/flags/' + + country_code.toLowerCase() + + '.gif' + ) + + '"/>'; + } else { + return ''; + } + }; + var render_user_info = function(result){ var user_html = '<div class="userinfo">' + @@ -145,33 +182,29 @@ $(document).ready(function(){ '>' + result['timesince'] + '</span> ' + - render_user_link(result) + + render_user_link(result); + if (result['u_is_anonymous'] === false){ + user_html += render_user_flag(result); //render_user_badge_and_karma(result) + - '</div>'; + } + user_html += '</div>'; return user_html; }; - var render_tag = function(tag_name){ - var url = askbot['urls']['questions'] + - '?tags=' + encodeURI(tag_name); - var tag_title = $.i18n._( - "see questions tagged '{tag}'" - ).replace( - '{tag}', - tag_name - ); - return '<a ' + - 'href="' + url + '" ' + - 'title="' + tag_title + '" rel="tag"' + - '>' + tag_name + '</a>'; + var render_tag = function(tag_name, linkable, deletable){ + var tag = new Tag(); + tag.setName(tag_name); + tag.setDeletable(deletable); + tag.setLinkable(linkable); + return tag.getElement().outerHTML(); }; - var render_tags = function(tags){ - var tags_html = '<div class="tags">'; - for (var i=0; i<tags.length; i++){ - tags_html += render_tag(tags[i]); - } - tags_html += '</div>'; + var render_tags = function(tags, linkable, deletable){ + var tags_html = '<ul class="tags">'; + $.each(tags, function(idx, item){ + tags_html += render_tag(item, linkable, deletable); + }); + tags_html += '</ul>'; return tags_html; }; @@ -182,22 +215,25 @@ $(document).ready(function(){ render_counter( question['votes'], question['votes_word'], + 'votes', question['votes_class'] ) + render_counter( question['answers'], question['answers_word'], + 'answers', question['answers_class'] ) + render_counter( question['views'], question['views_word'], + 'views', question['views_class'] ) + + render_user_info(question) + '</div>' + render_title(question) + - render_user_info(question) + - render_tags(question['tags']) + + render_tags(question['tags'], true, false) + '</div>'; return entry_html; }; @@ -228,7 +264,7 @@ $(document).ready(function(){ } var html = ''; for (var i=0; i<tags.length; i++){ - html += render_tag(tags[i]['name']); + html += render_tag(tags[i]['name'], true, false); html += '<span class="tag-number">× ' + tags[i]['used_count'] + '</span>' + @@ -253,11 +289,38 @@ $(document).ready(function(){ $('#question-count').html(count_html); }; + var get_old_tags = function(container){ + var tag_elements = container.find('.tag'); + var old_tags = []; + tag_elements.each(function(idx, element){ + old_tags.push($(element).html()); + }); + return old_tags; + }; + + var render_search_tags = function(tags){ + var search_tags = $('#search-tags'); + search_tags.children().remove(); + var tags_html = ''; + $.each(tags, function(idx, tag_name){ + var tag = new Tag(); + tag.setName(tag_name); + tag.setDeletable(true); + tag.setLinkable(false); + tag.setDeleteHandler( + function(){ + remove_search_tag(tag_name); + } + ); + search_tags.append(tag.getElement()); + }); + }; + var create_relevance_tab = function(){ relevance_tab = $('<a></a>'); relevance_tab.attr('href', '?sort=relevance-desc'); relevance_tab.attr('id', 'by_relevance'); - relevance_tab.html(sortButtonData['relevance']['label']); + relevance_tab.html('<span>' + sortButtonData['relevance']['label'] + '</span>'); return relevance_tab; } @@ -308,7 +371,60 @@ $(document).ready(function(){ } }; - var render_result = function(data, text_status, xhr){ + var remove_search_tag = function(tag_name){ + $.ajax({ + url: askbot['urls']['questions'], + data: {remove_tag: tag_name}, + dataType: 'json', + success: render_result, + complete: try_again + }); + }; + + var activate_search_tags = function(){ + var search_tags = $('#search-tags .tag-left'); + $.each(search_tags, function(idx, element){ + var tag = new Tag(); + tag.decorate($(element)); + //todo: setDeleteHandler and setHandler + //must work after decorate & must have getName + tag.setDeleteHandler( + function(){ + remove_search_tag(tag.getName()); + } + ); + }); + }; + + var render_ask_page_result = function(data, text_status, xhr){ + var container = $('#' + q_list_sel); + container.children().remove(); + if (data.length > 5){ + container.css('overflow-y', 'scroll'); + container.css('height', '120px'); + } else { + container.css('height', data.length * 24 + 'px'); + container.css('overflow-y', 'hidden'); + } + $.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); + }); + }; + + var render_main_page_result = function(data, text_status, xhr){ var old_list = $('#' + q_list_sel); var new_list = $('<div></div>'); if (data['questions'].length > 0){ @@ -320,6 +436,7 @@ $(document).ready(function(){ new_list.attr('id', q_list_sel); render_paginator(data['paginator']); set_question_count(data['question_counter']); + render_search_tags(data['query_data']['tags']); render_faces(data['faces']); render_related_tags(data['related_tags']); render_relevance_sort_tab(); @@ -337,7 +454,7 @@ $(document).ready(function(){ var send_query = function(query_text, sort_method){ var post_data = {query: query_text}; $.ajax({ - url: askbot['urls']['questions'], + url: search_url, data: {query: query_text, sort: sort_method}, dataType: 'json', success: render_result, @@ -347,9 +464,8 @@ $(document).ready(function(){ } var reset_query = function(sort_method){ - refresh_x_button(); $.ajax({ - url: askbot['urls']['questions'], + url: search_url, data: {reset_query: true, sort: sort_method}, dataType: 'json', success: render_result, @@ -358,5 +474,62 @@ $(document).ready(function(){ prev_text = ''; } - listen(); -}); + var refresh_main_page = function(){ + $.ajax({ + url: askbot['urls']['questions'], + data: {preserve_state: true}, + dataType: 'json', + success: render_main_page_result + }); + }; + + return { + refresh: function(){ + refresh_main_page(); + }, + init: function(mode){ + if (mode === 'main_page'){ + //live search for the main page + query = $('input#keywords'); + search_url = askbot['urls']['questions']; + render_result = render_main_page_result; + + process_query = function(){ + if (prev_text.length === 0 && showSortByRelevance){ + if (sortMethod === 'activity-desc'){ + prevSortMethod = sortMethod; + sortMethod = 'relevance-desc'; + } + } + send_query(cur_text, sortMethod); + }; + restart_query = function() { + reset_sort_method(); + refresh_x_button(); + reset_query(sortMethod); + running = true; + }; + + activate_search_tags(); + main_page_search_listen(); + } else { + query = $('input#id_title.questionTitleInput'); + search_url = askbot['urls']['api_get_questions']; + render_result = render_ask_page_result; + process_query = function(){ + send_query(cur_text); + }; + restart_query = function(){ + $('#' + q_list_sel).css('height',0).children().remove(); + running = false; + prev_text = ''; + //ask_page_search_listen(); + }; + ask_page_search_listen(); + } + prev_text = $.trim(query.val()); + running = false; + } + }; + +}; |