]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/commitdiff
Convert toggler to ES6 class
authorColin Marshall <colin.michael.marshall@gmail.com>
Thu, 4 Feb 2016 01:41:35 +0000 (18:41 -0700)
committerColin Marshall <colin.michael.marshall@gmail.com>
Thu, 4 Feb 2016 01:41:35 +0000 (18:41 -0700)
js/foundation.toggler.js

index ffc34763ecf8b0e3a0d02d9849a2412f5fc94d75..553d8001ac408fa8484b9bc416fc28d144c5070c 100644 (file)
@@ -1,12 +1,11 @@
+'use strict';
 /**
  * Toggler module.
  * @module foundation.toggler
  * @requires foundation.util.motion
  */
 
-!function(Foundation, $) {
-  'use strict';
-
+ export default class Toggler {
   /**
    * Creates a new instance of Toggler.
    * @class
@@ -14,7 +13,7 @@
    * @param {Object} element - jQuery object to add the trigger to.
    * @param {Object} options - Overrides to the default plugin settings.
    */
-  function Toggler(element, options) {
+  constructor(element, options) {
     this.$element = element;
     this.options = $.extend({}, Toggler.defaults, element.data(), options);
     this.className = '';
     Foundation.registerPlugin(this, 'Toggler');
   }
 
-  Toggler.defaults = {
-    /**
-     * Tells the plugin if the element should animated when toggled.
-     * @option
-     * @example false
-     */
-    animate: false
-  };
+  
 
   /**
    * Initializes the Toggler plugin by parsing the toggle class from data-toggler, or animation classes from data-animate.
    * @function
    * @private
    */
-  Toggler.prototype._init = function() {
+  _init() {
     var input;
     // Parse animation classes if they were set
     if (this.options.animate) {
       .attr('aria-controls', id);
     // If the target is hidden, add aria-hidden
     this.$element.attr('aria-expanded', this.$element.is(':hidden') ? false : true);
-  };
+  }
 
   /**
    * Initializes events for the toggle trigger.
    * @function
    * @private
    */
-  Toggler.prototype._events = function() {
+  _events() {
     this.$element.off('toggle.zf.trigger').on('toggle.zf.trigger', this.toggle.bind(this));
-  };
+  }
 
   /**
    * Toggles the target class on the target element. An event is fired from the original trigger depending on if the resultant state was "on" or "off".
    * @fires Toggler#on
    * @fires Toggler#off
    */
-  Toggler.prototype.toggle = function() {
+  toggle() {
     this[ this.options.animate ? '_toggleAnimate' : '_toggleClass']();
-  };
+  }
 
-  Toggler.prototype._toggleClass = function() {
+  _toggleClass() {
     this.$element.toggleClass(this.className);
 
     var isOn = this.$element.hasClass(this.className);
     }
 
     this._updateARIA(isOn);
-  };
+  }
 
-  Toggler.prototype._toggleAnimate = function() {
+  _toggleAnimate() {
     var _this = this;
 
     if (this.$element.is(':hidden')) {
         _this._updateARIA(false);
       });
     }
-  };
+  }
 
-  Toggler.prototype._updateARIA = function(isOn) {
+  _updateARIA(isOn) {
     this.$element.attr('aria-expanded', isOn ? true : false);
-  };
+  }
 
   /**
    * Destroys the instance of Toggler on the element.
    * @function
    */
-  Toggler.prototype.destroy= function() {
+  destroy() {
     this.$element.off('.zf.toggler');
     Foundation.unregisterPlugin(this);
-  };
-
-  Foundation.plugin(Toggler, 'Toggler');
+  }
+}
 
-  // Exports for AMD/Browserify
-  if (typeof module !== 'undefined' && typeof module.exports !== 'undefined')
-    module.exports = Toggler;
-  if (typeof define === 'function')
-    define(['foundation'], function() {
-      return Toggler;
-    });
+Toggler.defaults = {
+  /**
+   * Tells the plugin if the element should animated when toggled.
+   * @option
+   * @example false
+   */
+  animate: false
+};
 
-}(Foundation, jQuery);
+// Window exports
+if (window.Foundation) {
+  Foundation.plugin(Toggler, 'Toggler');
+}
+
+// Exports for AMD/Browserify
+if (typeof module !== 'undefined' && typeof module.exports !== 'undefined')
+  module.exports = Toggler;
+if (typeof define === 'function')
+  define(['foundation'], function() {
+    return Toggler;
+  });