Home > Enterprise >  JS Conditional statement
JS Conditional statement

Time:10-01

/**
 * AxonCreator - Website Builder
 *
 * NOTICE OF LICENSE
 *
 * @author    axonvip.com <support@axonvip.com>
 * @copyright 2021 axonvip.com
 * @license   You can not resell or redistribute this software.
 *
 * https://www.gnu.org/licenses/gpl-3.0.html
 */
var AxpsSearchModule;
!(function (d) {
    "use strict";
    AxpsSearchModule = {
        init: function () {
            this.mdCategoryDropdown(), this.mdSearch();
        },
        mdSearch: function () {
            d("body").on("shown.bs.modal", "#search-popup", function () {
                var e,
                    a,
                    i = d("#search-popup").find("input[name=s]").val();
                d("#search-popup").find("input[name=s]").val("").focus().val(i),
                    d(window).width() < 768 ||
                        ((a = (e = d("#search-popup").find(".category-dropdown-inner")).innerWidth()   e.closest("form").find(".search-submit").innerWidth()   17),
                        (i = "padding-right"),
                        d("body").hasClass("rtl") && (i = "padding-left"),
                        e.closest("form").find("input[name=s]").css(i, a));
            });
            function a() {
                d("form.has-ajax-search").each(function () {
                    var a,
                        e,
                        i,
                        n,
                        r,
                        s,
                        t,
                        o,
                        c = d(this);
                    c.hasClass("fs-initialized") ||
                        (c.addClass("fs-initialized"),
                        (a = '<div class="view-all-results view-all-products"><span>'   opSearch.all_results_product   "</span></div>"),
                        (e = c.attr("action")),
                        (i = parseInt(opSearch.count)),
                        (n = c.find('[name="c"]')),
                        (r = "cart_default"),
                        (s = c.closest(".search-wrapper").find(".search-results")),
                        c.removeClass("has-ajax-search"),
                        void 0 !== opSearch.imageType && "" != opSearch.imageType && (r = opSearch.imageType),
                        0 != opSearch.search_string && c.find("input[name=s]").val(opSearch.search_string),
                        c.find("input[name=s]").click(function () {
                            3 <= c.find("input[name=s]").val().length && "" !== s.find(".autocomplete-suggestions").html() && s.find(".autocomplete-suggestions").css({ display: "flex" });
                        }),
                        s.on("click", ".view-all-products", function () {
                            c.submit();
                        }),
                        c.find("input[name=s]").devbridgeAutocomplete({
                            serviceUrl: e,
                            appendTo: s,
                            type: "POST",
                            dataType: "json",
                            paramName: "s",
                            minChars: 3,
                            params: { c: n.val(), resultsPerPage: i },
                            onSelect: function (e) {
                                void 0 !== e.permalink && 0 < e.permalink.length && (d("body").hasClass("product-id-"   e.id) ? location.reload() : (window.location.href = e.permalink));
                            },
                            onSearchStart: function (e) {
                                c.addClass("search-loading");
                            },
                            beforeRender: function (e) {
                                d(e).find(".search-divider-text").parent().addClass("search-divider"), d(e).find(".no-result").parent().addClass("search-no-result"), d(e).find(".view-all-products").parent().replaceWith(a);
                            },
                            onSearchComplete: function (e, a) {
                                c.removeClass("search-loading");
                            },
                            transformResult: function (e) {
                                var s = [];
                                return (
                                    s.push({ value: "", divider: opSearch.divider }),
                                    0 < e.products.length
                                        ? (e.products.forEach(function (e) {
                                              var a = (e.has_discount ? '<span class="regular-price">'   e.regular_price   "</span> " : "")   '<span class="price">'   e.price   "</span>",
                                                  i = "";
                                              e.cover && void 0 !== e.cover.bySize[r].url && (i = e.cover.bySize[r].url);
                                              var n = opSearch.sku   " "   (e.reference || "N/A");
                                              s.push({ value: e.name, permalink: e.url, price: a, thumbnail: '<img class="img-responsive" src="'   i   '" alt="" title="">', sku: n });
                                          }),
                                          s.push({ value: "", view_all_products: !0, permalink: "" }))
                                        : s.push({ value: opSearch.noProducts, no_found: !0, permalink: "" }),
                                    { suggestions: s }
                                );
                            },
                            formatResult: function (e, a) {
                                var i = "("   (a = "&" === a ? "&#038;" : a).replace(/[\-\[\]\/\{\}\(\)\*\ \?\.\\\^\$\|]/g, "\\$&")   ")",
                                    a = "";
                                return (
                                    e.divider && (a  = ' <h5 class="search-divider-text">'   e.divider   "</h5>"),
                                    e.thumbnail && (a  = ' <div class="search-thumb">'   e.thumbnail   "</div>"),
                                    e.value && (a  = '<h4 class="search-title">'   e.value.replace(new RegExp(i, "gi"), "<strong>$1</strong>").replace(/&lt;(\/?strong)&gt;/g, "<$1>")   "</h4>"),
                                    e.no_found && (a = '<div class="no-result">'   e.value   "</div>"),
                                    e.view_all_products && (a = '<div class="view-all-products"></div>'),
                                    e.sku && (a  = ' <div class="search-sku">'   e.sku   "</div>"),
                                    e.price && (a  = ' <div class="search-price">'   e.price   "</div>"),
                                    a
                                );
                            },
                        }),
                        n.length &&
                            ((t = c.find("input[name=s]").devbridgeAutocomplete()),
                            (o = e),
                            n.on("cat_selected", function () {
                                t.setOptions({ serviceUrl: o, params: { c: n.val(), resultsPerPage: i } }), t.hide(), t.onValueChange();
                            })),
                        d("body").on("click", function (e) {
                            e = e.target;
                            d(e).is(".search-wrapper") || d(e).parents().is(".search-wrapper") || c.find("input[name=s]").devbridgeAutocomplete("hide");
                        }),
                        d(".search-results").on("click", function (e) {
                            e.stopPropagation();
                        }));
                });
            }
            a(),
                prestashop.on("updatedProductList", function (e) {
                    a();
                });
        },
        mdCategoryDropdown: function () {
            d(".category-dropdown-inner").each(function () {
                var i,
                    n,
                    s,
                    r = d(this);
                function t() {
                    r.removeClass("shown");
                }
                function o() {
                    var e, a;
                    d(window).width() < 768 ||
                        ((e = r.innerWidth()   r.closest("form").find(".search-submit").innerWidth()   17), (a = "padding-right"), d("body").hasClass("rtl") && (a = "padding-left"), r.closest("form").find("input[name=s]").css(a, e));
                }
                r.hasClass("cd-initialized") ||
                    (r.addClass("cd-initialized"),
                    (i = r.find("> a")),
                    (n = r.find("> input")),
                    (s = r.find("> .list-wrapper")),
                    opSearch.current_category_id &&
                        (s.find(".active-item").removeClass("active-item"),
                        s
                            .find("a[data-value="   opSearch.current_category_id   "]")
                            .parent()
                            .addClass("active-item"),
                        i.text(s.find("a[data-value="   opSearch.current_category_id   "]").text()),
                        n.val(opSearch.current_category_id),
                        s.find("ul:not(.children) > li:first-child").show()),
                    o(),
                    d("body").on("click", function (e) {
                        e = e.target;
                        if (r.hasClass("shown") && !d(e).is(".category-dropdown-inner") && !d(e).parents().is(".category-dropdown-inner")) return t(), !1;
                    }),
                    i.on("click", function (e) {
                        return e.preventDefault(), r.hasClass("shown") ? t() : (r.addClass("shown"), void 0 !== d.fn.devbridgeAutocomplete && r.closest("form").find("input[name=s]").devbridgeAutocomplete("hide")), !1;
                    }),
                    s.on("click", "a", function (e) {
                        e.preventDefault();
                        var a = d(this).data("value"),
                            e = d(this).text();
                        s.find(".active-item").removeClass("active-item"),
                            d(this).parent().addClass("active-item"),
                            0 !== a ? s.find("ul:not(.children) > li:first-child").show() : 0 === a && s.find("ul:not(.children) > li:first-child").hide(),
                            i.text(e),
                            n.val(a).trigger("cat_selected"),
                            t(),
                            o();
                    }));
            });
        },
    };
})(jQuery),
    jQuery(document).ready(function () {
        AxpsSearchModule.init();
    });

In the below code the e.price is the price of a product, i want to know how i can put If conditional statement in this so that price with price 0 shall not be show(no price),and if amount is greater then 0 then it will be show. I tried if(e.price == 0) but i dont know how and in which line it should be written.

formatResult: function(e, a) {
  var i = "("   (a = "&" === a ? "&#038;" : a).replace(/[\-\[\]\/\{\}\(\)\*\ \?\.\\\^\$\|]/g, "\\$&")   ")",
    a = "";
  return (
    e.divider && (a  = ' <h5 class="search-divider-text">'   e.divider   "</h5>"),
    e.thumbnail && (a  = ' <div class="search-thumb">'   e.thumbnail   "</div>"),
    e.value && (a  = '<h4 class="search-title">'   e.value.replace(new RegExp(i, "gi"), "<strong>$1</strong>").replace(/&lt;(\/?strong)&gt;/g, "<$1>")   "</h4>"),
    e.no_found && (a = '<div class="no-result">'   e.value   "</div>"),
    e.view_all_products && (a = '<div class="view-all-products"></div>'),
    e.sku && (a  = ' <div class="search-sku">'   e.sku   "</div>"),
    e.price && (a  = ' <div class="search-price">'   e.price   "</div>"),
    a
  );
},

CodePudding user response:

you can use a Template literal with a short hand condition like so -
<div class="search-price"> ${e.price === 0 ? '' : e.price} </div>

CodePudding user response:

Just a suggestion, no guarantee it works 100%


formatResult: function(e, a) {
  var i = "("   (a = "&" === a ? "&#038;" : a).replace(/[\-\[\]\/\{\}\(\)\*\ \?\.\\\^\$\|]/g, "\\$&")   ")",
    a = "";

// Here
   if(e.price<=0){
      e.price = ""
   }
// Here

  return (
    e.divider && (a  = ' <h5 class="search-divider-text">'   e.divider   "</h5>"),
    e.thumbnail && (a  = ' <div class="search-thumb">'   e.thumbnail   "</div>"),
    e.value && (a  = '<h4 class="search-title">'   e.value.replace(new RegExp(i, "gi"), "<strong>$1</strong>").replace(/&lt;(\/?strong)&gt;/g, "<$1>")   "</h4>"),
    e.no_found && (a = '<div class="no-result">'   e.value   "</div>"),
    e.view_all_products && (a = '<div class="view-all-products"></div>'),
    e.sku && (a  = ' <div class="search-sku">'   e.sku   "</div>"),
    e.price && (a  = ' <div class="search-price">'   e.price   "</div>"),
    a
  );
},
  • Related