Commit 18427e7c32b31a9caae8010e4d2270008666302f
Committed by
Gabriela Navarro
1 parent
da4e5385
Exists in
master
and in
5 other branches
Add search ajax
Signed-off-by: Fabio Teixeira <fabio1079@gmail.com> Signed-off-by: Hebert Douglas <hebertdougl@gmail.com> Signed-off-by: Luciano Prestes <lucianopcbr@gmail.com>
Showing
3 changed files
with
124 additions
and
69 deletions
Show diff stats
lib/ext/search_controller.rb
| ... | ... | @@ -22,32 +22,7 @@ class SearchController |
| 22 | 22 | |
| 23 | 23 | |
| 24 | 24 | def software_infos |
| 25 | - @titles[:software_infos] = _("Public Software Catalog") | |
| 26 | - @category_filters = [] | |
| 27 | - @categories = Category.all | |
| 28 | - @selected_categories = params[:selected_categories] | |
| 29 | - | |
| 30 | - @selected_categories_name = [] | |
| 31 | - @message_selected_options = "Most options" | |
| 32 | - unless @selected_categories.nil? | |
| 33 | - @message_selected_options = _("Selected options: ") | |
| 34 | - @selected_categories.each do |k, v| | |
| 35 | - @message_selected_options << "#{k}; " | |
| 36 | - @selected_categories_name << k | |
| 37 | - end | |
| 38 | - end | |
| 39 | - | |
| 40 | - @categories.sort!{|a, b| a.name <=> b.name} | |
| 41 | - @categories_groupe_one = [] | |
| 42 | - @categories_groupe_two = [] | |
| 43 | - (0..(@categories.count - 1)).each do |i| | |
| 44 | - if i % 2 == 0 | |
| 45 | - @categories_groupe_one << @categories[i] | |
| 46 | - else | |
| 47 | - @categories_groupe_two << @categories[i] | |
| 48 | - end | |
| 49 | - end | |
| 50 | - | |
| 25 | + prepare_search_page | |
| 51 | 26 | results = filter_software_infos_list |
| 52 | 27 | results = results.paginate(:per_page => 24, :page => params[:page]) |
| 53 | 28 | @searches[@asset] = {:results => results} |
| ... | ... | @@ -96,4 +71,53 @@ class SearchController |
| 96 | 71 | |
| 97 | 72 | filtered_community_list |
| 98 | 73 | end |
| 74 | + | |
| 75 | + def get_search_result | |
| 76 | + redirect_to "/" unless request.xhr? | |
| 77 | + | |
| 78 | + selected_categories = {} | |
| 79 | + params[:categories_ids].each do |id| | |
| 80 | + selected_categories[Category.find(id).name] = id | |
| 81 | + end | |
| 82 | + params[:selected_categories] = selected_categories | |
| 83 | + | |
| 84 | + prepare_search_page | |
| 85 | + | |
| 86 | + results = filter_software_infos_list | |
| 87 | + results = results.paginate(:per_page => 24, :page => params[:page]) | |
| 88 | + | |
| 89 | + @searches[@asset] = {:results => results} | |
| 90 | + @search = results | |
| 91 | + render "/software_infos", :layout=>false | |
| 92 | + end | |
| 93 | + | |
| 94 | + protected | |
| 95 | + | |
| 96 | + def prepare_search_page | |
| 97 | + @titles[:software_infos] = _("Public Software Catalog") | |
| 98 | + @category_filters = [] | |
| 99 | + @categories = Category.all | |
| 100 | + @selected_categories = params[:selected_categories] | |
| 101 | + | |
| 102 | + @selected_categories_name = [] | |
| 103 | + @message_selected_options = "Most options" | |
| 104 | + unless @selected_categories.nil? | |
| 105 | + @message_selected_options = _("Selected options: ") | |
| 106 | + @selected_categories.each do |k, v| | |
| 107 | + @message_selected_options << "#{k}; " | |
| 108 | + @selected_categories_name << k | |
| 109 | + end | |
| 110 | + end | |
| 111 | + | |
| 112 | + @categories.sort!{|a, b| a.name <=> b.name} | |
| 113 | + @categories_groupe_one = [] | |
| 114 | + @categories_groupe_two = [] | |
| 115 | + (0..(@categories.count - 1)).each do |i| | |
| 116 | + if i % 2 == 0 | |
| 117 | + @categories_groupe_one << @categories[i] | |
| 118 | + else | |
| 119 | + @categories_groupe_two << @categories[i] | |
| 120 | + end | |
| 121 | + end | |
| 122 | + end | |
| 99 | 123 | end | ... | ... |
public/software-catalog.js
| 1 | -(function(){ | |
| 1 | +(function($){ | |
| 2 | 2 | "use strict"; |
| 3 | 3 | |
| 4 | 4 | var AJAX_URL = { |
| 5 | 5 | get_categories: |
| 6 | - url_with_subdirectory("/plugin/mpog_software/get_categories") | |
| 6 | + url_with_subdirectory("/plugin/mpog_software/get_categories"), | |
| 7 | + get_search_result: | |
| 8 | + url_with_subdirectory("/search/get_search_result") | |
| 7 | 9 | }; |
| 8 | 10 | |
| 9 | 11 | function create_catalog_element(first, value, id) { |
| ... | ... | @@ -18,7 +20,7 @@ |
| 18 | 20 | function add_item_to_catalog(value, id) { |
| 19 | 21 | var already_has = false; |
| 20 | 22 | |
| 21 | - jQuery("#catalog-list ul li").each(function(i, li){ | |
| 23 | + $("#catalog-list ul li").each(function(i, li){ | |
| 22 | 24 | var regex = new RegExp(value, "g"); |
| 23 | 25 | |
| 24 | 26 | if( regex.test(li.innerHTML) ) { |
| ... | ... | @@ -27,21 +29,21 @@ |
| 27 | 29 | }); |
| 28 | 30 | |
| 29 | 31 | if( !already_has ) { |
| 30 | - var catalog_list = jQuery("#catalog-list ul li"); | |
| 31 | - var current_ids = jQuery("#filter").val(); | |
| 32 | + var catalog_list = $("#catalog-list ul li"); | |
| 33 | + var current_ids = $("#filter").val(); | |
| 32 | 34 | var first = catalog_list.length == 0; |
| 33 | 35 | |
| 34 | 36 | current_ids += first ? id : ","+id; |
| 35 | 37 | |
| 36 | - jQuery("#filter").val(current_ids); | |
| 38 | + $("#filter").val(current_ids); | |
| 37 | 39 | |
| 38 | - jQuery("#catalog-list ul").append(create_catalog_element(first, value, id)); | |
| 40 | + $("#catalog-list ul").append(create_catalog_element(first, value, id)); | |
| 39 | 41 | } |
| 40 | 42 | } |
| 41 | 43 | |
| 42 | 44 | function remote_catalog_item() { |
| 43 | 45 | var current_id = this.getAttribute("data-id"); |
| 44 | - var filter_ids = jQuery("#filter").val(); | |
| 46 | + var filter_ids = $("#filter").val(); | |
| 45 | 47 | var id_list = []; |
| 46 | 48 | |
| 47 | 49 | filter_ids.split(",").forEach(function(id){ |
| ... | ... | @@ -50,60 +52,89 @@ |
| 50 | 52 | } |
| 51 | 53 | }); |
| 52 | 54 | |
| 53 | - jQuery("#filter").val(id_list.join(",")); | |
| 55 | + $("#filter").val(id_list.join(",")); | |
| 54 | 56 | |
| 55 | - jQuery(this).parent().remove(); | |
| 57 | + $(this).parent().remove(); | |
| 56 | 58 | } |
| 57 | 59 | |
| 58 | 60 | function show_head_message() { |
| 59 | - if (jQuery("#filter-categories-select-catalog").text().blank()){ | |
| 60 | - jQuery("#filter-categories-select-catalog").hide(); | |
| 61 | - jQuery("#filter-option-catalog-software").show(); | |
| 61 | + if ($("#filter-categories-select-catalog").text().blank()){ | |
| 62 | + $("#filter-categories-select-catalog").hide(); | |
| 63 | + $("#filter-option-catalog-software").show(); | |
| 62 | 64 | }else{ |
| 63 | - jQuery("#filter-categories-select-catalog").show(); | |
| 64 | - jQuery("#filter-option-catalog-software").hide(); | |
| 65 | + $("#filter-categories-select-catalog").show(); | |
| 66 | + $("#filter-option-catalog-software").hide(); | |
| 65 | 67 | } |
| 66 | 68 | } |
| 67 | 69 | |
| 68 | 70 | function slideDowsCategoriesOptionAndHideOptionCatalog() { |
| 69 | - jQuery("#filter-categories-option").slideDown(); | |
| 70 | - jQuery("#filter-option-catalog-software").hide(); | |
| 71 | + $("#filter-categories-option").slideDown(); | |
| 72 | + $("#filter-option-catalog-software").hide(); | |
| 71 | 73 | } |
| 72 | 74 | |
| 73 | 75 | function slideDownCategoriesOptionAndHideCategoriesSelect() { |
| 74 | - jQuery("#filter-categories-option").slideDown(); | |
| 75 | - jQuery("#filter-categories-select-catalog").hide(); | |
| 76 | + $("#filter-categories-option").slideDown(); | |
| 77 | + $("#filter-categories-select-catalog").hide(); | |
| 76 | 78 | } |
| 77 | 79 | |
| 78 | 80 | function slideUpCategoriesAndShowHeadMessage() { |
| 79 | - jQuery("#filter-categories-option").slideUp(); | |
| 81 | + $("#filter-categories-option").slideUp(); | |
| 80 | 82 | show_head_message(); |
| 81 | 83 | } |
| 82 | 84 | |
| 83 | 85 | function clearCatalogCheckbox(){ |
| 84 | - jQuery("#filter-categories-option").slideUp(); | |
| 85 | - jQuery("#filter-option-catalog-software").show(); | |
| 86 | - jQuery("#group-categories input:checked").each(function() { | |
| 87 | - jQuery(this).prop('checked', false); | |
| 86 | + $("#filter-categories-option").slideUp(); | |
| 87 | + $("#filter-option-catalog-software").show(); | |
| 88 | + $("#group-categories input:checked").each(function() { | |
| 89 | + $(this).prop('checked', false); | |
| 88 | 90 | }); |
| 89 | 91 | } |
| 90 | 92 | |
| 93 | + function dispatch_search_ajax(callback) { | |
| 94 | + var query_text = $("#search-input").val(); | |
| 95 | + var selected_categories_ids = []; | |
| 96 | + | |
| 97 | + $(".categories-catalog:checked").each(function(index, element) { | |
| 98 | + selected_categories_ids.push(element.value); | |
| 99 | + }); | |
| 100 | + | |
| 101 | + | |
| 102 | + $.ajax({ | |
| 103 | + url: AJAX_URL.get_search_result, | |
| 104 | + type: "GET", | |
| 105 | + data: { | |
| 106 | + query: query_text, | |
| 107 | + categories_ids: selected_categories_ids | |
| 108 | + }, | |
| 109 | + success: function(response) { | |
| 110 | + console.log(response); | |
| 111 | + callback(response); | |
| 112 | + } | |
| 113 | + }); | |
| 114 | + } | |
| 115 | + | |
| 116 | + function update_page_list() { | |
| 117 | + | |
| 118 | + } | |
| 119 | + | |
| 91 | 120 | function selectCheckboxCategory() { |
| 92 | - jQuery("#filter-categories-option").slideUp(); | |
| 93 | - jQuery("#filter-categories-select-catalog").show(); | |
| 94 | - jQuery("#filter-option-catalog-software").hide(); | |
| 121 | + $("#filter-categories-option").slideUp(); | |
| 122 | + $("#filter-categories-select-catalog").show(); | |
| 123 | + $("#filter-option-catalog-software").hide(); | |
| 124 | + | |
| 125 | + dispatch_search_ajax(update_page_list); | |
| 95 | 126 | } |
| 96 | 127 | |
| 97 | 128 | function selectProjectSoftwareCheckbox() { |
| 98 | - jQuery("#filter-categories-option").slideUp(); | |
| 99 | - jQuery("#filter-categories-select-catalog").show(); | |
| 100 | - jQuery("#filter-option-catalog-software").hide(); | |
| 129 | + $("#filter-categories-option").slideUp(); | |
| 130 | + $("#filter-categories-select-catalog").show(); | |
| 131 | + $("#filter-option-catalog-software").hide(); | |
| 101 | 132 | } |
| 102 | 133 | |
| 103 | 134 | function set_autocomplate() { |
| 104 | - jQuery("#software-catalog").autocomplete({ | |
| 135 | + $("#software-catalog").autocomplete({ | |
| 105 | 136 | source : function(request, response){ |
| 106 | - jQuery.ajax({ | |
| 137 | + $.ajax({ | |
| 107 | 138 | type: "GET", |
| 108 | 139 | url: AJAX_URL.get_categories, |
| 109 | 140 | data: {query: request.term}, |
| ... | ... | @@ -128,21 +159,21 @@ |
| 128 | 159 | } |
| 129 | 160 | |
| 130 | 161 | function set_events() { |
| 131 | - jQuery(".catalog-remove-item").click(remote_catalog_item); | |
| 132 | - jQuery("#filter-option-catalog-software").click(slideDowsCategoriesOptionAndHideOptionCatalog); | |
| 133 | - jQuery("#filter-categories-select-catalog").click(slideDownCategoriesOptionAndHideCategoriesSelect); | |
| 134 | - jQuery("#close-filter-catalog").click(slideUpCategoriesAndShowHeadMessage); | |
| 135 | - jQuery("#cleanup-filter-catalg").click(clearCatalogCheckbox); | |
| 136 | - jQuery(".categories-catalog").click(selectCheckboxCategory); | |
| 137 | - jQuery(".project-software").click(selectProjectSoftwareCheckbox); | |
| 162 | + $(".catalog-remove-item").click(remote_catalog_item); | |
| 163 | + $("#filter-option-catalog-software").click(slideDowsCategoriesOptionAndHideOptionCatalog); | |
| 164 | + $("#filter-categories-select-catalog").click(slideDownCategoriesOptionAndHideCategoriesSelect); | |
| 165 | + $("#close-filter-catalog").click(slideUpCategoriesAndShowHeadMessage); | |
| 166 | + $("#cleanup-filter-catalg").click(clearCatalogCheckbox); | |
| 167 | + $(".categories-catalog").click(selectCheckboxCategory); | |
| 168 | + $(".project-software").click(selectProjectSoftwareCheckbox); | |
| 138 | 169 | } |
| 139 | 170 | |
| 140 | 171 | |
| 141 | - jQuery(document).ready(function(){ | |
| 172 | + $(document).ready(function(){ | |
| 142 | 173 | set_autocomplate(); |
| 143 | 174 | set_events(); |
| 144 | 175 | show_head_message(); |
| 145 | - jQuery("#filter-categories-option").hide(); | |
| 176 | + $("#filter-categories-option").hide(); | |
| 146 | 177 | }); |
| 147 | 178 | |
| 148 | -})(); | |
| 179 | +})(jQuery); | ... | ... |
views/search/_catalog_filter.html.erb
| ... | ... | @@ -15,7 +15,7 @@ |
| 15 | 15 | <% @categories_groupe_one.each do |category| %> |
| 16 | 16 | <label> |
| 17 | 17 | <%= check_box_tag("selected_categories[#{category.name}]", category.id, @selected_categories_name.include?(category.name), :class => "categories-catalog") %> |
| 18 | - <%= _("#{category.name}") %> | |
| 18 | + <span><%= _("#{category.name}") %></span> | |
| 19 | 19 | </label> <br> |
| 20 | 20 | <% end %> |
| 21 | 21 | </div> | ... | ... |