let TEMPLATE_TABLE_CONTAINER = null; let TEMPLATE_ROW_ITEM = null; let TEMPLATE_SEPARATOR_ITEM = null; let ROW_ITEM_COUNT = 3; let PAGE_ITEM_COUNT = 24; let CURRENT_PAGE_INDEX = 1; let COMPANY_PROFILE_URL = "https:///certified-companies/showprofile/?tok=&lang="; let LAST_SEARCH_RESULT = null; function getComponentFromMappingName(mappingName, mapping_conf_map) { if (mappingName in mapping_conf_map) { let compMapping = mapping_conf_map[mappingName]; let selector = compMapping["Selector"].replaceAll("", compMapping["Id"]); let comp = $("iframe").contents().find(selector); if (comp == null || $(comp).length == 0) { ConsoleLog("Error: Component not found: " + selector); return null; } return comp; } else { ConsoleLog("Error: Mapping definition not found: " + mappingName); return null; } } function SearchCompanies(e, mapping_conf_map) { e.preventDefault(); let companyNameComp = getComponentFromMappingName("Search_Name_Input", mapping_conf_map); let companyNameVal = ""; if (companyNameComp != null) { companyNameVal = $(companyNameComp).val(); } let searchIndustryComp = getComponentFromMappingName("Industry_DropDown", mapping_conf_map); let selectedIndustryVal = 0; let selectedIndustryText = ""; if (searchIndustryComp != null) { selectedIndustryVal = $(searchIndustryComp).val(); selectedIndustryText = $("option:selected", searchIndustryComp).html(); } let searchSizeComp = getComponentFromMappingName("Company_Size_DropDown", mapping_conf_map); let selectedSizeVal = 0; let selectedSizeText = ""; if (searchSizeComp != null) { selectedSizeVal = $(searchSizeComp).val(); selectedSizeText = $("option:selected", searchSizeComp).html(); } let searchCountryComp = getComponentFromMappingName("Country_DropDown", mapping_conf_map); let selectedCountryVal = 0; let selectedCountryText = ""; if (searchCountryComp != null) { selectedCountryVal = $(searchCountryComp).val(); selectedCountryText = $("option:selected", searchCountryComp).html(); } let params = {}; params["brk"] = $("#PARAMETER_BROKER").val(); params["lang"] = $("#PARAMETER_LANGUAGE").val(); params["nm"] = companyNameVal; params["sid"] = selectedSizeVal; params["stxt"] = selectedSizeText; params["iid"] = selectedIndustryVal; params["itxt"] = selectedIndustryText; params["ctr"] = $("#cprfl_country").val(); params["clst"] = $("#cprfl_clist").val(); params["sco"] = selectedCountryVal; params["scotxt"] = selectedCountryText; let language = $("#PARAMETER_LANGUAGE").val(); $.ajax( "GetCompanySearchResult", { type : 'POST', data: params, error: function(jqXHR, textStatus, errorThrown) { ConsoleLog("Error: " + textStatus); errorHandler(textStatus); }, success: function (respStr, status) { if (status === "success") { let respJson = JSON.parse(respStr); if(respJson.length > 0 && "CompanyProfileSearchResults" in respJson[0]/*"success" in respJson && respJson.success == true*/) { let searchResultsObjArr = respJson[0]["CompanyProfileSearchResults"]; let resultsArr = getResultsArrFromResponse(searchResultsObjArr); CURRENT_PAGE_INDEX = 1; if (resultsArr == null) { showPagesLabel(0,0,0, mapping_conf_map); } LAST_SEARCH_RESULT = resultsArr; renderCompanySearchResults(LAST_SEARCH_RESULT, mapping_conf_map, language); let prevPageComp = getComponentFromMappingName("Previous_Page_Button", mapping_conf_map); if (prevPageComp != null) { $(prevPageComp).unbind("click"); $(prevPageComp).click(function() { //ConsoleLog("Current Page Index = " + CURRENT_PAGE_INDEX); if (CURRENT_PAGE_INDEX > 1) { if (LAST_SEARCH_RESULT == null) { return; } CURRENT_PAGE_INDEX --; renderCompanySearchResults(LAST_SEARCH_RESULT, mapping_conf_map, language); } }) } let nextPageComp = getComponentFromMappingName("Next_Page_Button", mapping_conf_map); if (nextPageComp != null) { $(nextPageComp).unbind("click"); $(nextPageComp).click(function() { //ConsoleLog("Current Page Index = " + CURRENT_PAGE_INDEX); if (LAST_SEARCH_RESULT == null) { return; } if (CURRENT_PAGE_INDEX < Math.ceil(LAST_SEARCH_RESULT.length / PAGE_ITEM_COUNT)) { CURRENT_PAGE_INDEX ++; renderCompanySearchResults(LAST_SEARCH_RESULT, mapping_conf_map, language); } }) } } else { ConsoleLog("Error in CompanySearchResults service: Invalid Response" /*+ respJson["Error"]*/); errorHandler("Error getting company search results data: Invalid Response" /*+ respJson["Error"]*/); } } else { ConsoleLog("Error calling CompanySearchResults service with AJAX!"); errorHandler("Error getting company search results data: Network Error"); } } }); } function getResultsArrFromResponse(resp) { if (resp.length == 0 || !("Results" in resp[0])) { return null; } return resp[0]["Results"]; } function showPagesLabel(startItem, endItem, totalCount, mapping_conf_map) { let pagesLabelComp = getComponentFromMappingName("Pages_Label", mapping_conf_map) if (pagesLabelComp != null) { if (endItem > totalCount) { endItem = totalCount; } let label = startItem + " - " + endItem + " / " + totalCount; $(pagesLabelComp).html(label); } } function renderCompanySearchResults(resultsArr, mapping_conf_map, language="EN") { if (TEMPLATE_ROW_ITEM == null) { TEMPLATE_ROW_ITEM = getComponentFromMappingName("Company_List_Row", mapping_conf_map); TEMPLATE_TABLE_CONTAINER = $(TEMPLATE_ROW_ITEM).parent(); TEMPLATE_SEPARATOR_ITEM = getComponentFromMappingName("Company_List_Row_Separator_Item", mapping_conf_map); $(TEMPLATE_ROW_ITEM).removeAttr("id"); $(TEMPLATE_ROW_ITEM).remove(); if (TEMPLATE_SEPARATOR_ITEM != null) { $(TEMPLATE_SEPARATOR_ITEM).removeAttr("id"); $(TEMPLATE_SEPARATOR_ITEM).remove(); } } $(TEMPLATE_TABLE_CONTAINER).empty(); if (resultsArr == null) { return; } showPagesLabel(CURRENT_PAGE_INDEX*PAGE_ITEM_COUNT-PAGE_ITEM_COUNT+1,CURRENT_PAGE_INDEX*PAGE_ITEM_COUNT,resultsArr.length, mapping_conf_map); let itemTemplateMapping = mapping_conf_map["Company_List_Item"]; let itemSelector = itemTemplateMapping["Selector"].replaceAll("", itemTemplateMapping["Id"]); let counter = 0; let currentRow = null; let currentRowIndex = 0; for (let k = CURRENT_PAGE_INDEX*PAGE_ITEM_COUNT-PAGE_ITEM_COUNT; k < CURRENT_PAGE_INDEX*PAGE_ITEM_COUNT; k++) { let index = k; if (resultsArr.length <= k) { break; } let nextCompanyData = resultsArr[k]; currentRowIndex = index % ROW_ITEM_COUNT; if (currentRowIndex == 0) { currentRow = $(TEMPLATE_ROW_ITEM).clone(); $(TEMPLATE_TABLE_CONTAINER).append(currentRow); if (TEMPLATE_SEPARATOR_ITEM != null) { //ConsoleLog("ADDING SEPARATOR BETWEEN ROWS..."); let cloneSeparator = $(TEMPLATE_SEPARATOR_ITEM).clone(); $(TEMPLATE_TABLE_CONTAINER).append(cloneSeparator); } else { ConsoleLog("SEPARATOR ITEM DOESN'T EXIST..."); } } let itemContainer = $(itemSelector, currentRow)[currentRowIndex]; //resizeWithAspectRatio($("img", itemContainer), 400, 240); //$("img", itemContainer).css("width", "400px").css("height", "240px"); if (!("ImageURL" in nextCompanyData) || nextCompanyData["ImageURL"] == "") { //$("img", itemContainer).attr("src", "#"); } else { $("img", $("[id$='_image']", itemContainer)).attr("src", nextCompanyData["ImageURL"]); $("img", $("[id$='_image']", itemContainer)).removeAttr("srcset"); } let targetDomainReplace = nextCompanyData["WebCompanyProfileDomain"]; let companyNameSelector = mapping_conf_map["Company_Name_Header"]["Selector"]; companyNameSelector = companyNameSelector.replaceAll("", mapping_conf_map["Company_Name_Header"]["Id"]); let companyIndustrySelector = mapping_conf_map["Company_Industry_Header"]["Selector"]; companyIndustrySelector = companyIndustrySelector.replaceAll("", mapping_conf_map["Company_Industry_Header"]["Id"]); let companyCountrySelector = mapping_conf_map["Company_Country_Header"]["Selector"]; companyCountrySelector = companyCountrySelector.replaceAll("", mapping_conf_map["Company_Country_Header"]["Id"]); let companySizeSelector = mapping_conf_map["Company_Size_Header"]["Selector"]; companySizeSelector = companySizeSelector.replaceAll("", mapping_conf_map["Company_Size_Header"]["Id"]); companyNameSelector = companyNameSelector.replace("xxx", currentRowIndex+1); companyIndustrySelector = companyIndustrySelector.replace("xxx", currentRowIndex+1); companyCountrySelector = companyCountrySelector.replace("xxx", currentRowIndex+1); companySizeSelector = companySizeSelector.replace("xxx", currentRowIndex+1); $(companyNameSelector, itemContainer).html(nextCompanyData["Company_Name"]); $(companyIndustrySelector, itemContainer).html(nextCompanyData["Company_Sector"]); $(companyCountrySelector, itemContainer).html(nextCompanyData["Company_Country"]); $(companySizeSelector, itemContainer).html(nextCompanyData["Company_Size"]); let anchorToProfile = $(""); let profileUrl = COMPANY_PROFILE_URL.replace("", nextCompanyData["PublicLongUniqueToken"]); profileUrl = profileUrl.replace("", targetDomainReplace); profileUrl = profileUrl.replace("", language); $(anchorToProfile).attr("href", profileUrl); let anchorClone = $(anchorToProfile).clone(); $("img", itemContainer).wrap(anchorToProfile); $("h2", $("#company" + (currentRowIndex+1) + "_name", itemContainer)).wrap(anchorClone); let companyNameContainerId = mapping_conf_map["Company_Name_Header"]["Id"]; companyNameContainerId = companyNameContainerId.replace("xxx", currentRowIndex+1); let companyIndustryContainerId = mapping_conf_map["Company_Industry_Header"]["Id"]; companyIndustryContainerId = companyIndustryContainerId.replace("xxx", currentRowIndex+1); let companyCountryContainerId = mapping_conf_map["Company_Country_Header"]["Id"]; companyCountryContainerId = companyCountryContainerId.replace("xxx", currentRowIndex+1); let companySizeContainerId = mapping_conf_map["Company_Size_Header"]["Id"]; companySizeContainerId = companySizeContainerId.replace("xxx", currentRowIndex+1); $("#" + companyNameContainerId, itemContainer).removeAttr("id"); $("#" + companyIndustryContainerId, itemContainer).removeAttr("id"); $("#" + companyCountryContainerId, itemContainer).removeAttr("id"); $("#" + companySizeContainerId, itemContainer).removeAttr("id"); } if (currentRow != null && currentRowIndex < ROW_ITEM_COUNT-1) { for (let i=ROW_ITEM_COUNT-1; i>currentRowIndex;i--) { $(itemSelector, currentRow)[i].remove(); } } }