var myCityID = "myCity"; var myBranchID = "myBranch"; var schools = []; var schoolOnlyFromList = false; if (typeof (mySchoolCheck) == "undefined") { var mySchoolCheck = "אני מעוניין/ת להדפיס סמל בית ספר"; } if (typeof (myCityTitle) == "undefined") { var myCityTitle = "עיר"; } if (typeof (myBranchTitle) == "undefined") { var myBranchTitle = "בית ספר"; } ccpubsub.subscribe("ready", function () { if (document.querySelector('.cc-page-type-product')) { var url_path = "https://cashcow-cdn.azureedge.net/system/schools.json?v=101"; var showScoolsCheckBox = myCityEL = myBranchEL = null; document.querySelectorAll('.attributes .attr_cont .cc-select-name.attr-title').forEach(function (_this) { switch (_this.innerText) { case mySchoolCheck: showScoolsCheckBox = _this.closest(".attr_cont").querySelector('input'); break; case myCityTitle: myCityEL = _this.closest(".attr_cont").querySelector('input'); break; case myBranchTitle: myBranchEL = _this.closest(".attr_cont").querySelector('input'); break; case "אני מעוניין/ת להדפיס סמל בית ספר": showScoolsCheckBox = _this.closest(".attr_cont").querySelector('input'); break; case "עיר": myCityEL = _this.closest(".attr_cont").querySelector('input'); break; case "בית ספר": myBranchEL = _this.closest(".attr_cont").querySelector('input'); break; } }) if (myCityEL && myBranchEL) { myCityEL.id = myCityID; myCityEL.placeholder = "התחילו להקליד את שם העיר ובחרו מהרשימה"; myCityEL.autocomplete = "off"; myCityEL.parentElement.classList.add(myCityID); myCityEL.parentElement.classList.add("autocomplete"); myBranchEL.id = myBranchID; myBranchEL.placeholder = "התחילו להקליד את שם בית הספר ובחרו מהרשימה"; myBranchEL.autocomplete = "off"; myBranchEL.parentElement.classList.add(myBranchID); myBranchEL.parentElement.classList.add("autocomplete"); hideSchools(myCityEL, myBranchEL); $.get(url_path, function (json) { schools = json.schools; /*An array containing all the country names in the world:*/ var myCity = Array.from(new Set(schools.map(a => a.City))); var myBranch = schools.map(a => a.Branch); /*initiate the autocomplete function on the "myInput" element, and pass along the countries array as possible autocomplete values:*/ autocomplete(document.getElementById(myCityID), myCity); autocomplete(document.getElementById(myBranchID), myBranch); }) } if (showScoolsCheckBox) { if (myCityEL && myBranchEL) { if (showScoolsCheckBox.checked) { showSchools(myCityEL, myBranchEL); } showScoolsCheckBox.addEventListener("change", function (e) { (this.checked) ? showSchools(myCityEL, myBranchEL) : hideSchools(myCityEL, myBranchEL); }); } } } }); function showSchools(myCityEL, myBranchEL) { myCityEL.parentElement.parentElement.classList.remove("hidden"); myBranchEL.parentElement.parentElement.classList.remove("hidden"); } function hideSchools(myCityEL, myBranchEL) { myCityEL.parentElement.parentElement.classList.add("hidden"); myBranchEL.parentElement.parentElement.classList.add("hidden"); } function citySelected(selectedCity) { var myBranchByCity = schools.filter(b => b.City == selectedCity).map(a => a.Branch); var branch = document.getElementById(myBranchID); branch.value = ""; autocomplete(branch, myBranchByCity); } function cityNotSelected() { var branch = document.getElementById(myBranchID); branch.value = ""; autocomplete(branch, myBranch); } function autocomplete(inp, arr) { /*the autocomplete function takes two arguments, the text field element and an array of possible autocompleted values:*/ var currentFocus; /*execute a function when someone writes in the text field:*/ inp.addEventListener("input", function (e) { if (this.id == myCityID) { cityNotSelected(); } var a, b, i, bstrong, val = this.value; /*close any already open lists of autocompleted values*/ closeAllLists(); if (!val) { return false; } currentFocus = -1; /*create a DIV element that will contain the items (values):*/ a = document.createElement("DIV"); a.setAttribute("id", this.id + "autocomplete-list"); a.setAttribute("class", "autocomplete-items"); /*append the DIV element as a child of the autocomplete container:*/ this.parentNode.appendChild(a); /*for each item in the array...*/ for (i = 0; i < arr.length; i++) { /*check if the item starts with the same letters as the text field value:*/ //if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) { if (arr[i].toUpperCase().includes(val.toUpperCase())) { /*create a DIV element for each matching element:*/ bstrong = arr[i]; b = document.createElement("DIV"); /*make the matching letters bold:*/ b.innerHTML = bstrong.replace(val, '' + val + ''); /*insert a input field that will hold the current array item's value:*/ b.innerHTML += ""; /*execute a function when someone clicks on the item value (DIV element):*/ b.addEventListener("click", function (e) { /*insert the value for the autocomplete text field:*/ inp.value = this.innerText; /*close the list of autocompleted values, (or any other open lists of autocompleted values:*/ if (this.parentElement.id.split("autocomplete-list")[0] == myCityID) { citySelected(inp.value); } closeAllLists(); }); a.appendChild(b); } } }); /*execute a function presses a key on the keyboard:*/ inp.addEventListener("keydown", function (e) { var x = document.getElementById(this.id + "autocomplete-list"); if (x) x = x.getElementsByTagName("div"); if (e.keyCode == 40) { /*If the arrow DOWN key is pressed, increase the currentFocus variable:*/ currentFocus++; /*and and make the current item more visible:*/ addActive(x); } else if (e.keyCode == 38) { //up /*If the arrow UP key is pressed, decrease the currentFocus variable:*/ currentFocus--; /*and and make the current item more visible:*/ addActive(x); } else if (e.keyCode == 13) { /*If the ENTER key is pressed, prevent the form from being submitted,*/ e.preventDefault(); if (currentFocus > -1) { /*and simulate a click on the "active" item:*/ if (x) x[currentFocus].click(); } } else if (e.keyCode == 9) { //tab if (document.querySelector('.autocomplete-items')) { e.preventDefault(); currentFocus++; addActive(x); } } }); inp.addEventListener("focus", function (e) { if (window.innerWidth < 768) { inp.scrollIntoView({ behavior: "smooth" }); window.scrollBy(0, -55, "smooth"); } }); inp.addEventListener("focusout", function (e) { if (this.id == myCityID) { if (schools.filter(a => a.City == this.value).length == 0) { // city not in list if (schoolOnlyFromList) { this.value = ""; } } else { citySelected(this.value); } } else if (this.id == myBranchID) { if (schools.filter(a => a.Branch == this.value).length == 0) { // Branch not in list if (schoolOnlyFromList) { this.value = ""; } } } }); function addActive(x) { /*a function to classify an item as "active":*/ if (!x) return false; /*start by removing the "active" class on all items:*/ removeActive(x); if (currentFocus >= x.length) currentFocus = 0; if (currentFocus < 0) currentFocus = (x.length - 1); /*add class "autocomplete-active":*/ x[currentFocus].classList.add("autocomplete-active"); } function removeActive(x) { /*a function to remove the "active" class from all autocomplete items:*/ for (var i = 0; i < x.length; i++) { x[i].classList.remove("autocomplete-active"); } } function closeAllLists(elmnt) { /*close all autocomplete lists in the document, except the one passed as an argument:*/ var x = document.getElementsByClassName("autocomplete-items"); for (var i = 0; i < x.length; i++) { if (elmnt != x[i] && elmnt != inp) { x[i].parentNode.removeChild(x[i]); } } } /*execute a function when someone clicks in the document:*/ document.addEventListener("click", function (e) { closeAllLists(e.target); }); }