var dicegeocacheing = {}; dicegeocacheing.manageGeocoderToLocalStorage = function (geometryId, data) { var googlePlaceCache = document.getElementById("cacheingGooglePlaces").value; if (googlePlaceCache == 'true') { var dicegeo = dicegeocacheing.getGeoToStorage(geometryId, 'dicegeocoder'); if (dicegeo != undefined) { var newElement = document.createElement("input"); newElement.type = "hidden"; newElement.id = geometryId; newElement.value = dicegeo; var att = document.createAttribute("class"); // Create a "class" attribute att.value = "geometry"; // Set the value of the class attribute newElement.setAttributeNode(att); var targetElement = document.querySelector("input[id=location]") || document.querySelector('input[id=search-field-location]'); dicegeocacheing.insertAfter(newElement, targetElement); var splitDiceGeo = dicegeo.split('_'); if (splitDiceGeo.length > 1) { var newElement1 = document.createElement("input"); newElement1.type = "hidden"; newElement1.id = 'pc'; newElement1.value = 'true'; var att1 = document.createAttribute("class"); // Create a "class" attribute att1.value = "postalCode"; // Set the value of the class attribute newElement1.setAttributeNode(att1); dicegeocacheing.insertAfter(newElement1, targetElement); } return dicegeo; } else if (dicegeo === undefined && data.length > 2) return dicegeocacheing.appendGeoToStorage(geometryId, data, 'dicegeocoder', 50); else return dicegeo; } } dicegeocacheing.insertAfter = function (newElement, targetElement){ var parent = targetElement.parentNode; if (parent.lastChild == targetElement) { parent.appendChild(newElement); } else { parent.insertBefore(newElement, targetElement.nextSibling); } } dicegeocacheing.appendGeoToStorage = function (geometryId, data, localstoragename, itemCount) { var dicegeocoder = JSON.parse(localStorage.getItem(localstoragename)) || []; var dicegeo = {}; dicegeo[geometryId] = data; if (localstoragename === 'dicegeoplaces') { var countIndex = document.getElementById("cacheingGoogleAPICount") ? parseInt(document.getElementById("cacheingGoogleAPICount").value) : 3; var inx = dicegeocacheing.getGeoInxToStorage(geometryId, dicegeocoder, countIndex); if (inx == -1) { dicegeocacheing.pushGeoToStorage(localstoragename, dicegeocoder, itemCount, dicegeo); } else { //dicegeocoder[inx] = dicegeo; //localStorage.setItem(localstoragename, JSON.stringify(dicegeocoder)); } } else { dicegeocacheing.pushGeoToStorage(localstoragename, dicegeocoder, itemCount, dicegeo); } return data; } dicegeocacheing.pushGeoToStorage = function (localstoragename, dicegeocoder, itemCount, dicegeo) { if (dicegeocoder && dicegeocoder.length == itemCount) { dicegeocoder.shift(); } Array.prototype.push.call(dicegeocoder, dicegeo); localStorage.setItem(localstoragename, JSON.stringify(dicegeocoder)); } dicegeocacheing.getGeoToStorage = function (geometryId, localstoragename) { var dicegeocoder = JSON.parse(localStorage.getItem(localstoragename)) || []; for (var i = 0; i < dicegeocoder.length; i++) { var dicegeo = dicegeocoder[i]; if (dicegeo[geometryId]){ return dicegeo[geometryId]; } } return undefined; } dicegeocacheing.getGeoInxToStorage = function (geometryId, dicegeocoder, countIndex) { var prefix = geometryId.substring(0, countIndex); for (var i = 0; i < dicegeocoder.length; i++) { var dicegeo = dicegeocoder[i]; for (var key in dicegeo) { if (prefix === key.substring(0, countIndex)){ return i; } } } return -1; } dicegeocacheing.managePlacesToLocalStorage = function (placeRequest, predictions) { var googlePlaceCache = document.getElementById("cacheingGooglePlaces").value; if (googlePlaceCache == 'true') { var geometryId = placeRequest.input.toLowerCase().replace(/[^a-zA-Z0-9\s]/g,"").replace(/\s+/g, '-').replace(/^[ \t]+/gm, ''); geometryId = geometryId + placeRequest.types[0] + placeRequest.componentRestrictions.country; var dicegeo = dicegeocacheing.getGeoToStorage(geometryId, 'dicegeoplaces'); if (dicegeo != undefined) { return dicegeo; } else if (dicegeo === undefined && predictions.length > 2) return dicegeocacheing.appendGeoToStorage(geometryId, predictions, 'dicegeoplaces', 20); else return dicegeo; } }