]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/commitdiff
feat: add `onLoad` utility to wait for page load even after it is already loaded
authorNicolas Coden <nicolas@ncoden.fr>
Sun, 18 Mar 2018 20:16:11 +0000 (21:16 +0100)
committerNicolas Coden <nicolas@ncoden.fr>
Sun, 18 Mar 2018 20:16:11 +0000 (21:16 +0100)
gulp/tasks/javascript.js
js/entries/foundation.js
js/entries/plugins/foundation.core.js
js/foundation.util.core.js
test/javascript/util/core.js

index da41933cef7df0deac08fa02b7a0a411aeca616a..99032df2c42802f0aa7be2cdf1938e910f7fa991 100644 (file)
@@ -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}',
index cc312ad183a7bd1a212a11dceb5c1e7b23f517ee..3a6ff58f4e9db6d6d96a93071078e6aab07aeed7 100644 (file)
@@ -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;
index cf4afd9cb0774d433ba42041d01edb9ff07b9f35..06bc55e24d5b3b02affa62910a2498aa8d3b25b6 100644 (file)
@@ -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.
index 23099a57fb51629da0cd9d6bdaeb740e6203d5f5..1c01ae03cc1a87b178b0f27010628d3ae1694d7f 100644 (file)
@@ -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};
index 53419238b5f8daa3c5c438c9e04a1f6bc41264b7..984b8f7f8b2da6cf4064de51249eac4a22e03493 100644 (file)
@@ -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() {
   });
 });