From f59e95c91b777e64143708a5a52a0c5bf729aeb5 Mon Sep 17 00:00:00 2001 From: Nicolas Coden Date: Sun, 18 Mar 2018 21:16:11 +0100 Subject: [PATCH] feat: add `onLoad` utility to wait for page load even after it is already loaded --- gulp/tasks/javascript.js | 2 +- js/entries/foundation.js | 1 + js/entries/plugins/foundation.core.js | 3 ++- js/foundation.util.core.js | 16 +++++++++++++++- test/javascript/util/core.js | 7 +++++-- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/gulp/tasks/javascript.js b/gulp/tasks/javascript.js index da41933ce..99032df2c 100644 --- a/gulp/tasks/javascript.js +++ b/gulp/tasks/javascript.js @@ -20,7 +20,7 @@ gulp.task('javascript', ['javascript:foundation', 'javascript:deps', 'javascript var pluginsAsExternals = { 'jquery': 'jQuery', './foundation.core': '{Foundation: window.Foundation}', - './foundation.util.core' : '{rtl: window.Foundation.rtl, GetYoDigits: window.Foundation.GetYoDigits, transitionend: window.Foundation.transitionend, RegExpEscape: window.Foundation.RegExpEscape}', + './foundation.util.core' : '{rtl: window.Foundation.rtl, GetYoDigits: window.Foundation.GetYoDigits, transitionend: window.Foundation.transitionend, RegExpEscape: window.Foundation.RegExpEscape, onLoad: window.Foundation.onLoad}', './foundation.util.imageLoader' : '{onImagesLoaded: window.Foundation.onImagesLoaded}', './foundation.util.keyboard' : '{Keyboard: window.Foundation.Keyboard}', './foundation.util.mediaQuery' : '{MediaQuery: window.Foundation.MediaQuery}', diff --git a/js/entries/foundation.js b/js/entries/foundation.js index cc312ad18..3a6ff58f4 100644 --- a/js/entries/foundation.js +++ b/js/entries/foundation.js @@ -41,6 +41,7 @@ Foundation.rtl = CoreUtils.rtl; Foundation.GetYoDigits = CoreUtils.GetYoDigits; Foundation.transitionend = CoreUtils.transitionend; Foundation.RegExpEscape = CoreUtils.RegExpEscape; +Foundation.onLoad = CoreUtils.onLoad; Foundation.Box = Box; Foundation.onImagesLoaded = onImagesLoaded; diff --git a/js/entries/plugins/foundation.core.js b/js/entries/plugins/foundation.core.js index cf4afd9cb..06bc55e24 100644 --- a/js/entries/plugins/foundation.core.js +++ b/js/entries/plugins/foundation.core.js @@ -6,11 +6,12 @@ Foundation.addToJquery($); // These are now separated out, but historically were a part of this module, // and since this is here for backwards compatibility we include them in // this entry. -import {rtl, GetYoDigits, transitionend, RegExpEscape} from '../../foundation.util.core'; +import {rtl, GetYoDigits, transitionend, RegExpEscape, onLoad} from '../../foundation.util.core'; Foundation.rtl = rtl; Foundation.GetYoDigits = GetYoDigits; Foundation.transitionend = transitionend; Foundation.RegExpEscape = RegExpEscape; +Foundation.onLoad = onLoad; // Every plugin depends on plugin now, we can include that on the core for the // script inclusion path. diff --git a/js/foundation.util.core.js b/js/foundation.util.core.js index 23099a57f..1c01ae03c 100644 --- a/js/foundation.util.core.js +++ b/js/foundation.util.core.js @@ -61,4 +61,18 @@ function transitionend($elem){ } } -export {rtl, GetYoDigits, RegExpEscape, transitionend}; +/** + * Call the given function once the window is loaded, + * or immediately if the window is already loaded. + * @function + * + * @param {Function} fn - function to call on window load. + */ +function onLoad(fn) { + if (document.readyState === 'complete') + setTimeout(() => fn(), 0); + else + $(window).one('load', () => fn()); +} + +export {rtl, GetYoDigits, RegExpEscape, transitionend, onLoad}; diff --git a/test/javascript/util/core.js b/test/javascript/util/core.js index 53419238b..984b8f7f8 100644 --- a/test/javascript/util/core.js +++ b/test/javascript/util/core.js @@ -100,7 +100,7 @@ describe('Foundation core', function() { name.should.be.a('string'); name.should.be.equal(''); }); - + it('should handle a named function expression', function() { var D = function foo(){}; var name = Foundation.getFnName(D); @@ -109,10 +109,13 @@ describe('Foundation core', function() { name.should.be.equal('foo'); }); }); - + describe('transitionEnd()', function() { }); + describe('onLoad()', function (done) { + }); + describe('throttle()', function() { }); }); -- 2.47.2