/*
	Author: Thelma Suyam
	Filename: textToggle.js
	Description: This is to auto expand one lined text on click
	How to use:  
	Note: 
*/

;(function($) {
    'use strict'

    B2C.textExpand = {
        init: function(option) {
            this._default = {
                target: '.js-text-expand',
                container: '.js-text-expand-container',
                closeButton: '.js-text-close'
            }

            this._option = $.extend({}, this._default, option)

            if (!$(this._option.target)) return
            this.eventListener()
        },

        eventListener: function() {
            let self = this

            $(this._option.target).on('click', function(e) {
                e.preventDefault()
                self.toggle(self, this, false)
            })

            $(this._option.closeButton).on('click', function(e) {
                e.preventDefault()
                self.toggle(self, this, true)
            })
        },

        toggle: function(self, element, expanded) {
            expanded === true
                ? $(element)
                      .closest(this._option.container)
                      .removeClass('js-expanded')
                : $(element)
                      .closest(this._option.container)
                      .addClass('js-expanded')
        }
    }

    const defaultTextExpand = Object.create(B2C.textExpand)

    defaultTextExpand.init()
})(jQuery)
