diff --git a/CrmGoogleMaps/html/apiKeyConfiguration.html b/CrmGoogleMaps/html/apiKeyConfiguration.html index c3b5577..574875e 100644 --- a/CrmGoogleMaps/html/apiKeyConfiguration.html +++ b/CrmGoogleMaps/html/apiKeyConfiguration.html @@ -11,7 +11,7 @@
CRM Google Maps - v1.2.0.0 + v1.3.0 PayPal diff --git a/CrmGoogleMaps/img/refresh.png b/CrmGoogleMaps/img/refresh.png new file mode 100644 index 0000000..4aeeaa2 Binary files /dev/null and b/CrmGoogleMaps/img/refresh.png differ diff --git a/CrmGoogleMaps/img/refresh.svg b/CrmGoogleMaps/img/refresh.svg deleted file mode 100644 index 09eee92..0000000 --- a/CrmGoogleMaps/img/refresh.svg +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/CrmGoogleMaps/js/apiKeyConfiguration.js b/CrmGoogleMaps/js/apiKeyConfiguration.js index 228301e..ce8e954 100644 --- a/CrmGoogleMaps/js/apiKeyConfiguration.js +++ b/CrmGoogleMaps/js/apiKeyConfiguration.js @@ -1,7 +1,7 @@ function getStoredApiKey() { var req = new XMLHttpRequest(); req.open("GET", - Xrm.Page.context.getClientUrl() + "/api/data/v8.2/fic_googlemapsconfigurations?$select=fic_apikey,fic_usegoogleaddress,fic_markericon,fic_defaultzoom", + Xrm.Page.context.getClientUrl() + "/api/data/v" + VERSION + "/fic_googlemapsconfigurations?$select=fic_apikey,fic_usegoogleaddress,fic_markericon,fic_defaultzoom", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); @@ -40,7 +40,7 @@ function createKey(apiKey) { entity.fic_markericon = $('#markerIcon').val(); entity.fic_defaultzoom = $('#defaultzoom').val(); var req = new XMLHttpRequest(); - req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/fic_googlemapsconfigurations", true); + req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v" + VERSION + "/fic_googlemapsconfigurations", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); @@ -70,7 +70,7 @@ function updateKey(key, id) { entity.fic_defaultzoom = $('#defaultzoom').val(); var req = new XMLHttpRequest(); req.open("PATCH", - Xrm.Page.context.getClientUrl() + "/api/data/v8.2/fic_googlemapsconfigurations(" + id + ")", + Xrm.Page.context.getClientUrl() + "/api/data/v" + VERSION + "/fic_googlemapsconfigurations(" + id + ")", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); @@ -95,7 +95,7 @@ function updateKey(key, id) { function upsertKey(key) { var req = new XMLHttpRequest(); - req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/fic_googlemapsconfigurations", true); + req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v" + VERSION + "/fic_googlemapsconfigurations", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); @@ -119,6 +119,7 @@ function upsertKey(key) { req.send(); } +var VERSION = parent.Xrm.Page.context.getVersion() != "" ? parent.Xrm.Page.context.getVersion().substring(0, 3) : "8.0"; getStoredApiKey(); document.getElementById("update").addEventListener("click", function () { @@ -154,4 +155,3 @@ for (var i = 1; i <= 20; i++) { $("#defaultzoom").append(new Option(i, i)); } } - diff --git a/CrmGoogleMaps/js/googleMaps.js b/CrmGoogleMaps/js/googleMaps.js index 4f5b425..ce8e954 100644 --- a/CrmGoogleMaps/js/googleMaps.js +++ b/CrmGoogleMaps/js/googleMaps.js @@ -1,162 +1,116 @@ -var useGoogleAddress = false; -var markerIcon = ""; -var defaultZoom = null; -var map; -var addresses = []; -var addressProcessedCounter = 0; -var lat = []; -var lng = []; -var markers = []; - -function RefreshButton(topLeftDiv) { - var imageUI = document.createElement('img'); - imageUI.style.height = '20px'; - imageUI.style.width = '20px'; - imageUI.src = '../img/refresh.svg'; - imageUI.style.backgroundColor = '#fff'; - imageUI.style.border = '2px solid #fff'; - imageUI.style.borderRadius = '3px'; - imageUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)'; - imageUI.style.cursor = 'pointer'; - topLeftDiv.appendChild(imageUI); - - topLeftDiv.addEventListener('click', function () { - initialize(); - }); -} - -function initialize() { - var p = GetParameters(); - var map_canvas = document.getElementById('map_canvas'); - var map_options = { - mapTypeId: google.maps.MapTypeId.ROADMAP, - mapTypeControl: false - } - map = map || new google.maps.Map(map_canvas, map_options); - - var topLeftControl = document.createElement('div'); - topLeftControl.style.margin = '10px 14px'; - var centerControl = new RefreshButton(topLeftControl); - - topLeftControl.index = 1; - if (map.controls[google.maps.ControlPosition.TOP_LEFT].length == 0) { - map.controls[google.maps.ControlPosition.TOP_LEFT].push(topLeftControl); - } - - for (var i = 0; i < markers.length; i++) { - markers[i].setMap(null); - } - addressProcessedCounter = 0; - lat = []; - lng = []; - markers = []; - addresses = p.address.split(','); - if (p.zoom != undefined) { defaultZoom = parseInt(p.zoom); } - $.each(addresses, function (index, value) { - var addressString = generateAddressString(value); - setMarker(addressString) - }); - -} - -function generateAddressString(field) { - var addressString = []; - if (field.indexOf('+') !== -1) { - var fields = field.split('+'); - $.each(fields, function (index, value) { - var fieldValue = window.parent.Xrm.Page.data.entity.attributes.get(value).getValue(); - if (fieldValue != null) { - addressString.push(fieldValue); +function getStoredApiKey() { + var req = new XMLHttpRequest(); + req.open("GET", + Xrm.Page.context.getClientUrl() + "/api/data/v" + VERSION + "/fic_googlemapsconfigurations?$select=fic_apikey,fic_usegoogleaddress,fic_markericon,fic_defaultzoom", + true); + req.setRequestHeader("OData-MaxVersion", "4.0"); + req.setRequestHeader("OData-Version", "4.0"); + req.setRequestHeader("Accept", "application/json"); + req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); + req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); + req.onreadystatechange = function () { + if (this.readyState === 4) { + req.onreadystatechange = null; + if (this.status === 200) { + var results = JSON.parse(this.response); + if (results.value.length == 0) { + return; + } else { + var configId = results.value[0]["fic_apikey"]; + $('#apikey').val(configId); + var markerIcon = results.value[0]["fic_markericon"]; + $('#markerIcon').val(markerIcon); + var useGoogleAddresses = results.value[0]["fic_usegoogleaddress"]; + var defaultZoom = results.value[0]["fic_defaultzoom"]; + $('#defaultzoom').val(defaultZoom); + $('#useGoogleAddresses').prop('checked', useGoogleAddresses); + } + } else { + Xrm.Utility.alertDialog(this.statusText); } - }); - return addressString.join(); - } else { - return window.parent.Xrm.Page.data.entity.attributes.get(field).getValue();; - } + } + }; + req.send(); } -function setCenterAndZoom() { - map.setCenter(new google.maps.LatLng( - ((Math.max.apply(Math, lat) + Math.min.apply(Math, lat)) / 2.0), ((Math.max.apply(Math, lng) + Math.min.apply(Math, lng)) / 2.0))); - if (markers.length == 1) { - map.setZoom(defaultZoom); - } else { - map.fitBounds(new google.maps.LatLngBounds( - //bottom left - new google.maps.LatLng(Math.min.apply(Math, lat), Math.min.apply(Math, lng)), - //top right - new google.maps.LatLng(Math.max.apply(Math, lat), Math.max.apply(Math, lng)))); - } +function createKey(apiKey) { + var entity = {}; + entity.fic_apikey = apiKey; + entity.fic_usegoogleaddress = $('#useGoogleAddresses').prop('checked'); + entity.fic_markericon = $('#markerIcon').val(); + entity.fic_defaultzoom = $('#defaultzoom').val(); + var req = new XMLHttpRequest(); + req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v" + VERSION + "/fic_googlemapsconfigurations", true); + req.setRequestHeader("OData-MaxVersion", "4.0"); + req.setRequestHeader("OData-Version", "4.0"); + req.setRequestHeader("Accept", "application/json"); + req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); + req.onreadystatechange = function () { + if (this.readyState === 4) { + req.onreadystatechange = null; + if (this.status === 204) { + $(".alert").text("Settings have been created successfuly!"); + $(".alert").show(); + setTimeout(function () { + $(".alert").hide(); + }, 3000); + } else { + Xrm.Utility.alertDialog(this.statusText); + } + } + }; + req.send(JSON.stringify(entity)); } -function setMarker(address) { - var geocoder = new google.maps.Geocoder(); - geocoder.geocode({ - 'address': address - }, - - function (results, status) { - addressProcessedCounter++; - if (status == google.maps.GeocoderStatus.OK) { - lat.push(results[0].geometry.location.lat()); - lng.push(results[0].geometry.location.lng()); - - var marker = new google.maps.Marker({ - map: map, - position: results[0].geometry.location, - title: useGoogleAddress ? results[0].formatted_address : address, - icon: markerIcon - }); - markers.push(marker); - google.maps.event.addListener(marker, - 'click', - function () { - var infowindow = new google.maps.InfoWindow({ - content: marker.title, - position: marker.position, - }); - infowindow.open(map); - }); - } else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) { +function updateKey(key, id) { + var entity = {}; + entity.fic_apikey = key; + entity.fic_usegoogleaddress = $('#useGoogleAddresses').prop('checked'); + entity.fic_markericon = $('#markerIcon').val(); + entity.fic_defaultzoom = $('#defaultzoom').val(); + var req = new XMLHttpRequest(); + req.open("PATCH", + Xrm.Page.context.getClientUrl() + "/api/data/v" + VERSION + "/fic_googlemapsconfigurations(" + id + ")", + true); + req.setRequestHeader("OData-MaxVersion", "4.0"); + req.setRequestHeader("OData-Version", "4.0"); + req.setRequestHeader("Accept", "application/json"); + req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); + req.onreadystatechange = function () { + if (this.readyState === 4) { + req.onreadystatechange = null; + if (this.status === 204) { + $(".alert").text("Settings have been updated successfuly!"); + $(".alert").show(); + setTimeout(function () { + $(".alert").hide(); + }, 3000); } else { - console.log("Geocode was not successful for the following reason: " + status); - } - if (addressProcessedCounter == addresses.length) { - if (markers.length != 0) { - $("#map_canvas").show(); - setCenterAndZoom(map); - } else { - $("#no_results").show(); - } + Xrm.Utility.alertDialog(this.statusText); } - }); + } + }; + req.send(JSON.stringify(entity)); } -function loadScript() { +function upsertKey(key) { var req = new XMLHttpRequest(); - req.open("GET", - Xrm.Page.context.getClientUrl() + "/api/data/v8.2/fic_googlemapsconfigurations?$select=fic_apikey,fic_usegoogleaddress,fic_markericon,fic_defaultzoom", - true); + req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v" + VERSION + "/fic_googlemapsconfigurations", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); - req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var results = JSON.parse(this.response); - if (results.value.length == 0) return; - var apiKey = results.value[0]["fic_apikey"]; - useGoogleAddress = results.value[0]["fic_usegoogleaddress"]; - markerIcon = results.value[0]["fic_markericon"]; - defaultZoom = results.value[0]["fic_defaultzoom"]; - var script = document.createElement('script'); - script.type = 'text/javascript'; - script.src = 'https://maps.googleapis.com/maps/api/js?key=' + apiKey; - script.onload = initialize; - document.body.appendChild(script); + if (results.value.length == 0) { + createKey(key); + } else { + var configId = results.value[0]["fic_googlemapsconfigurationid"].replace("{", "").replace("}", ""); + updateKey(key, configId); + } } else { Xrm.Utility.alertDialog(this.statusText); } @@ -165,4 +119,39 @@ function loadScript() { req.send(); } -loadScript(); \ No newline at end of file +var VERSION = parent.Xrm.Page.context.getVersion() != "" ? parent.Xrm.Page.context.getVersion().substring(0, 3) : "8.0"; +getStoredApiKey(); +document.getElementById("update").addEventListener("click", + function () { + var key = document.getElementById('apikey').value; + upsertKey(key); + }, + false); + +$("#markerIcon").change(function () { + markerPreview = $('#markerIcon').val(); + $("#markerPreview").attr("src", markerPreview); + $("#markerPreview").show(); +}); + +for (var i = 1; i <= 20; i++) { + switch (i) { + case 1: + $("#defaultzoom").append(new Option(i + " (World)", i)); + break; + case 5: + $("#defaultzoom").append(new Option(i + " (Continent)", i)); + break; + case 10: + $("#defaultzoom").append(new Option(i + " (City)", i)); + break; + case 15: + $("#defaultzoom").append(new Option(i + " (Street)", i)); + break; + case 20: + $("#defaultzoom").append(new Option(i + " (Building)", i)); + break; + default: + $("#defaultzoom").append(new Option(i, i)); + } +}