/*
	Author: Sol
	Filename: textToggle.js
	Description: Toggle text on click
	Dependencies:
		- jQuery
	How to use:
		- Add a 'js-text-toggle' class on the element that you want to have a text toggle.
		- To customize the text, add this data attribute on the target element:
			- data-hideText="[ text here ]" ( this wil be the first text when you click the element )
			- data-showText="[ text here ]"
	Example : 
		<button class="btn btn--standard js-text-toggle" type="button" data-toggle="collapse" data-target="#toggleExample" data-hideText="Hide" data-showText="Show">Hide</button>
*/

;(function($) {
    'use strict'

    B2C.textToggle = {
        init: function(option) {
            this._default = {
                target: '.js-text-toggle',
                showText: 'Show more',
                hideText: 'Show less',
                activeClass: 'active'
            }

            B2C.textToggle._option = $.extend(this._default, option)

            if (!$(B2C.textToggle._option.target).length) return

            this.eventListener()
        },

        eventListener: function() {
            let self = B2C.textToggle
            $(this._option.target).on('click', self.toggleText)
        },

        toggleText: function() {
            let self = B2C.textToggle,
                newShowText = $(this).data('show-text')
                    ? $(this).data('show-text')
                    : self._option.showText,
                newHideText = $(this).data('hide-text')
                    ? $(this).data('hide-text')
                    : self._option.hideText

            // Toggle the active class for action
            $(this).toggleClass(self._option.activeClass)

            //   Condition after toggling the active class
            if ($(this).hasClass(self._option.activeClass))
                $(this).text(newHideText)
            else $(this).text(newShowText)
        }
    }
    B2C.textToggle.init()
})(jQuery)
