]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/commitdiff
Convert util.timerAndImageLoader to ES6
authorGeoff Kimball <geoff@zurb.com>
Wed, 3 Feb 2016 19:47:10 +0000 (11:47 -0800)
committerGeoff Kimball <geoff@zurb.com>
Wed, 3 Feb 2016 19:47:10 +0000 (11:47 -0800)
js/foundation.util.timerAndImageLoader.js

index 4440405089f8c2d5be4bb3e04503e46170b42aca..f15083ab1d0b1c7c71d6dd2de46d08df50e49e94 100644 (file)
@@ -1,82 +1,84 @@
-!function($, Foundation){
-  'use strict';
-  var Timer = function(elem, options, cb){
-    var _this = this,
-        duration = options.duration,//options is an object for easily adding features later.
-        nameSpace = Object.keys(elem.data())[0] || 'timer',
-        remain = -1,
-        start,
-        timer;
+'use strict';
 
+export function Timer(elem, options, cb) {
+  var _this = this,
+      duration = options.duration,//options is an object for easily adding features later.
+      nameSpace = Object.keys(elem.data())[0] || 'timer',
+      remain = -1,
+      start,
+      timer;
+
+  this.isPaused = false;
+
+  this.restart = function() {
+    remain = -1;
+    clearTimeout(timer);
+    this.start();
+  }
+
+  this.start = function() {
     this.isPaused = false;
-    
-    this.restart = function(){
-      remain = -1;
-      clearTimeout(timer);
-      this.start();
-    };
+    // if(!elem.data('paused')){ return false; }//maybe implement this sanity check if used for other things.
+    clearTimeout(timer);
+    remain = remain <= 0 ? duration : remain;
+    elem.data('paused', false);
+    start = Date.now();
+    timer = setTimeout(function(){
+      if(options.infinite){
+        _this.restart();//rerun the timer.
+      }
+      cb();
+    }, remain);
+    elem.trigger(`timerstart.zf.${nameSpace}`);
+  }
+
+  this.pause = function() {
+    this.isPaused = true;
+    //if(elem.data('paused')){ return false; }//maybe implement this sanity check if used for other things.
+    clearTimeout(timer);
+    elem.data('paused', true);
+    var end = Date.now();
+    remain = remain - (end - start);
+    elem.trigger(`timerpaused.zf.${nameSpace}`);
+  }
+}
 
-    this.start = function(){
-      this.isPaused = false
-      // if(!elem.data('paused')){ return false; }//maybe implement this sanity check if used for other things.
-      clearTimeout(timer);
-      remain = remain <= 0 ? duration : remain;
-      elem.data('paused', false);
-      start = Date.now();
-      timer = setTimeout(function(){
-        if(options.infinite){
-          _this.restart();//rerun the timer.
-        }
-        cb();
-      }, remain);
-      elem.trigger(`timerstart.zf.${nameSpace}`);
-    };
+/**
+ * Runs a callback function when images are fully loaded.
+ * @param {Object} images - Image(s) to check if loaded.
+ * @param {Func} callback - Function to execute when image is fully loaded.
+ */
+export function onImagesLoaded(images, callback){
+  var self = this,
+      unloaded = images.length;
 
-    this.pause = function(){
-      this.isPaused = true;
-      //if(elem.data('paused')){ return false; }//maybe implement this sanity check if used for other things.
-      clearTimeout(timer);
-      elem.data('paused', true);
-      var end = Date.now();
-      remain = remain - (end - start);
-      elem.trigger(`timerpaused.zf.${nameSpace}`);
-    };
-  };
-  /**
-   * Runs a callback function when images are fully loaded.
-   * @param {Object} images - Image(s) to check if loaded.
-   * @param {Func} callback - Function to execute when image is fully loaded.
-   */
-  var onImagesLoaded = function(images, callback){
-    var self = this,
-        unloaded = images.length;
+  if (unloaded === 0) {
+    callback();
+  }
+
+  images.each(function() {
+    if (this.complete) {
+      singleImageLoaded();
+    }
+    else if (typeof this.naturalWidth !== 'undefined' && this.naturalWidth > 0) {
+      singleImageLoaded();
+    }
+    else {
+      $(this).one('load', function() {
+        singleImageLoaded();
+      });
+    }
+  });
 
+  function singleImageLoaded() {
+    unloaded--;
     if (unloaded === 0) {
       callback();
     }
+  }
+}
 
-    var singleImageLoaded = function() {
-      unloaded--;
-      if (unloaded === 0) {
-        callback();
-      }
-    };
-
-    images.each(function() {
-      if (this.complete) {
-        singleImageLoaded();
-      }
-      else if (typeof this.naturalWidth !== 'undefined' && this.naturalWidth > 0) {
-        singleImageLoaded();
-      }
-      else {
-        $(this).one('load', function() {
-          singleImageLoaded();
-        });
-      }
-    });
-  };
-
+if (window.Foundation) {
   Foundation.Timer = Timer;
   Foundation.onImagesLoaded = onImagesLoaded;
-}(jQuery, window.Foundation);
+}