Commit 344efc3cb162438e0c9efa263caa9140d43f7350
1 parent
810d0938
Exists in
master
and in
5 other branches
Execute software catalog search javascript only when needed
Showing
4 changed files
with
234 additions
and
221 deletions
Show diff stats
lib/software_communities_plugin.rb
| ... | ... | @@ -133,9 +133,9 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin |
| 133 | 133 | views/user-edit-profile.js |
| 134 | 134 | views/create-institution.js |
| 135 | 135 | views/complete-registration.js |
| 136 | + views/search-software-catalog.js | |
| 136 | 137 | initializer.js |
| 137 | 138 | app.js |
| 138 | - software-catalog.js | |
| 139 | 139 | mpog-software-block.js |
| 140 | 140 | mpog-download-block.js |
| 141 | 141 | ) | ... | ... |
public/initializer.js
public/software-catalog.js
| ... | ... | @@ -1,219 +0,0 @@ |
| 1 | -(function($){ | |
| 2 | - "use strict"; | |
| 3 | - | |
| 4 | - var AJAX_URL = { | |
| 5 | - software_infos: | |
| 6 | - url_with_subdirectory("/search/software_infos") | |
| 7 | - }; | |
| 8 | - | |
| 9 | - | |
| 10 | - function show_head_message() { | |
| 11 | - if ($("#filter-categories-select-catalog").text()){ | |
| 12 | - $("#filter-categories-select-catalog").hide(); | |
| 13 | - $("#filter-option-catalog-software").show(); | |
| 14 | - }else{ | |
| 15 | - $("#filter-categories-select-catalog").show(); | |
| 16 | - $("#filter-option-catalog-software").hide(); | |
| 17 | - } | |
| 18 | - } | |
| 19 | - | |
| 20 | - | |
| 21 | - function slideDowsCategoriesOptionAndHideOptionCatalog() { | |
| 22 | - $("#filter-categories-option").slideDown(); | |
| 23 | - $("#filter-option-catalog-software").hide(); | |
| 24 | - } | |
| 25 | - | |
| 26 | - | |
| 27 | - function slideDownCategoriesOptionAndHideCategoriesSelect() { | |
| 28 | - $("#filter-categories-option").slideDown(); | |
| 29 | - $("#filter-categories-select-catalog").hide(); | |
| 30 | - } | |
| 31 | - | |
| 32 | - | |
| 33 | - function slideUpCategoriesAndShowHeadMessage() { | |
| 34 | - $("#filter-categories-option").slideUp(); | |
| 35 | - show_head_message(); | |
| 36 | - } | |
| 37 | - | |
| 38 | - | |
| 39 | - function clearCatalogCheckbox(){ | |
| 40 | - $("#filter-categories-option").slideUp(); | |
| 41 | - $("#filter-option-catalog-software").show(); | |
| 42 | - $("#group-categories input:checked").each(function() { | |
| 43 | - $(this).prop('checked', false); | |
| 44 | - }); | |
| 45 | - | |
| 46 | - dispatch_search_ajax(update_search_page_on_ajax, true); | |
| 47 | - } | |
| 48 | - | |
| 49 | - | |
| 50 | - function selectCheckboxCategory() { | |
| 51 | - $("#filter-categories-option").slideUp(); | |
| 52 | - $("#filter-categories-select-catalog").show(); | |
| 53 | - $("#filter-option-catalog-software").hide(); | |
| 54 | - | |
| 55 | - dispatch_search_ajax(update_search_page_on_ajax, true); | |
| 56 | - } | |
| 57 | - | |
| 58 | - | |
| 59 | - function dispatch_search_ajax(callback, enable_load) { | |
| 60 | - var search_params = get_search_params(); | |
| 61 | - | |
| 62 | - if(enable_load) { | |
| 63 | - open_loading("Loading"); | |
| 64 | - } | |
| 65 | - | |
| 66 | - $.ajax({ | |
| 67 | - url: AJAX_URL.software_infos, | |
| 68 | - type: "GET", | |
| 69 | - data: search_params, | |
| 70 | - success: callback, | |
| 71 | - error: function(){ | |
| 72 | - close_loading(); | |
| 73 | - } | |
| 74 | - }); | |
| 75 | - } | |
| 76 | - | |
| 77 | - | |
| 78 | - function get_search_params() { | |
| 79 | - var params = {}; | |
| 80 | - | |
| 81 | - params.query = $("#search-input").val(); | |
| 82 | - params.selected_categories = []; | |
| 83 | - | |
| 84 | - $(".categories-catalog:checked").each(function(index, element) { | |
| 85 | - params.selected_categories.push(element.value); | |
| 86 | - }); | |
| 87 | - | |
| 88 | - params.software_display = $("#software_display").val(); | |
| 89 | - params.sort = $("#sort").val(); | |
| 90 | - | |
| 91 | - params.include_non_public = $("#include_non_public").is(":checked"); | |
| 92 | - | |
| 93 | - return params; | |
| 94 | - } | |
| 95 | - | |
| 96 | - function get_result_div_core(message){ | |
| 97 | - var div_result = $(".search-results-type-empty"); | |
| 98 | - var html = '<div class="search-results-innerbox search-results-type-empty"> <div>'+message+' </div></div>' | |
| 99 | - | |
| 100 | - div_result.replaceWith('<div class="search-results-innerbox search-results-type-empty"> <div>Nenhum software encontrado</div> '+message+'</div>') | |
| 101 | - } | |
| 102 | - | |
| 103 | - function catalog_message(){ | |
| 104 | - var result_list = $("#search-results").find('.search-results-empty').html(); | |
| 105 | - var selected_categories_field = $("#filter-categories-select-catalog"); | |
| 106 | - | |
| 107 | - if(result_list.length > 1 && selected_categories_field.html().length < 1){ | |
| 108 | - get_result_div_core("Tente filtros mais abrangentes"); | |
| 109 | - }else if (result_list.length > 1 && selected_categories_field.html().length >= 1) { | |
| 110 | - get_result_div_core("Tente filtros mais abrangentes ou confira os <mudar>softwares das categorias individualmente</mudar>"); | |
| 111 | - } | |
| 112 | - } | |
| 113 | - | |
| 114 | - function update_search_page_on_ajax(response) { | |
| 115 | - response = $(response); | |
| 116 | - var search_list = $("#search-results"); | |
| 117 | - var selected_categories_field = $("#filter-categories-select-catalog"); | |
| 118 | - var pagination = $("#software-pagination"); | |
| 119 | - var software_count = $("#software-count"); | |
| 120 | - | |
| 121 | - var result_list = response.find("#search-results").html(); | |
| 122 | - var result_categories = response.find("#filter-categories-select-catalog").html(); | |
| 123 | - var result_pagination = response.find("#software-pagination").html(); | |
| 124 | - var result_software_count = response.find("#software-count").html(); | |
| 125 | - | |
| 126 | - catalog_message() | |
| 127 | - | |
| 128 | - search_list.html(result_list); | |
| 129 | - selected_categories_field.html(result_categories); | |
| 130 | - pagination.html(result_pagination); | |
| 131 | - software_count.html(result_software_count); | |
| 132 | - show_head_message(); | |
| 133 | - highlight_searched_terms(); | |
| 134 | - | |
| 135 | - hide_load_after_ajax(); | |
| 136 | - } | |
| 137 | - | |
| 138 | - | |
| 139 | - function hide_load_after_ajax() { | |
| 140 | - if ($("#overlay_loading_modal").is(":visible")) { | |
| 141 | - close_loading(); | |
| 142 | - setTimeout(hide_load_after_ajax, 1000); | |
| 143 | - } | |
| 144 | - } | |
| 145 | - | |
| 146 | - | |
| 147 | - function highlight_searched_terms() { | |
| 148 | - var searched_terms = $("#search-input").val(); | |
| 149 | - | |
| 150 | - if( searched_terms.length === 0 ) { | |
| 151 | - return undefined; | |
| 152 | - } | |
| 153 | - | |
| 154 | - var content_result = $(".search-content-result"); | |
| 155 | - var regex = new RegExp("("+searched_terms.replace(/\s/g, "|")+")", "gi"); | |
| 156 | - | |
| 157 | - content_result.each(function(i, e){ | |
| 158 | - var element = $(e); | |
| 159 | - | |
| 160 | - var new_text = element.text().replace(regex, function(text) { | |
| 161 | - return "<strong>"+text+"</strong>"; | |
| 162 | - }); | |
| 163 | - | |
| 164 | - element.html(new_text); | |
| 165 | - }); | |
| 166 | - } | |
| 167 | - | |
| 168 | - | |
| 169 | - function selectProjectSoftwareCheckbox() { | |
| 170 | - $("#filter-categories-option").slideUp(); | |
| 171 | - $("#filter-categories-select-catalog").show(); | |
| 172 | - $("#filter-option-catalog-software").hide(); | |
| 173 | - | |
| 174 | - dispatch_search_ajax(update_search_page_on_ajax, true); | |
| 175 | - show_head_message(); | |
| 176 | - } | |
| 177 | - | |
| 178 | - function update_page_by_ajax_on_select_change() { | |
| 179 | - dispatch_search_ajax(update_search_page_on_ajax, true); | |
| 180 | - } | |
| 181 | - | |
| 182 | - function update_page_by_text_filter() { | |
| 183 | - var text = this.value; | |
| 184 | - dispatch_search_ajax(update_search_page_on_ajax, false); | |
| 185 | - } | |
| 186 | - | |
| 187 | - function search_input_keyup() { | |
| 188 | - var timer = null; | |
| 189 | - | |
| 190 | - $("#search-input").keyup(function() { | |
| 191 | - timer = setTimeout(update_page_by_text_filter, 400); | |
| 192 | - }).keydown(function() { | |
| 193 | - clearTimeout(timer); | |
| 194 | - }); | |
| 195 | - } | |
| 196 | - | |
| 197 | - function set_events() { | |
| 198 | - $("#filter-option-catalog-software").click(slideDowsCategoriesOptionAndHideOptionCatalog); | |
| 199 | - $("#filter-categories-select-catalog").click(slideDownCategoriesOptionAndHideCategoriesSelect); | |
| 200 | - $("#close-filter-catalog").click(slideUpCategoriesAndShowHeadMessage); | |
| 201 | - $("#cleanup-filter-catalg").click(clearCatalogCheckbox); | |
| 202 | - $(".categories-catalog").click(selectCheckboxCategory); | |
| 203 | - $(".project-software").click(selectProjectSoftwareCheckbox); | |
| 204 | - $("#software_display").change(update_page_by_ajax_on_select_change); | |
| 205 | - $("#sort").change(update_page_by_ajax_on_select_change); | |
| 206 | - | |
| 207 | - search_input_keyup(); | |
| 208 | - } | |
| 209 | - | |
| 210 | - $(document).ready(function(){ | |
| 211 | - set_events(); | |
| 212 | - catalog_message(); | |
| 213 | - show_head_message(); | |
| 214 | - | |
| 215 | - | |
| 216 | - | |
| 217 | - $("#filter-categories-option").hide(); | |
| 218 | - }); | |
| 219 | -})(jQuery); |
| ... | ... | @@ -0,0 +1,231 @@ |
| 1 | +modulejs.define('SearchSoftwareCatalog', ['jquery', 'NoosferoRoot'], function($, NoosferoRoot) { | |
| 2 | + 'use strict'; | |
| 3 | + | |
| 4 | + var AJAX_URL = { | |
| 5 | + software_infos: | |
| 6 | + NoosferoRoot.urlWithSubDirectory("/search/software_infos") | |
| 7 | + }; | |
| 8 | + | |
| 9 | + | |
| 10 | + function show_head_message() { | |
| 11 | + if ($("#filter-categories-select-catalog").text()){ | |
| 12 | + $("#filter-categories-select-catalog").hide(); | |
| 13 | + $("#filter-option-catalog-software").show(); | |
| 14 | + }else{ | |
| 15 | + $("#filter-categories-select-catalog").show(); | |
| 16 | + $("#filter-option-catalog-software").hide(); | |
| 17 | + } | |
| 18 | + } | |
| 19 | + | |
| 20 | + | |
| 21 | + function slideDowsCategoriesOptionAndHideOptionCatalog() { | |
| 22 | + $("#filter-categories-option").slideDown(); | |
| 23 | + $("#filter-option-catalog-software").hide(); | |
| 24 | + } | |
| 25 | + | |
| 26 | + | |
| 27 | + function slideDownCategoriesOptionAndHideCategoriesSelect() { | |
| 28 | + $("#filter-categories-option").slideDown(); | |
| 29 | + $("#filter-categories-select-catalog").hide(); | |
| 30 | + } | |
| 31 | + | |
| 32 | + | |
| 33 | + function slideUpCategoriesAndShowHeadMessage() { | |
| 34 | + $("#filter-categories-option").slideUp(); | |
| 35 | + show_head_message(); | |
| 36 | + } | |
| 37 | + | |
| 38 | + | |
| 39 | + function clearCatalogCheckbox(){ | |
| 40 | + $("#filter-categories-option").slideUp(); | |
| 41 | + $("#filter-option-catalog-software").show(); | |
| 42 | + $("#group-categories input:checked").each(function() { | |
| 43 | + $(this).prop('checked', false); | |
| 44 | + }); | |
| 45 | + | |
| 46 | + dispatch_search_ajax(update_search_page_on_ajax, true); | |
| 47 | + } | |
| 48 | + | |
| 49 | + | |
| 50 | + function selectCheckboxCategory() { | |
| 51 | + $("#filter-categories-option").slideUp(); | |
| 52 | + $("#filter-categories-select-catalog").show(); | |
| 53 | + $("#filter-option-catalog-software").hide(); | |
| 54 | + | |
| 55 | + dispatch_search_ajax(update_search_page_on_ajax, true); | |
| 56 | + } | |
| 57 | + | |
| 58 | + | |
| 59 | + function dispatch_search_ajax(callback, enable_load) { | |
| 60 | + var search_params = get_search_params(); | |
| 61 | + | |
| 62 | + if(enable_load) { | |
| 63 | + open_loading("Loading"); | |
| 64 | + } | |
| 65 | + | |
| 66 | + $.ajax({ | |
| 67 | + url: AJAX_URL.software_infos, | |
| 68 | + type: "GET", | |
| 69 | + data: search_params, | |
| 70 | + success: callback, | |
| 71 | + error: function(){ | |
| 72 | + close_loading(); | |
| 73 | + } | |
| 74 | + }); | |
| 75 | + } | |
| 76 | + | |
| 77 | + | |
| 78 | + function get_search_params() { | |
| 79 | + var params = {}; | |
| 80 | + | |
| 81 | + params.query = $("#search-input").val(); | |
| 82 | + params.selected_categories = []; | |
| 83 | + | |
| 84 | + $(".categories-catalog:checked").each(function(index, element) { | |
| 85 | + params.selected_categories.push(element.value); | |
| 86 | + }); | |
| 87 | + | |
| 88 | + params.software_display = $("#software_display").val(); | |
| 89 | + params.sort = $("#sort").val(); | |
| 90 | + | |
| 91 | + params.include_non_public = $("#include_non_public").is(":checked"); | |
| 92 | + | |
| 93 | + return params; | |
| 94 | + } | |
| 95 | + | |
| 96 | + | |
| 97 | + function get_result_div_core(message){ | |
| 98 | + var div_result = $(".search-results-type-empty"); | |
| 99 | + var html = '<div class="search-results-innerbox search-results-type-empty"> <div>'+message+' </div></div>' | |
| 100 | + | |
| 101 | + div_result.replaceWith('<div class="search-results-innerbox search-results-type-empty"> <div>Nenhum software encontrado</div> '+message+'</div>') | |
| 102 | + } | |
| 103 | + | |
| 104 | + | |
| 105 | + function catalog_message(){ | |
| 106 | + var result_list = $("#search-results").find('.search-results-empty'); | |
| 107 | + var selected_categories_field = $("#filter-categories-select-catalog"); | |
| 108 | + | |
| 109 | + if(result_list.length > 1 && selected_categories_field.html().length < 1){ | |
| 110 | + get_result_div_core("Tente filtros mais abrangentes"); | |
| 111 | + }else if (result_list.length > 1 && selected_categories_field.html().length >= 1) { | |
| 112 | + get_result_div_core("Tente filtros mais abrangentes ou confira os softwares das categorias individualmente"); | |
| 113 | + } | |
| 114 | + } | |
| 115 | + | |
| 116 | + | |
| 117 | + function update_search_page_on_ajax(response) { | |
| 118 | + response = $(response); | |
| 119 | + var search_list = $("#search-results"); | |
| 120 | + var selected_categories_field = $("#filter-categories-select-catalog"); | |
| 121 | + var pagination = $("#software-pagination"); | |
| 122 | + var software_count = $("#software-count"); | |
| 123 | + | |
| 124 | + var result_list = response.find("#search-results").html(); | |
| 125 | + var result_categories = response.find("#filter-categories-select-catalog").html(); | |
| 126 | + var result_pagination = response.find("#software-pagination").html(); | |
| 127 | + var result_software_count = response.find("#software-count").html(); | |
| 128 | + | |
| 129 | + catalog_message() | |
| 130 | + | |
| 131 | + search_list.html(result_list); | |
| 132 | + selected_categories_field.html(result_categories); | |
| 133 | + pagination.html(result_pagination); | |
| 134 | + software_count.html(result_software_count); | |
| 135 | + show_head_message(); | |
| 136 | + highlight_searched_terms(); | |
| 137 | + | |
| 138 | + hide_load_after_ajax(); | |
| 139 | + } | |
| 140 | + | |
| 141 | + | |
| 142 | + function hide_load_after_ajax() { | |
| 143 | + if ($("#overlay_loading_modal").is(":visible")) { | |
| 144 | + close_loading(); | |
| 145 | + setTimeout(hide_load_after_ajax, 1000); | |
| 146 | + } | |
| 147 | + } | |
| 148 | + | |
| 149 | + | |
| 150 | + function highlight_searched_terms() { | |
| 151 | + var searched_terms = $("#search-input").val(); | |
| 152 | + | |
| 153 | + if( searched_terms.length === 0 ) { | |
| 154 | + return undefined; | |
| 155 | + } | |
| 156 | + | |
| 157 | + var content_result = $(".search-content-result"); | |
| 158 | + var regex = new RegExp("("+searched_terms.replace(/\s/g, "|")+")", "gi"); | |
| 159 | + | |
| 160 | + content_result.each(function(i, e){ | |
| 161 | + var element = $(e); | |
| 162 | + | |
| 163 | + var new_text = element.text().replace(regex, function(text) { | |
| 164 | + return "<strong>"+text+"</strong>"; | |
| 165 | + }); | |
| 166 | + | |
| 167 | + element.html(new_text); | |
| 168 | + }); | |
| 169 | + } | |
| 170 | + | |
| 171 | + | |
| 172 | + function selectProjectSoftwareCheckbox() { | |
| 173 | + $("#filter-categories-option").slideUp(); | |
| 174 | + $("#filter-categories-select-catalog").show(); | |
| 175 | + $("#filter-option-catalog-software").hide(); | |
| 176 | + | |
| 177 | + dispatch_search_ajax(update_search_page_on_ajax, true); | |
| 178 | + show_head_message(); | |
| 179 | + } | |
| 180 | + | |
| 181 | + | |
| 182 | + function update_page_by_ajax_on_select_change() { | |
| 183 | + dispatch_search_ajax(update_search_page_on_ajax, true); | |
| 184 | + } | |
| 185 | + | |
| 186 | + function update_page_by_text_filter() { | |
| 187 | + var text = this.value; | |
| 188 | + dispatch_search_ajax(update_search_page_on_ajax, false); | |
| 189 | + } | |
| 190 | + | |
| 191 | + | |
| 192 | + function search_input_keyup() { | |
| 193 | + var timer = null; | |
| 194 | + | |
| 195 | + $("#search-input").keyup(function() { | |
| 196 | + timer = setTimeout(update_page_by_text_filter, 400); | |
| 197 | + }).keydown(function() { | |
| 198 | + clearTimeout(timer); | |
| 199 | + }); | |
| 200 | + } | |
| 201 | + | |
| 202 | + | |
| 203 | + function set_events() { | |
| 204 | + $("#filter-option-catalog-software").click(slideDowsCategoriesOptionAndHideOptionCatalog); | |
| 205 | + $("#filter-categories-select-catalog").click(slideDownCategoriesOptionAndHideCategoriesSelect); | |
| 206 | + $("#close-filter-catalog").click(slideUpCategoriesAndShowHeadMessage); | |
| 207 | + $("#cleanup-filter-catalg").click(clearCatalogCheckbox); | |
| 208 | + $(".categories-catalog").click(selectCheckboxCategory); | |
| 209 | + $(".project-software").click(selectProjectSoftwareCheckbox); | |
| 210 | + $("#software_display").change(update_page_by_ajax_on_select_change); | |
| 211 | + $("#sort").change(update_page_by_ajax_on_select_change); | |
| 212 | + | |
| 213 | + search_input_keyup(); | |
| 214 | + } | |
| 215 | + | |
| 216 | + | |
| 217 | + return { | |
| 218 | + isCurrentPage: function() { | |
| 219 | + return $('#software-search-container').length === 1; | |
| 220 | + }, | |
| 221 | + | |
| 222 | + | |
| 223 | + init: function() { | |
| 224 | + set_events(); | |
| 225 | + catalog_message(); | |
| 226 | + show_head_message(); | |
| 227 | + | |
| 228 | + $("#filter-categories-option").hide(); | |
| 229 | + } | |
| 230 | + } | |
| 231 | +}); | |
| 0 | 232 | \ No newline at end of file | ... | ... |