diff --git a/html/js/product-preferences.js b/html/js/product-preferences.js index ed5fb1b893df7..cb1b60ba246b6 100644 --- a/html/js/product-preferences.js +++ b/html/js/product-preferences.js @@ -5,6 +5,7 @@ var attribute_groups; // All supported attribute groups and attributes + translated strings var preferences; // All supported preferences + translated strings var use_user_product_preferences_for_ranking = JSON.parse(localStorage.getItem('use_user_product_preferences_for_ranking')); +var default_preferences = { "nutriscore" : "very_important", "nova" : "important", "ecoscore" : "important" }; function get_user_product_preferences () { // Retrieve user preferences from local storage @@ -17,7 +18,7 @@ function get_user_product_preferences () { } else { // Default preferences - user_product_preferences = { "nutriscore" : "very_important", "nova" : "important", "ecoscore" : "important" }; + user_product_preferences = default_preferences; } return user_product_preferences; @@ -84,6 +85,39 @@ function display_selected_preferences (target_selected_summary, product_preferen } +function generate_preferences_switch_button(preferences_text, checkbox_id) { + + var checked = ''; + if (use_user_product_preferences_for_ranking) { + checked = " checked"; + } + + var html = '
' + + '' + + '
' + + ''; + + return html; +} + +function activate_preferences_switch_buttons(change) { + + $(".preferences_checkboxes").change(function() { + + localStorage.setItem('use_user_product_preferences_for_ranking', this.checked); + use_user_product_preferences_for_ranking = this.checked; + + // Update the other checkbox value + $(".preferences_checkboxes").prop('checked',use_user_product_preferences_for_ranking); + + // Call the change callback if we have one (e.g. to update search results) + if (change) { + change(); + } + }); + +} + // display a switch to use preferences (on list of products pages) and a button to edit preferences function display_use_preferences_switch_and_edit_preferences_button (target_selected, target_selection_form, change) { @@ -98,16 +132,8 @@ function display_use_preferences_switch_and_edit_preferences_button (target_sele // we are on a page with multiple products if (page_type == 'products') { - - var checked = ''; - if (use_user_product_preferences_for_ranking) { - checked = " checked"; - } - - html += '
' - + '' - + '
' - + '' + html_edit_preferences; + + html += generate_preferences_switch_button(preferences_text, "preferences_switch_in_list_of_products") + html_edit_preferences; } else { @@ -117,16 +143,7 @@ function display_use_preferences_switch_and_edit_preferences_button (target_sele $( target_selected ).html(html); if (page_type == 'products') { - $("#preferences_checkbox").change(function() { - - localStorage.setItem('use_user_product_preferences_for_ranking', this.checked); - use_user_product_preferences_for_ranking = this.checked; - - // Call the change callback if we have one (e.g. to update search results) - if (change) { - change(); - } - }); + activate_preferences_switch_buttons(change); } $( "#show_selection_form").on( "click", function() { @@ -243,7 +260,9 @@ function display_user_product_preferences (target_selected, target_selection_for + " " + lang().close + '' + "

" + lang().preferences_edit_your_food_preferences + "

" + "

" + lang().preferences_locally_saved + "

" - + '' + lang().delete_all_preferences + '' + + generate_preferences_switch_button(lang().classify_products_according_to_your_preferences, "preferences_switch_in_preferences") + "
" + + '' + lang().reset_preferences + '' + + ' ' + lang().reset_preferences_details + '' @@ -254,6 +273,8 @@ function display_user_product_preferences (target_selected, target_selection_for + " " + lang().close + '

' + '' ); + + activate_preferences_switch_buttons(change); $( ".attribute_radio").change( function () { if (this.checked) { @@ -274,8 +295,8 @@ function display_user_product_preferences (target_selected, target_selection_for display_use_preferences_switch_and_edit_preferences_button(target_selected, target_selection_form, change); } - $( "#delete_all_preferences_button").on("click", function() { - user_product_preferences = {}; + $( "#reset_preferences_button").on("click", function() { + user_product_preferences = default_preferences; localStorage.setItem('user_product_preferences', JSON.stringify(user_product_preferences)); // Redisplay user preferences @@ -288,9 +309,9 @@ function display_user_product_preferences (target_selected, target_selection_for } }); - $("#delete_all_preferences_button").on('keydown', (event) => { + $("#reset_preferences_button").on('keydown', (event) => { if (event.key === 'Space' || event.key === 'Enter') { - $("#delete_all_preferences_button").trigger("click"); + $("#reset_preferences_button").trigger("click"); } }); diff --git a/html/js/product-search.js b/html/js/product-search.js index ad4406514b968..c999bb133ac24 100644 --- a/html/js/product-search.js +++ b/html/js/product-search.js @@ -8,15 +8,18 @@ // // Output values are returned in the product object // -// - match_status: -// very_good_match -// good_match -// poor_match -// unknown_match -// may_not_match -// does_not_match -// // - match_score: number from 0 to 100 +// - the score is 0 if +// - otherwise the score is a weighted average of how well the product matches +// each attribute selected by the user +// +// - match_status: +// - very_good_match score >= 75 +// - good_match score >= 50 +// - poor_match score < 50 +// - unknown_match at least one mandatory attribute is unknown, or unknown attributes weight more than 50% of the score +// - may_not_match at least one mandatory attribute score is <= 50 (e.g. may contain traces of an allergen) +// - does_not_match at least one mandatory attribute score is <= 10 (e.g. contains an allergen, is not vegan) // // - match_attributes: array of arrays of attributes corresponding to the product and // each set of preferences: mandatory, very_important, important @@ -125,6 +128,10 @@ function match_product_to_preferences (product, product_preferences) { else if ("may_not_match" in product.attributes_for_status) { product.match_status = "may_not_match"; } + // If one of the mandatory attribute is unknown, set an unknown match + else if ("unknown_match" in product.attributes_for_status) { + product.match_status = "unknown_match"; + } // If too many attributes are unknown, set an unknown match else if (sum_of_factors_for_unknown_attributes >= sum_of_factors / 2) { product.match_status = "unknown_match"; diff --git a/po/common/common.pot b/po/common/common.pot index 5b065668766f8..c0e39f34514cc 100644 --- a/po/common/common.pot +++ b/po/common/common.pot @@ -6131,4 +6131,14 @@ msgstr "May not match" # The translation needs to be short as it is displayed at the top of small product cards msgctxt "products_match_does_not_match" msgid "Does not match" -msgstr "Does not match" \ No newline at end of file +msgstr "Does not match" + +msgctxt "reset_preferences" +msgid "Use default preferences" +msgstr "Use default preferences" + +msgctxt "reset_preferences_details" +msgid "Nutri-Score, Eco-Score and food processing level (NOVA)" +msgstr "Nutri-Score, Eco-Score and food processing level (NOVA)" + + diff --git a/po/common/en.po b/po/common/en.po index a67d507f53958..7798549a58411 100644 --- a/po/common/en.po +++ b/po/common/en.po @@ -6155,3 +6155,13 @@ msgstr "May not match" msgctxt "products_match_does_not_match" msgid "Does not match" msgstr "Does not match" + +msgctxt "reset_preferences" +msgid "Use default preferences" +msgstr "Use default preferences" + +msgctxt "reset_preferences_details" +msgid "Nutri-Score, Eco-Score and food processing level (NOVA)" +msgstr "Nutri-Score, Eco-Score and food processing level (NOVA)" + +