/*
	Author: Sol
	Filename: classtoggle.js
	Description: Toggle Class on click
	Dependencies:
		- jQuery
	How to use:
		- Add a "js-class-toggler" class to the triggering element 
		- Add a "js-class-toggle-target" class to the element you want to toggle the "active" class
		===== Or =====
		- Add a "js-class-toggler" class to the triggering element.
		- Add this attribute to the toggler [data-target=".test" data-class-to-toggle="text-danger"] to customize the target and the class you want to toggle
	Example :
	1.
		<p class="js-class-toggle-target">Click that button below</p>
		<a href="#" class="btn btn--standard js-class-toggler mb-1" data-class-to-toggle="text-warning">Buy Now</a>
	2.
		<p class="test">Click that button below</p>
		<a href="#" class="btn btn--standard js-class-toggler" data-target=".test" data-class-to-toggle="text-danger">Buy Now</a>
*/

;(function($) {
  "use strict"

  B2C.classToggle = {
    init: function(option) {
      this._default = {
        toggler: ".js-class-toggler",
        target: ".js-class-toggle-target",
        togglingClass: "is-active"
      }

      this._option = $.extend(this._default, option)
      if (!$(this._option.toggler).length) return

      this.eventListener()
    },

    eventListener: function() {
      let self = B2C.classToggle
      $(document).on("click", this._option.toggler, self.toggle)
    },

    toggle: function() {
      let self = B2C.classToggle,
        classToggleTarget = $(this).data("target")
          ? $(this).data("target")
          : self._option.target,
        classToToggle = $(this).data("class-to-toggle")
          ? $(this).data("class-to-toggle")
          : self._option.togglingClass

      if ($(classToggleTarget).length) {
        $(classToggleTarget).toggleClass(classToToggle)
      } else {
        $(this).toggleClass(classToToggle)
      }
    }
  }
  B2C.classToggle.init()
})(jQuery)
