Skip to content

Commit

Permalink
feat: allow barcode and edit link in search results (#6494)
Browse files Browse the repository at this point in the history
fixes #5994
  • Loading branch information
alexgarel authored Mar 24, 2022
1 parent 5d9836d commit 41fe83f
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 253 deletions.
27 changes: 25 additions & 2 deletions cgi/user.pl
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@

$template_data_ref->{sections} = [];

if ($user_ref) {
if ($user_ref) {
push @{$template_data_ref->{sections}}, {
id => "user",
fields => [
Expand Down Expand Up @@ -236,12 +236,35 @@
push @{$team_section_ref->{fields}}, {
field => "team_". $i,
label => sprintf(lang("team_s"), $i),
};
};
};

push @{$template_data_ref->{sections}}, {%$team_section_ref};
}

# Contributor section
my $contributor_section_ref = {
id => "contributor_settings",
name => lang("contributor_settings") . " (" . lang("optional") . ")",
description => "contributor_settings_description",
fields => [
{
field => "display_barcode",
type => "checkbox",
label => display_icon("barcode") . lang("display_barcode_in_search"),
value => $user_ref->{display_barcode} && "on",
},
{
field => "edit_link",
type => "checkbox",
label => display_icon("edit") . lang("edit_link_in_search"),
value => $user_ref->{edit_link} && "on",
},
]
};

push @{$template_data_ref->{sections}}, {%$contributor_section_ref};

# Admin section
if ($admin) {
my $administrator_section_ref = {
Expand Down
4 changes: 4 additions & 0 deletions docker/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ services:
USER_GID: ${USER_GID:-1000}
volumes:
- ./html:/opt/product-opener/html/
- ./icons:/opt/product-opener/icons
- ./scss:/opt/product-opener/scss
- ./gulpfile.js:/opt/product-opener/
- ./snyk:/opt/product-opener/
mongodb:
# Note: keep it as close a possible to mongodb.yml
image: mongo:4.4
Expand Down
98 changes: 59 additions & 39 deletions html/js/product-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,14 @@ function rank_products(products, product_preferences, use_user_product_preferenc
}


function display_products(target, product_groups, use_user_product_preferences_for_ranking ) {
function product_edit_url(product) {
return `/cgi/product.pl?type=edit&code=${product.code}`;
}


function display_products(target, product_groups, user_prefs ) {

if (use_user_product_preferences_for_ranking) {
if (user_prefs.use_ranking) {
$( target ).html('<ul id="products_tabs_titles" class="tabs" data-tab></ul>'
+ '<div id="products_tabs_content" class="tabs-content"></div>');
}
Expand All @@ -157,52 +162,66 @@ function display_products(target, product_groups, use_user_product_preferences_f
$.each( product_group, function(key, product) {

var product_html = "";

// Show the green / grey / colors for matching products only if we are using the user preferences
if (use_user_product_preferences_for_ranking) {
product_html += '<li><a href="' + product.url + '" class="list_product_a list_product_a_match_' + product.match_status + '">';
}
else {
product_html += '<li><a href="' + product.url + '" class="list_product_a">';
let css_classes = 'list_product_a';
if (user_prefs.use_ranking) {
css_classes += ' list_product_a_match_' + product.match_status;
}
product_html += `<li><a href=${product.url}" class="${css_classes}">`;
product_html += '<div class="list_product_img_div">';

if (product.image_front_thumb_url) {
product_html += '<img src="' + product.image_front_thumb_url + '" class="list_product_img">';
}
else {
product_html += '<img src="/images/icons/product-silhouette-transparent.svg" class="list_product_img">';
}


const img_src =
product.image_front_thumb_url ||
"/images/icons/product-silhouette-transparent.svg"
;
product_html += `<img src="${img_src}" class="list_product_img">`;

product_html += "</div>";

if (product.product_display_name) {
product_html += '<div class="list_product_name">' + product.product_display_name + "</div>";
}
else {
product_html += '<div class="list_product_name">' + product.code + "</div>";
}

$.each(product.match_attributes.mandatory.concat(product.match_attributes.very_important, product.match_attributes.important), function (key, attribute) {

if (attribute.icon_url) {
var title = attribute.title;

if (attribute.description_short) {
title += ' - ' + attribute.description_short;
}

if (attribute.missing) {
title += " - " + attribute.missing;
}
}

product_html += '<img class="list_product_icons" src="' + attribute.icon_url + '" title="' + title + '">';
}
});

product_html += "</a></li>";

products_html.push(product_html);
// add some specific fields
if (user_prefs.display.display_barcode) {
product_html += `<span class="list_display_barcode">${product.code}</span>`;
}
product_html += "</a>";
if (user_prefs.display.edit_link) {
const edit_url = product_edit_url(product);
const edit_title = lang().edit_product_page;
product_html += `
<a class="list_edit_link"
alt="Edit ${product.product_display_name}"
href="${edit_url}"
title="${edit_title}">
<img src="/images/icons/dist/edit.svg">
</a>
`;
}
product_html += "</li>";

products_html.push(product_html);
});

var active = "";
Expand All @@ -217,11 +236,11 @@ function display_products(target, product_groups, use_user_product_preferences_f
}
}
else {
text_or_icon = '<img src="/images/icons/match-' + product_group_id + '.svg" class="icon">'
text_or_icon = '<img src="/images/attributes/match-' + product_group_id + '.svg" class="icon">'
+ ' <span style="color:grey">' + product_group.length + "</span>";
}

if (use_user_product_preferences_for_ranking) {
if (user_prefs.use_ranking) {
$("#products_tabs_titles").append(
'<li class="tabs tab-title tab_products-title' + active + '">'
+ '<a id="tab_products_' + product_group_id + '" href="#products_' + product_group_id + '" title="' + lang()["products_match_" + product_group_id] + '">'
Expand Down Expand Up @@ -298,20 +317,21 @@ function display_product_summary(target, product) {
}


function rank_and_display_products (target, products) {

function rank_and_display_products (target, products, contributor_prefs) {


// Retrieve user preferences from local storage

var user_product_preferences = get_user_product_preferences();

var user_prefs = {
use_ranking: JSON.parse(localStorage.getItem('use_user_product_preferences_for_ranking')),
product: get_user_product_preferences(),
display: contributor_prefs,
};

// Retrieve whether we should use the preferences for ranking, or only for displaying the products

var use_user_product_preferences_for_ranking = JSON.parse(localStorage.getItem('use_user_product_preferences_for_ranking'));

var ranked_products = rank_products(products, user_product_preferences, use_user_product_preferences_for_ranking);

display_products(target, ranked_products, use_user_product_preferences_for_ranking);

var ranked_products = rank_products(products, user_prefs.product, user_prefs.use_ranking);
display_products(target, ranked_products, user_prefs);

$(document).foundation('equalizer', 'reflow');
}

Expand Down
3 changes: 3 additions & 0 deletions icons/barcode.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion lib/ProductOpener/Config2_docker.pm
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ my $is_localhost = index($po_domain, 'localhost') != -1;

$server_domain = $is_localhost && $po_port != '80' ? "$po_domain:$po_port" : $po_domain;
@ssl_subdomains = $is_localhost ? qw() : qw(*);
$producers_platform = $ENV{PRODUCERS_PLATFORM} || "0";
$producers_platform = $ENV{PRODUCERS_PLATFORM} || undef;
if (defined $producers_platform && $producers_platform && $producers_platform != "0") {
$producers_platform = undef;
}

# server paths
$data_root = "/mnt/podata";
Expand Down
23 changes: 20 additions & 3 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4217,13 +4217,19 @@ sub display_search_results($) {
if ($search_api_url !~ /\?/) {
$search_api_url =~ s/\&/\?/;
}

my $contributor_prefs_json = decode_utf8(encode_json({
display_barcode => $User{display_barcode},
edit_link => $User{edit_link},
}));

my $preferences_text = lang("classify_products_according_to_your_preferences");

$scripts .= <<JS
<script type="text/javascript">
var page_type = "products";
var preferences_text = "$preferences_text";
var contributor_prefs = $contributor_prefs_json;
var products = [];
</script>
JS
Expand All @@ -4238,7 +4244,9 @@ JS


$initjs .= <<JS
display_user_product_preferences("#preferences_selected", "#preferences_selection_form", function () { rank_and_display_products("#search_results", products); });
display_user_product_preferences("#preferences_selected", "#preferences_selection_form", function () {
rank_and_display_products("#search_results", products, contributor_prefs);
});
search_products("#search_results", products, "$search_api_url");
JS
;
Expand Down Expand Up @@ -5324,10 +5332,17 @@ sub search_and_display_products($$$$$) {
$products_json = decode_utf8(encode_json($request_ref->{structured_response}{products}));
}

my $contributor_prefs_json = decode_utf8(encode_json({
display_barcode => $User{display_barcode},
edit_link => $User{edit_link},
}));


$scripts .= <<JS
<script type="text/javascript">
var page_type = "products";
var preferences_text = "$preferences_text";
var contributor_prefs = $contributor_prefs_json;
var products = $products_json;
</script>
JS
Expand All @@ -5340,8 +5355,10 @@ JS
;

$initjs .= <<JS
display_user_product_preferences("#preferences_selected", "#preferences_selection_form", function () { rank_and_display_products("#search_results", products); });
rank_and_display_products("#search_results", products);
display_user_product_preferences("#preferences_selected", "#preferences_selection_form", function () {
rank_and_display_products("#search_results", products, contributor_prefs);
});
rank_and_display_products("#search_results", products, contributor_prefs);
JS
;

Expand Down
Loading

0 comments on commit 41fe83f

Please sign in to comment.