/* eslint-disable */ // Run everything in a SIAF to avoid clashes with anything outside (function() { // Any default params we want var _domain = 'https://www.electricalsafetyfirst.org.uk'; if (location.hostname == 'www.esf.local') { _domain = 'https://' + location.hostname; } var _cdn = _domain + '/_client/'; var _default_width_bak = 250; // likely not used, a failsafe if the script calls the dom before the stylesheet is appended // Define a local var for jQuery var jQuery; // Before anything else, use the script tag (of this script) to determine the current base url this .js sits on var _script = document.getElementById('esf-travel-adaptors-widget'); var _script_path = _script.src; var _script_folder = _script_path.substr(0, _script_path.lastIndexOf('/') + 1); /* Check we have version 2.2.4 exactly, else load it from jqueryCDN ---------------------------------------------------------------------------------------- */ if (window.jQuery === undefined || window.jQuery.fn.jquery !== '2.2.4') { var script_tag = document.createElement('script'); script_tag.setAttribute('type', 'text/javascript'); script_tag.setAttribute('src', 'https://code.jquery.com/jquery-2.2.4.min.js'); if (script_tag.readyState) { script_tag.onreadystatechange = function() { // Old versions of IE need onreadystatechange here if (this.readyState == 'complete' || this.readyState == 'loaded') { scriptLoadHandler(); } }; } else { script_tag.onload = scriptLoadHandler; } // Don't assume the site has a head tag (I know), so look for it, if not there add it to docelement (document.getElementsByTagName('head')[0] || document.documentElement).appendChild(script_tag); } else { // They already have version 2.2.4, great! jQuery = window.jQuery; init(); } /* Once jQuery is init'd, call this ---------------------------------------------------------------------------------------- */ function scriptLoadHandler() { // Ensure we don't conflict with other libs that use $ (e.g. Prototype) jQuery = window.jQuery.noConflict(true); // Call our init function init(); } function validateNumber(val, min_val, type) { var patt = /^\s*\d+\s*$/; var result = patt.test(val) ? parseInt(val) : 0; // minimum value check? if (val == undefined && min_val != undefined) { // they simply ommitted the value all together, no problem, just set it to the min val but DON'T alert them result = min_val; } else if (min_val != undefined && result < min_val) { // they specified a val but its too small, tell them and set the value to the min console.log('ESF Widget: The minumum ' + type + ' is ' + min_val); result = min_val; } return result; } function getPlugData(country) { jQuery.getJSON(_domain + '/api/traveladaptor/get', { country: country }, function(data) { var country = data.country; var voltages = data.voltage.join(' / '); var frequencies = data.frequency.join(' / '); var plugTypes = data.plugTypes; var count = plugTypes.length; var label = 'types'; var intent = 'may'; var adaptors = 'adaptors'; var types = plugTypes.map((t) => t.name).join(' and '); if (count == 1) { label = 'type'; intent = 'will'; adaptors = 'adaptor'; } var list = `
In ${country}, you ${intent} need ${count} ${label} of ${adaptors}, ${types}.
` + list + `${country} also operates on a ${voltages} supply voltage and ${frequencies} frequency.
For more advice on staying safe whilst travelling, visit Electrical Safety First`; jQuery('.travel-adaptors__results').html(markup); jQuery('.travel-adaptors__destinations').hide(); jQuery('.travel-adaptors__results').show(); }); } function renderInitialMarkup(container) { var markup = ``; container.html(markup); } function getAddressCountry(address, length) { if (!address) { return; } const findType = (type) => type.types[0] === 'country'; const location = address.map((obj) => obj); const rr = location.filter(findType)[0]; return length === 'short' ? rr.short_name : rr.long_name; } function initPlaces() { var input = jQuery('.travel-adaptors__form input')[0]; autocomplete = new google.maps.places.Autocomplete(input); autocomplete.addListener('place_changed', function() { var place = autocomplete.getPlace(); if (place !== undefined) { var country = getAddressCountry(place.address_components, 'long'); getPlugData(country); } }); } window.initPlaces = initPlaces; /* All the ESF X-ray Sliders widget specifics go here ---------------------------------------------------------------------------------------- */ function init() { jQuery(document).ready(function($) { var container = $('.travel-adaptors__container'); if (container.length == 0) { return false; } // Apply the css to the doc var css_link = $('', { rel: 'stylesheet', type: 'text/css', href: _cdn + 'styles/travel-adaptors.css' }); css_link.appendTo('head'); var google_maps_link = $('