aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Somerville <matthew-github@dracos.co.uk>2017-11-14 16:08:16 +0000
committerMatthew Somerville <matthew-github@dracos.co.uk>2017-11-15 16:01:23 +0000
commit865df7790d20ffbeacd8dd10316f4a7f733dcb1d (patch)
tree4e22de2f83e1d5d1f2dda5b6b32287350c8bb4bd
parent57fc2d335b97f2c692ce0575787be3164d8388f4 (diff)
[fixmystreet.com] Factor out front-page-only JavaScript.
-rw-r--r--templates/web/fixmystreet.com/footer_extra_js.html15
-rw-r--r--templates/web/fixmystreet.com/header_extra.html4
-rw-r--r--web/cobrands/fixmystreet.com/front.js38
-rw-r--r--web/cobrands/fixmystreet.com/js.js27
4 files changed, 53 insertions, 31 deletions
diff --git a/templates/web/fixmystreet.com/footer_extra_js.html b/templates/web/fixmystreet.com/footer_extra_js.html
index 1b140822e..2b024bfa4 100644
--- a/templates/web/fixmystreet.com/footer_extra_js.html
+++ b/templates/web/fixmystreet.com/footer_extra_js.html
@@ -3,7 +3,14 @@ IF bodyclass.match('mappage');
scripts.push(
version('/vendor/OpenLayers.Projection.OrdnanceSurvey.js'),
);
-END ~%]
-[% scripts.push(
- version('/cobrands/fixmystreet.com/js.js'),
-) %]
+END;
+IF bodyclass.match('frontpage');
+ scripts.push(
+ version('/cobrands/fixmystreet.com/front.js'),
+ );
+ELSE;
+ scripts.push(
+ version('/cobrands/fixmystreet.com/js.js'),
+ );
+END;
+%]
diff --git a/templates/web/fixmystreet.com/header_extra.html b/templates/web/fixmystreet.com/header_extra.html
index 588c72650..74280a6f9 100644
--- a/templates/web/fixmystreet.com/header_extra.html
+++ b/templates/web/fixmystreet.com/header_extra.html
@@ -2,6 +2,10 @@
<link rel="prefetch" href="[% version('/vendor/OpenLayers.Projection.OrdnanceSurvey.js') %]">
[% END %]
+[% IF bodyclass.match('frontpage') %]
+<link rel="prefetch" href="[% version('/cobrands/fixmystreet.com/js.js') %]">
+[% END %]
+
[% IF c.req.uri.host == 'osm.fixmystreet.com' %]
<link rel="canonical" href="https://www.fixmystreet.com[% c.req.uri.path_query %]">
[% END %]
diff --git a/web/cobrands/fixmystreet.com/front.js b/web/cobrands/fixmystreet.com/front.js
new file mode 100644
index 000000000..f544fb657
--- /dev/null
+++ b/web/cobrands/fixmystreet.com/front.js
@@ -0,0 +1,38 @@
+(function(){
+ if (!document.querySelector) { return; }
+ if ( -1 !== navigator.userAgent.indexOf('Google Page Speed')) { return; }
+ if (document.cookie.indexOf('has_seen_country_message') !== -1) { return; }
+
+ /* Front page banner for other countries */
+ var request = new XMLHttpRequest();
+ request.open('GET', 'https://gaze.mysociety.org/gaze-rest?f=get_country_from_ip', true);
+ request.onreadystatechange = function() {
+ if (this.readyState === 4) {
+ if (this.status >= 200 && this.status < 400) {
+ var data = this.responseText;
+ if ( data && data != 'GB\n' ) {
+ var banner = document.createElement('div');
+ banner.className = 'top_banner top_banner--country';
+ var close = document.createElement('a');
+ close.className = 'top_banner__close';
+ close.innerHTML = 'Close';
+ close.href = '#';
+ close.onclick = function(e) {
+ document.querySelector('.top_banner--country').style.display = 'none';
+ var t = new Date(); t.setFullYear(t.getFullYear() + 1);
+ document.cookie = 'has_seen_country_message=1; path=/; expires=' + t.toUTCString();
+ };
+ var p = document.createElement('p');
+ p.innerHTML = 'This site is for reporting <strong>problems in the UK</strong>. There are FixMyStreet sites <a href="http://fixmystreet.org/sites/">all over the world</a>, or you could set up your own using the <a href="http://fixmystreet.org/">FixMyStreet Platform</a>.';
+ banner.appendChild(close);
+ banner.appendChild(p);
+ document.body.insertBefore(banner, document.body.firstChild);
+ document.querySelector('.top_banner--country').style.display = 'block';
+ }
+ }
+ }
+ };
+ request.send();
+ request = null;
+
+})();
diff --git a/web/cobrands/fixmystreet.com/js.js b/web/cobrands/fixmystreet.com/js.js
index 087d5d15f..9ecb8b8c2 100644
--- a/web/cobrands/fixmystreet.com/js.js
+++ b/web/cobrands/fixmystreet.com/js.js
@@ -11,30 +11,3 @@
jQuery.validator.addMethod('validName', valid_name_factory(0), translation_strings.name.required);
jQuery.validator.addMethod('validNameU', valid_name_factory(1), translation_strings.name.required);
})();
-
-$(function(){
-
- /* Front page banner for other countries */
-
- $('.top_banner__close').live('click', function() {
- $('.top_banner--country').hide();
- var t = new Date(); t.setFullYear(t.getFullYear() + 1);
- document.cookie = 'has_seen_country_message=1; path=/; expires=' + t.toUTCString();
- });
-
- if ( $('body.frontpage').length && -1 == navigator.userAgent.indexOf('Google Page Speed')) {
- if (document.cookie.indexOf('has_seen_country_message') === -1) {
- $.ajax({
- url: 'https://gaze.mysociety.org/gaze-rest?f=get_country_from_ip',
- success: function(data) {
- if ( data && data != 'GB\n' ) {
- var banner = '<div class="top_banner top_banner--country"><a href="#" class="top_banner__close">Close</a> <p>This site is for reporting <strong>problems in the UK</strong>. There are FixMyStreet sites <a href="http://www.fixmystreet.org/sites/">all over the world</a>, or you could set up your own using the <a href="http://www.fixmystreet.org/">FixMyStreet Platform</a>.</p></div>';
- $('body').prepend(banner);
- $('.top_banner--country').slideDown('slow');
- }
- }
- });
- }
- }
-
-});