From 220876c750c8d3aac1e2c40937c119ebcfac6925 Mon Sep 17 00:00:00 2001 From: Gabriela Navarro Date: Tue, 2 Dec 2014 11:24:14 -0200 Subject: [PATCH] Add categories filter on search software info page --- controllers/mpog_software_plugin_controller.rb | 38 +++++++++++++++++++++++++------------- lib/mpog_software_plugin.rb | 2 +- public/software-catalog.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ views/search/_catalog_filter.html.erb | 33 +++++++++++++++++++++++++++++++++ views/search/software_infos.html.erb | 2 ++ 5 files changed, 136 insertions(+), 14 deletions(-) create mode 100644 public/software-catalog.js create mode 100644 views/search/_catalog_filter.html.erb diff --git a/controllers/mpog_software_plugin_controller.rb b/controllers/mpog_software_plugin_controller.rb index 4722753..5716475 100644 --- a/controllers/mpog_software_plugin_controller.rb +++ b/controllers/mpog_software_plugin_controller.rb @@ -15,18 +15,6 @@ class MpogSoftwarePluginController < ApplicationController end end - def get_institutions - list = [] - - if request.xhr? and params[:query] - list = Institution.search_institution(params[:query]).map{ |institution| - {:value=>institution.name, :id=>institution.id} - } - end - - render :json => list.to_json - end - def hide_registration_incomplete_percentage response = false @@ -151,6 +139,30 @@ class MpogSoftwarePluginController < ApplicationController end end + def get_institutions + list = [] + + if request.xhr? and params[:query] + list = Institution.search_institution(params[:query]).map{ |institution| + {:value=>institution.name, :id=>institution.id} + } + end + + render :json => list.to_json + end + + def get_categories + list = [] + + if request.xhr? and params[:query] + Category.where("name ILIKE ?", "%#{params[:query]}%").collect { |c| + list << {:label=>c.name, :id=>c.id} if c.name != "Software" + } + end + + render :json => list.to_json + end + def get_brazil_states redirect_to "/" unless request.xhr? @@ -227,7 +239,7 @@ class MpogSoftwarePluginController < ApplicationController software_list.each do |software| software[:name] = Community.find(software.community_id).name software[:languages_list] = [] - + software.software_languages.each do |sl| software[:languages_list] << {} index = software[:languages_list].count - 1 diff --git a/lib/mpog_software_plugin.rb b/lib/mpog_software_plugin.rb index dce2559..77378ac 100644 --- a/lib/mpog_software_plugin.rb +++ b/lib/mpog_software_plugin.rb @@ -140,7 +140,7 @@ class MpogSoftwarePlugin < Noosfero::Plugin end def js_files - ["spb-utils.js", "mpog-software.js", "mpog-software-validations.js", "mpog-user-validations.js", "mpog-institution-validations.js", "mpog-incomplete-registration.js", "mpog-search.js", "jquery.maskedinput.min.js"] + ["spb-utils.js", "mpog-software.js", "mpog-software-validations.js", "mpog-user-validations.js", "mpog-institution-validations.js", "mpog-incomplete-registration.js", "mpog-search.js", "jquery.maskedinput.min.js", "software-catalog.js"] end def add_new_organization_buttons diff --git a/public/software-catalog.js b/public/software-catalog.js new file mode 100644 index 0000000..77ef251 --- /dev/null +++ b/public/software-catalog.js @@ -0,0 +1,75 @@ +(function(){ + "use strict"; + var AJAX_URL = { + get_categories: + url_with_subdirectory("/plugin/mpog_software/get_categories") + }; + + function create_catalog_element(html_list, value, id) { + var li_tag = document.createElement("li"); + var first = html_list.length == 0; + + if( first ) + li_tag.innerHTML = value + " x"; + else + li_tag.innerHTML = ", " + value + " x"; + + return li_tag; + } + + function add_item_to_catalog(value, id) { + var already_has = false; + + jQuery("#catalog-list ul li").each(function(i, li){ + var regex = new RegExp(value, "g"); + + if( regex.test(li.innerHTML) ) { + already_has = true; + } + }); + + if( !already_has ) { + jQuery("#catalog-list ul").append(create_catalog_element(jQuery("#catalog-list ul li"), value, id)); + } + } + + function remote_catalog_item() { + jQuery(this).parent().remove(); + } + + function set_autocomplate() { + jQuery("#software-catalog").autocomplete({ + source : function(request, response){ + jQuery.ajax({ + type: "GET", + url: AJAX_URL.get_categories, + data: {query: request.term}, + success: function(result){ + response(result); + } + }) + }, + + select : function (event, selected) { + var value = selected.item.value; + var id = selected.item.id; + + this.value = ""; + + add_item_to_catalog(value, id); + set_events(); + + return false; + } + }); + } + + function set_events() { + jQuery(".catalog-remove-item").click(remote_catalog_item); + } + + jQuery(document).ready(function(){ + set_autocomplate(); + set_events(); + }); +})(); diff --git a/views/search/_catalog_filter.html.erb b/views/search/_catalog_filter.html.erb new file mode 100644 index 0000000..879cb39 --- /dev/null +++ b/views/search/_catalog_filter.html.erb @@ -0,0 +1,33 @@ + + +
+
+
    +
    + +
    + <%= text_field(:software, :catalog, :id=>"software-catalog") %> +
    +
    \ No newline at end of file diff --git a/views/search/software_infos.html.erb b/views/search/software_infos.html.erb index 1ddf80d..dbc8ffb 100644 --- a/views/search/software_infos.html.erb +++ b/views/search/software_infos.html.erb @@ -2,6 +2,8 @@ <%= render :partial => 'search_form', :locals => { :hint => _("Type words about the %s you're looking for") % @asset.to_s.singularize } %> +<%= render :partial => "catalog_filter" %> + <%= render :partial => 'results_header' %> <%= display_results(@searches, @asset) %> -- libgit2 0.21.2