1 /** 2 @name Alphagov 3 @namespace 4 @description A set of resuable methods for setting, changing and removing cookies 5 @requires core.js 6 */ 7 var Alphagov = { 8 /** 9 @name Alphagov.cookie_domain 10 @function 11 @description 12 13 @returns A string with the entire host path 14 15 @example 16 Alphagov.cookie_domain() 17 */ 18 cookie_domain: function() { 19 var host_parts = document.location.host.split(':')[0].split('.').slice(-3); 20 return '.' + host_parts.join('.'); 21 }, 22 23 /** 24 @name Alphagov.read_cookie 25 @function 26 @description 27 28 @param String name Name of the cookie 29 30 @returns The value found for the matching cookie 31 32 @example 33 Alphagov.read_cookie("qwerty") 34 */ 35 read_cookie: function(name) { 36 var cookieValue = null; 37 if (document.cookie && document.cookie != '') { 38 var cookies = document.cookie.split(';'); 39 for (var i = 0; i < cookies.length; i++) { 40 var cookie = jQuery.trim(cookies[i]); 41 if (cookie.substring(0, name.length + 1) == (name + '=')) { 42 cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); 43 break; 44 } 45 } 46 } 47 return cookieValue; 48 }, 49 50 /** 51 @name Alphagov.delete_cookie 52 @function 53 @description 54 55 @param String name Name of the cookie 56 57 @returns The value found for the matching cookie 58 59 @example 60 Alphagov.delete_cookie("qwerty") 61 */ 62 delete_cookie: function(name) { 63 if (document.cookie && document.cookie != '') { 64 var date = new Date(); 65 date.setTime(date.getTime()-(24*60*60*1000)); // 1 day ago 66 document.cookie = name + "=; expires=" + date.toGMTString() + "; domain=" + Alphagov.cookie_domain() + "; path=/"; 67 } 68 }, 69 70 /** 71 @name Alphagov.write_permanent_cookie 72 @function 73 @description 74 75 @param String name Name of the cookie 76 @param String value Value of the cookie 77 78 @returns The value found for the matching cookie 79 80 @example 81 Alphagov.write_permanent_cookie("foo", "bar") 82 */ 83 write_permanent_cookie: function(name, value) { 84 var date = new Date(2021, 12, 31); 85 document.cookie = name + "=" + encodeURIComponent(value) + "; expires=" + date.toGMTString() + "; domain=" + Alphagov.cookie_domain() + "; path=/"; 86 }, 87 88 /** 89 @name Alphagov.get_display_place_name 90 @function 91 @description 92 93 @param String locality_name Locality name 94 @param String council_name Council name 95 96 @returns Result 97 98 @example 99 Alphagov.get_display_place_name("foo", "bar") 100 */ 101 get_display_place_name: function(locality_name, council_name){ 102 var result = ''; 103 104 //get long/short version of council name 105 council_short_name = council_name.replace(' Borough Council', '').replace(' County Council', '').replace(' District Council', '').replace(' Council', ''); 106 107 if(council_short_name != '' && council_short_name != undefined){ 108 result = locality_name + ', ' + council_short_name; 109 }else{ 110 result = locality_name; 111 } 112 113 return result; 114 } 115 } 116 117 118 /** 119 @name AlphaGeo 120 @namespace 121 @description Set of methods for getting geo information 122 @requires AlphaGov, core.js 123 */ 124 var AlphaGeo = { 125 /** 126 @name AlphaGeo.locate 127 @function 128 @description Based on Matt Patterson's Alphagov Locator jQuery plugin 129 130 @param String id Selector ID for the element containing the location feature widget (Can be the form ID or the container) 131 @param {Object} [opts] Options. 132 Options 133 @param ignoreKnown Set to false if you want to ignore the known value if it has been set, on page load 134 @param errorSelector A selector where error messages will be shown 135 136 @returns false 137 138 @example 139 AlphaGeo.locate("#global-locator-form", "{ 140 ignoreKnown: false, 141 errorSelector: '#global-locator-error', 142 noJSSubmit: false 143 }") 144 */ 145 146 locate: function(id, opts){ 147 console.log('dsf') 148 var locator_form = $(id).closest('form'), 149 locator_box = $(id), 150 opts = opts || {}, 151 ignoreKnown = opts.ignoreKnown || false, 152 errorSelector = opts.errorSelector || '#global-app-error', 153 noJSSUbmit = opts.noJSSUbmit || false, 154 ask_ui = locator_box.find('.ask_location'), 155 locating_ui = locator_box.find('.finding_location'), 156 found_ui = locator_box.find('.found_location'), 157 all_ui = ask_ui.add(locating_ui).add(found_ui), 158 geolocate_ui; 159 160 161 var setup_geolocation_api_ui = function() { 162 var geolocation_ui_node = ask_ui.find('.locate-me'); 163 if (geolocation_ui_node.length > 0) return geolocation_ui_node; 164 return $('<p class="locate-me">or <a href="#">locate me automatically</a></p>').appendTo(ask_ui); 165 }; 166 var show_ui = function(ui_to_show) { 167 all_ui.addClass('hidden'); 168 ui_to_show.removeClass('hidden'); 169 }; 170 var update_geo_labels = function(geo_data) { 171 if (geo_data.councils && geo_data.councils.length > 0) { 172 display_name = Alphagov.get_display_place_name(geo_data.locality, geo_data.councils[geo_data.councils.length - 1].name); 173 } else { 174 display_name = geo_data.locality 175 } 176 found_ui.find('h3').text(display_name); 177 found_ui.find('a').text('Not in ' + geo_data.locality + '?'); 178 }; 179 var update_geo_fields = function(geo_data) { 180 locator_box.find('input[name=lat]').val(geo_data.lat); 181 locator_box.find('input[name=lon]').val(geo_data.lon); 182 }; 183 var clear_geo_fields = function() { 184 locator_box.find('input[name=lat]').val(''); 185 locator_box.find('input[name=lon]').val(''); 186 }; 187 var dispatch_location = function(response_data) { 188 if (response_data.current_location === undefined || !response_data.current_location) { 189 $(error_area_selector).empty().append("<p>Please enter a valid UK postcode.</p>").removeClass('hidden'); 190 show_ui(ask_ui); 191 } else if (! response_data.current_location.ward) { 192 $(error_area_selector).empty().append("<p>Sorry, that postcode was not recognised.</p>").removeClass('hidden'); 193 show_ui(ask_ui); 194 } else { 195 $(error_area_selector).empty().addClass("hidden"); 196 $(document).trigger('location-changed', response_data); 197 } 198 } 199 var changed_location = function(data) { 200 if (data.current_location != undefined) { 201 update_geo_labels(data.current_location); 202 update_geo_fields(data.current_location); 203 show_ui(found_ui); 204 locator_box.data('located', true); 205 } 206 else { 207 show_ui(ask_ui); 208 locator_box.data('located', false); 209 } 210 } 211 var reset_location = function() { 212 clear_geo_fields(); 213 found_ui.find('h3').text(''); 214 show_ui(ask_ui); 215 locator_box.data('located', false); 216 } 217 218 // Check to see if we're starting located 219 locator_box.data('located', !found_ui.hasClass('hidden')) 220 // Locator setup 221 found_ui.find('a').click(function (e) { 222 $(document).trigger('location-removed'); 223 e.preventDefault(); 224 }); 225 if (navigator.geolocation) { 226 console.log('geo') 227 var geolocate_ui = setup_geolocation_api_ui(); 228 geolocate_ui.bind('location-started', function () { 229 console.log('start') 230 show_ui(locating_ui); 231 }); 232 geolocate_ui.bind('location-failed', function () { 233 console.log('this') 234 $(id).text('We were not able to locate you.'); 235 show_ui(ask_ui); 236 }); 237 geolocate_ui.bind('location-completed', function (event, details) { 238 update_geo_fields(details); 239 if (noJSSUbmit) { 240 locator_form.submit(); 241 } 242 else { 243 $.post(locator_form[0].action, locator_form.serialize(), dispatch_location, 'json'); 244 } 245 }); 246 geolocate_ui.find('a').click(function (e) { 247 var parent_element = $(this).closest('.locate-me'); 248 249 geolocate_ui.trigger('location-started'); 250 browserSupportFlag = true; 251 navigator.geolocation.getCurrentPosition( 252 function(position) { 253 var new_location = {lat: position.coords.latitude, lon: position.coords.longitude}; 254 geolocate_ui.trigger('location-completed', new_location); 255 }, 256 function() { 257 geolocate_ui.trigger('location-failed'); 258 } 259 ); 260 261 e.preventDefault(); 262 // return false; 263 }); 264 } 265 if (!ignoreKnown) { 266 $(document).bind('location-known', function(e, data) { 267 changed_location(data); 268 }); 269 } 270 $(document).bind('location-changed', function(e, data) { 271 changed_location(data); 272 }); 273 $(document).bind('location-removed', function() { 274 reset_location(); 275 }); 276 locator_box.bind('reset-locator-form', function() { 277 reset_location(); 278 }); 279 locator_box.bind('check-locator-form-state', function() { 280 show_ui(locator_box.data('located') ? found_ui : ask_ui); 281 }); 282 locator_form.submit(function(e) { 283 clear_geo_fields(); 284 if (!noJSSUbmit) { 285 e.preventDefault(); 286 $.post(locator_form[0].action, locator_form.serialize(), dispatch_location, 'json'); 287 } 288 show_ui(locating_ui); 289 }); 290 291 292 }, 293 294 /** 295 @name AlphaGeo.readAndParseJSONCookie 296 @function 297 @description 298 299 @param String name Name of the cookie 300 301 @returns A JSON object or false if no match found 302 303 @example 304 AlphaGeo.readAndParseJSONCookie("qwerty") 305 */ 306 readAndParseJSONCookie: function(name) { 307 var cookie = Alphagov.read_cookie(name); 308 if (cookie) { 309 var json_cookie = $.base64Decode(cookie); 310 var geo_json = jQuery.parseJSON(json_cookie); 311 return geo_json; 312 } 313 return false; 314 }, 315 316 /** 317 @name AlphaGeo.locationName 318 @function 319 @description 320 321 @returns A string containing the current "friendly name" for the current location, set in the cookie, or false if no cookie found. 322 323 @example 324 AlphaGeo.locationName() 325 */ 326 locationName: function() { 327 var geo_json = AlphaGeo.readAndParseJSONCookie('geo'); 328 if (geo_json && geo_json["friendly_name"]) { 329 return geo_json["friendly_name"]; 330 } else { 331 return null; 332 } 333 }, 334 335 /** 336 @name AlphaGeo.councils 337 @function 338 @description 339 340 @returns A string containing the current "friendly name" for the current location, set in the cookie, or false if no cookie found. 341 342 @example 343 AlphaGeo.councils() 344 */ 345 councils: function() { 346 var geo_json = AlphaGeo.readAndParseJSONCookie('geo'); 347 var councils = []; 348 if (geo_json && geo_json["ward"]) { 349 councils = councils.concat(geo_json["ward"]); 350 } 351 if (geo_json && geo_json["council"]) { 352 councils = councils.concat(geo_json["council"]); 353 } 354 return councils; 355 }, 356 357 /** 358 @name AlphaGeo.locationCoords 359 @function 360 @description 361 362 @returns A string containing the current "friendly name" for the current location, set in the cookie, or false if no cookie found. 363 364 @example 365 AlphaGeo.locationCoords() 366 */ 367 locationCoords: function() { 368 var geo_json = AlphaGeo.readAndParseJSONCookie('geo'); 369 if (geo_json && geo_json["fuzzy_point"]) { 370 return {lat: geo_json.fuzzy_point.lat, lon: geo_json.fuzzy_point.lon}; 371 } else { 372 return {lat: null, lon: null}; 373 } 374 }, 375 376 /** 377 @name AlphaGeo.locator_object 378 @function 379 @description 380 381 @returns A string containing the current "friendly name" for the current location, set in the cookie, or false if no cookie found. 382 383 @example 384 AlphaGeo.locator_object() 385 */ 386 locator_object: function() { 387 return { 388 locality: AlphaGeo.locationName(), 389 lat: AlphaGeo.locationCoords().lat, 390 lon: AlphaGeo.locationCoords().lon, 391 councils: AlphaGeo.councils() 392 } 393 }, 394 395 /** 396 @name AlphaGeo.deleteGeoCookie 397 @function 398 @description Removes any currently set geo cookies 399 400 @returns true 401 402 @example 403 AlphaGeo.deleteGeoCookie() 404 */ 405 deleteGeoCookie: function() { 406 Alphagov.delete_cookie('geo'); 407 } 408 }; 409 410 411