Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow base scripts to run within a module loader #290

Merged
merged 1 commit into from
Jul 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions javascripts/govuk/analytics/analytics.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(function() {
(function(global) {
"use strict";
window.GOVUK = window.GOVUK || {};

var GOVUK = global.GOVUK || {};

// For usage and initialisation see:
// https://github.com/alphagov/govuk_frontend_toolkit/blob/master/docs/analytics.md#create-an-analytics-tracker
Expand Down Expand Up @@ -61,4 +62,6 @@
};

GOVUK.Analytics = Analytics;
})();

global.GOVUK = GOVUK;
})(window);
9 changes: 7 additions & 2 deletions javascripts/govuk/analytics/download-link-tracker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
(function() {
(function(global) {
"use strict";

var GOVUK = global.GOVUK || {};

GOVUK.analyticsPlugins = GOVUK.analyticsPlugins || {};
GOVUK.analyticsPlugins.downloadLinkTracker = function (options) {
var options = options || {},
Expand Down Expand Up @@ -32,4 +35,6 @@
return $target;
}
}
}());

global.GOVUK = GOVUK;
})(window);
19 changes: 12 additions & 7 deletions javascripts/govuk/analytics/error-tracking.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Extension to track errors using google analytics as a data store.
(function() {
(function(global) {
"use strict";

var GOVUK = global.GOVUK || {};

GOVUK.analyticsPlugins = GOVUK.analyticsPlugins || {};

GOVUK.analyticsPlugins.error = function (options) {
Expand Down Expand Up @@ -35,12 +38,14 @@
return false;
}

if (window.addEventListener) {
window.addEventListener('error', trackJavaScriptError, false);
} else if (window.attachEvent) {
window.attachEvent('onerror', trackJavaScriptError);
if (global.addEventListener) {
global.addEventListener('error', trackJavaScriptError, false);
} else if (global.attachEvent) {
global.attachEvent('onerror', trackJavaScriptError);
} else {
window.onerror = trackJavaScriptError;
global.onerror = trackJavaScriptError;
}
}
}());

global.GOVUK = GOVUK;
})(window);
11 changes: 8 additions & 3 deletions javascripts/govuk/analytics/external-link-tracker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
(function() {
(function(global) {
"use strict";

var GOVUK = global.GOVUK || {};

GOVUK.analyticsPlugins = GOVUK.analyticsPlugins || {};
GOVUK.analyticsPlugins.externalLinkTracker = function () {

Expand Down Expand Up @@ -33,6 +36,8 @@
}

GOVUK.analyticsPlugins.externalLinkTracker.getHostname = function() {
return window.location.hostname;
return global.location.hostname;
}
}());

global.GOVUK = GOVUK;
})(window);
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(function() {
(function(global) {
"use strict";
window.GOVUK = window.GOVUK || {};

var GOVUK = global.GOVUK || {};

var GoogleAnalyticsUniversalTracker = function(id, cookieDomain) {
configureProfile(id, cookieDomain);
Expand All @@ -20,7 +21,7 @@
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
})(global,document,'script','//www.google-analytics.com/analytics.js','ga');
};

// https://developers.google.com/analytics/devguides/collection/analyticsjs/pages
Expand Down Expand Up @@ -140,10 +141,12 @@
};

function sendToGa() {
if (typeof window.ga === "function") {
ga.apply(window, arguments);
if (typeof global.ga === "function") {
ga.apply(global, arguments);
}
}

GOVUK.GoogleAnalyticsUniversalTracker = GoogleAnalyticsUniversalTracker;
})();

global.GOVUK = GOVUK;
})(window);
9 changes: 7 additions & 2 deletions javascripts/govuk/analytics/mailto-link-tracker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
(function() {
(function(global) {
"use strict";

var GOVUK = global.GOVUK || {};

GOVUK.analyticsPlugins = GOVUK.analyticsPlugins || {};
GOVUK.analyticsPlugins.mailtoLinkTracker = function () {

Expand Down Expand Up @@ -30,4 +33,6 @@
return $target;
}
}
}());

global.GOVUK = GOVUK;
})(window);
17 changes: 10 additions & 7 deletions javascripts/govuk/analytics/print-intent.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Extension to monitor attempts to print pages.
(function () {
(function (global) {
"use strict";

var GOVUK = global.GOVUK || {};

GOVUK.analyticsPlugins = GOVUK.analyticsPlugins || {};

GOVUK.analyticsPlugins.printIntent = function () {
Expand All @@ -11,15 +13,15 @@
});

// Most browsers
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print'),
if (global.matchMedia) {
var mediaQueryList = global.matchMedia('print'),
mqlListenerCount = 0;
mediaQueryList.addListener(function (mql) {
if (!mql.matches && mqlListenerCount === 0) {
printAttempt();
mqlListenerCount++;
// If we try and print again within 3 seconds, don't log it
window.setTimeout(function () {
setTimeout(function () {
mqlListenerCount = 0;
// printing will be tracked again now
}, 3000);
Expand All @@ -28,10 +30,11 @@
}

// IE < 10
if (window.onafterprint) {
window.onafterprint = printAttempt;
if (global.onafterprint) {
global.onafterprint = printAttempt;
}

};

}());
global.GOVUK = GOVUK;
})(window);
10 changes: 7 additions & 3 deletions javascripts/govuk/modules.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
(function($, root) {
(function(global) {
"use strict";
root.GOVUK = root.GOVUK || {};

var $ = global.jQuery;
var GOVUK = global.GOVUK || {};
GOVUK.Modules = GOVUK.Modules || {};

GOVUK.modules = {
Expand Down Expand Up @@ -53,4 +55,6 @@
}
}
}
})(jQuery, window);

global.GOVUK = GOVUK;
})(window);
10 changes: 7 additions & 3 deletions javascripts/govuk/modules/auto-track-event.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
(function(Modules) {
(function(global) {
"use strict";

Modules.AutoTrackEvent = function() {
var GOVUK = global.GOVUK || {};
GOVUK.Modules = GOVUK.Modules || {};

GOVUK.Modules.AutoTrackEvent = function() {
this.start = function(element) {
var options = {nonInteraction: 1}, // automatic events shouldn't affect bounce rate
category = element.data('track-category'),
Expand All @@ -23,4 +26,5 @@
}
};

})(window.GOVUK.Modules);
global.GOVUK = GOVUK;
})(window);
13 changes: 8 additions & 5 deletions javascripts/govuk/multivariate-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(function() {
(function(global) {
"use strict";
window.GOVUK = window.GOVUK || {};
var $ = window.$;

var $ = global.jQuery;
var GOVUK = global.GOVUK || {};

// A multivariate test framework
//
Expand Down Expand Up @@ -130,5 +131,7 @@
return "multivariatetest_cohort_" + this.name;
};

window.GOVUK.MultivariateTest = MultivariateTest;
}());
GOVUK.MultivariateTest = MultivariateTest;

global.GOVUK = GOVUK;
})(window);
11 changes: 8 additions & 3 deletions javascripts/govuk/primary-links.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
(function() {
(function(global) {
"use strict";
window.GOVUK = window.GOVUK || {};

var $ = global.jQuery;
var GOVUK = global.GOVUK || {};

// Only show the first {n} items in a list, documentation is in the README.md
var PrimaryList = function(el, selector){
Expand Down Expand Up @@ -39,6 +41,7 @@
$(window).trigger('govuk.pageSizeChanged');
}
};

GOVUK.PrimaryList = PrimaryList;

GOVUK.primaryLinks = {
Expand All @@ -48,4 +51,6 @@
});
}
};
}());

global.GOVUK = GOVUK;
})(window);
12 changes: 6 additions & 6 deletions javascripts/govuk/selection-buttons.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
(function () {
(function (global) {
"use strict";
var root = this,
$ = root.jQuery;

if (typeof GOVUK === 'undefined') { root.GOVUK = {}; }
var $ = global.jQuery;
var GOVUK = global.GOVUK || {};

var SelectionButtons = function (elmsOrSelector, opts) {
var $elms;
Expand Down Expand Up @@ -107,5 +106,6 @@
}
};

root.GOVUK.SelectionButtons = SelectionButtons;
}).call(this);
GOVUK.SelectionButtons = SelectionButtons;
global.GOVUK = GOVUK;
})(window);
29 changes: 15 additions & 14 deletions javascripts/govuk/stick-at-top-when-scrolling.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(function () {
(function (global) {
"use strict";
var root = this,
$ = root.jQuery;
if(typeof root.GOVUK === 'undefined') { root.GOVUK = {}; }

var $ = global.jQuery;
var GOVUK = global.GOVUK || {};

// Stick elements to top of screen when you scroll past, documentation is in the README.md
var sticky = {
Expand All @@ -16,22 +16,22 @@
sticky.$els = $els;

if(sticky._scrollTimeout === false) {
$(root).scroll(sticky.onScroll);
sticky._scrollTimeout = root.setInterval(sticky.checkScroll, 50);
$(global).scroll(sticky.onScroll);
sticky._scrollTimeout = global.setInterval(sticky.checkScroll, 50);
}
$(root).resize(sticky.onResize);
$(global).resize(sticky.onResize);
}
if(root.GOVUK.stopScrollingAtFooter){
if(GOVUK.stopScrollingAtFooter){
$els.each(function(i,el){
var $img = $(el).find('img');
if($img.length > 0){
var image = new Image();
image.onload = function(){
root.GOVUK.stopScrollingAtFooter.addEl($(el), $(el).outerHeight());
GOVUK.stopScrollingAtFooter.addEl($(el), $(el).outerHeight());
};
image.src = $img.attr('src');
} else {
root.GOVUK.stopScrollingAtFooter.addEl($(el), $(el).outerHeight());
GOVUK.stopScrollingAtFooter.addEl($(el), $(el).outerHeight());
}
});
}
Expand All @@ -43,14 +43,14 @@
if(sticky._hasScrolled === true){
sticky._hasScrolled = false;

var windowVerticalPosition = $(root).scrollTop();
var windowVerticalPosition = $(global).scrollTop();
sticky.$els.each(function(i, el){
var $el = $(el),
scrolledFrom = $el.data('scrolled-from');

if (scrolledFrom && windowVerticalPosition < scrolledFrom){
sticky.release($el);
} else if($(root).width() > 768 && windowVerticalPosition >= $el.offset().top) {
} else if($(global).width() > 768 && windowVerticalPosition >= $el.offset().top) {
sticky.stick($el);
}
});
Expand All @@ -72,5 +72,6 @@
}
}
};
root.GOVUK.stickAtTopWhenScrolling = sticky;
}).call(this);
GOVUK.stickAtTopWhenScrolling = sticky;
global.GOVUK = GOVUK;
})(window);
Loading