Commit 6f4ed994639d3479311f0588e22b58db43738d61

Authored by Gabriela Navarro
Committed by Luciano Prestes
1 parent 220876c7

Remove a category item from the filter list

Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
Signed-off-by: Gabriela Navarro <navarro1703@gmail.com>
public/software-catalog.js
1 (function(){ 1 (function(){
2 - "use strict";  
3 - var AJAX_URL = { 2 + "use strict";
  3 +
  4 + var AJAX_URL = {
4 get_categories: 5 get_categories:
5 url_with_subdirectory("/plugin/mpog_software/get_categories") 6 url_with_subdirectory("/plugin/mpog_software/get_categories")
6 }; 7 };
7 8
8 - function create_catalog_element(html_list, value, id) {  
9 - var li_tag = document.createElement("li");  
10 - var first = html_list.length == 0; 9 + function create_catalog_element(first, value, id) {
  10 + var li_tag = document.createElement("li");
11 11
12 - if( first )  
13 - li_tag.innerHTML = value + " <span class='catalog-remove-item'>x</span>";  
14 - else  
15 - li_tag.innerHTML = ", " + value + " <span class='catalog-remove-item'>x</span>"; 12 + if( first )
  13 + li_tag.innerHTML = value + " <span class='catalog-remove-item' data-id='"+id+"'>x</span>";
  14 + else
  15 + li_tag.innerHTML = ", " + value + " <span class='catalog-remove-item' data-id='"+id+"'>x</span>";
16 16
17 - return li_tag;  
18 - } 17 + return li_tag;
  18 + }
19 19
20 function add_item_to_catalog(value, id) { 20 function add_item_to_catalog(value, id) {
21 - var already_has = false; 21 + var already_has = false;
  22 +
  23 + jQuery("#catalog-list ul li").each(function(i, li){
  24 + var regex = new RegExp(value, "g");
  25 +
  26 + if( regex.test(li.innerHTML) ) {
  27 + already_has = true;
  28 + }
  29 + });
  30 +
  31 + if( !already_has ) {
  32 + var catalog_list = jQuery("#catalog-list ul li");
  33 + var current_ids = jQuery("#filter").val();
  34 + var first = catalog_list.length == 0;
22 35
23 - jQuery("#catalog-list ul li").each(function(i, li){  
24 - var regex = new RegExp(value, "g"); 36 + current_ids += first ? id : ","+id;
25 37
26 - if( regex.test(li.innerHTML) ) {  
27 - already_has = true;  
28 - }  
29 - }); 38 + jQuery("#filter").val(current_ids);
30 39
31 - if( !already_has ) {  
32 - jQuery("#catalog-list ul").append(create_catalog_element(jQuery("#catalog-list ul li"), value, id));  
33 - } 40 + jQuery("#catalog-list ul").append(create_catalog_element(first, value, id));
  41 + }
34 } 42 }
35 43
36 function remote_catalog_item() { 44 function remote_catalog_item() {
37 - jQuery(this).parent().remove(); 45 + var current_id = this.getAttribute("data-id");
  46 + var filter_ids = jQuery("#filter").val();
  47 + var id_list = [];
  48 +
  49 + filter_ids.split(",").forEach(function(id){
  50 + if( current_id != id ) {
  51 + id_list.push(id);
  52 + }
  53 + });
  54 +
  55 + jQuery("#filter").val(id_list.join(","));
  56 +
  57 + jQuery(this).parent().remove();
38 } 58 }
39 59
40 function set_autocomplate() { 60 function set_autocomplate() {
41 - jQuery("#software-catalog").autocomplete({ 61 + jQuery("#software-catalog").autocomplete({
42 source : function(request, response){ 62 source : function(request, response){
43 jQuery.ajax({ 63 jQuery.ajax({
44 type: "GET", 64 type: "GET",
@@ -51,25 +71,25 @@ @@ -51,25 +71,25 @@
51 }, 71 },
52 72
53 select : function (event, selected) { 73 select : function (event, selected) {
54 - var value = selected.item.value;  
55 - var id = selected.item.id; 74 + var value = selected.item.value;
  75 + var id = selected.item.id;
56 76
57 - this.value = ""; 77 + this.value = "";
58 78
59 - add_item_to_catalog(value, id);  
60 - set_events(); 79 + add_item_to_catalog(value, id);
  80 + set_events();
61 81
62 - return false; 82 + return false;
63 } 83 }
64 }); 84 });
65 } 85 }
66 86
67 - function set_events() { 87 + function set_events() {
68 jQuery(".catalog-remove-item").click(remote_catalog_item); 88 jQuery(".catalog-remove-item").click(remote_catalog_item);
69 - } 89 + }
70 90
71 - jQuery(document).ready(function(){  
72 - set_autocomplate();  
73 - set_events();  
74 - }); 91 + jQuery(document).ready(function(){
  92 + set_autocomplate();
  93 + set_events();
  94 + });
75 })(); 95 })();
views/search/_catalog_filter.html.erb
@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 display: inline; 3 display: inline;
4 margin-right: 5px; 4 margin-right: 5px;
5 font-size: 14px; 5 font-size: 14px;
  6 + padding: 5px;
6 } 7 }
7 8
8 #catalog-list ul li span { 9 #catalog-list ul li span {