DragonOsman

script.js

Jan 30th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. requirejs.config({
  2.     baseUrl: ".",
  3.     paths: {
  4.         app: "./script.js"
  5.     }
  6. });
  7.  
  8. function init_map() {
  9.     var map = new google.maps.Map(document.getElementById("map"), {
  10.         center: { lat: -34.397, lng: 150.644 },
  11.         zoom: 6
  12.     });
  13.     var info_window = new google.maps.InfoWindow;
  14.  
  15.     // Try HTML5 Geolocation
  16.     if (navigator.geolocation) {
  17.         navigator.geolocation.getCurrentPosition(function(position) {
  18.             var pos = {
  19.                 lat: position.coords.latitude,
  20.                 lng: position.coords.longitude
  21.             };
  22.  
  23.             var form = create_form();
  24.             var input1 = create_input("text", "current_amount", "Amount");
  25.             var select1 = create_select("from_currency", "from", "From");
  26.             var select2 = create_select("to_currency", "to", "To");
  27.             var xreq = xml_request(
  28.                 "https://openexchangerates.org/api/currencies.json?prettyprint=true&show_alternative=false&show_inactive=false&app_id=MY_APP_ID",
  29.                 function(response) {
  30.                     var results = response, arr, i, len, key, val;
  31.                     for (i = 0, arr = Object.keys(results), len = arr.length; i < len; ++i) {
  32.                         key = arr[i];
  33.                         val = results[key];
  34.                         var option = document.createElement("option");
  35.                         option.name = key;
  36.                         option.id = key;
  37.                         option.innerHTML = key + " - " + val;
  38.                         select1.appendChild(option);
  39.                         select2.appendChild(option);
  40.                     }
  41.  
  42.                     for (var i = 0; i < select1.options.length; i++) {
  43.                         if (select1.options[i] == results["USD"]) {
  44.                             select1.options[i].selected = "selected";
  45.                             select1.options[i].disabled = "disabled";
  46.                         }
  47.                     }
  48.  
  49.                     var geo_coder = new google.maps.Geocoder();
  50.                     var currency_abbr = require("./scripts/country-currency-map.min.js").getCurrencyAbbreviation;
  51.                     geo_coder.geocode({ "location": pos }, function (geocode_results, status) {
  52.                         if (status == google.maps.GeocoderStatus.OK) {
  53.                             if (geocode_results[1]) {
  54.                                 var country = null;
  55.                                 for (var r = 0, rl = geocode_results.length; r < rl; r++) {
  56.                                     var result = geocode_results[r]
  57.                                     if (!country && result.types[0] === "country") {
  58.                                         country = result.address_components[0].long_name;
  59.                                         currency_abbr(country);
  60.                                     }
  61.                                 }
  62.                             }
  63.                         }
  64.                     });
  65.  
  66.                     for (var i = 0; i < select2.options.length; i++) {
  67.                         if (select2.options[i] == results[currency_abbr]) {
  68.                             select2.options[i].selected = "selected";
  69.                         }
  70.                     }
  71.                 });
  72.             var input2 = create_input("submit", "submit", "Convert");
  73.  
  74.             form.appendChild(input1);
  75.             form.appendChild(select1);
  76.             form.appendChild(select2);
  77.             form.appendChild(input2);
  78.  
  79.             info_window.setPosition(pos);
  80.             info_window.setContent(form);
  81.             info_window.open(map);
  82.             map.setCenter(pos);
  83.         }, function() {
  84.             handle_location_error(true, info_window, map.getCenter());
  85.         });
  86.     } else {
  87.         // Browser doesn't support Geolocation
  88.         handle_location_error(false, info_window, map.getCenter());
  89.     }
  90. }
  91.  
  92. function handle_location_error(browser_has_geolocation, info_window, pos) {
  93.     info_window.setPosition(pos);
  94.     info_window.setContent(browser_has_geolocation ? "Error: The Geolocation service failed." : "Error: Your browser doesn't support Geolocation");
  95.     info_window.open(map);
  96. }
  97.  
  98. function create_form() {
  99.     var form = document.createElement("form");
  100.     form.method = "post";
  101.     form.action = "/currency_converter.exe";
  102.     return form;
  103. }
  104.  
  105. function create_input(type, name, value) {
  106.     var input = document.createElement("input");
  107.     input.type = type;
  108.     input.name = name;
  109.     input.value = value;
  110.     return input;
  111. }
  112.  
  113. function create_select(name, id, value) {
  114.     var select = document.createElement("select");
  115.     select.name = name;
  116.     select.id = id;
  117.     select.value = value;
  118.     return select;
  119. }
  120.  
  121. function xml_request(url, success_callback) {
  122.     var xreq = new XMLHttpRequest();
  123.     xreq.withCredentials = true;
  124.  
  125.     // Check if the XMLHttpRequest object has a "withCredentials" property.
  126.     // "withCredentials" only exists on XMLHTTPRequest2 objects.
  127.     if ("withCredentials" in xreq) {
  128.         xreq.open("GET", url, true);
  129.  
  130.         // Otherwise, check if XDomainRequest.
  131.         // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
  132.     } else if (typeof XDomainRequest != "undefined") {
  133.         xreq = new XDomainRequest();
  134.         xreq.open("GET", url, true);
  135.  
  136.         // Otherwise, CORS is not supported by the browser.
  137.     } else {
  138.         xreq = null;
  139.     }
  140.  
  141.     if (typeof xreq != null) {
  142.         xreq.setRequestHeader("Accept", "application/json");
  143.         xreq.setRequestHeader("Accept-Charset", "utf-8");
  144.         xreq.setRequestHeader("Accept-Language", "en-US");
  145.         xreq.responseType = "json";
  146.         xreq.onload = function() {
  147.             if (xreq.status == 200) {
  148.                 success_callback(this.response);
  149.             } else {
  150.                 form.innHTML = "Something went wrong";
  151.             }
  152.         };
  153.         xreq.onerror = function() {
  154.             console.log("There was an error!");
  155.         };
  156.         xreq.send({ form: "data" });
  157.     }
  158.     return xreq;
  159. }
Add Comment
Please, Sign In to add comment